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.chain.server.command; 018 019import com.echothree.control.user.chain.common.edit.ChainActionEdit; 020import com.echothree.control.user.chain.common.edit.ChainEditFactory; 021import com.echothree.control.user.chain.common.form.EditChainActionForm; 022import com.echothree.control.user.chain.common.result.ChainResultFactory; 023import com.echothree.control.user.chain.common.result.EditChainActionResult; 024import com.echothree.control.user.chain.common.spec.ChainActionSpec; 025import com.echothree.model.control.chain.server.control.ChainControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.chain.server.entity.ChainAction; 030import com.echothree.model.data.chain.server.entity.ChainActionSet; 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.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043 044@Dependent 045public class EditChainActionCommand 046 extends BaseAbstractEditCommand<ChainActionSpec, ChainActionEdit, EditChainActionResult, ChainAction, ChainAction> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.ChainAction.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("ChainActionSetName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("ChainActionName", FieldType.ENTITY_NAME, true, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("ChainActionName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 071 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 072 ); 073 } 074 075 /** Creates a new instance of EditChainActionCommand */ 076 public EditChainActionCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditChainActionResult getResult() { 082 return ChainResultFactory.getEditChainActionResult(); 083 } 084 085 @Override 086 public ChainActionEdit getEdit() { 087 return ChainEditFactory.getChainActionEdit(); 088 } 089 090 ChainActionSet chainActionSet; 091 092 @Override 093 public ChainAction getEntity(EditChainActionResult result) { 094 var chainControl = Session.getModelController(ChainControl.class); 095 ChainAction chainAction = null; 096 var chainKindName = spec.getChainKindName(); 097 var chainKind = chainControl.getChainKindByName(chainKindName); 098 099 if(chainKind != null) { 100 var chainTypeName = spec.getChainTypeName(); 101 var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName); 102 103 if(chainType != null) { 104 var chainName = spec.getChainName(); 105 var chain = chainControl.getChainByName(chainType, chainName); 106 107 if(chain != null) { 108 var chainActionSetName = spec.getChainActionSetName(); 109 110 chainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName); 111 112 if(chainActionSet != null) { 113 var chainActionName = spec.getChainActionName(); 114 115 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 116 chainAction = chainControl.getChainActionByName(chainActionSet, chainActionName); 117 } else { // EditMode.UPDATE 118 chainAction = chainControl.getChainActionByNameForUpdate(chainActionSet, chainActionName); 119 } 120 121 if(chainAction == null) { 122 addExecutionError(ExecutionErrors.UnknownChainActionName.name(), chainKindName, chainTypeName, chainName, chainActionSetName, 123 chainActionName); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownChainActionSetName.name(), chainKindName, chainTypeName, chainName, chainActionSetName); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownChainName.name(), chainKindName, chainTypeName, chainName); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName); 136 } 137 138 return chainAction; 139 } 140 141 @Override 142 public ChainAction getLockEntity(ChainAction chainAction) { 143 return chainAction; 144 } 145 146 @Override 147 public void fillInResult(EditChainActionResult result, ChainAction chainAction) { 148 var chainControl = Session.getModelController(ChainControl.class); 149 150 result.setChainAction(chainControl.getChainActionTransfer(getUserVisit(), chainAction)); 151 } 152 153 @Override 154 public void doLock(ChainActionEdit edit, ChainAction chainAction) { 155 var chainControl = Session.getModelController(ChainControl.class); 156 var chainActionDescription = chainControl.getChainActionDescription(chainAction, getPreferredLanguage()); 157 var chainActionDetail = chainAction.getLastDetail(); 158 159 edit.setChainActionName(chainActionDetail.getChainActionName()); 160 edit.setSortOrder(chainActionDetail.getSortOrder().toString()); 161 162 if(chainActionDescription != null) { 163 edit.setDescription(chainActionDescription.getDescription()); 164 } 165 } 166 167 @Override 168 public void canUpdate(ChainAction chainAction) { 169 var chainControl = Session.getModelController(ChainControl.class); 170 var chainActionName = edit.getChainActionName(); 171 var duplicateChainAction = chainControl.getChainActionByName(chainActionSet, chainActionName); 172 173 if(duplicateChainAction != null && !chainAction.equals(duplicateChainAction)) { 174 var chainActionSetDetail = chainActionSet.getLastDetail(); 175 var chainDetail = chainActionSetDetail.getChain().getLastDetail(); 176 var chainTypeDetail = chainDetail.getChainType().getLastDetail(); 177 var chainKindDetail = chainTypeDetail.getChainKind().getLastDetail(); 178 179 addExecutionError(ExecutionErrors.DuplicateChainActionName.name(), chainKindDetail.getChainKindName(), chainTypeDetail.getChainTypeName(), 180 chainDetail.getChainName(), chainActionSetDetail.getChainActionSetName(), chainActionName); 181 } 182 } 183 184 @Override 185 public void doUpdate(ChainAction chainAction) { 186 var chainControl = Session.getModelController(ChainControl.class); 187 var partyPK = getPartyPK(); 188 var chainActionDetailValue = chainControl.getChainActionDetailValueForUpdate(chainAction); 189 var chainActionDescription = chainControl.getChainActionDescriptionForUpdate(chainAction, getPreferredLanguage()); 190 var description = edit.getDescription(); 191 192 chainActionDetailValue.setChainActionName(edit.getChainActionName()); 193 chainActionDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 194 195 chainControl.updateChainActionFromValue(chainActionDetailValue, partyPK); 196 197 if(chainActionDescription == null && description != null) { 198 chainControl.createChainActionDescription(chainAction, getPreferredLanguage(), description, partyPK); 199 } else if(chainActionDescription != null && description == null) { 200 chainControl.deleteChainActionDescription(chainActionDescription, partyPK); 201 } else if(chainActionDescription != null && description != null) { 202 var chainActionDescriptionValue = chainControl.getChainActionDescriptionValue(chainActionDescription); 203 204 chainActionDescriptionValue.setDescription(description); 205 chainControl.updateChainActionDescriptionFromValue(chainActionDescriptionValue, partyPK); 206 } 207 } 208 209}