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.edit.CommandMessageTranslationEdit;
020import com.echothree.control.user.core.common.edit.CoreEditFactory;
021import com.echothree.control.user.core.common.form.EditCommandMessageTranslationForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditCommandMessageTranslationResult;
024import com.echothree.control.user.core.common.spec.CommandMessageTranslationSpec;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.data.core.server.entity.CommandMessage;
027import com.echothree.model.data.core.server.entity.CommandMessageTranslation;
028import com.echothree.model.data.core.server.entity.CommandMessageType;
029import com.echothree.model.data.core.server.value.CommandMessageTranslationValue;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.persistence.Session;
038import java.util.Arrays;
039import java.util.Collections;
040import java.util.List;
041
042public class EditCommandMessageTranslationCommand
043        extends BaseAbstractEditCommand<CommandMessageTranslationSpec, CommandMessageTranslationEdit, EditCommandMessageTranslationResult, CommandMessageTranslation, CommandMessage> {
044    
045    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
046    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
047    
048    static {
049        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
050                new FieldDefinition("CommandMessageTypeName", FieldType.ENTITY_NAME, true, null, null),
051                new FieldDefinition("CommandMessageKey", FieldType.COMMAND_NAME, true, null, null),
052                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
053                ));
054        
055        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
056                new FieldDefinition("Translation", FieldType.STRING, true, 1L, 512L)
057                ));
058    }
059    
060    /** Creates a new instance of EditCommandMessageTranslationCommand */
061    public EditCommandMessageTranslationCommand(UserVisitPK userVisitPK, EditCommandMessageTranslationForm form) {
062        super(userVisitPK, form, null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
063    }
064    
065    @Override
066    public EditCommandMessageTranslationResult getResult() {
067        return CoreResultFactory.getEditCommandMessageTranslationResult();
068    }
069
070    @Override
071    public CommandMessageTranslationEdit getEdit() {
072        return CoreEditFactory.getCommandMessageTranslationEdit();
073    }
074
075    @Override
076    public CommandMessageTranslation getEntity(EditCommandMessageTranslationResult result) {
077        var coreControl = getCoreControl();
078        CommandMessageTranslation commandMessageTranslation = null;
079        String commandMessageTypeName = spec.getCommandMessageTypeName();
080        CommandMessageType commandMessageType = coreControl.getCommandMessageTypeByName(commandMessageTypeName);
081
082        if(commandMessageType != null) {
083            String commandMessageKey = spec.getCommandMessageKey();
084            CommandMessage commandMessage = coreControl.getCommandMessageByKey(commandMessageType, commandMessageKey);
085
086            if(commandMessage != null) {
087                var partyControl = Session.getModelController(PartyControl.class);
088                String languageIsoName = spec.getLanguageIsoName();
089                Language language = partyControl.getLanguageByIsoName(languageIsoName);
090
091                if(language != null) {
092                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
093                        commandMessageTranslation = coreControl.getCommandMessageTranslation(commandMessage, language);
094                    } else { // EditMode.UPDATE
095                        commandMessageTranslation = coreControl.getCommandMessageTranslationForUpdate(commandMessage, language);
096                    }
097
098                    if(commandMessageTranslation == null) {
099                        addExecutionError(ExecutionErrors.UnknownCommandMessageTranslation.name(), commandMessageTypeName, commandMessageKey, languageIsoName);
100                    }
101                } else {
102                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
103                }
104            } else {
105                addExecutionError(ExecutionErrors.UnknownCommandMessageKey.name(), commandMessageTypeName, commandMessageKey);
106            }
107        } else {
108            addExecutionError(ExecutionErrors.UnknownCommandMessageTypeName.name(), commandMessageTypeName);
109        }
110
111        return commandMessageTranslation;
112    }
113
114    @Override
115    public CommandMessage getLockEntity(CommandMessageTranslation commandMessageTranslation) {
116        return commandMessageTranslation.getCommandMessage();
117    }
118
119    @Override
120    public void fillInResult(EditCommandMessageTranslationResult result, CommandMessageTranslation commandMessageTranslation) {
121        var coreControl = getCoreControl();
122
123        result.setCommandMessageTranslation(coreControl.getCommandMessageTranslationTransfer(getUserVisit(), commandMessageTranslation));
124    }
125
126    @Override
127    public void doLock(CommandMessageTranslationEdit edit, CommandMessageTranslation commandMessageTranslation) {
128        edit.setTranslation(commandMessageTranslation.getTranslation());
129    }
130
131    @Override
132    public void doUpdate(CommandMessageTranslation commandMessageTranslation) {
133        var coreControl = getCoreControl();
134        CommandMessageTranslationValue commandMessageTranslationValue = coreControl.getCommandMessageTranslationValue(commandMessageTranslation);
135        commandMessageTranslationValue.setTranslation(edit.getTranslation());
136
137        coreControl.updateCommandMessageTranslationFromValue(commandMessageTranslationValue, getPartyPK());
138    }
139    
140}