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.google.common.base.Splitter;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
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    @Inject
076    WorkflowControl workflowControl;
077
078    @Inject
079    CompanyLogic companyLogic;
080
081    @Inject
082    LeaveLogic leaveLogic;
083
084    @Inject
085    PartyLogic partyLogic;
086
087    @Inject
088    SearchLogic searchLogic;
089
090    /** Creates a new instance of SearchLeavesCommand */
091    public SearchLeavesCommand() {
092        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
093    }
094    
095    @Override
096    protected BaseResult execute() {
097        var result = SearchResultFactory.getSearchLeavesResult();
098        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.LEAVE.name());
099
100        if(!hasExecutionErrors()) {
101            var searchTypeName = form.getSearchTypeName();
102            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
103
104            if(!hasExecutionErrors()) {
105                var partyName = form.getPartyName();
106                var party = partyName == null ? null : partyLogic.getPartyByName(this, partyName);
107
108                if(!hasExecutionErrors()) {
109                    var partyCompany = companyLogic.getPartyCompanyByName(this, form.getCompanyName(), null, null, false);
110
111                    if(!hasExecutionErrors()) {
112                        var leaveTypeName = form.getLeaveTypeName();
113                        var leaveType = leaveTypeName == null ? null : leaveLogic.getLeaveTypeByName(this, leaveTypeName);
114
115                        if(!hasExecutionErrors()) {
116                            var leaveReasonName = form.getLeaveReasonName();
117                            var leaveReason = leaveReasonName == null ? null : leaveLogic.getLeaveReasonByName(this, leaveReasonName);
118
119                            if(!hasExecutionErrors()) {
120                                var leaveStatusChoice = form.getLeaveStatusChoice();
121                                var leaveStatusWorkflowStep = leaveStatusChoice == null ? null
122                                        : workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(LeaveStatusConstants.Workflow_LEAVE_STATUS),
123                                        leaveStatusChoice);
124
125                                if(leaveStatusChoice == null || leaveStatusWorkflowStep != null) {
126                                    var userVisit = getUserVisit();
127                                    var createdSince = form.getCreatedSince();
128                                    var modifiedSince = form.getModifiedSince();
129                                    var fields = form.getFields();
130
131                                    var leaveSearchEvaluator = new LeaveSearchEvaluator(userVisit, searchType,
132                                            searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind),
133                                            searchLogic.getDefaultSearchSortDirection(null));
134
135                                    leaveSearchEvaluator.setLeaveName(form.getLeaveName());
136                                    leaveSearchEvaluator.setParty(party);
137                                    leaveSearchEvaluator.setCompanyParty(partyCompany == null ? null : partyCompany.getParty());
138                                    leaveSearchEvaluator.setLeaveType(leaveType);
139                                    leaveSearchEvaluator.setLeaveReason(leaveReason);
140                                    leaveSearchEvaluator.setLeaveStatusWorkflowStep(leaveStatusWorkflowStep);
141                                    leaveSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
142                                    leaveSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
143                                    leaveSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
144
145                                    result.setCount(leaveSearchEvaluator.execute(this));
146                                } else {
147                                    addExecutionError(ExecutionErrors.UnknownLeaveStatusChoice.name(), leaveStatusChoice);
148                                }
149                            }
150                        }
151                    }
152                }
153            }
154        }
155        
156        return result;
157    }
158
159}