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