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