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