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