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.SearchEmployeesForm; 020import com.echothree.control.user.search.common.result.SearchResultFactory; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.search.common.SearchKinds; 024import com.echothree.model.control.search.server.control.SearchControl; 025import com.echothree.model.control.employee.server.search.EmployeeSearchEvaluator; 026import com.echothree.model.control.search.server.logic.SearchLogic; 027import com.echothree.model.control.employee.common.workflow.EmployeeAvailabilityConstants; 028import com.echothree.model.control.employee.common.workflow.EmployeeStatusConstants; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.control.workflow.server.control.WorkflowControl; 032import com.echothree.model.data.party.server.entity.PartyAliasType; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.common.command.BaseResult; 038import com.echothree.util.server.control.BaseSimpleCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import com.echothree.util.server.control.SecurityRoleDefinition; 042import com.echothree.util.server.persistence.Session; 043import com.google.common.base.Splitter; 044import java.util.List; 045import javax.enterprise.context.Dependent; 046 047@Dependent 048public class SearchEmployeesCommand 049 extends BaseSimpleCommand<SearchEmployeesForm> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 058 new SecurityRoleDefinition(SecurityRoleGroups.Employee.name(), SecurityRoles.Search.name()) 059 )) 060 )); 061 062 FORM_FIELD_DEFINITIONS = List.of( 063 new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("FirstName", FieldType.STRING, false, 1L, 20L), 065 new FieldDefinition("FirstNameSoundex", FieldType.BOOLEAN, false, null, null), 066 new FieldDefinition("MiddleName", FieldType.STRING, false, 1L, 20L), 067 new FieldDefinition("MiddleNameSoundex", FieldType.BOOLEAN, false, null, null), 068 new FieldDefinition("LastName", FieldType.STRING, false, 1L, 20L), 069 new FieldDefinition("LastNameSoundex", FieldType.BOOLEAN, false, null, null), 070 new FieldDefinition("EmployeeName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("EmployeeStatusChoice", FieldType.ENTITY_NAME, false, null, null), 075 new FieldDefinition("EmployeeAvailabilityChoice", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null), 077 new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null), 078 new FieldDefinition("Fields", FieldType.STRING, false, null, null) 079 ); 080 } 081 082 /** Creates a new instance of SearchEmployeesCommand */ 083 public SearchEmployeesCommand() { 084 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 085 } 086 087 @Override 088 protected BaseResult execute() { 089 var result = SearchResultFactory.getSearchEmployeesResult(); 090 var employeeName = form.getEmployeeName(); 091 var partyName = form.getPartyName(); 092 var parameterCount = (employeeName == null ? 0 : 1) + (partyName == null ? 0 : 1); 093 094 if(parameterCount < 2) { 095 var searchControl = Session.getModelController(SearchControl.class); 096 var searchKind = searchControl.getSearchKindByName(SearchKinds.EMPLOYEE.name()); 097 098 if(searchKind != null) { 099 var searchTypeName = form.getSearchTypeName(); 100 var searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName); 101 102 if(searchType != null) { 103 var partyAliasTypeName = form.getPartyAliasTypeName(); 104 var alias = form.getAlias(); 105 PartyAliasType partyAliasType = null; 106 107 if(partyAliasTypeName != null) { 108 var partyControl = Session.getModelController(PartyControl.class); 109 var partyType = partyControl.getPartyTypeByName(PartyTypes.CUSTOMER.name()); 110 111 if(partyType != null) { 112 partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName); 113 114 if(partyAliasType == null) { 115 addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), PartyTypes.CUSTOMER.name(), partyAliasTypeName); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownPartyTypeName.name(), PartyTypes.CUSTOMER.name()); 119 } 120 } 121 122 if(!hasExecutionErrors()) { 123 var workflowControl = Session.getModelController(WorkflowControl.class); 124 var employeeStatusChoice = form.getEmployeeStatusChoice(); 125 var employeeStatusWorkflowStep = employeeStatusChoice == null ? null : workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS), employeeStatusChoice); 126 127 if(employeeStatusChoice == null || employeeStatusWorkflowStep != null) { 128 var employeeAvailabilityChoice = form.getEmployeeAvailabilityChoice(); 129 var employeeAvailabilityWorkflowStep = employeeAvailabilityChoice == null ? null : workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(EmployeeAvailabilityConstants.Workflow_EMPLOYEE_AVAILABILITY), employeeAvailabilityChoice); 130 131 if(employeeAvailabilityChoice == null || employeeAvailabilityWorkflowStep != null) { 132 var searchLogic = SearchLogic.getInstance(); 133 var userVisit = getUserVisit(); 134 var employeeSearchEvaluator = new EmployeeSearchEvaluator(userVisit, searchType, searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind), searchLogic.getDefaultSearchSortDirection(null)); 135 var createdSince = form.getCreatedSince(); 136 var modifiedSince = form.getModifiedSince(); 137 var fields = form.getFields(); 138 139 employeeSearchEvaluator.setFirstName(form.getFirstName()); 140 employeeSearchEvaluator.setFirstNameSoundex(Boolean.parseBoolean(form.getFirstNameSoundex())); 141 employeeSearchEvaluator.setMiddleName(form.getMiddleName()); 142 employeeSearchEvaluator.setMiddleNameSoundex(Boolean.parseBoolean(form.getMiddleNameSoundex())); 143 employeeSearchEvaluator.setLastName(form.getLastName()); 144 employeeSearchEvaluator.setLastNameSoundex(Boolean.parseBoolean(form.getLastNameSoundex())); 145 employeeSearchEvaluator.setPartyAliasType(partyAliasType); 146 employeeSearchEvaluator.setAlias(alias); 147 employeeSearchEvaluator.setPartyEmployeeName(form.getEmployeeName()); 148 employeeSearchEvaluator.setPartyName(form.getPartyName()); 149 employeeSearchEvaluator.setEmployeeStatusWorkflowStep(employeeStatusWorkflowStep); 150 employeeSearchEvaluator.setEmployeeAvailabilityWorkflowStep(employeeAvailabilityWorkflowStep); 151 employeeSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince)); 152 employeeSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince)); 153 employeeSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0])); 154 155 result.setCount(employeeSearchEvaluator.execute(this)); 156 } else { 157 addExecutionError(ExecutionErrors.UnknownEmployeeAvailabilityChoice.name(), employeeAvailabilityChoice); 158 } 159 } else { 160 addExecutionError(ExecutionErrors.UnknownEmployeeStatusChoice.name(), employeeStatusChoice); 161 } 162 } 163 } else { 164 addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.EMPLOYEE.name(), searchTypeName); 165 } 166 } else { 167 addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.EMPLOYEE.name()); 168 } 169 } else { 170 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 171 } 172 173 return result; 174 } 175 176}