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