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.LeaveReasonEdit; 021import com.echothree.control.user.employee.common.form.EditLeaveReasonForm; 022import com.echothree.control.user.employee.common.result.EditLeaveReasonResult; 023import com.echothree.control.user.employee.common.result.EmployeeResultFactory; 024import com.echothree.control.user.employee.common.spec.LeaveReasonSpec; 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.LeaveReason; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.EditMode; 035import com.echothree.util.server.control.BaseAbstractEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class EditLeaveReasonCommand 045 extends BaseAbstractEditCommand<LeaveReasonSpec, LeaveReasonEdit, EditLeaveReasonResult, LeaveReason, LeaveReason> { 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.LeaveReason.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("LeaveReasonName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("LeaveReasonName", 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 /** Creates a new instance of EditLeaveReasonCommand */ 075 public EditLeaveReasonCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 public EditLeaveReasonResult getResult() { 081 return EmployeeResultFactory.getEditLeaveReasonResult(); 082 } 083 084 @Override 085 public LeaveReasonEdit getEdit() { 086 return EmployeeEditFactory.getLeaveReasonEdit(); 087 } 088 089 @Override 090 public LeaveReason getEntity(EditLeaveReasonResult result) { 091 LeaveReason leaveReason; 092 var leaveReasonName = spec.getLeaveReasonName(); 093 094 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 095 leaveReason = employeeControl.getLeaveReasonByName(leaveReasonName); 096 } else { // EditMode.UPDATE 097 leaveReason = employeeControl.getLeaveReasonByNameForUpdate(leaveReasonName); 098 } 099 100 if(leaveReason != null) { 101 result.setLeaveReason(employeeControl.getLeaveReasonTransfer(getUserVisit(), leaveReason)); 102 } else { 103 addExecutionError(ExecutionErrors.UnknownLeaveReasonName.name(), leaveReasonName); 104 } 105 106 return leaveReason; 107 } 108 109 @Override 110 public LeaveReason getLockEntity(LeaveReason leaveReason) { 111 return leaveReason; 112 } 113 114 @Override 115 public void fillInResult(EditLeaveReasonResult result, LeaveReason leaveReason) { 116 result.setLeaveReason(employeeControl.getLeaveReasonTransfer(getUserVisit(), leaveReason)); 117 } 118 119 @Override 120 public void doLock(LeaveReasonEdit edit, LeaveReason leaveReason) { 121 var leaveReasonDescription = employeeControl.getLeaveReasonDescription(leaveReason, getPreferredLanguage()); 122 var leaveReasonDetail = leaveReason.getLastDetail(); 123 124 edit.setLeaveReasonName(leaveReasonDetail.getLeaveReasonName()); 125 edit.setIsDefault(leaveReasonDetail.getIsDefault().toString()); 126 edit.setSortOrder(leaveReasonDetail.getSortOrder().toString()); 127 128 if(leaveReasonDescription != null) { 129 edit.setDescription(leaveReasonDescription.getDescription()); 130 } 131 } 132 133 @Override 134 public void canUpdate(LeaveReason leaveReason) { 135 var leaveReasonName = edit.getLeaveReasonName(); 136 var duplicateLeaveReason = employeeControl.getLeaveReasonByName(leaveReasonName); 137 138 if(duplicateLeaveReason != null && !leaveReason.equals(duplicateLeaveReason)) { 139 addExecutionError(ExecutionErrors.DuplicateLeaveReasonName.name(), leaveReasonName); 140 } 141 } 142 143 @Override 144 public void doUpdate(LeaveReason leaveReason) { 145 var partyPK = getPartyPK(); 146 var leaveReasonDetailValue = employeeControl.getLeaveReasonDetailValueForUpdate(leaveReason); 147 var leaveReasonDescription = employeeControl.getLeaveReasonDescriptionForUpdate(leaveReason, getPreferredLanguage()); 148 var description = edit.getDescription(); 149 150 leaveReasonDetailValue.setLeaveReasonName(edit.getLeaveReasonName()); 151 leaveReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 152 leaveReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 153 154 employeeControl.updateLeaveReasonFromValue(leaveReasonDetailValue, partyPK); 155 156 if(leaveReasonDescription == null && description != null) { 157 employeeControl.createLeaveReasonDescription(leaveReason, getPreferredLanguage(), description, partyPK); 158 } else if(leaveReasonDescription != null && description == null) { 159 employeeControl.deleteLeaveReasonDescription(leaveReasonDescription, partyPK); 160 } else if(leaveReasonDescription != null && description != null) { 161 var leaveReasonDescriptionValue = employeeControl.getLeaveReasonDescriptionValue(leaveReasonDescription); 162 163 leaveReasonDescriptionValue.setDescription(description); 164 employeeControl.updateLeaveReasonDescriptionFromValue(leaveReasonDescriptionValue, partyPK); 165 } 166 } 167 168}