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