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.model.control.customer.server.search; 018 019import com.echothree.model.control.customer.server.analysis.CustomerAnalyzer; 020import com.echothree.model.control.customer.server.control.CustomerControl; 021import com.echothree.model.control.index.common.IndexFields; 022import com.echothree.model.control.index.common.Indexes; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.party.server.search.PartySearchEvaluator; 025import com.echothree.model.control.search.server.search.EntityInstancePKHolder; 026import com.echothree.model.data.core.server.factory.EntityInstanceFactory; 027import com.echothree.model.data.customer.server.entity.Customer; 028import com.echothree.model.data.customer.server.entity.CustomerType; 029import com.echothree.model.data.party.server.entity.Language; 030import com.echothree.model.data.search.server.entity.SearchDefaultOperator; 031import com.echothree.model.data.search.server.entity.SearchSortDirection; 032import com.echothree.model.data.search.server.entity.SearchSortOrder; 033import com.echothree.model.data.search.server.entity.SearchType; 034import com.echothree.model.data.user.server.entity.UserVisit; 035import com.echothree.util.server.message.ExecutionErrorAccumulator; 036import com.echothree.util.server.persistence.Session; 037import org.apache.lucene.analysis.Analyzer; 038 039public class CustomerSearchEvaluator 040 extends PartySearchEvaluator { 041 042 private CustomerType customerType; 043 private String customerName; 044 045 /** Creates a new instance of CustomerSearchEvaluator */ 046 public CustomerSearchEvaluator(UserVisit userVisit, SearchType searchType, SearchDefaultOperator searchDefaultOperator, 047 SearchSortOrder searchSortOrder, SearchSortDirection searchSortDirection) { 048 super(userVisit, searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, PartyTypes.CUSTOMER.name(), 049 IndexFields.customerName.name(), Indexes.CUSTOMER.name()); 050 } 051 052 public EntityInstancePKHolder getEntityInstancePKHolderByCustomerType(CustomerType customerType) { 053 return getEntityInstancePKHolderFromQuery(EntityInstanceFactory.getInstance().prepareStatement( 054 "SELECT _PK_ " + 055 "FROM entityinstances, partytypes, parties, partydetails, customers " + 056 "WHERE par_activedetailid = pardt_partydetailid " + 057 "AND ptyp_partytypename = ? AND ptyp_partytypeid = pardt_ptyp_partytypeid " + 058 "AND par_partyid = cu_par_partyid AND cu_cuty_customertypeid = ? AND cu_thrutime = ? " + 059 "AND eni_ent_entitytypeid = ? AND par_partyid = eni_entityuniqueid"), 060 PartyTypes.CUSTOMER.name(), customerType, Session.MAX_TIME, entityType); 061 } 062 063 public CustomerType getCustomerType() { 064 return customerType; 065 } 066 067 public void setCustomerType(CustomerType customerType) { 068 this.customerType = customerType; 069 } 070 071 public String getCustomerName() { 072 return customerName; 073 } 074 075 public void setCustomerName(String customerName) { 076 this.customerName = customerName; 077 } 078 079 @Override 080 public Analyzer getAnalyzer(final ExecutionErrorAccumulator eea, final Language language) { 081 return new CustomerAnalyzer(eea, language, entityType, partyType, entityNameIndexField); 082 } 083 084 @Override 085 protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) { 086 EntityInstancePKHolder resultSet = null; 087 088 if(customerName == null) { 089 resultSet = super.executeSearch(eea); 090 091 if((resultSet == null || resultSet.size() > 0) && getCustomerType() != null) { 092 EntityInstancePKHolder entityInstancePKHolder = getEntityInstancePKHolderByCustomerType(customerType); 093 094 if(resultSet == null) { 095 resultSet = entityInstancePKHolder; 096 } else { 097 resultSet.retainAll(entityInstancePKHolder); 098 } 099 } 100 } else { 101 Customer customer = null; 102 103 if(customerName != null) { 104 var customerControl = Session.getModelController(CustomerControl.class); 105 106 customer = customerControl.getCustomerByName(customerName); 107 } 108 109 if(customer != null) { 110 resultSet = new EntityInstancePKHolder(1); 111 resultSet.add(getCoreControl().getEntityInstanceByBasePK(customer.getPartyPK()).getPrimaryKey(), null); 112 } 113 } 114 115 return resultSet; 116 } 117 118}