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.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.employee.server.entity.LeaveReasonDescription; 031import com.echothree.model.data.employee.server.entity.LeaveReasonDetail; 032import com.echothree.model.data.employee.server.value.LeaveReasonDescriptionValue; 033import com.echothree.model.data.employee.server.value.LeaveReasonDetailValue; 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.EditMode; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import com.echothree.util.server.persistence.Session; 044import java.util.Arrays; 045import java.util.Collections; 046import java.util.List; 047 048public class EditLeaveReasonCommand 049 extends BaseAbstractEditCommand<LeaveReasonSpec, LeaveReasonEdit, EditLeaveReasonResult, LeaveReason, LeaveReason> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 059 new SecurityRoleDefinition(SecurityRoleGroups.LeaveReason.name(), SecurityRoles.Edit.name()) 060 ))) 061 ))); 062 063 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 064 new FieldDefinition("LeaveReasonName", FieldType.ENTITY_NAME, true, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("LeaveReasonName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 070 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 071 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 072 )); 073 } 074 075 /** Creates a new instance of EditLeaveReasonCommand */ 076 public EditLeaveReasonCommand(UserVisitPK userVisitPK, EditLeaveReasonForm form) { 077 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditLeaveReasonResult getResult() { 082 return EmployeeResultFactory.getEditLeaveReasonResult(); 083 } 084 085 @Override 086 public LeaveReasonEdit getEdit() { 087 return EmployeeEditFactory.getLeaveReasonEdit(); 088 } 089 090 @Override 091 public LeaveReason getEntity(EditLeaveReasonResult result) { 092 var employeeControl = Session.getModelController(EmployeeControl.class); 093 LeaveReason leaveReason = null; 094 String leaveReasonName = spec.getLeaveReasonName(); 095 096 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 097 leaveReason = employeeControl.getLeaveReasonByName(leaveReasonName); 098 } else { // EditMode.UPDATE 099 leaveReason = employeeControl.getLeaveReasonByNameForUpdate(leaveReasonName); 100 } 101 102 if(leaveReason != null) { 103 result.setLeaveReason(employeeControl.getLeaveReasonTransfer(getUserVisit(), leaveReason)); 104 } else { 105 addExecutionError(ExecutionErrors.UnknownLeaveReasonName.name(), leaveReasonName); 106 } 107 108 return leaveReason; 109 } 110 111 @Override 112 public LeaveReason getLockEntity(LeaveReason leaveReason) { 113 return leaveReason; 114 } 115 116 @Override 117 public void fillInResult(EditLeaveReasonResult result, LeaveReason leaveReason) { 118 var employeeControl = Session.getModelController(EmployeeControl.class); 119 120 result.setLeaveReason(employeeControl.getLeaveReasonTransfer(getUserVisit(), leaveReason)); 121 } 122 123 @Override 124 public void doLock(LeaveReasonEdit edit, LeaveReason leaveReason) { 125 var employeeControl = Session.getModelController(EmployeeControl.class); 126 LeaveReasonDescription leaveReasonDescription = employeeControl.getLeaveReasonDescription(leaveReason, getPreferredLanguage()); 127 LeaveReasonDetail leaveReasonDetail = leaveReason.getLastDetail(); 128 129 edit.setLeaveReasonName(leaveReasonDetail.getLeaveReasonName()); 130 edit.setIsDefault(leaveReasonDetail.getIsDefault().toString()); 131 edit.setSortOrder(leaveReasonDetail.getSortOrder().toString()); 132 133 if(leaveReasonDescription != null) { 134 edit.setDescription(leaveReasonDescription.getDescription()); 135 } 136 } 137 138 @Override 139 public void canUpdate(LeaveReason leaveReason) { 140 var employeeControl = Session.getModelController(EmployeeControl.class); 141 String leaveReasonName = edit.getLeaveReasonName(); 142 LeaveReason duplicateLeaveReason = employeeControl.getLeaveReasonByName(leaveReasonName); 143 144 if(duplicateLeaveReason != null && !leaveReason.equals(duplicateLeaveReason)) { 145 addExecutionError(ExecutionErrors.DuplicateLeaveReasonName.name(), leaveReasonName); 146 } 147 } 148 149 @Override 150 public void doUpdate(LeaveReason leaveReason) { 151 var employeeControl = Session.getModelController(EmployeeControl.class); 152 var partyPK = getPartyPK(); 153 LeaveReasonDetailValue leaveReasonDetailValue = employeeControl.getLeaveReasonDetailValueForUpdate(leaveReason); 154 LeaveReasonDescription leaveReasonDescription = employeeControl.getLeaveReasonDescriptionForUpdate(leaveReason, getPreferredLanguage()); 155 String description = edit.getDescription(); 156 157 leaveReasonDetailValue.setLeaveReasonName(edit.getLeaveReasonName()); 158 leaveReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 159 leaveReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 160 161 employeeControl.updateLeaveReasonFromValue(leaveReasonDetailValue, partyPK); 162 163 if(leaveReasonDescription == null && description != null) { 164 employeeControl.createLeaveReasonDescription(leaveReason, getPreferredLanguage(), description, partyPK); 165 } else if(leaveReasonDescription != null && description == null) { 166 employeeControl.deleteLeaveReasonDescription(leaveReasonDescription, partyPK); 167 } else if(leaveReasonDescription != null && description != null) { 168 LeaveReasonDescriptionValue leaveReasonDescriptionValue = employeeControl.getLeaveReasonDescriptionValue(leaveReasonDescription); 169 170 leaveReasonDescriptionValue.setDescription(description); 171 employeeControl.updateLeaveReasonDescriptionFromValue(leaveReasonDescriptionValue, partyPK); 172 } 173 } 174 175}