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.chain.server.transfer;
018
019import com.echothree.model.control.chain.common.transfer.ChainTransfer;
020import com.echothree.model.control.chain.common.transfer.ChainTypeTransfer;
021import com.echothree.model.control.chain.server.control.ChainControl;
022import com.echothree.model.control.sequence.common.transfer.SequenceTransfer;
023import com.echothree.model.control.sequence.server.control.SequenceControl;
024import com.echothree.model.data.chain.server.entity.Chain;
025import com.echothree.model.data.chain.server.entity.ChainDetail;
026import com.echothree.model.data.sequence.server.entity.Sequence;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.util.server.persistence.Session;
029
030public class ChainTransferCache
031        extends BaseChainTransferCache<Chain, ChainTransfer> {
032    
033    SequenceControl sequenceControl = Session.getModelController(SequenceControl.class);
034    
035    /** Creates a new instance of ChainTransferCache */
036    public ChainTransferCache(UserVisit userVisit, ChainControl chainControl) {
037        super(userVisit, chainControl);
038        
039        setIncludeEntityInstance(true);
040    }
041    
042    public ChainTransfer getChainTransfer(Chain chain) {
043        ChainTransfer chainTransfer = get(chain);
044        
045        if(chainTransfer == null) {
046            ChainDetail chainDetail = chain.getLastDetail();
047            ChainTypeTransfer chainTypeTransfer = chainControl.getChainTypeTransfer(userVisit, chainDetail.getChainType());
048            String chainName = chainDetail.getChainName();
049            Sequence chainInstanceSequence = chainDetail.getChainInstanceSequence();
050            SequenceTransfer chainInstanceSequenceTransfer = chainInstanceSequence == null? null: sequenceControl.getSequenceTransfer(userVisit, chainInstanceSequence);
051            Boolean isDefault = chainDetail.getIsDefault();
052            Integer sortOrder = chainDetail.getSortOrder();
053            String description = chainControl.getBestChainDescription(chain, getLanguage());
054            
055            chainTransfer = new ChainTransfer(chainTypeTransfer, chainName, chainInstanceSequenceTransfer, isDefault, sortOrder, description);
056            put(chain, chainTransfer);
057        }
058        
059        return chainTransfer;
060    }
061    
062}