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