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.employee.server.command; 018 019import com.echothree.control.user.employee.common.edit.EmployeeEditFactory; 020import com.echothree.control.user.employee.common.edit.TerminationReasonEdit; 021import com.echothree.control.user.employee.common.form.EditTerminationReasonForm; 022import com.echothree.control.user.employee.common.result.EditTerminationReasonResult; 023import com.echothree.control.user.employee.common.result.EmployeeResultFactory; 024import com.echothree.control.user.employee.common.spec.TerminationReasonSpec; 025import com.echothree.model.control.employee.server.control.EmployeeControl; 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.employee.server.entity.TerminationReason; 030import com.echothree.model.data.employee.server.entity.TerminationReasonDescription; 031import com.echothree.model.data.employee.server.entity.TerminationReasonDetail; 032import com.echothree.model.data.employee.server.value.TerminationReasonDescriptionValue; 033import com.echothree.model.data.employee.server.value.TerminationReasonDetailValue; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.common.command.BaseResult; 039import com.echothree.util.common.command.EditMode; 040import com.echothree.util.server.control.BaseEditCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.Session; 045import java.util.ArrayList; 046import java.util.Collections; 047import java.util.List; 048 049public class EditTerminationReasonCommand 050 extends BaseEditCommand<TerminationReasonSpec, TerminationReasonEdit> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 054 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 060 new SecurityRoleDefinition(SecurityRoleGroups.TerminationReason.name(), SecurityRoles.Edit.name()) 061 )) 062 )); 063 064 List<FieldDefinition> temp = new ArrayList<>(1); 065 temp.add(new FieldDefinition("TerminationReasonName", FieldType.ENTITY_NAME, true, null, null)); 066 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 067 068 temp = new ArrayList<>(4); 069 temp.add(new FieldDefinition("TerminationReasonName", FieldType.ENTITY_NAME, true, null, null)); 070 temp.add(new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null)); 071 temp.add(new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)); 072 temp.add(new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)); 073 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 074 } 075 076 /** Creates a new instance of EditTerminationReasonCommand */ 077 public EditTerminationReasonCommand(UserVisitPK userVisitPK, EditTerminationReasonForm form) { 078 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 protected BaseResult execute() { 083 var employeeControl = Session.getModelController(EmployeeControl.class); 084 EditTerminationReasonResult result = EmployeeResultFactory.getEditTerminationReasonResult(); 085 086 if(editMode.equals(EditMode.LOCK)) { 087 String terminationReasonName = spec.getTerminationReasonName(); 088 TerminationReason terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName); 089 090 if(terminationReason != null) { 091 result.setTerminationReason(employeeControl.getTerminationReasonTransfer(getUserVisit(), terminationReason)); 092 093 if(lockEntity(terminationReason)) { 094 TerminationReasonDescription terminationReasonDescription = employeeControl.getTerminationReasonDescription(terminationReason, getPreferredLanguage()); 095 TerminationReasonEdit edit = EmployeeEditFactory.getTerminationReasonEdit(); 096 TerminationReasonDetail terminationReasonDetail = terminationReason.getLastDetail(); 097 098 result.setEdit(edit); 099 edit.setTerminationReasonName(terminationReasonDetail.getTerminationReasonName()); 100 edit.setIsDefault(terminationReasonDetail.getIsDefault().toString()); 101 edit.setSortOrder(terminationReasonDetail.getSortOrder().toString()); 102 103 if(terminationReasonDescription != null) 104 edit.setDescription(terminationReasonDescription.getDescription()); 105 } else { 106 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 107 } 108 109 result.setEntityLock(getEntityLockTransfer(terminationReason)); 110 } else { 111 addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName); 112 } 113 } else if(editMode.equals(EditMode.UPDATE)) { 114 String terminationReasonName = spec.getTerminationReasonName(); 115 TerminationReason terminationReason = employeeControl.getTerminationReasonByNameForUpdate(terminationReasonName); 116 117 if(terminationReason != null) { 118 if(lockEntityForUpdate(terminationReason)) { 119 try { 120 var partyPK = getPartyPK(); 121 TerminationReasonDetailValue terminationReasonDetailValue = employeeControl.getTerminationReasonDetailValueForUpdate(terminationReason); 122 TerminationReasonDescription terminationReasonDescription = employeeControl.getTerminationReasonDescriptionForUpdate(terminationReason, getPreferredLanguage()); 123 String description = edit.getDescription(); 124 125 terminationReasonDetailValue.setTerminationReasonName(edit.getTerminationReasonName()); 126 terminationReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 127 terminationReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 128 129 employeeControl.updateTerminationReasonFromValue(terminationReasonDetailValue, partyPK); 130 131 if(terminationReasonDescription == null && description != null) { 132 employeeControl.createTerminationReasonDescription(terminationReason, getPreferredLanguage(), description, partyPK); 133 } else if(terminationReasonDescription != null && description == null) { 134 employeeControl.deleteTerminationReasonDescription(terminationReasonDescription, partyPK); 135 } else if(terminationReasonDescription != null && description != null) { 136 TerminationReasonDescriptionValue terminationReasonDescriptionValue = employeeControl.getTerminationReasonDescriptionValue(terminationReasonDescription); 137 138 terminationReasonDescriptionValue.setDescription(description); 139 employeeControl.updateTerminationReasonDescriptionFromValue(terminationReasonDescriptionValue, partyPK); 140 } 141 } finally { 142 unlockEntity(terminationReason); 143 } 144 } else { 145 addExecutionError(ExecutionErrors.EntityLockStale.name()); 146 } 147 } else { 148 addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName); 149 } 150 } 151 152 return result; 153 } 154 155}