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.model.control.customer.server.search;
018
019import static com.echothree.model.jooq.server.keys.customer.CustomerForeignKeys.CUSTOMERS_PARTY_FK;
020import static com.echothree.model.jooq.server.keys.party.PartyForeignKeys.PARTIES_ACTIVE_DETAIL_FK;
021import static com.echothree.model.jooq.server.keys.party.PartyForeignKeys.PARTY_DETAILS_PARTY_TYPE_FK;
022import static com.echothree.model.jooq.server.tables.core.EntityInstances.EntityInstances;
023import static com.echothree.model.jooq.server.tables.customer.Customers.Customers;
024import static com.echothree.model.jooq.server.tables.party.Parties.Parties;
025import static com.echothree.model.jooq.server.tables.party.PartyDetails.PartyDetails;
026import static com.echothree.model.jooq.server.tables.party.PartyTypes.PartyTypes;
027import com.echothree.model.control.core.server.control.EntityInstanceControl;
028import com.echothree.model.control.customer.server.analyzer.CustomerAnalyzer;
029import com.echothree.model.control.customer.server.control.CustomerControl;
030import com.echothree.model.control.index.common.IndexFields;
031import com.echothree.model.control.index.common.Indexes;
032import com.echothree.model.control.index.server.analyzer.BasicAnalyzer;
033import com.echothree.model.control.party.server.search.PartySearchEvaluator;
034import com.echothree.model.control.search.server.search.EntityInstancePKHolder;
035import com.echothree.model.data.customer.server.entity.Customer;
036import com.echothree.model.data.customer.server.entity.CustomerType;
037import com.echothree.model.data.party.server.entity.Language;
038import com.echothree.model.data.search.server.entity.SearchDefaultOperator;
039import com.echothree.model.data.search.server.entity.SearchSortDirection;
040import com.echothree.model.data.search.server.entity.SearchSortOrder;
041import com.echothree.model.data.search.server.entity.SearchType;
042import com.echothree.model.data.user.server.entity.UserVisit;
043import com.echothree.util.server.message.ExecutionErrorAccumulator;
044import com.echothree.util.server.persistence.Session;
045
046public class CustomerSearchEvaluator
047        extends PartySearchEvaluator {
048    
049    private CustomerType customerType;
050    private String customerName;
051    
052    /** Creates a new instance of CustomerSearchEvaluator */
053    public CustomerSearchEvaluator(UserVisit userVisit, SearchType searchType, SearchDefaultOperator searchDefaultOperator,
054            SearchSortOrder searchSortOrder, SearchSortDirection searchSortDirection) {
055        super(userVisit, searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, com.echothree.model.control.party.common.PartyTypes.CUSTOMER.name(),
056                IndexFields.customerName.name(), Indexes.CUSTOMER.name());
057    }
058    
059    public EntityInstancePKHolder getEntityInstancePKHolderByCustomerType(CustomerType customerType) {
060        var query = session.getDslContext()
061                .select(EntityInstances.ENTITY_INSTANCE)
062                .from(EntityInstances)
063                .join(Parties).on(Parties.PARTY.cast(Long.class).eq(EntityInstances.ENTITY_UNIQUE))
064                .join(PartyDetails).onKey(PARTIES_ACTIVE_DETAIL_FK)
065                .join(PartyTypes).onKey(PARTY_DETAILS_PARTY_TYPE_FK)
066                .join(Customers).onKey(CUSTOMERS_PARTY_FK)
067                .where(PartyTypes.PARTY_TYPE_NAME.eq(com.echothree.model.control.party.common.PartyTypes.CUSTOMER.name()),
068                        Customers.CUSTOMER_TYPE.eq(customerType.getPrimaryKey()),
069                        Customers.THRU_TIME.eq(Session.MAX_TIME),
070                        EntityInstances.ENTITY_TYPE.eq(entityType.getPrimaryKey()));
071
072        return getEntityInstancePKHolderFromQuery(query);
073    }
074    
075    public CustomerType getCustomerType() {
076        return customerType;
077    }
078    
079    public void setCustomerType(CustomerType customerType) {
080        this.customerType = customerType;
081    }
082    
083    public String getCustomerName() {
084        return customerName;
085    }
086    
087    public void setCustomerName(String customerName) {
088        this.customerName = customerName;
089    }
090
091    @Override
092    public BasicAnalyzer getAnalyzer(final ExecutionErrorAccumulator eea, final Language language) {
093        return new CustomerAnalyzer(eea, language, entityType, partyType, entityNameIndexField);
094    }
095
096    @Override
097    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
098        EntityInstancePKHolder resultSet = null;
099        
100        if(customerName == null) {
101            resultSet = super.executeSearch(eea);
102            
103            if((resultSet == null || resultSet.size() > 0) && getCustomerType() != null) {
104                var entityInstancePKHolder = getEntityInstancePKHolderByCustomerType(customerType);
105                
106                if(resultSet == null) {
107                    resultSet = entityInstancePKHolder;
108                } else {
109                    resultSet.retainAll(entityInstancePKHolder);
110                }
111            }
112        } else {
113            Customer customer = null;
114            
115            if(customerName != null) {
116                var customerControl = Session.getModelController(CustomerControl.class);
117                
118                customer = customerControl.getCustomerByName(customerName);
119            }
120            
121            if(customer != null) {
122                var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
123
124                resultSet = new EntityInstancePKHolder(1);
125                resultSet.add(entityInstanceControl.getEntityInstanceByBasePK(customer.getPartyPK()).getPrimaryKey(), null);
126            }
127        }
128        
129        return resultSet;
130    }
131    
132}