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.search.server.command;
018
019import com.echothree.control.user.search.common.form.SearchLeavesForm;
020import com.echothree.control.user.search.common.result.SearchResultFactory;
021import com.echothree.model.control.employee.server.logic.LeaveLogic;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.logic.CompanyLogic;
024import com.echothree.model.control.party.server.logic.PartyLogic;
025import com.echothree.model.control.employee.server.search.LeaveSearchEvaluator;
026import com.echothree.model.control.search.common.SearchKinds;
027import com.echothree.model.control.search.server.logic.SearchLogic;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.control.employee.common.workflow.LeaveStatusConstants;
031import com.echothree.model.control.workflow.server.control.WorkflowControl;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
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 com.google.common.base.Splitter;
043import java.util.List;
044import javax.enterprise.context.Dependent;
045
046@Dependent
047public class SearchLeavesCommand
048        extends BaseSimpleCommand<SearchLeavesForm> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.Leave.name(), SecurityRoles.Search.name())
058                        ))
059                ));
060                
061        FORM_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("LeaveName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("LeaveTypeName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("LeaveReasonName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("LeaveStatusChoice", FieldType.ENTITY_NAME, false, null, null),
069                new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null),
070                new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null),
071                new FieldDefinition("Fields", FieldType.STRING, false, null, null)
072                );
073    }
074
075    /** Creates a new instance of SearchLeavesCommand */
076    public SearchLeavesCommand() {
077        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
078    }
079    
080    @Override
081    protected BaseResult execute() {
082        var searchLogic = SearchLogic.getInstance();
083        var result = SearchResultFactory.getSearchLeavesResult();
084        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.LEAVE.name());
085
086        if(!hasExecutionErrors()) {
087            var searchTypeName = form.getSearchTypeName();
088            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
089
090            if(!hasExecutionErrors()) {
091                var partyName = form.getPartyName();
092                var party = partyName == null ? null : PartyLogic.getInstance().getPartyByName(this, partyName);
093
094                if(!hasExecutionErrors()) {
095                    var partyCompany = CompanyLogic.getInstance().getPartyCompanyByName(this, form.getCompanyName(), null, null, false);
096
097                    if(!hasExecutionErrors()) {
098                        var leaveTypeName = form.getLeaveTypeName();
099                        var leaveType = leaveTypeName == null ? null : LeaveLogic.getInstance().getLeaveTypeByName(this, leaveTypeName);
100
101                        if(!hasExecutionErrors()) {
102                            var leaveReasonName = form.getLeaveReasonName();
103                            var leaveReason = leaveReasonName == null ? null : LeaveLogic.getInstance().getLeaveReasonByName(this, leaveReasonName);
104
105                            if(!hasExecutionErrors()) {
106                                var workflowControl = Session.getModelController(WorkflowControl.class);
107                                var leaveStatusChoice = form.getLeaveStatusChoice();
108                                var leaveStatusWorkflowStep = leaveStatusChoice == null ? null
109                                        : workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(LeaveStatusConstants.Workflow_LEAVE_STATUS),
110                                        leaveStatusChoice);
111
112                                if(leaveStatusChoice == null || leaveStatusWorkflowStep != null) {
113                                    var userVisit = getUserVisit();
114                                    var createdSince = form.getCreatedSince();
115                                    var modifiedSince = form.getModifiedSince();
116                                    var fields = form.getFields();
117
118                                    var leaveSearchEvaluator = new LeaveSearchEvaluator(userVisit, searchType,
119                                            searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind),
120                                            searchLogic.getDefaultSearchSortDirection(null));
121
122                                    leaveSearchEvaluator.setLeaveName(form.getLeaveName());
123                                    leaveSearchEvaluator.setParty(party);
124                                    leaveSearchEvaluator.setCompanyParty(partyCompany == null ? null : partyCompany.getParty());
125                                    leaveSearchEvaluator.setLeaveType(leaveType);
126                                    leaveSearchEvaluator.setLeaveReason(leaveReason);
127                                    leaveSearchEvaluator.setLeaveStatusWorkflowStep(leaveStatusWorkflowStep);
128                                    leaveSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
129                                    leaveSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
130                                    leaveSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
131
132                                    result.setCount(leaveSearchEvaluator.execute(this));
133                                } else {
134                                    addExecutionError(ExecutionErrors.UnknownLeaveStatusChoice.name(), leaveStatusChoice);
135                                }
136                            }
137                        }
138                    }
139                }
140            }
141        }
142        
143        return result;
144    }
145
146}