001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.ChainOptions;
020import com.echothree.model.control.chain.common.transfer.ChainInstanceEntityRoleTransfer;
021import com.echothree.model.control.chain.common.transfer.ChainInstanceTransfer;
022import com.echothree.model.control.chain.server.control.ChainControl;
023import com.echothree.model.data.chain.server.entity.ChainInstance;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.common.transfer.MapWrapper;
026import com.echothree.util.server.persistence.Session;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class ChainInstanceTransferCache
031        extends BaseChainTransferCache<ChainInstance, ChainInstanceTransfer> {
032
033    ChainControl chainControl = Session.getModelController(ChainControl.class);
034
035    boolean includeChainInstanceStatus;
036    boolean includeChainInstanceEntityRoles;
037    
038    /** Creates a new instance of ChainInstanceTransferCache */
039    protected ChainInstanceTransferCache() {
040        super();
041        
042        var options = session.getOptions();
043        if(options != null) {
044            includeChainInstanceStatus = options.contains(ChainOptions.ChainInstanceIncludeChainInstanceStatus);
045            includeChainInstanceEntityRoles = options.contains(ChainOptions.ChainInstanceIncludeChainInstanceEntityRoles);
046        }
047        
048        setIncludeEntityInstance(true);
049    }
050    
051    public ChainInstanceTransfer getChainInstanceTransfer(UserVisit userVisit, ChainInstance chainInstance) {
052        var chainInstanceTransfer = get(chainInstance);
053        
054        if(chainInstanceTransfer == null) {
055            var chainInstanceDetail = chainInstance.getLastDetail();
056            var chainInstanceName = chainInstanceDetail.getChainInstanceName();
057            var chain = chainControl.getChainTransfer(userVisit, chainInstanceDetail.getChain());
058            
059            chainInstanceTransfer = new ChainInstanceTransfer(chainInstanceName, chain);
060            put(userVisit, chainInstance, chainInstanceTransfer);
061            
062            if(includeChainInstanceStatus) {
063                chainInstanceTransfer.setChainInstanceStatus(chainControl.getChainInstanceStatusTransfer(userVisit, chainInstance));
064            }
065            
066            if(includeChainInstanceEntityRoles) {
067                var chainInstanceEntityRoleTransfers = chainControl.getChainInstanceEntityRoleTransfersByChainInstance(userVisit, chainInstance);
068                var chainInstanceEntityRoles = new MapWrapper<ChainInstanceEntityRoleTransfer>(chainInstanceEntityRoleTransfers.size());
069
070                chainInstanceEntityRoleTransfers.forEach((chainInstanceEntityRoleTransfer) -> {
071                    chainInstanceEntityRoles.put(chainInstanceEntityRoleTransfer.getChainEntityRoleType().getChainEntityRoleTypeName(), chainInstanceEntityRoleTransfer);
072                });
073
074                chainInstanceTransfer.setChainInstanceEntityRoles(chainInstanceEntityRoles);
075            }
076        }
077        
078        return chainInstanceTransfer;
079    }
080    
081}