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