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.cancellationpolicy.server.command; 018 019import com.echothree.control.user.cancellationpolicy.common.edit.CancellationKindEdit; 020import com.echothree.control.user.cancellationpolicy.common.edit.CancellationPolicyEditFactory; 021import com.echothree.control.user.cancellationpolicy.common.form.EditCancellationKindForm; 022import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory; 023import com.echothree.control.user.cancellationpolicy.common.spec.CancellationKindSpec; 024import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.control.sequence.server.control.SequenceControl; 029import com.echothree.model.data.sequence.server.entity.SequenceType; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.server.control.BaseEditCommand; 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 EditCancellationKindCommand 046 extends BaseEditCommand<CancellationKindSpec, CancellationKindEdit> { 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.CancellationKind.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null) 062 ); 063 064 EDIT_FIELD_DEFINITIONS = List.of( 065 new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("CancellationSequenceTypeName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 068 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 069 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 070 ); 071 } 072 073 @Inject 074 CancellationPolicyControl cancellationPolicyControl; 075 076 @Inject 077 SequenceControl sequenceControl; 078 079 080 /** Creates a new instance of EditCancellationKindCommand */ 081 public EditCancellationKindCommand() { 082 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 083 } 084 085 @Override 086 protected BaseResult execute() { 087 var result = CancellationPolicyResultFactory.getEditCancellationKindResult(); 088 089 if(editMode.equals(EditMode.LOCK)) { 090 var cancellationKindName = spec.getCancellationKindName(); 091 var cancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName); 092 093 if(cancellationKind != null) { 094 result.setCancellationKind(cancellationPolicyControl.getCancellationKindTransfer(getUserVisit(), cancellationKind)); 095 096 if(lockEntity(cancellationKind)) { 097 var cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescription(cancellationKind, getPreferredLanguage()); 098 var edit = CancellationPolicyEditFactory.getCancellationKindEdit(); 099 var cancellationKindDetail = cancellationKind.getLastDetail(); 100 var cancellationSequenceType = cancellationKindDetail.getCancellationSequenceType(); 101 102 result.setEdit(edit); 103 edit.setCancellationKindName(cancellationKindDetail.getCancellationKindName()); 104 edit.setCancellationSequenceTypeName(cancellationSequenceType == null? null: cancellationSequenceType.getLastDetail().getSequenceTypeName()); 105 edit.setIsDefault(cancellationKindDetail.getIsDefault().toString()); 106 edit.setSortOrder(cancellationKindDetail.getSortOrder().toString()); 107 108 if(cancellationKindDescription != null) 109 edit.setDescription(cancellationKindDescription.getDescription()); 110 } else { 111 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 112 } 113 114 result.setEntityLock(getEntityLockTransfer(cancellationKind)); 115 } else { 116 addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName); 117 } 118 } else if(editMode.equals(EditMode.UPDATE)) { 119 var cancellationKindName = spec.getCancellationKindName(); 120 var cancellationKind = cancellationPolicyControl.getCancellationKindByNameForUpdate(cancellationKindName); 121 122 if(cancellationKind != null) { 123 cancellationKindName = edit.getCancellationKindName(); 124 var duplicateCancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName); 125 126 if(duplicateCancellationKind == null || cancellationKind.equals(duplicateCancellationKind)) { 127 var cancellationSequenceTypeName = edit.getCancellationSequenceTypeName(); 128 SequenceType cancellationSequenceType = null; 129 130 if(cancellationSequenceTypeName != null) { 131 cancellationSequenceType = sequenceControl.getSequenceTypeByName(cancellationSequenceTypeName); 132 } 133 134 if(cancellationSequenceTypeName == null || cancellationSequenceType != null) { 135 if(lockEntityForUpdate(cancellationKind)) { 136 try { 137 var partyPK = getPartyPK(); 138 var cancellationKindDetailValue = cancellationPolicyControl.getCancellationKindDetailValueForUpdate(cancellationKind); 139 var cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescriptionForUpdate(cancellationKind, getPreferredLanguage()); 140 var description = edit.getDescription(); 141 142 cancellationKindDetailValue.setCancellationKindName(edit.getCancellationKindName()); 143 cancellationKindDetailValue.setCancellationSequenceTypePK(cancellationSequenceType == null? null: cancellationSequenceType.getPrimaryKey()); 144 cancellationKindDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 145 cancellationKindDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 146 147 cancellationPolicyControl.updateCancellationKindFromValue(cancellationKindDetailValue, partyPK); 148 149 if(cancellationKindDescription == null && description != null) { 150 cancellationPolicyControl.createCancellationKindDescription(cancellationKind, getPreferredLanguage(), description, partyPK); 151 } else if(cancellationKindDescription != null && description == null) { 152 cancellationPolicyControl.deleteCancellationKindDescription(cancellationKindDescription, partyPK); 153 } else if(cancellationKindDescription != null && description != null) { 154 var cancellationKindDescriptionValue = cancellationPolicyControl.getCancellationKindDescriptionValue(cancellationKindDescription); 155 156 cancellationKindDescriptionValue.setDescription(description); 157 cancellationPolicyControl.updateCancellationKindDescriptionFromValue(cancellationKindDescriptionValue, partyPK); 158 } 159 } finally { 160 unlockEntity(cancellationKind); 161 } 162 } else { 163 addExecutionError(ExecutionErrors.EntityLockStale.name()); 164 } 165 } else { 166 addExecutionError(ExecutionErrors.UnknownCancellationSequenceTypeName.name(), cancellationSequenceTypeName); 167 } 168 } else { 169 addExecutionError(ExecutionErrors.DuplicateCancellationKindName.name(), cancellationKindName); 170 } 171 } else { 172 addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName); 173 } 174 } 175 176 return result; 177 } 178 179}