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.core.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.core.common.CoreOptions;
021import com.echothree.model.control.core.common.transfer.CommandMessageTransfer;
022import com.echothree.model.control.core.common.transfer.CommandMessageTranslationTransfer;
023import com.echothree.model.control.core.server.control.CommandControl;
024import com.echothree.model.data.core.server.entity.CommandMessage;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.util.common.transfer.MapWrapper;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class CommandMessageTransferCache
031        extends BaseCoreTransferCache<CommandMessage, CommandMessageTransfer> {
032
033    @Inject
034    CommandControl commandControl;
035
036    boolean includeTranslations;
037    
038    /** Creates a new instance of CommandMessageTransferCache */
039    protected CommandMessageTransferCache() {
040        super();
041        
042        var options = session.getOptions();
043        if(options != null) {
044            includeTranslations = options.contains(CoreOptions.CommandMessageIncludeTranslations);
045        }
046        
047        setIncludeEntityInstance(true);
048    }
049    
050    public CommandMessageTransfer getCommandMessageTransfer(UserVisit userVisit, CommandMessage commandMessage) {
051        var commandMessageTransfer = get(commandMessage);
052        
053        if(commandMessageTransfer == null) {
054            var commandMessageDetail = commandMessage.getLastDetail();
055            var commandMessageType = commandControl.getCommandMessageTypeTransfer(userVisit, commandMessageDetail.getCommandMessageType());
056            var commandMessageKey = commandMessageDetail.getCommandMessageKey();
057            var commandMessageTranslation = commandControl.getBestCommandMessageTranslation(commandMessage, getLanguage(userVisit));
058            var translation = commandMessageTranslation == null ? null : commandMessageTranslation.getTranslation();
059    
060            commandMessageTransfer = new CommandMessageTransfer(commandMessageType, commandMessageKey, translation);
061            put(userVisit, commandMessage, commandMessageTransfer);
062            
063            if(includeTranslations) {
064                var commandMessageTranslationTransfers = commandControl.getCommandMessageTranslationTransfersByCommandMessage(userVisit, commandMessage);
065                var commandMessageTranslations = new MapWrapper<CommandMessageTranslationTransfer>();
066
067                commandMessageTranslationTransfers.forEach((commandMessageTranslationTransfer) -> {
068                    commandMessageTranslations.put(commandMessageTranslationTransfer.getLanguage().getLanguageIsoName(), commandMessageTranslationTransfer);
069                });
070
071                commandMessageTransfer.setCommandMessageTranslations(commandMessageTranslations);
072            }
073        }
074        
075        return commandMessageTransfer;
076    }
077    
078}