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.core.server.control.CommandControl;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.data.core.server.entity.Command;
028import com.echothree.model.data.core.server.entity.CommandDescription;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.List;
037import javax.enterprise.context.Dependent;
038
039@Dependent
040public class EditCommandDescriptionCommand
041        extends BaseAbstractEditCommand<CommandDescriptionSpec, CommandDescriptionEdit, EditCommandDescriptionResult, CommandDescription, Command> {
042    
043    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
044    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
045    
046    static {
047        SPEC_FIELD_DEFINITIONS = List.of(
048                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("CommandName", FieldType.COMMAND_NAME, true, null, null),
050                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
051                );
052        
053        EDIT_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
055                );
056    }
057    
058    /** Creates a new instance of EditCommandDescriptionCommand */
059    public EditCommandDescriptionCommand() {
060        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
061    }
062    
063    @Override
064    public EditCommandDescriptionResult getResult() {
065        return CoreResultFactory.getEditCommandDescriptionResult();
066    }
067
068    @Override
069    public CommandDescriptionEdit getEdit() {
070        return CoreEditFactory.getCommandDescriptionEdit();
071    }
072
073    @Override
074    public CommandDescription getEntity(EditCommandDescriptionResult result) {
075        var commandControl = Session.getModelController(CommandControl.class);
076        CommandDescription commandDescription = null;
077        var componentVendorName = spec.getComponentVendorName();
078        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
079
080        if(componentVendor != null) {
081            var commandName = spec.getCommandName();
082            var command = commandControl.getCommandByName(componentVendor, commandName);
083
084            if(command != null) {
085                var partyControl = Session.getModelController(PartyControl.class);
086                var languageIsoName = spec.getLanguageIsoName();
087                var language = partyControl.getLanguageByIsoName(languageIsoName);
088
089                if(language != null) {
090                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
091                        commandDescription = commandControl.getCommandDescription(command, language);
092                    } else { // EditMode.UPDATE
093                        commandDescription = commandControl.getCommandDescriptionForUpdate(command, language);
094                    }
095
096                    if(commandDescription == null) {
097                        addExecutionError(ExecutionErrors.UnknownCommandDescription.name(), componentVendorName, commandName, languageIsoName);
098                    }
099                } else {
100                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
101                }
102            } else {
103                addExecutionError(ExecutionErrors.UnknownCommandName.name(), componentVendorName, commandName);
104            }
105        } else {
106            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
107        }
108
109        return commandDescription;
110    }
111
112    @Override
113    public Command getLockEntity(CommandDescription commandDescription) {
114        return commandDescription.getCommand();
115    }
116
117    @Override
118    public void fillInResult(EditCommandDescriptionResult result, CommandDescription commandDescription) {
119        var commandControl = Session.getModelController(CommandControl.class);
120
121        result.setCommandDescription(commandControl.getCommandDescriptionTransfer(getUserVisit(), commandDescription));
122    }
123
124    @Override
125    public void doLock(CommandDescriptionEdit edit, CommandDescription commandDescription) {
126        edit.setDescription(commandDescription.getDescription());
127    }
128
129    @Override
130    public void doUpdate(CommandDescription commandDescription) {
131        var commandControl = Session.getModelController(CommandControl.class);
132        var commandDescriptionValue = commandControl.getCommandDescriptionValue(commandDescription);
133        commandDescriptionValue.setDescription(edit.getDescription());
134
135        commandControl.updateCommandDescriptionFromValue(commandDescriptionValue, getPartyPK());
136    }
137    
138}