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.content.server.search;
018
019import com.echothree.model.control.content.server.analyzer.ContentCatalogItemAnalyzer;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.index.common.IndexConstants;
023import com.echothree.model.control.index.common.IndexFieldVariations;
024import com.echothree.model.control.index.common.IndexFields;
025import com.echothree.model.control.index.common.IndexTypes;
026import com.echothree.model.control.index.server.analyzer.BasicAnalyzer;
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 ContentCatalogItemSearchEvaluator
042        extends BaseSearchEvaluator {
043    
044    /** Creates a new instance of ContentCatalogItemSearchEvaluator */
045    public ContentCatalogItemSearchEvaluator(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.ContentCatalogItem.name(), IndexTypes.CONTENT_CATALOG_ITEM.name(), language, null);
049        
050        setField(IndexFields.description.name());
051    }
052
053    @Override
054    public SortField[] getSortFields(String searchSortOrderName) {
055        SortField[] sortFields = null;
056        var reverse = searchSortDirection.getLastDetail().getSearchSortDirectionName().equals(SearchSortDirections.DESCENDING.name());
057        
058        if(searchSortOrderName.equals(SearchSortOrders.SCORE.name())) {
059            sortFields = new SortField[]{
060                new SortField(null, SortField.Type.SCORE, reverse),
061                new SortField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(), SortField.Type.STRING, reverse)
062            };
063        } else if(searchSortOrderName.equals(SearchSortOrders.DESCRIPTION.name())) {
064            sortFields = new SortField[]{new SortField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(), SortField.Type.STRING, reverse)};
065        } else if(searchSortOrderName.equals(SearchSortOrders.ITEM_NAME.name())) {
066            sortFields = new SortField[]{
067                new SortField(IndexFields.itemName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(), SortField.Type.STRING, reverse)
068            };
069        } else if(searchSortOrderName.equals(SearchSortOrders.CREATED_TIME.name())) {
070            sortFields = new SortField[]{new SortField(IndexFields.createdTime.name(), SortField.Type.LONG, reverse)};
071        } else if(searchSortOrderName.equals(SearchSortOrders.MODIFIED_TIME.name())) {
072            sortFields = new SortField[]{new SortField(IndexFields.modifiedTime.name(), SortField.Type.LONG, reverse)};
073        }
074        
075        return sortFields;
076    }
077
078    @Override
079    public BasicAnalyzer getAnalyzer(final ExecutionErrorAccumulator eea, final Language language) {
080        return new ContentCatalogItemAnalyzer(eea, language, entityType);
081    }
082
083    protected EntityInstancePKHolder executeQSearch(final ExecutionErrorAccumulator eea, EntityInstancePKHolder resultSet) {
084        if(resultSet == null || resultSet.size() > 0) {
085            if(q != null) {
086                if(resultSet == null || resultSet.size() > 0) {
087                    var entityInstancePKHolder = executeQuery(eea);
088
089                    if(resultSet == null) {
090                        resultSet = entityInstancePKHolder;
091                    } else {
092                        resultSet.retainAll(entityInstancePKHolder);
093                    }
094                }
095            }
096        }
097        
098        return resultSet;
099    }
100    
101    @Override
102    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
103        var resultSet = super.executeSearch(eea);
104        
105        resultSet = executeQSearch(eea, resultSet);
106        
107        return resultSet;
108    }
109
110}