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