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