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.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.EmployeeResultFactory; 023import com.echothree.control.user.employee.common.spec.TerminationReasonSpec; 024import com.echothree.model.control.employee.server.control.EmployeeControl; 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.ArrayList; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class EditTerminationReasonCommand 045 extends BaseEditCommand<TerminationReasonSpec, TerminationReasonEdit> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.TerminationReason.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("TerminationReasonName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("TerminationReasonName", 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 EmployeeControl employeeControl; 073 074 075 /** Creates a new instance of EditTerminationReasonCommand */ 076 public EditTerminationReasonCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var result = EmployeeResultFactory.getEditTerminationReasonResult(); 083 084 if(editMode.equals(EditMode.LOCK)) { 085 var terminationReasonName = spec.getTerminationReasonName(); 086 var terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName); 087 088 if(terminationReason != null) { 089 result.setTerminationReason(employeeControl.getTerminationReasonTransfer(getUserVisit(), terminationReason)); 090 091 if(lockEntity(terminationReason)) { 092 var terminationReasonDescription = employeeControl.getTerminationReasonDescription(terminationReason, getPreferredLanguage()); 093 var edit = EmployeeEditFactory.getTerminationReasonEdit(); 094 var terminationReasonDetail = terminationReason.getLastDetail(); 095 096 result.setEdit(edit); 097 edit.setTerminationReasonName(terminationReasonDetail.getTerminationReasonName()); 098 edit.setIsDefault(terminationReasonDetail.getIsDefault().toString()); 099 edit.setSortOrder(terminationReasonDetail.getSortOrder().toString()); 100 101 if(terminationReasonDescription != null) 102 edit.setDescription(terminationReasonDescription.getDescription()); 103 } else { 104 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 105 } 106 107 result.setEntityLock(getEntityLockTransfer(terminationReason)); 108 } else { 109 addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName); 110 } 111 } else if(editMode.equals(EditMode.UPDATE)) { 112 var terminationReasonName = spec.getTerminationReasonName(); 113 var terminationReason = employeeControl.getTerminationReasonByNameForUpdate(terminationReasonName); 114 115 if(terminationReason != null) { 116 if(lockEntityForUpdate(terminationReason)) { 117 try { 118 var partyPK = getPartyPK(); 119 var terminationReasonDetailValue = employeeControl.getTerminationReasonDetailValueForUpdate(terminationReason); 120 var terminationReasonDescription = employeeControl.getTerminationReasonDescriptionForUpdate(terminationReason, getPreferredLanguage()); 121 var description = edit.getDescription(); 122 123 terminationReasonDetailValue.setTerminationReasonName(edit.getTerminationReasonName()); 124 terminationReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 125 terminationReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 126 127 employeeControl.updateTerminationReasonFromValue(terminationReasonDetailValue, partyPK); 128 129 if(terminationReasonDescription == null && description != null) { 130 employeeControl.createTerminationReasonDescription(terminationReason, getPreferredLanguage(), description, partyPK); 131 } else if(terminationReasonDescription != null && description == null) { 132 employeeControl.deleteTerminationReasonDescription(terminationReasonDescription, partyPK); 133 } else if(terminationReasonDescription != null && description != null) { 134 var terminationReasonDescriptionValue = employeeControl.getTerminationReasonDescriptionValue(terminationReasonDescription); 135 136 terminationReasonDescriptionValue.setDescription(description); 137 employeeControl.updateTerminationReasonDescriptionFromValue(terminationReasonDescriptionValue, partyPK); 138 } 139 } finally { 140 unlockEntity(terminationReason); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.EntityLockStale.name()); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName); 147 } 148 } 149 150 return result; 151 } 152 153}