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.echothree.util.server.persistence.Session; 040import com.google.common.base.Splitter; 041import java.util.List; 042import javax.enterprise.context.Dependent; 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 /** Creates a new instance of SearchVendorsCommand */ 079 public SearchVendorsCommand() { 080 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 081 } 082 083 @Override 084 protected BaseResult execute() { 085 var result = SearchResultFactory.getSearchVendorsResult(); 086 var searchControl = Session.getModelController(SearchControl.class); 087 var searchKind = searchControl.getSearchKindByName(SearchKinds.VENDOR.name()); 088 089 if(searchKind != null) { 090 var searchTypeName = form.getSearchTypeName(); 091 var searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName); 092 093 if(searchType != null) { 094 var partyAliasTypeName = form.getPartyAliasTypeName(); 095 var alias = form.getAlias(); 096 PartyAliasType partyAliasType = null; 097 098 if(partyAliasTypeName != null) { 099 var partyControl = Session.getModelController(PartyControl.class); 100 var partyType = partyControl.getPartyTypeByName(PartyTypes.VENDOR.name()); 101 102 if(partyType != null) { 103 partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName); 104 105 if(partyAliasType == null) { 106 addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), PartyTypes.VENDOR.name(), partyAliasTypeName); 107 } 108 } else { 109 addExecutionError(ExecutionErrors.UnknownPartyTypeName.name(), PartyTypes.VENDOR.name()); 110 } 111 } 112 113 if(!hasExecutionErrors()) { 114 var searchLogic = SearchLogic.getInstance(); 115 var userVisit = getUserVisit(); 116 var vendorSearchEvaluator = new VendorSearchEvaluator(userVisit, searchType, 117 searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind), 118 searchLogic.getDefaultSearchSortDirection(null)); 119 var createdSince = form.getCreatedSince(); 120 var modifiedSince = form.getModifiedSince(); 121 var fields = form.getFields(); 122 123 vendorSearchEvaluator.setFirstName(form.getFirstName()); 124 vendorSearchEvaluator.setFirstNameSoundex(Boolean.parseBoolean(form.getFirstNameSoundex())); 125 vendorSearchEvaluator.setMiddleName(form.getMiddleName()); 126 vendorSearchEvaluator.setMiddleNameSoundex(Boolean.parseBoolean(form.getMiddleNameSoundex())); 127 vendorSearchEvaluator.setLastName(form.getLastName()); 128 vendorSearchEvaluator.setLastNameSoundex(Boolean.parseBoolean(form.getLastNameSoundex())); 129 vendorSearchEvaluator.setQ(this, form.getName()); 130 vendorSearchEvaluator.setPartyAliasType(partyAliasType); 131 vendorSearchEvaluator.setAlias(alias); 132 vendorSearchEvaluator.setVendorName(form.getVendorName()); 133 vendorSearchEvaluator.setPartyName(form.getPartyName()); 134 vendorSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince)); 135 vendorSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince)); 136 vendorSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0])); 137 138 if(!hasExecutionErrors()) { 139 result.setCount(vendorSearchEvaluator.execute(this)); 140 } 141 } 142 } else { 143 addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.VENDOR.name(), searchTypeName); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.VENDOR.name()); 147 } 148 149 return result; 150 } 151}