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.control.user.core.server.command;
018
019import com.echothree.control.user.core.common.form.GetCommandMessageTranslationForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.control.user.core.common.result.GetCommandMessageTranslationResult;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.data.core.server.entity.CommandMessage;
024import com.echothree.model.data.core.server.entity.CommandMessageTranslation;
025import com.echothree.model.data.core.server.entity.CommandMessageType;
026import com.echothree.model.data.party.server.entity.Language;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.persistence.Session;
034import java.util.Arrays;
035import java.util.Collections;
036import java.util.List;
037
038public class GetCommandMessageTranslationCommand
039        extends BaseSimpleCommand<GetCommandMessageTranslationForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
045                new FieldDefinition("CommandMessageTypeName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("CommandMessageKey", FieldType.COMMAND_NAME, true, null, null)
047                ));
048    }
049    
050    /** Creates a new instance of GetCommandMessageTranslationCommand */
051    public GetCommandMessageTranslationCommand(UserVisitPK userVisitPK, GetCommandMessageTranslationForm form) {
052        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var coreControl = getCoreControl();
058        GetCommandMessageTranslationResult result = CoreResultFactory.getGetCommandMessageTranslationResult();
059        String commandMessageTypeName = form.getCommandMessageTypeName();
060        CommandMessageType commandMessageType = coreControl.getCommandMessageTypeByName(commandMessageTypeName);
061
062        if(commandMessageType != null) {
063            String commandMessageKey = form.getCommandMessageKey();
064            CommandMessage commandMessage = coreControl.getCommandMessageByKey(commandMessageType, commandMessageKey);
065
066            if(commandMessage != null) {
067                var partyControl = Session.getModelController(PartyControl.class);
068                String languageIsoName = form.getLanguageIsoName();
069                Language language = partyControl.getLanguageByIsoName(languageIsoName);
070
071                if(language != null) {
072                    CommandMessageTranslation commandMessageTranslation = coreControl.getCommandMessageTranslation(commandMessage, language);
073
074                    if(commandMessageTranslation != null) {
075                        result.setCommandMessageTranslation(coreControl.getCommandMessageTranslationTransfer(getUserVisit(), commandMessageTranslation));
076                    } else {
077                        addExecutionError(ExecutionErrors.UnknownCommandMessageTranslation.name());
078                    }
079                } else {
080                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
081                }
082            } else {
083                addExecutionError(ExecutionErrors.UnknownCommandMessageKey.name(), commandMessageKey);
084            }
085        } else {
086            addExecutionError(ExecutionErrors.UnknownCommandMessageTypeName.name(), commandMessageTypeName);
087        }
088
089        return result;
090    }
091    
092}