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