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.CancellationPolicyEditFactory; 020import com.echothree.control.user.cancellationpolicy.common.edit.CancellationReasonEdit; 021import com.echothree.control.user.cancellationpolicy.common.form.EditCancellationReasonForm; 022import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory; 023import com.echothree.control.user.cancellationpolicy.common.spec.CancellationReasonSpec; 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.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.command.EditMode; 034import com.echothree.util.server.control.BaseEditCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import java.util.List; 039import javax.enterprise.context.Dependent; 040import javax.inject.Inject; 041 042@Dependent 043public class EditCancellationReasonCommand 044 extends BaseEditCommand<CancellationReasonSpec, CancellationReasonEdit> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 048 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.CancellationReason.name(), SecurityRoles.Edit.name()) 055 )) 056 )); 057 058 SPEC_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("CancellationReasonName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("CancellationReasonName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 ); 069 } 070 071 @Inject 072 CancellationPolicyControl cancellationPolicyControl; 073 074 075 /** Creates a new instance of EditCancellationReasonCommand */ 076 public EditCancellationReasonCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var result = CancellationPolicyResultFactory.getEditCancellationReasonResult(); 083 var cancellationKindName = spec.getCancellationKindName(); 084 var cancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName); 085 086 if(cancellationKind != null) { 087 if(editMode.equals(EditMode.LOCK)) { 088 var cancellationReasonName = spec.getCancellationReasonName(); 089 var cancellationReason = cancellationPolicyControl.getCancellationReasonByName(cancellationKind, cancellationReasonName); 090 091 if(cancellationReason != null) { 092 result.setCancellationReason(cancellationPolicyControl.getCancellationReasonTransfer(getUserVisit(), cancellationReason)); 093 094 if(lockEntity(cancellationReason)) { 095 var cancellationReasonDescription = cancellationPolicyControl.getCancellationReasonDescription(cancellationReason, getPreferredLanguage()); 096 var edit = CancellationPolicyEditFactory.getCancellationReasonEdit(); 097 var cancellationReasonDetail = cancellationReason.getLastDetail(); 098 099 result.setEdit(edit); 100 edit.setCancellationReasonName(cancellationReasonDetail.getCancellationReasonName()); 101 edit.setIsDefault(cancellationReasonDetail.getIsDefault().toString()); 102 edit.setSortOrder(cancellationReasonDetail.getSortOrder().toString()); 103 104 if(cancellationReasonDescription != null) 105 edit.setDescription(cancellationReasonDescription.getDescription()); 106 } else { 107 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 108 } 109 110 result.setEntityLock(getEntityLockTransfer(cancellationReason)); 111 } else { 112 addExecutionError(ExecutionErrors.UnknownCancellationReasonName.name(), cancellationReasonName); 113 } 114 } else if(editMode.equals(EditMode.UPDATE)) { 115 var cancellationReasonName = spec.getCancellationReasonName(); 116 var cancellationReason = cancellationPolicyControl.getCancellationReasonByNameForUpdate(cancellationKind, cancellationReasonName); 117 118 if(cancellationReason != null) { 119 cancellationReasonName = edit.getCancellationReasonName(); 120 var duplicateCancellationReason = cancellationPolicyControl.getCancellationReasonByName(cancellationKind, cancellationReasonName); 121 122 if(duplicateCancellationReason == null || cancellationReason.equals(duplicateCancellationReason)) { 123 if(lockEntityForUpdate(cancellationReason)) { 124 try { 125 var partyPK = getPartyPK(); 126 var cancellationReasonDetailValue = cancellationPolicyControl.getCancellationReasonDetailValueForUpdate(cancellationReason); 127 var cancellationReasonDescription = cancellationPolicyControl.getCancellationReasonDescriptionForUpdate(cancellationReason, getPreferredLanguage()); 128 var description = edit.getDescription(); 129 130 cancellationReasonDetailValue.setCancellationReasonName(edit.getCancellationReasonName()); 131 cancellationReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 132 cancellationReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 133 134 cancellationPolicyControl.updateCancellationReasonFromValue(cancellationReasonDetailValue, partyPK); 135 136 if(cancellationReasonDescription == null && description != null) { 137 cancellationPolicyControl.createCancellationReasonDescription(cancellationReason, getPreferredLanguage(), description, partyPK); 138 } else if(cancellationReasonDescription != null && description == null) { 139 cancellationPolicyControl.deleteCancellationReasonDescription(cancellationReasonDescription, partyPK); 140 } else if(cancellationReasonDescription != null && description != null) { 141 var cancellationReasonDescriptionValue = cancellationPolicyControl.getCancellationReasonDescriptionValue(cancellationReasonDescription); 142 143 cancellationReasonDescriptionValue.setDescription(description); 144 cancellationPolicyControl.updateCancellationReasonDescriptionFromValue(cancellationReasonDescriptionValue, partyPK); 145 } 146 } finally { 147 unlockEntity(cancellationReason); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.EntityLockStale.name()); 151 } 152 } else { 153 addExecutionError(ExecutionErrors.DuplicateCancellationReasonName.name(), cancellationReasonName); 154 } 155 } else { 156 addExecutionError(ExecutionErrors.UnknownCancellationReasonName.name(), cancellationReasonName); 157 } 158 } 159 } else { 160 addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName); 161 } 162 163 return result; 164 } 165 166}