001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.form.CreateLeaveForm; 020import com.echothree.model.control.employee.server.control.EmployeeControl; 021import com.echothree.model.control.employee.server.logic.LeaveLogic; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.party.server.control.PartyControl; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.control.uom.common.UomConstants; 027import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic; 028import com.echothree.model.control.employee.common.workflow.LeaveStatusConstants; 029import com.echothree.model.control.workflow.server.control.WorkflowControl; 030import com.echothree.model.control.workflow.server.logic.WorkflowSecurityLogic; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.model.data.workflow.server.entity.WorkflowEntrance; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.common.command.BaseResult; 037import com.echothree.util.server.control.BaseSimpleCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import java.util.Arrays; 043import java.util.Collections; 044import java.util.List; 045import javax.enterprise.context.RequestScoped; 046 047@RequestScoped 048public class CreateLeaveCommand 049 extends BaseSimpleCommand<CreateLeaveForm> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 058 new SecurityRoleDefinition(SecurityRoleGroups.Leave.name(), SecurityRoles.Create.name()) 059 ))) 060 ))); 061 062 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("LeaveTypeName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("LeaveReasonName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("StartTime", FieldType.DATE_TIME, true, null, null), 068 new FieldDefinition("EndTime", FieldType.DATE_TIME, false, null, null), 069 new FieldDefinition("TotalTime", FieldType.UNSIGNED_LONG, false, null, null), 070 new FieldDefinition("TotalTimeUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("LeaveStatus", FieldType.ENTITY_NAME, false, null, null) 072 )); 073 } 074 075 /** Creates a new instance of CreateLeaveCommand */ 076 public CreateLeaveCommand() { 077 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var partyControl = Session.getModelController(PartyControl.class); 083 var partyName = form.getPartyName(); 084 var party = partyControl.getPartyByName(partyName); 085 086 if(party != null) { 087 var companyName = form.getCompanyName(); 088 var partyCompany = partyControl.getPartyCompanyByName(companyName); 089 090 if(partyCompany != null) { 091 var employeeControl = Session.getModelController(EmployeeControl.class); 092 var leaveTypeName = form.getLeaveTypeName(); 093 var leaveType = employeeControl.getLeaveTypeByName(leaveTypeName); 094 095 if(leaveTypeName == null || leaveType != null) { 096 var leaveReasonName = form.getLeaveReasonName(); 097 var leaveReason = employeeControl.getLeaveReasonByName(leaveReasonName); 098 099 if(leaveReasonName == null || leaveReason != null) { 100 var unitOfMeasureTypeLogic = UnitOfMeasureTypeLogic.getInstance(); 101 var totalTime = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, UomConstants.UnitOfMeasureKindUseType_TIME, 102 form.getTotalTime(), form.getTotalTimeUnitOfMeasureTypeName(), 103 null, ExecutionErrors.MissingRequiredTotalTime.name(), null, ExecutionErrors.MissingRequiredTotalTimeUnitOfMeasureTypeName.name(), 104 null, ExecutionErrors.UnknownTotalTimeUnitOfMeasureTypeName.name()); 105 106 if(!hasExecutionErrors()) { 107 var createdBy = getPartyPK(); 108 WorkflowEntrance leaveStatus = null; 109 var leaveStatusChoice = form.getLeaveStatus(); 110 111 if(leaveStatusChoice != null) { 112 var workflowControl = Session.getModelController(WorkflowControl.class); 113 leaveStatus = workflowControl.getWorkflowEntranceUsingNames(this, LeaveStatusConstants.Workflow_LEAVE_STATUS, 114 leaveStatusChoice); 115 116 if(!hasExecutionErrors()) { 117 WorkflowSecurityLogic.getInstance().checkAddEntityToWorkflow(this, leaveStatus, createdBy); 118 } 119 } 120 121 if(!hasExecutionErrors()) { 122 var startTime = Long.valueOf(form.getStartTime()); 123 var strEndTime = form.getEndTime(); 124 var endTime = strEndTime == null ? null : Long.valueOf(strEndTime); 125 126 if(endTime == null || endTime > startTime) { 127 LeaveLogic.getInstance().createLeave(session, party, partyCompany.getParty(), leaveType, leaveReason, startTime, endTime, 128 totalTime, leaveStatus, createdBy); 129 } else { 130 addExecutionError(endTime.equals(startTime) ? ExecutionErrors.EndTimeEqualToStartTime.name() : ExecutionErrors.EndTimeBeforeStartTime.name()); 131 } 132 } 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownLeaveReasonName.name(), leaveReasonName); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownLeaveTypeName.name(), leaveTypeName); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName); 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 145 } 146 147 return null; 148 } 149 150}