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.Optional;
023
024import org.apache.avalon.framework.configuration.Configurable;
025import org.apache.avalon.framework.configuration.Configuration;
026import org.apache.avalon.framework.configuration.ConfigurationException;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030
031import org.ametys.cms.search.query.Query;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.runtime.plugin.component.PluginAware;
035import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap;
036import org.ametys.web.frontoffice.search.metamodel.Returnable;
037import org.ametys.web.frontoffice.search.metamodel.ReturnableExtensionPoint;
038import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
039import org.ametys.web.frontoffice.search.metamodel.Searchable;
040import org.ametys.web.repository.page.Page;
041
042/**
043 * {@link Searchable} for {@link Page}s
044 */
045public class PageSearchable implements Searchable, Serviceable, PluginAware, Configurable
046{
047    /** The prefix for the ids of criterion definitions */
048    protected static final String __CRITERION_DEFINITIONS_PREFIX_ID = "PageSearchable$";
049    
050    /** The page returnable */
051    protected Returnable _pageReturnable;
052    /** The ametys object resolver */
053    protected AmetysObjectResolver _resolver;
054    /** The label */
055    protected I18nizableText _label;
056    /** The criteria position */
057    protected int _criteriaPosition;
058    
059    private String _pluginName;
060
061    @Override
062    public void service(ServiceManager manager) throws ServiceException
063    {
064        ReturnableExtensionPoint resultTypeEP = (ReturnableExtensionPoint) manager.lookup(ReturnableExtensionPoint.ROLE);
065        _pageReturnable = resultTypeEP.getExtension(PageReturnable.ROLE);
066        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
067    }
068    
069    @Override
070    public void configure(Configuration configuration) throws ConfigurationException
071    {
072        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "plugin.web");
073        _criteriaPosition = configuration.getChild("criteriaPosition").getValueAsInteger();
074    }
075    
076    @Override
077    public void setPluginInfo(String pluginName, String featureName, String id)
078    {
079        _pluginName = pluginName;
080    }
081    
082    @Override
083    public I18nizableText getLabel()
084    {
085        return _label;
086    }
087    
088    @Override
089    public Collection<SearchCriterionDefinition> getCriteria(AdditionalParameterValueMap additionalParameterValues)
090    {
091        List<SearchCriterionDefinition> criteria = new ArrayList<>();
092        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));
093        criteria.add(titleCriterionDefinition);
094        
095        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));
096        criteria.add(pageSearchCriterionDefinition);
097        
098        return criteria;
099    }
100    
101    @Override
102    public int criteriaPosition()
103    {
104        return _criteriaPosition;
105    }
106
107    @Override
108    public Optional<Query> joinQuery(Query queryOnCriterion, SearchCriterionDefinition criterion, Collection<Returnable> returnables, AdditionalParameterValueMap additionalParameters)
109    {
110        return returnables.contains(_pageReturnable) ? Optional.of(queryOnCriterion) : Optional.empty();
111    }
112    
113    @Override
114    public Collection<Returnable> relationsWith()
115    {
116        return Collections.singleton(_pageReturnable);
117    }
118}