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.CommandDescriptionEdit;
020import com.echothree.control.user.core.common.edit.CoreEditFactory;
021import com.echothree.control.user.core.common.form.EditCommandDescriptionForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditCommandDescriptionResult;
024import com.echothree.control.user.core.common.spec.CommandDescriptionSpec;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.data.core.server.entity.Command;
027import com.echothree.model.data.core.server.entity.CommandDescription;
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 EditCommandDescriptionCommand
040        extends BaseAbstractEditCommand<CommandDescriptionSpec, CommandDescriptionEdit, EditCommandDescriptionResult, CommandDescription, Command> {
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("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("CommandName", 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("Description", FieldType.STRING, true, 1L, 132L)
054        );
055    }
056
057    @Inject
058    PartyControl partyControl;
059
060    
061    /** Creates a new instance of EditCommandDescriptionCommand */
062    public EditCommandDescriptionCommand() {
063        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
064    }
065    
066    @Override
067    public EditCommandDescriptionResult getResult() {
068        return CoreResultFactory.getEditCommandDescriptionResult();
069    }
070
071    @Override
072    public CommandDescriptionEdit getEdit() {
073        return CoreEditFactory.getCommandDescriptionEdit();
074    }
075
076    @Override
077    public CommandDescription getEntity(EditCommandDescriptionResult result) {
078        CommandDescription commandDescription = null;
079        var componentVendorName = spec.getComponentVendorName();
080        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
081
082        if(componentVendor != null) {
083            var commandName = spec.getCommandName();
084            var command = commandControl.getCommandByName(componentVendor, commandName);
085
086            if(command != 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                        commandDescription = commandControl.getCommandDescription(command, language);
093                    } else { // EditMode.UPDATE
094                        commandDescription = commandControl.getCommandDescriptionForUpdate(command, language);
095                    }
096
097                    if(commandDescription == null) {
098                        addExecutionError(ExecutionErrors.UnknownCommandDescription.name(), componentVendorName, commandName, languageIsoName);
099                    }
100                } else {
101                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
102                }
103            } else {
104                addExecutionError(ExecutionErrors.UnknownCommandName.name(), componentVendorName, commandName);
105            }
106        } else {
107            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
108        }
109
110        return commandDescription;
111    }
112
113    @Override
114    public Command getLockEntity(CommandDescription commandDescription) {
115        return commandDescription.getCommand();
116    }
117
118    @Override
119    public void fillInResult(EditCommandDescriptionResult result, CommandDescription commandDescription) {
120        result.setCommandDescription(commandControl.getCommandDescriptionTransfer(getUserVisit(), commandDescription));
121    }
122
123    @Override
124    public void doLock(CommandDescriptionEdit edit, CommandDescription commandDescription) {
125        edit.setDescription(commandDescription.getDescription());
126    }
127
128    @Override
129    public void doUpdate(CommandDescription commandDescription) {
130        var commandDescriptionValue = commandControl.getCommandDescriptionValue(commandDescription);
131        commandDescriptionValue.setDescription(edit.getDescription());
132
133        commandControl.updateCommandDescriptionFromValue(commandDescriptionValue, getPartyPK());
134    }
135    
136}