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.SearchVendorsForm; 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.vendor.server.search.VendorSearchEvaluator; 026import com.echothree.model.control.search.server.logic.SearchLogic; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.party.server.entity.PartyAliasType; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.server.control.BaseSimpleCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.google.common.base.Splitter; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class SearchVendorsCommand 046 extends BaseSimpleCommand<SearchVendorsForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.Vendor.name(), SecurityRoles.Search.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("FirstName", FieldType.STRING, false, 1L, 20L), 062 new FieldDefinition("FirstNameSoundex", FieldType.BOOLEAN, false, null, null), 063 new FieldDefinition("MiddleName", FieldType.STRING, false, 1L, 20L), 064 new FieldDefinition("MiddleNameSoundex", FieldType.BOOLEAN, false, null, null), 065 new FieldDefinition("LastName", FieldType.STRING, false, 1L, 20L), 066 new FieldDefinition("LastNameSoundex", FieldType.BOOLEAN, false, null, null), 067 new FieldDefinition("Name", FieldType.STRING, false, null, null), 068 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null), 069 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null), 073 new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null), 074 new FieldDefinition("Fields", FieldType.STRING, false, null, null) 075 ); 076 } 077 078 @Inject 079 PartyControl partyControl; 080 081 @Inject 082 SearchControl searchControl; 083 084 @Inject 085 SearchLogic searchLogic; 086 087 /** Creates a new instance of SearchVendorsCommand */ 088 public SearchVendorsCommand() { 089 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 090 } 091 092 @Override 093 protected BaseResult execute() { 094 var result = SearchResultFactory.getSearchVendorsResult(); 095 var searchKind = searchControl.getSearchKindByName(SearchKinds.VENDOR.name()); 096 097 if(searchKind != null) { 098 var searchTypeName = form.getSearchTypeName(); 099 var searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName); 100 101 if(searchType != null) { 102 var partyAliasTypeName = form.getPartyAliasTypeName(); 103 var alias = form.getAlias(); 104 PartyAliasType partyAliasType = null; 105 106 if(partyAliasTypeName != null) { 107 var partyType = partyControl.getPartyTypeByName(PartyTypes.VENDOR.name()); 108 109 if(partyType != null) { 110 partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName); 111 112 if(partyAliasType == null) { 113 addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), PartyTypes.VENDOR.name(), partyAliasTypeName); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownPartyTypeName.name(), PartyTypes.VENDOR.name()); 117 } 118 } 119 120 if(!hasExecutionErrors()) { 121 var userVisit = getUserVisit(); 122 var vendorSearchEvaluator = new VendorSearchEvaluator(userVisit, searchType, 123 searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind), 124 searchLogic.getDefaultSearchSortDirection(null)); 125 var createdSince = form.getCreatedSince(); 126 var modifiedSince = form.getModifiedSince(); 127 var fields = form.getFields(); 128 129 vendorSearchEvaluator.setFirstName(form.getFirstName()); 130 vendorSearchEvaluator.setFirstNameSoundex(Boolean.parseBoolean(form.getFirstNameSoundex())); 131 vendorSearchEvaluator.setMiddleName(form.getMiddleName()); 132 vendorSearchEvaluator.setMiddleNameSoundex(Boolean.parseBoolean(form.getMiddleNameSoundex())); 133 vendorSearchEvaluator.setLastName(form.getLastName()); 134 vendorSearchEvaluator.setLastNameSoundex(Boolean.parseBoolean(form.getLastNameSoundex())); 135 vendorSearchEvaluator.setQ(this, form.getName()); 136 vendorSearchEvaluator.setPartyAliasType(partyAliasType); 137 vendorSearchEvaluator.setAlias(alias); 138 vendorSearchEvaluator.setVendorName(form.getVendorName()); 139 vendorSearchEvaluator.setPartyName(form.getPartyName()); 140 vendorSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince)); 141 vendorSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince)); 142 vendorSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0])); 143 144 if(!hasExecutionErrors()) { 145 result.setCount(vendorSearchEvaluator.execute(this)); 146 } 147 } 148 } else { 149 addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.VENDOR.name(), searchTypeName); 150 } 151 } else { 152 addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.VENDOR.name()); 153 } 154 155 return result; 156 } 157}