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