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