001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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 javax.inject.Inject;
020import com.echothree.model.control.chain.common.ChainConstants;
021import com.echothree.model.control.chain.common.ChainOptions;
022import com.echothree.model.control.chain.common.transfer.ChainActionTransfer;
023import com.echothree.model.control.chain.server.control.ChainControl;
024import com.echothree.model.data.chain.server.entity.ChainAction;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class ChainActionTransferCache
030        extends BaseChainTransferCache<ChainAction, ChainActionTransfer> {
031
032    @Inject
033    ChainControl chainControl;
034
035    boolean includeRelated;
036
037    /** Creates a new instance of ChainActionTransferCache */
038    protected ChainActionTransferCache() {
039        super();
040
041        var options = session.getOptions();
042        if(options != null) {
043            includeRelated = options.contains(ChainOptions.ChainActionIncludeRelated);
044        }
045        
046        setIncludeEntityInstance(true);
047    }
048
049    public ChainActionTransfer getChainActionTransfer(UserVisit userVisit, ChainAction chainAction) {
050        var chainActionTransfer = get(chainAction);
051
052        if(chainActionTransfer == null) {
053            var chainActionDetail = chainAction.getLastDetail();
054            var chainActionSetTransfer = chainControl.getChainActionSetTransfer(userVisit, chainActionDetail.getChainActionSet());
055            var chainActionName = chainActionDetail.getChainActionName();
056            var chainActionType = chainActionDetail.getChainActionType();
057            var chainActionTypeTransfer = chainControl.getChainActionTypeTransfer(userVisit, chainActionType);
058            var sortOrder = chainActionDetail.getSortOrder();
059            var description = chainControl.getBestChainActionDescription(chainAction, getLanguage(userVisit));
060
061            chainActionTransfer = new ChainActionTransfer(chainActionSetTransfer, chainActionName, chainActionTypeTransfer, sortOrder, description);
062            put(userVisit, chainAction, chainActionTransfer);
063
064            if(includeRelated) {
065                var chainActionTypeName = chainActionType.getLastDetail().getChainActionTypeName();
066                if(chainActionTypeName.equals(ChainConstants.ChainActionType_LETTER)) {
067                    chainActionTransfer.setChainActionLetter(chainControl.getChainActionLetterTransfer(userVisit, chainAction));
068                } else if(chainActionTypeName.equals(ChainConstants.ChainActionType_SURVEY)) {
069                    chainActionTransfer.setChainActionSurvey(chainControl.getChainActionSurveyTransfer(userVisit, chainAction));
070                } else if(chainActionTypeName.equals(ChainConstants.ChainActionType_CHAIN_ACTION_SET)) {
071                    chainActionTransfer.setChainActionChainActionSet(chainControl.getChainActionChainActionSetTransfer(userVisit, chainAction));
072                }
073            }
074        }
075
076        return chainActionTransfer;
077    }
078}