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.ChainActionSetEdit; 020import com.echothree.control.user.chain.common.edit.ChainEditFactory; 021import com.echothree.control.user.chain.common.form.EditChainActionSetForm; 022import com.echothree.control.user.chain.common.result.ChainResultFactory; 023import com.echothree.control.user.chain.common.result.EditChainActionSetResult; 024import com.echothree.control.user.chain.common.spec.ChainActionSetSpec; 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.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 java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class EditChainActionSetCommand 046 extends BaseAbstractEditCommand<ChainActionSetSpec, ChainActionSetEdit, EditChainActionSetResult, ChainActionSet, ChainActionSet> { 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.ChainActionSet.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 ); 066 067 EDIT_FIELD_DEFINITIONS = List.of( 068 new FieldDefinition("ChainActionSetName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("IsDefault", FieldType.BOOLEAN, 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 @Inject 076 ChainControl chainControl; 077 078 /** Creates a new instance of EditChainActionSetCommand */ 079 public EditChainActionSetCommand() { 080 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 081 } 082 083 @Override 084 public EditChainActionSetResult getResult() { 085 return ChainResultFactory.getEditChainActionSetResult(); 086 } 087 088 @Override 089 public ChainActionSetEdit getEdit() { 090 return ChainEditFactory.getChainActionSetEdit(); 091 } 092 093 Chain chain; 094 095 @Override 096 public ChainActionSet getEntity(EditChainActionSetResult result) { 097 ChainActionSet chainActionSet = 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 108 chain = chainControl.getChainByName(chainType, chainName); 109 110 if(chain != null) { 111 var chainActionSetName = spec.getChainActionSetName(); 112 113 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 114 chainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName); 115 } else { // EditMode.UPDATE 116 chainActionSet = chainControl.getChainActionSetByNameForUpdate(chain, chainActionSetName); 117 } 118 119 if(chainActionSet == null) { 120 addExecutionError(ExecutionErrors.UnknownChainActionSetName.name(), chainKindName, chainTypeName, chainName, chainActionSetName); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.UnknownChainName.name(), chainKindName, chainTypeName, chainName); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName); 130 } 131 132 return chainActionSet; 133 } 134 135 @Override 136 public ChainActionSet getLockEntity(ChainActionSet chainActionSet) { 137 return chainActionSet; 138 } 139 140 @Override 141 public void fillInResult(EditChainActionSetResult result, ChainActionSet chainActionSet) { 142 result.setChainActionSet(chainControl.getChainActionSetTransfer(getUserVisit(), chainActionSet)); 143 } 144 145 @Override 146 public void doLock(ChainActionSetEdit edit, ChainActionSet chainActionSet) { 147 var chainActionSetDescription = chainControl.getChainActionSetDescription(chainActionSet, getPreferredLanguage()); 148 var chainActionSetDetail = chainActionSet.getLastDetail(); 149 150 edit.setChainActionSetName(chainActionSetDetail.getChainActionSetName()); 151 edit.setIsDefault(chainActionSetDetail.getIsDefault().toString()); 152 edit.setSortOrder(chainActionSetDetail.getSortOrder().toString()); 153 154 if(chainActionSetDescription != null) { 155 edit.setDescription(chainActionSetDescription.getDescription()); 156 } 157 } 158 159 @Override 160 public void canUpdate(ChainActionSet chainActionSet) { 161 var chainActionSetName = edit.getChainActionSetName(); 162 var duplicateChainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName); 163 164 if(duplicateChainActionSet != null && !chainActionSet.equals(duplicateChainActionSet)) { 165 var chainDetail = chain.getLastDetail(); 166 var chainTypeDetail = chainDetail.getChainType().getLastDetail(); 167 var chainKindDetail = chainTypeDetail.getChainKind().getLastDetail(); 168 169 addExecutionError(ExecutionErrors.DuplicateChainActionSetName.name(), chainKindDetail.getChainKindName(), chainTypeDetail.getChainTypeName(), 170 chainDetail.getChainName(), chainActionSetName); 171 } 172 } 173 174 @Override 175 public void doUpdate(ChainActionSet chainActionSet) { 176 var partyPK = getPartyPK(); 177 var chainActionSetDetailValue = chainControl.getChainActionSetDetailValueForUpdate(chainActionSet); 178 var chainActionSetDescription = chainControl.getChainActionSetDescriptionForUpdate(chainActionSet, getPreferredLanguage()); 179 var description = edit.getDescription(); 180 181 chainActionSetDetailValue.setChainActionSetName(edit.getChainActionSetName()); 182 chainActionSetDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 183 chainActionSetDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 184 185 chainControl.updateChainActionSetFromValue(chainActionSetDetailValue, partyPK); 186 187 if(chainActionSetDescription == null && description != null) { 188 chainControl.createChainActionSetDescription(chainActionSet, getPreferredLanguage(), description, partyPK); 189 } else if(chainActionSetDescription != null && description == null) { 190 chainControl.deleteChainActionSetDescription(chainActionSetDescription, partyPK); 191 } else if(chainActionSetDescription != null && description != null) { 192 var chainActionSetDescriptionValue = chainControl.getChainActionSetDescriptionValue(chainActionSetDescription); 193 194 chainActionSetDescriptionValue.setDescription(description); 195 chainControl.updateChainActionSetDescriptionFromValue(chainActionSetDescriptionValue, partyPK); 196 } 197 } 198 199}