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