001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.index.server.transfer;
018
019import com.echothree.model.control.index.common.IndexOptions;
020import com.echothree.model.control.index.common.transfer.IndexTransfer;
021import com.echothree.model.control.index.common.transfer.IndexTypeTransfer;
022import com.echothree.model.control.index.server.control.IndexControl;
023import com.echothree.model.control.party.common.transfer.LanguageTransfer;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.data.index.server.entity.Index;
026import com.echothree.model.data.index.server.entity.IndexDetail;
027import com.echothree.model.data.index.server.entity.IndexStatus;
028import com.echothree.model.data.party.server.entity.Language;
029import com.echothree.model.data.user.server.entity.UserVisit;
030import com.echothree.util.server.persistence.Session;
031import java.util.Set;
032
033public class IndexTransferCache
034        extends BaseIndexTransferCache<Index, IndexTransfer> {
035
036    PartyControl partyControl = Session.getModelController(PartyControl.class);
037    
038    /** Creates a new instance of IndexTransferCache */
039    public IndexTransferCache(UserVisit userVisit, IndexControl indexControl) {
040        super(userVisit, indexControl);
041        
042        var options = session.getOptions();
043        if(options != null) {
044            setIncludeKey(options.contains(IndexOptions.IndexIncludeKey));
045            setIncludeGuid(options.contains(IndexOptions.IndexIncludeGuid));
046        }
047        
048        setIncludeEntityInstance(true);
049    }
050
051    public IndexTransfer getIndexTransfer(Index index) {
052        IndexTransfer indexTransfer = get(index);
053
054        if(indexTransfer == null) {
055            IndexDetail indexDetail = index.getLastDetail();
056            IndexStatus indexStatus = indexControl.getIndexStatus(index);
057            String indexName = indexDetail.getIndexName();
058            IndexTypeTransfer indexTypeTransfer = indexControl.getIndexTypeTransfer(userVisit, indexDetail.getIndexType());
059            Language language = indexDetail.getLanguage();
060            LanguageTransfer languageTransfer = language == null ? null : partyControl.getLanguageTransfer(userVisit, language);
061            String directory = indexDetail.getDirectory();
062            Boolean isDefault = indexDetail.getIsDefault();
063            Integer sortOrder = indexDetail.getSortOrder();
064            String description = indexControl.getBestIndexDescription(index, getLanguage());
065            Long unformattedCreatedTime = indexStatus.getCreatedTime();
066            String createdTime = formatTypicalDateTime(unformattedCreatedTime);
067
068            indexTransfer = new IndexTransfer(indexName, indexTypeTransfer, languageTransfer, directory, isDefault, sortOrder, description,
069                    unformattedCreatedTime, createdTime);
070            put(index, indexTransfer);
071        }
072
073        return indexTransfer;
074    }
075
076}