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.CommandEdit;
020import com.echothree.control.user.core.common.edit.CoreEditFactory;
021import com.echothree.control.user.core.common.form.EditCommandForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditCommandResult;
024import com.echothree.control.user.core.common.spec.CommandSpec;
025import com.echothree.model.control.core.server.control.CommandControl;
026import com.echothree.model.data.core.server.entity.Command;
027import com.echothree.model.data.core.server.entity.ComponentVendor;
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 com.echothree.util.server.persistence.Session;
035import java.util.List;
036import javax.enterprise.context.Dependent;
037
038@Dependent
039public class EditCommandCommand
040        extends BaseAbstractEditCommand<CommandSpec, CommandEdit, EditCommandResult, Command, 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                );
050        
051        EDIT_FIELD_DEFINITIONS = List.of(
052                new FieldDefinition("CommandName", FieldType.COMMAND_NAME, true, null, null),
053                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
054                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
055                );
056    }
057    
058    /** Creates a new instance of EditCommandCommand */
059    public EditCommandCommand() {
060        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
061    }
062    
063    @Override
064    public EditCommandResult getResult() {
065        return CoreResultFactory.getEditCommandResult();
066    }
067
068    @Override
069    public CommandEdit getEdit() {
070        return CoreEditFactory.getCommandEdit();
071    }
072
073    ComponentVendor componentVendor = null;
074    
075    @Override
076    public Command getEntity(EditCommandResult result) {
077        var commandControl = Session.getModelController(CommandControl.class);
078        Command command = null;
079        var componentVendorName = spec.getComponentVendorName();
080        
081        componentVendor = componentControl.getComponentVendorByName(componentVendorName);
082
083        if(componentVendor != null) {
084            var commandName = spec.getCommandName();
085
086            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
087                command = commandControl.getCommandByName(componentVendor, commandName);
088            } else { // EditMode.UPDATE
089                command = commandControl.getCommandByNameForUpdate(componentVendor, commandName);
090            }
091
092            if(command == null) {
093                addExecutionError(ExecutionErrors.UnknownCommandName.name(), componentVendorName, commandName);
094            }
095        } else {
096            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
097        }
098
099        return command;
100    }
101
102    @Override
103    public Command getLockEntity(Command command) {
104        return command;
105    }
106
107    @Override
108    public void fillInResult(EditCommandResult result, Command command) {
109        var commandControl = Session.getModelController(CommandControl.class);
110
111        result.setCommand(commandControl.getCommandTransfer(getUserVisit(), command));
112    }
113
114    @Override
115    public void doLock(CommandEdit edit, Command command) {
116        var commandControl = Session.getModelController(CommandControl.class);
117        var commandDescription = commandControl.getCommandDescription(command, getPreferredLanguage());
118        var commandDetail = command.getLastDetail();
119
120        edit.setCommandName(commandDetail.getCommandName());
121        edit.setSortOrder(commandDetail.getSortOrder().toString());
122
123        if(commandDescription != null) {
124            edit.setDescription(commandDescription.getDescription());
125        }
126    }
127
128    @Override
129    public void canUpdate(Command command) {
130        var commandControl = Session.getModelController(CommandControl.class);
131        var commandName = edit.getCommandName();
132        var duplicateCommand = commandControl.getCommandByName(componentVendor, commandName);
133
134        if(duplicateCommand != null && !command.equals(duplicateCommand)) {
135            addExecutionError(ExecutionErrors.DuplicateCommandName.name(), commandName);
136        }
137    }
138
139    @Override
140    public void doUpdate(Command command) {
141        var commandControl = Session.getModelController(CommandControl.class);
142        var partyPK = getPartyPK();
143        var commandDetailValue = commandControl.getCommandDetailValueForUpdate(command);
144        var commandDescription = commandControl.getCommandDescriptionForUpdate(command, getPreferredLanguage());
145        var description = edit.getDescription();
146
147        commandDetailValue.setCommandName(edit.getCommandName());
148        commandDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
149
150        commandControl.updateCommandFromValue(commandDetailValue, partyPK);
151
152        if(commandDescription == null && description != null) {
153            commandControl.createCommandDescription(command, getPreferredLanguage(), description, partyPK);
154        } else {
155            if(commandDescription != null && description == null) {
156                commandControl.deleteCommandDescription(commandDescription, partyPK);
157            } else {
158                if(commandDescription != null && description != null) {
159                    var commandDescriptionValue = commandControl.getCommandDescriptionValue(commandDescription);
160
161                    commandDescriptionValue.setDescription(description);
162                    commandControl.updateCommandDescriptionFromValue(commandDescriptionValue, partyPK);
163                }
164            }
165        }
166    }
167     
168}