001/*
002 *  Copyright 2018 Anyware Services
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 */
016package org.ametys.web.frontoffice.search.metamodel;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020import java.util.Optional;
021import java.util.stream.Stream;
022
023import org.ametys.cms.search.Sort;
024import org.ametys.cms.search.Sort.Order;
025import org.ametys.cms.search.model.SystemProperty;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.web.frontoffice.search.metamodel.impl.CommonPertinenceSortDefinition;
028import org.ametys.web.frontoffice.search.metamodel.impl.CommonPseudoContentTypeFacetDefinition;
029import org.ametys.web.frontoffice.search.metamodel.impl.CommonSystemPropertyBasedSortDefinition;
030import org.ametys.web.frontoffice.search.metamodel.impl.CommonTitleSortDefinition;
031import org.ametys.web.frontoffice.search.metamodel.impl.SiteSearchCriterionDefinition;
032import org.ametys.web.frontoffice.search.metamodel.impl.TitleOrWordingSearchCriterionDefinition;
033import org.ametys.web.frontoffice.search.metamodel.impl.WordingSearchCriterionDefinition;
034import org.ametys.web.frontoffice.search.metamodel.impl.WordingSearchCriterionDefinition.WordingType;
035
036/**
037 * Common implementations for criteria, facets, sorts...
038 */
039public final class SearchServiceCommonImpls
040{
041    private static final String __COMMON_PREFIX = "common$";
042    /** The prefix for the ids of wording criterion definitions */
043    private static final String __WORDING_CRITERION_DEFINITIONS_PREFIX_ID = __COMMON_PREFIX + "wording$";
044    /** The prefix for the ids of title or wording criterion definitions */
045    private static final String __TITLE_OR_WORDING_CRITERION_DEFINITIONS_PREFIX_ID = __COMMON_PREFIX + "titleorwording$";
046    /** The ids of site criterion definition */
047    private static final String __SITE_CRITERION_DEFINITION_ID = __COMMON_PREFIX + "site";
048    
049    private SearchServiceCommonImpls()
050    {
051        // Nothing
052    }
053    
054    static Map<String, SearchCriterionDefinition> getCommonCriterionDefinitions(SearchServiceCreationHelper helper)
055    {
056        Map<String, SearchCriterionDefinition> criterionDefs = new LinkedHashMap<>();
057        
058        // "Wording" (based on "full text" indexation)
059        _getWordingCriteria()
060                .forEach(c -> criterionDefs.put(c.getId(), c));
061        
062        // "Title or Wording" (based on "full text" indexation)
063        _getTitleOrWordingCriteria()
064                .forEach(c -> criterionDefs.put(c.getId(), c));
065        
066        // "Site" common criterion
067        final String siteKey = __SITE_CRITERION_DEFINITION_ID;
068        final String pluginName = "web";
069        final String catalogue = "plugin.web";
070        criterionDefs.put(siteKey, new SiteSearchCriterionDefinition(siteKey, pluginName, new I18nizableText(catalogue, "PLUGINS_WEB_SERVICE_SEARCH_COMMON_CRITERION_SITE"), helper._siteEnumerator, helper._siteQueryHelper, Optional.empty()));
071        
072        return criterionDefs;
073    }
074    
075    
076    private static Stream<SearchCriterionDefinition> _getWordingCriteria()
077    {
078        return Stream.of(new SearchCriterionDefinition[] {
079            new WordingSearchCriterionDefinition(__WORDING_CRITERION_DEFINITIONS_PREFIX_ID + "textfield", "web", new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_COMMON_CRITERION_FULL_TEXT"), Optional.empty(), WordingType.TEXTFIELD),
080        });
081    }
082    
083    private static Stream<SearchCriterionDefinition> _getTitleOrWordingCriteria()
084    {
085        return Stream.of(new SearchCriterionDefinition[] {
086            new TitleOrWordingSearchCriterionDefinition(__TITLE_OR_WORDING_CRITERION_DEFINITIONS_PREFIX_ID + "textfield", "web", new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_COMMON_CRITERION_TITLE_OR_FULL_TEXT"), Optional.empty(), WordingType.TEXTFIELD),
087        });
088    }
089
090    static Map<String, FacetDefinition> getCommonFacetDefinitions(SearchServiceCreationHelper helper)
091    {
092        Map<String, FacetDefinition> facetDefs = new LinkedHashMap<>();
093        
094        CommonPseudoContentTypeFacetDefinition pseudoContentTypeFacetDefinition = new CommonPseudoContentTypeFacetDefinition(helper._cTypeEP);
095        facetDefs.put(pseudoContentTypeFacetDefinition.getId(), pseudoContentTypeFacetDefinition);
096        
097        return facetDefs;
098    }
099    
100    static Map<String, SortDefinition> getCommonSortDefinitions(SearchServiceCreationHelper helper)
101    {
102        Map<String, SortDefinition> sortDefs = new LinkedHashMap<>();
103        
104        CommonPertinenceSortDefinition pertinenceSort = new CommonPertinenceSortDefinition();
105        sortDefs.put(pertinenceSort.getId(), pertinenceSort);
106        CommonTitleSortDefinition titleSort = new CommonTitleSortDefinition();
107        sortDefs.put(titleSort.getId(), titleSort);
108        
109        // Only DESC order is allowed for this kind of dates (last/first validation/modification) because ASC order is irrelevant (in 99% of the time) for search engine
110        Order[] dateOrders = new Sort.Order[] {Sort.Order.DESC};
111        
112        SystemProperty lastValidationSystemProp = helper._systemPropertyEP.getExtension("lastValidation");
113        CommonSystemPropertyBasedSortDefinition lastValidationSort = new CommonSystemPropertyBasedSortDefinition(lastValidationSystemProp, dateOrders);
114        sortDefs.put(lastValidationSort.getId(), lastValidationSort);
115        
116        SystemProperty firstValidationSystemProp = helper._systemPropertyEP.getExtension("firstValidation");
117        CommonSystemPropertyBasedSortDefinition firstValidationSort = new CommonSystemPropertyBasedSortDefinition(firstValidationSystemProp, dateOrders);
118        sortDefs.put(firstValidationSort.getId(), firstValidationSort);
119        
120        SystemProperty lastModifiedSystemProp = helper._systemPropertyEP.getExtension("lastModified");
121        CommonSystemPropertyBasedSortDefinition lastModificationSort = new CommonSystemPropertyBasedSortDefinition(lastModifiedSystemProp, dateOrders);
122        sortDefs.put(lastModificationSort.getId(), lastModificationSort);
123        
124        SystemProperty lastMajorValidationSystemProp = helper._systemPropertyEP.getExtension("lastMajorValidation");
125        CommonSystemPropertyBasedSortDefinition lastMajorValidationSort = new CommonSystemPropertyBasedSortDefinition(lastMajorValidationSystemProp, dateOrders);
126        sortDefs.put(lastMajorValidationSort.getId(), lastMajorValidationSort);
127        
128        return sortDefs;
129    }
130}