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