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 java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
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    @Inject
074    EmployeeControl employeeControl;
075
076    @Inject
077    PartyControl partyControl;
078
079    @Inject
080    WorkflowControl workflowControl;
081
082    @Inject
083    LeaveLogic leaveLogic;
084
085    @Inject
086    UnitOfMeasureTypeLogic unitOfMeasureTypeLogic;
087
088    @Inject
089    WorkflowSecurityLogic workflowSecurityLogic;
090
091    
092    /** Creates a new instance of CreateLeaveCommand */
093    public CreateLeaveCommand() {
094        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
095    }
096    
097    @Override
098    protected BaseResult execute() {
099        var partyName = form.getPartyName();
100        var party = partyControl.getPartyByName(partyName);
101
102        if(party != null) {
103            var companyName = form.getCompanyName();
104            var partyCompany = partyControl.getPartyCompanyByName(companyName);
105
106            if(partyCompany != null) {
107                var leaveTypeName = form.getLeaveTypeName();
108                var leaveType = employeeControl.getLeaveTypeByName(leaveTypeName);
109
110                if(leaveTypeName == null || leaveType != null) {
111                    var leaveReasonName = form.getLeaveReasonName();
112                    var leaveReason = employeeControl.getLeaveReasonByName(leaveReasonName);
113
114                    if(leaveReasonName == null || leaveReason != null) {
115                        var totalTime = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, UomConstants.UnitOfMeasureKindUseType_TIME,
116                                form.getTotalTime(), form.getTotalTimeUnitOfMeasureTypeName(),
117                                null, ExecutionErrors.MissingRequiredTotalTime.name(), null, ExecutionErrors.MissingRequiredTotalTimeUnitOfMeasureTypeName.name(),
118                                null, ExecutionErrors.UnknownTotalTimeUnitOfMeasureTypeName.name());
119
120                        if(!hasExecutionErrors()) {
121                            var createdBy = getPartyPK();
122                            WorkflowEntrance leaveStatus = null;
123                            var leaveStatusChoice = form.getLeaveStatus();
124
125                            if(leaveStatusChoice != null) {
126                                leaveStatus = workflowControl.getWorkflowEntranceUsingNames(this, LeaveStatusConstants.Workflow_LEAVE_STATUS,
127                                        leaveStatusChoice);
128
129                                if(!hasExecutionErrors()) {
130                                    workflowSecurityLogic.checkAddEntityToWorkflow(this, leaveStatus, createdBy);
131                                }
132                            }
133
134                            if(!hasExecutionErrors()) {
135                                var startTime = Long.valueOf(form.getStartTime());
136                                var strEndTime = form.getEndTime();
137                                var endTime = strEndTime == null ? null : Long.valueOf(strEndTime);
138
139                                if(endTime == null || endTime > startTime) {
140                                    leaveLogic.createLeave(session, party, partyCompany.getParty(), leaveType, leaveReason, startTime, endTime,
141                                            totalTime, leaveStatus, createdBy);
142                                } else {
143                                    addExecutionError(endTime.equals(startTime) ? ExecutionErrors.EndTimeEqualToStartTime.name() : ExecutionErrors.EndTimeBeforeStartTime.name());
144                                }
145                            }
146                        }
147                    } else {
148                        addExecutionError(ExecutionErrors.UnknownLeaveReasonName.name(), leaveReasonName);
149                    }
150                } else {
151                    addExecutionError(ExecutionErrors.UnknownLeaveTypeName.name(), leaveTypeName);
152                }
153            } else {
154                addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
155            }
156        } else {
157            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
158        }
159        
160        return null;
161    }
162    
163}