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