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