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.shipping.server.search;
018
019import com.echothree.model.control.core.common.ComponentVendors;
020import com.echothree.model.control.core.common.EntityTypes;
021import com.echothree.model.control.index.common.IndexConstants;
022import com.echothree.model.control.index.common.IndexFieldVariations;
023import com.echothree.model.control.index.common.IndexFields;
024import com.echothree.model.control.index.common.IndexTypes;
025import com.echothree.model.control.index.server.analyzer.BasicAnalyzer;
026import com.echothree.model.control.shipping.server.analyzer.ShippingMethodAnalyzer;
027import com.echothree.model.control.search.common.SearchSortDirections;
028import com.echothree.model.control.search.common.SearchSortOrders;
029import com.echothree.model.control.search.server.search.BaseSearchEvaluator;
030import com.echothree.model.control.search.server.search.EntityInstancePKHolder;
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.search.server.entity.SearchUseType;
037import com.echothree.model.data.user.server.entity.UserVisit;
038import com.echothree.util.server.message.ExecutionErrorAccumulator;
039import org.apache.lucene.search.SortField;
040
041public class ShippingMethodSearchEvaluator
042        extends BaseSearchEvaluator {
043
044    /** Creates a new instance of ShippingMethodSearchEvaluator */
045    public ShippingMethodSearchEvaluator(UserVisit userVisit, Language language, SearchType searchType, SearchDefaultOperator searchDefaultOperator,
046            SearchSortOrder searchSortOrder, SearchSortDirection searchSortDirection, SearchUseType searchUseType) {
047        super(userVisit, searchDefaultOperator, searchType, searchSortOrder, searchSortDirection, searchUseType, ComponentVendors.ECHO_THREE.name(),
048                EntityTypes.ShippingMethod.name(), IndexTypes.SHIPPING_METHOD.name(), language, null);
049        
050        setField(IndexFields.description.name());
051    }
052
053    /** Determines if the result of the search may be cached. For Items, the only field that may be used is the description, "q." If any
054     * others are utilized, the result may not be cached.
055     */
056    @Override
057    protected boolean isResultCachable() {
058        return countParameters() == 1 && q != null;
059    }
060    
061    @Override
062    public SortField[] getSortFields(String searchSortOrderName) {
063        SortField[] sortFields = null;
064        var reverse = searchSortDirection.getLastDetail().getSearchSortDirectionName().equals(SearchSortDirections.DESCENDING.name());
065        
066        if(searchSortOrderName.equals(SearchSortOrders.SCORE.name())) {
067            sortFields = new SortField[]{
068                new SortField(null, SortField.Type.SCORE, reverse),
069                new SortField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(), SortField.Type.STRING, reverse)
070            };
071        } else if(searchSortOrderName.equals(SearchSortOrders.DESCRIPTION.name())) {
072            sortFields = new SortField[]{new SortField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(), SortField.Type.STRING, reverse)};
073        } else if(searchSortOrderName.equals(SearchSortOrders.SHIPPING_METHOD_NAME.name())) {
074            sortFields = new SortField[]{
075                new SortField(IndexFields.shippingMethodName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(), SortField.Type.STRING, reverse)
076            };
077        } else if(searchSortOrderName.equals(SearchSortOrders.CREATED_TIME.name())) {
078            sortFields = new SortField[]{new SortField(IndexFields.createdTime.name(), SortField.Type.LONG, reverse)};
079        } else if(searchSortOrderName.equals(SearchSortOrders.MODIFIED_TIME.name())) {
080            sortFields = new SortField[]{new SortField(IndexFields.modifiedTime.name(), SortField.Type.LONG, reverse)};
081        }
082        
083        return sortFields;
084    }
085    
086    @Override
087    public BasicAnalyzer getAnalyzer(final ExecutionErrorAccumulator eea, final Language language) {
088        return new ShippingMethodAnalyzer(eea, language, entityType);
089    }
090    
091    protected EntityInstancePKHolder executeQSearch(final ExecutionErrorAccumulator eea, EntityInstancePKHolder resultSet) {
092        if(resultSet == null || resultSet.size() > 0) {
093            if(q != null) {
094                if(resultSet == null || resultSet.size() > 0) {
095                    var entityInstancePKHolder = executeQuery(eea);
096
097                    if(resultSet == null) {
098                        resultSet = entityInstancePKHolder;
099                    } else {
100                        resultSet.retainAll(entityInstancePKHolder);
101                    }
102                }
103            }
104        }
105        
106        return resultSet;
107    }
108    
109    @Override
110    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
111        var resultSet = super.executeSearch(eea);
112        
113        resultSet = executeQSearch(eea, resultSet);
114        
115        return resultSet;
116    }
117
118}