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.data.core.server.entity.Command;
026import com.echothree.model.data.core.server.entity.ComponentVendor;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.EditMode;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BaseAbstractEditCommand;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class EditCommandCommand
039        extends BaseAbstractEditCommand<CommandSpec, CommandEdit, EditCommandResult, Command, Command> {
040    
041    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
042    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
043    
044    static {
045        SPEC_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("CommandName", FieldType.COMMAND_NAME, true, null, null)
048        );
049        
050        EDIT_FIELD_DEFINITIONS = List.of(
051                new FieldDefinition("CommandName", FieldType.COMMAND_NAME, true, null, null),
052                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
053                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
054        );
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        Command command = null;
078        var componentVendorName = spec.getComponentVendorName();
079        
080        componentVendor = componentControl.getComponentVendorByName(componentVendorName);
081
082        if(componentVendor != null) {
083            var commandName = spec.getCommandName();
084
085            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
086                command = commandControl.getCommandByName(componentVendor, commandName);
087            } else { // EditMode.UPDATE
088                command = commandControl.getCommandByNameForUpdate(componentVendor, commandName);
089            }
090
091            if(command == null) {
092                addExecutionError(ExecutionErrors.UnknownCommandName.name(), componentVendorName, commandName);
093            }
094        } else {
095            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
096        }
097
098        return command;
099    }
100
101    @Override
102    public Command getLockEntity(Command command) {
103        return command;
104    }
105
106    @Override
107    public void fillInResult(EditCommandResult result, Command command) {
108        result.setCommand(commandControl.getCommandTransfer(getUserVisit(), command));
109    }
110
111    @Override
112    public void doLock(CommandEdit edit, Command command) {
113        var commandDescription = commandControl.getCommandDescription(command, getPreferredLanguage());
114        var commandDetail = command.getLastDetail();
115
116        edit.setCommandName(commandDetail.getCommandName());
117        edit.setSortOrder(commandDetail.getSortOrder().toString());
118
119        if(commandDescription != null) {
120            edit.setDescription(commandDescription.getDescription());
121        }
122    }
123
124    @Override
125    public void canUpdate(Command command) {
126        var commandName = edit.getCommandName();
127        var duplicateCommand = commandControl.getCommandByName(componentVendor, commandName);
128
129        if(duplicateCommand != null && !command.equals(duplicateCommand)) {
130            addExecutionError(ExecutionErrors.DuplicateCommandName.name(), commandName);
131        }
132    }
133
134    @Override
135    public void doUpdate(Command command) {
136        var partyPK = getPartyPK();
137        var commandDetailValue = commandControl.getCommandDetailValueForUpdate(command);
138        var commandDescription = commandControl.getCommandDescriptionForUpdate(command, getPreferredLanguage());
139        var description = edit.getDescription();
140
141        commandDetailValue.setCommandName(edit.getCommandName());
142        commandDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
143
144        commandControl.updateCommandFromValue(commandDetailValue, partyPK);
145
146        if(commandDescription == null && description != null) {
147            commandControl.createCommandDescription(command, getPreferredLanguage(), description, partyPK);
148        } else {
149            if(commandDescription != null && description == null) {
150                commandControl.deleteCommandDescription(commandDescription, partyPK);
151            } else {
152                if(commandDescription != null && description != null) {
153                    var commandDescriptionValue = commandControl.getCommandDescriptionValue(commandDescription);
154
155                    commandDescriptionValue.setDescription(description);
156                    commandControl.updateCommandDescriptionFromValue(commandDescriptionValue, partyPK);
157                }
158            }
159        }
160    }
161     
162}