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