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