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.WordingSearchCriterionDefinition;
033import org.ametys.web.frontoffice.search.metamodel.impl.WordingSearchCriterionDefinition.WordingType;
034
035/**
036 * Common implementations for criteria, facets, sorts...
037 */
038public final class SearchServiceCommonImpls
039{
040    private static final String __COMMON_PREFIX = "common$";
041    /** The prefix for the ids of wording criterion definitions */
042    private static final String __WORDING_CRITERION_DEFINITIONS_PREFIX_ID = __COMMON_PREFIX + "wording$";
043    /** The ids of site criterion definition */
044    private static final String __SITE_CRITERION_DEFINITION_ID = __COMMON_PREFIX + "site";
045    
046    private SearchServiceCommonImpls()
047    {
048        // Nothing
049    }
050    
051    static Map<String, SearchCriterionDefinition> getCommonCriterionDefinitions(SearchServiceCreationHelper helper)
052    {
053        Map<String, SearchCriterionDefinition> criterionDefs = new LinkedHashMap<>();
054        
055        // "Wording" (based on "full text" indexation)
056        _getWordingCriteria()
057                .forEach(c -> criterionDefs.put(c.getId(), c));
058        
059        // "Site" common criterion
060        final String siteKey = __SITE_CRITERION_DEFINITION_ID;
061        final String pluginName = "web";
062        final String catalogue = "plugin.web";
063        criterionDefs.put(siteKey, new SiteSearchCriterionDefinition(siteKey, pluginName, new I18nizableText(catalogue, "PLUGINS_WEB_SERVICE_SEARCH_COMMON_CRITERION_SITE"), helper._siteEnumerator, helper._siteQueryHelper, Optional.empty()));
064        
065        return criterionDefs;
066    }
067    
068    private static Stream<SearchCriterionDefinition> _getWordingCriteria()
069    {
070        return Stream.of(new SearchCriterionDefinition[] {
071            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),
072        });
073    }
074    
075    static Map<String, FacetDefinition> getCommonFacetDefinitions(SearchServiceCreationHelper helper)
076    {
077        Map<String, FacetDefinition> facetDefs = new LinkedHashMap<>();
078        
079        CommonPseudoContentTypeFacetDefinition pseudoContentTypeFacetDefinition = new CommonPseudoContentTypeFacetDefinition(helper._cTypeEP);
080        facetDefs.put(pseudoContentTypeFacetDefinition.getId(), pseudoContentTypeFacetDefinition);
081        
082        return facetDefs;
083    }
084    
085    static Map<String, SortDefinition> getCommonSortDefinitions(SearchServiceCreationHelper helper)
086    {
087        Map<String, SortDefinition> sortDefs = new LinkedHashMap<>();
088        
089        CommonPertinenceSortDefinition pertinenceSort = new CommonPertinenceSortDefinition();
090        sortDefs.put(pertinenceSort.getId(), pertinenceSort);
091        CommonTitleSortDefinition titleSort = new CommonTitleSortDefinition();
092        sortDefs.put(titleSort.getId(), titleSort);
093        
094        // 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
095        Order[] dateOrders = new Sort.Order[] {Sort.Order.DESC};
096        
097        SystemProperty lastValidationSystemProp = helper._systemPropertyEP.getExtension("lastValidation");
098        CommonSystemPropertyBasedSortDefinition lastValidationSort = new CommonSystemPropertyBasedSortDefinition(lastValidationSystemProp, dateOrders);
099        sortDefs.put(lastValidationSort.getId(), lastValidationSort);
100        
101        SystemProperty firstValidationSystemProp = helper._systemPropertyEP.getExtension("firstValidation");
102        CommonSystemPropertyBasedSortDefinition firstValidationSort = new CommonSystemPropertyBasedSortDefinition(firstValidationSystemProp, dateOrders);
103        sortDefs.put(firstValidationSort.getId(), firstValidationSort);
104        
105        SystemProperty lastModifiedSystemProp = helper._systemPropertyEP.getExtension("lastModified");
106        CommonSystemPropertyBasedSortDefinition lastModificationSort = new CommonSystemPropertyBasedSortDefinition(lastModifiedSystemProp, dateOrders);
107        sortDefs.put(lastModificationSort.getId(), lastModificationSort);
108        
109        SystemProperty lastMajorValidationSystemProp = helper._systemPropertyEP.getExtension("lastMajorValidation");
110        CommonSystemPropertyBasedSortDefinition lastMajorValidationSort = new CommonSystemPropertyBasedSortDefinition(lastMajorValidationSystemProp, dateOrders);
111        sortDefs.put(lastMajorValidationSort.getId(), lastMajorValidationSort);
112        
113        return sortDefs;
114    }
115}