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.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.result.EditCancellationKindResult; 024import com.echothree.control.user.cancellationpolicy.common.spec.CancellationKindSpec; 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.control.sequence.server.control.SequenceControl; 030import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKind; 031import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKindDescription; 032import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKindDetail; 033import com.echothree.model.data.cancellationpolicy.server.value.CancellationKindDescriptionValue; 034import com.echothree.model.data.cancellationpolicy.server.value.CancellationKindDetailValue; 035import com.echothree.model.data.sequence.server.entity.SequenceType; 036import com.echothree.model.data.user.common.pk.UserVisitPK; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.common.command.BaseResult; 041import com.echothree.util.common.command.EditMode; 042import com.echothree.util.server.control.BaseEditCommand; 043import com.echothree.util.server.control.CommandSecurityDefinition; 044import com.echothree.util.server.control.PartyTypeDefinition; 045import com.echothree.util.server.control.SecurityRoleDefinition; 046import com.echothree.util.server.persistence.Session; 047import java.util.Arrays; 048import java.util.Collections; 049import java.util.List; 050 051public class EditCancellationKindCommand 052 extends BaseEditCommand<CancellationKindSpec, CancellationKindEdit> { 053 054 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 055 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 056 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 057 058 static { 059 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 060 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 061 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 062 new SecurityRoleDefinition(SecurityRoleGroups.CancellationKind.name(), SecurityRoles.Edit.name()) 063 ))) 064 ))); 065 066 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null) 068 )); 069 070 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 071 new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null), 072 new FieldDefinition("CancellationSequenceTypeName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 074 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 075 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 076 )); 077 } 078 079 /** Creates a new instance of EditCancellationKindCommand */ 080 public EditCancellationKindCommand(UserVisitPK userVisitPK, EditCancellationKindForm form) { 081 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 082 } 083 084 @Override 085 protected BaseResult execute() { 086 var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class); 087 EditCancellationKindResult result = CancellationPolicyResultFactory.getEditCancellationKindResult(); 088 089 if(editMode.equals(EditMode.LOCK)) { 090 String cancellationKindName = spec.getCancellationKindName(); 091 CancellationKind cancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName); 092 093 if(cancellationKind != null) { 094 result.setCancellationKind(cancellationPolicyControl.getCancellationKindTransfer(getUserVisit(), cancellationKind)); 095 096 if(lockEntity(cancellationKind)) { 097 CancellationKindDescription cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescription(cancellationKind, getPreferredLanguage()); 098 CancellationKindEdit edit = CancellationPolicyEditFactory.getCancellationKindEdit(); 099 CancellationKindDetail cancellationKindDetail = cancellationKind.getLastDetail(); 100 SequenceType 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 String cancellationKindName = spec.getCancellationKindName(); 120 CancellationKind cancellationKind = cancellationPolicyControl.getCancellationKindByNameForUpdate(cancellationKindName); 121 122 if(cancellationKind != null) { 123 cancellationKindName = edit.getCancellationKindName(); 124 CancellationKind duplicateCancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName); 125 126 if(duplicateCancellationKind == null || cancellationKind.equals(duplicateCancellationKind)) { 127 var sequenceControl = Session.getModelController(SequenceControl.class); 128 String cancellationSequenceTypeName = edit.getCancellationSequenceTypeName(); 129 SequenceType cancellationSequenceType = null; 130 131 if(cancellationSequenceTypeName != null) { 132 cancellationSequenceType = sequenceControl.getSequenceTypeByName(cancellationSequenceTypeName); 133 } 134 135 if(cancellationSequenceTypeName == null || cancellationSequenceType != null) { 136 if(lockEntityForUpdate(cancellationKind)) { 137 try { 138 var partyPK = getPartyPK(); 139 CancellationKindDetailValue cancellationKindDetailValue = cancellationPolicyControl.getCancellationKindDetailValueForUpdate(cancellationKind); 140 CancellationKindDescription cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescriptionForUpdate(cancellationKind, getPreferredLanguage()); 141 String description = edit.getDescription(); 142 143 cancellationKindDetailValue.setCancellationKindName(edit.getCancellationKindName()); 144 cancellationKindDetailValue.setCancellationSequenceTypePK(cancellationSequenceType == null? null: cancellationSequenceType.getPrimaryKey()); 145 cancellationKindDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 146 cancellationKindDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 147 148 cancellationPolicyControl.updateCancellationKindFromValue(cancellationKindDetailValue, partyPK); 149 150 if(cancellationKindDescription == null && description != null) { 151 cancellationPolicyControl.createCancellationKindDescription(cancellationKind, getPreferredLanguage(), description, partyPK); 152 } else if(cancellationKindDescription != null && description == null) { 153 cancellationPolicyControl.deleteCancellationKindDescription(cancellationKindDescription, partyPK); 154 } else if(cancellationKindDescription != null && description != null) { 155 CancellationKindDescriptionValue cancellationKindDescriptionValue = cancellationPolicyControl.getCancellationKindDescriptionValue(cancellationKindDescription); 156 157 cancellationKindDescriptionValue.setDescription(description); 158 cancellationPolicyControl.updateCancellationKindDescriptionFromValue(cancellationKindDescriptionValue, partyPK); 159 } 160 } finally { 161 unlockEntity(cancellationKind); 162 } 163 } else { 164 addExecutionError(ExecutionErrors.EntityLockStale.name()); 165 } 166 } else { 167 addExecutionError(ExecutionErrors.UnknownCancellationSequenceTypeName.name(), cancellationSequenceTypeName); 168 } 169 } else { 170 addExecutionError(ExecutionErrors.DuplicateCancellationKindName.name(), cancellationKindName); 171 } 172 } else { 173 addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName); 174 } 175 } 176 177 return result; 178 } 179 180}