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 com.echothree.util.server.persistence.Session; 039import java.util.ArrayList; 040import java.util.List; 041import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditTerminationReasonCommand */ 072 public EditTerminationReasonCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 protected BaseResult execute() { 078 var employeeControl = Session.getModelController(EmployeeControl.class); 079 var result = EmployeeResultFactory.getEditTerminationReasonResult(); 080 081 if(editMode.equals(EditMode.LOCK)) { 082 var terminationReasonName = spec.getTerminationReasonName(); 083 var terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName); 084 085 if(terminationReason != null) { 086 result.setTerminationReason(employeeControl.getTerminationReasonTransfer(getUserVisit(), terminationReason)); 087 088 if(lockEntity(terminationReason)) { 089 var terminationReasonDescription = employeeControl.getTerminationReasonDescription(terminationReason, getPreferredLanguage()); 090 var edit = EmployeeEditFactory.getTerminationReasonEdit(); 091 var terminationReasonDetail = terminationReason.getLastDetail(); 092 093 result.setEdit(edit); 094 edit.setTerminationReasonName(terminationReasonDetail.getTerminationReasonName()); 095 edit.setIsDefault(terminationReasonDetail.getIsDefault().toString()); 096 edit.setSortOrder(terminationReasonDetail.getSortOrder().toString()); 097 098 if(terminationReasonDescription != null) 099 edit.setDescription(terminationReasonDescription.getDescription()); 100 } else { 101 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 102 } 103 104 result.setEntityLock(getEntityLockTransfer(terminationReason)); 105 } else { 106 addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName); 107 } 108 } else if(editMode.equals(EditMode.UPDATE)) { 109 var terminationReasonName = spec.getTerminationReasonName(); 110 var terminationReason = employeeControl.getTerminationReasonByNameForUpdate(terminationReasonName); 111 112 if(terminationReason != null) { 113 if(lockEntityForUpdate(terminationReason)) { 114 try { 115 var partyPK = getPartyPK(); 116 var terminationReasonDetailValue = employeeControl.getTerminationReasonDetailValueForUpdate(terminationReason); 117 var terminationReasonDescription = employeeControl.getTerminationReasonDescriptionForUpdate(terminationReason, getPreferredLanguage()); 118 var description = edit.getDescription(); 119 120 terminationReasonDetailValue.setTerminationReasonName(edit.getTerminationReasonName()); 121 terminationReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 122 terminationReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 123 124 employeeControl.updateTerminationReasonFromValue(terminationReasonDetailValue, partyPK); 125 126 if(terminationReasonDescription == null && description != null) { 127 employeeControl.createTerminationReasonDescription(terminationReason, getPreferredLanguage(), description, partyPK); 128 } else if(terminationReasonDescription != null && description == null) { 129 employeeControl.deleteTerminationReasonDescription(terminationReasonDescription, partyPK); 130 } else if(terminationReasonDescription != null && description != null) { 131 var terminationReasonDescriptionValue = employeeControl.getTerminationReasonDescriptionValue(terminationReasonDescription); 132 133 terminationReasonDescriptionValue.setDescription(description); 134 employeeControl.updateTerminationReasonDescriptionFromValue(terminationReasonDescriptionValue, partyPK); 135 } 136 } finally { 137 unlockEntity(terminationReason); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.EntityLockStale.name()); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName); 144 } 145 } 146 147 return result; 148 } 149 150}