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.impl;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.List;
022import java.util.Map;
023import java.util.Optional;
024
025import org.apache.avalon.framework.configuration.Configurable;
026import org.apache.avalon.framework.configuration.Configuration;
027import org.apache.avalon.framework.configuration.ConfigurationException;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031
032import org.ametys.cms.search.advanced.AbstractTreeNode;
033import org.ametys.cms.search.query.Query;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.runtime.plugin.component.PluginAware;
036import org.ametys.web.frontoffice.search.instance.model.SearchServiceCriterion;
037import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap;
038import org.ametys.web.frontoffice.search.metamodel.Returnable;
039import org.ametys.web.frontoffice.search.metamodel.ReturnableExtensionPoint;
040import org.ametys.web.frontoffice.search.metamodel.SearchServiceCriterionDefinition;
041import org.ametys.web.frontoffice.search.metamodel.Searchable;
042import org.ametys.web.frontoffice.search.requesttime.impl.SearchComponentHelper;
043import org.ametys.web.repository.page.Page;
044
045/**
046 * {@link Searchable} for {@link Page}s
047 */
048public class PageSearchable implements Searchable, Serviceable, PluginAware, Configurable
049{
050    /** The prefix for the ids of criterion definitions */
051    protected static final String __CRITERION_DEFINITIONS_PREFIX_ID = "PageSearchable$";
052    
053    /** The page returnable */
054    protected Returnable _pageReturnable;
055    /** The label */
056    protected I18nizableText _label;
057    /** The criteria position */
058    protected int _criteriaPosition;
059    /** The search component helper */
060    protected SearchComponentHelper _searchComponentHelper;
061    
062    private String _pluginName;
063
064    @Override
065    public void service(ServiceManager manager) throws ServiceException
066    {
067        ReturnableExtensionPoint resultTypeEP = (ReturnableExtensionPoint) manager.lookup(ReturnableExtensionPoint.ROLE);
068        _pageReturnable = resultTypeEP.getExtension(PageReturnable.ROLE);
069        _searchComponentHelper = (SearchComponentHelper) manager.lookup(SearchComponentHelper.ROLE);
070    }
071    
072    @Override
073    public void configure(Configuration configuration) throws ConfigurationException
074    {
075        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "plugin.web");
076        _criteriaPosition = configuration.getChild("criteriaPosition").getValueAsInteger();
077    }
078    
079    @Override
080    public void setPluginInfo(String pluginName, String featureName, String id)
081    {
082        _pluginName = pluginName;
083    }
084    
085    @Override
086    public I18nizableText getLabel()
087    {
088        return _label;
089    }
090    
091    @Override
092    public Collection<SearchServiceCriterionDefinition> getCriteria(AdditionalParameterValueMap additionalParameterValues)
093    {
094        List<SearchServiceCriterionDefinition> criteria = new ArrayList<>();
095        SearchServiceCriterionDefinition titleCriterionDefinition = new PageTitleCriterionDefinition();
096        titleCriterionDefinition.setName(__CRITERION_DEFINITIONS_PREFIX_ID + "title");
097        titleCriterionDefinition.setPluginName(_pluginName);
098        titleCriterionDefinition.setLabel(new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_SEARCHABLE_PAGE_CRITERION_TITLE"));
099        titleCriterionDefinition.setSearchable(this);
100        criteria.add(titleCriterionDefinition);
101        
102        PageCriterionDefinition pageCriterionDefinition = new PageCriterionDefinition();
103        pageCriterionDefinition.setName(__CRITERION_DEFINITIONS_PREFIX_ID + "page");
104        pageCriterionDefinition.setPluginName(_pluginName);
105        pageCriterionDefinition.setLabel(new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_SEARCHABLE_PAGE_CRITERION_PAGE"));
106        pageCriterionDefinition.setSearchable(this);
107        criteria.add(pageCriterionDefinition);
108        
109        return criteria;
110    }
111    
112    @Override
113    public int criteriaPosition()
114    {
115        return _criteriaPosition;
116    }
117
118    @Override
119    public Optional<Query> joinQuery(Query queryOnCriterion, SearchServiceCriterionDefinition criterion, Collection<Returnable> returnables, AdditionalParameterValueMap additionalParameters)
120    {
121        return returnables.contains(_pageReturnable) ? Optional.of(queryOnCriterion) : Optional.empty();
122    }
123    
124    @Override
125    public Collection<Returnable> relationsWith()
126    {
127        return Collections.singleton(_pageReturnable);
128    }
129    
130    @Override
131    public Query buildQuery(
132            AbstractTreeNode<SearchServiceCriterion<?>> criterionTree, 
133            Map<String, Object> userCriteria, 
134            Collection<Returnable> returnables,
135            Collection<Searchable> searchables, 
136            AdditionalParameterValueMap additionalParameters, 
137            String currentLang, 
138            Map<String, Object> contextualParameters)
139    {
140        return _searchComponentHelper.buildQuery(criterionTree, userCriteria, returnables, searchables, additionalParameters, currentLang, null, contextualParameters);
141    }
142}