001/*
002 *  Copyright 2015 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.cms.search.model.impl;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.configuration.Configurable;
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.data.type.ModelItemTypeExtensionPoint;
027import org.ametys.cms.data.type.indexing.IndexableElementType;
028import org.ametys.cms.search.model.CriterionDefinitionHelper;
029import org.ametys.cms.search.model.SearchModelCriterionDefinition;
030import org.ametys.cms.search.query.Query.LogicalOperator;
031import org.ametys.cms.search.query.Query.Operator;
032import org.ametys.runtime.model.ItemParserHelper;
033import org.ametys.runtime.model.ItemParserHelper.ConfigurationAndPluginName;
034import org.ametys.runtime.plugin.component.PluginAware;
035
036/**
037 * Base class for static {@link SearchModelCriterionDefinition}
038 * @param <T> Type of the criterion value
039 */
040public abstract class AbstractStaticSearchModelCriterionDefinition<T> extends DefaultSearchModelCriterionDefinition<T> implements Component, Serviceable, PluginAware, Configurable
041{
042    /** The extension point containing all available criterion types */
043    protected ModelItemTypeExtensionPoint _criterionTypeExtensionPoint;
044    
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _criterionTypeExtensionPoint = (ModelItemTypeExtensionPoint) manager.lookup(ModelItemTypeExtensionPoint.ROLE_CRITERION_DEFINITION);
048        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
049    }
050    
051    public void setPluginInfo(String pluginName, String featureName, String id)
052    {
053        setPluginName(pluginName);
054    }
055    
056    public void configure(Configuration configuration) throws ConfigurationException
057    {
058        setName(ItemParserHelper.parseName(configuration, "name"));
059        
060        ConfigurationAndPluginName configurationAndPluginName = new ConfigurationAndPluginName(configuration, getPluginName());
061        setLabel(ItemParserHelper.parseI18nizableText(configurationAndPluginName, "label"));
062        setDescription(ItemParserHelper.parseI18nizableText(configurationAndPluginName, "description"));
063        
064        setMultiple(ItemParserHelper.parseMultiple(configuration));
065        setParsedDefaultValues(ItemParserHelper.parseDefaultValues(configuration, this, ItemParserHelper::parseDefaultValue));
066    }
067    
068    @Override
069    public void setOperator(Operator operator)
070    {
071        throw new UnsupportedOperationException("Unable to set an operator on this criterion");
072    }
073    
074    @Override
075    public void setMultipleOperandOperator(LogicalOperator multipleOperand)
076    {
077        throw new UnsupportedOperationException("Unable to set a multiple operand on this criterion");
078    }
079    
080    @SuppressWarnings("unchecked")
081    @Override
082    public IndexableElementType<T> getType()
083    {
084        return (IndexableElementType<T>) _criterionTypeExtensionPoint.getExtension(getTypeId());
085    }
086    
087    /**
088     * Retrieves the id of  the property's type
089     * @return the id of  the property's type
090     */
091    protected abstract String getTypeId();
092}