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.contact.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.analysis.ContactMechanismAnalyzer;
026import com.echothree.model.control.search.common.SearchSortOrders;
027import com.echothree.model.control.search.common.SearchSortDirections;
028import com.echothree.model.control.search.server.search.BaseSearchEvaluator;
029import com.echothree.model.control.search.server.search.EntityInstancePKHolder;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.model.data.search.server.entity.SearchDefaultOperator;
032import com.echothree.model.data.search.server.entity.SearchSortDirection;
033import com.echothree.model.data.search.server.entity.SearchSortOrder;
034import com.echothree.model.data.search.server.entity.SearchType;
035import com.echothree.model.data.search.server.entity.SearchUseType;
036import com.echothree.model.data.user.server.entity.UserVisit;
037import com.echothree.util.server.message.ExecutionErrorAccumulator;
038import org.apache.lucene.analysis.Analyzer;
039import org.apache.lucene.search.SortField;
040
041public class ContactMechanismSearchEvaluator
042        extends BaseSearchEvaluator {
043    
044    /** Creates a new instance of ContactMechanismSearchEvaluator */
045    public ContactMechanismSearchEvaluator(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.ContactMechanism.name(), IndexTypes.CONTACT_MECHANISM.name(), language, null);
049        
050        setField(IndexFields.description.name());
051    }
052
053    @Override
054    protected SortField[] getSortFields(String searchSortOrderName) {
055        SortField sortField = null;
056        boolean reverse = searchSortDirection.getLastDetail().getSearchSortDirectionName().equals(SearchSortDirections.DESCENDING.name());
057        
058        if(searchSortOrderName.equals(SearchSortOrders.SCORE.name())) {
059            sortField = new SortField(null, SortField.Type.SCORE, reverse);
060        } else if(searchSortOrderName.equals(SearchSortOrders.CONTACT_MECHANISM_NAME.name())) {
061            sortField = new SortField(IndexFields.contactMechanismName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(), SortField.Type.STRING, reverse);
062        } else if(searchSortOrderName.equals(SearchSortOrders.CREATED_TIME.name())) {
063            sortField = new SortField(IndexFields.createdTime.name(), SortField.Type.LONG, reverse);
064        } else if(searchSortOrderName.equals(SearchSortOrders.MODIFIED_TIME.name())) {
065            sortField = new SortField(IndexFields.modifiedTime.name(), SortField.Type.LONG, reverse);
066        }
067        
068        return sortField == null ? null : new SortField[]{sortField};
069    }
070    
071    @Override
072    public Analyzer getAnalyzer(final ExecutionErrorAccumulator eea, final Language language) {
073        return new ContactMechanismAnalyzer(eea, language, entityType);
074    }
075    
076    protected EntityInstancePKHolder executeQSearch(final ExecutionErrorAccumulator eea, EntityInstancePKHolder resultSet) {
077        if(resultSet == null || resultSet.size() > 0) {
078            if(q != null) {
079                if(resultSet == null || resultSet.size() > 0) {
080                    EntityInstancePKHolder entityInstancePKHolder = executeQuery(eea);
081
082                    if(resultSet == null) {
083                        resultSet = entityInstancePKHolder;
084                    } else {
085                        resultSet.retainAll(entityInstancePKHolder);
086                    }
087                }
088            }
089        }
090        
091        return resultSet;
092    }
093    
094    @Override
095    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
096        EntityInstancePKHolder resultSet = super.executeSearch(eea);
097        
098        resultSet = executeQSearch(eea, resultSet);
099        
100        return resultSet;
101    }
102
103}