001/*
002 *  Copyright 2022 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.model.properties;
017
018import org.apache.avalon.framework.configuration.Configurable;
019import org.apache.avalon.framework.configuration.Configuration;
020import org.apache.avalon.framework.configuration.ConfigurationException;
021
022import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
023import org.ametys.cms.data.type.indexing.IndexableElementType;
024import org.ametys.runtime.i18n.I18nizableText;
025import org.ametys.runtime.model.DefaultElementDefinition;
026import org.ametys.runtime.model.type.ModelItemType;
027import org.ametys.runtime.plugin.ExtensionPoint;
028import org.ametys.runtime.plugin.component.PluginAware;
029
030/**
031 * Abstract class for single property
032 * @param <T> type of the property values
033 * @param <X> type of ametys object supported by this property
034 */
035public abstract class AbstractProperty<T, X extends ModelAwareDataAwareAmetysObject> extends DefaultElementDefinition<T> implements Property<T, X>, Configurable, PluginAware
036{
037    /** extension point containing the types available for this property */
038    protected ExtensionPoint<ModelItemType> _availableTypesExtensionPoint;
039    
040    public void configure(Configuration configuration) throws ConfigurationException
041    {
042        setName(configuration.getAttribute(_getNameConfigurationAttribute()));
043        setLabel(_parseI18nizableText(configuration, getPluginName(), "label"));
044        setDescription(_parseI18nizableText(configuration, getPluginName(), "description"));
045    }
046    
047    /**
048     * Retrieves the name of the configuration attribute that contains the name of the property
049     * @return the name of the configuration attribute that contains the name of the property
050     */
051    protected String _getNameConfigurationAttribute()
052    {
053        return "name";
054    }
055
056    /**
057     * Parse an i18n text.
058     * @param config the configuration to use.
059     * @param pluginName the current plugin name.
060     * @param name the child name.
061     * @return the i18n text.
062     * @throws ConfigurationException if the configuration is not valid.
063     */
064    protected I18nizableText _parseI18nizableText(Configuration config, String pluginName, String name) throws ConfigurationException
065    {
066        return I18nizableText.parseI18nizableText(config.getChild(name), "plugin." + pluginName, getName());
067    }
068    
069    @Override
070    public void setPluginInfo(String pluginName, String featureName, String id)
071    {
072        setPluginName(pluginName);
073    }
074    
075    public boolean isEditable()
076    {
077        return false;
078    }
079    
080    public void setAvailableTypeExtensionPoint(ExtensionPoint<ModelItemType> availableTypesExtensionPoint)
081    {
082        _availableTypesExtensionPoint = availableTypesExtensionPoint;
083    }
084    
085    @SuppressWarnings("unchecked")
086    @Override
087    public IndexableElementType<T> getType()
088    {
089        return (IndexableElementType<T>) _availableTypesExtensionPoint.getExtension(_getTypeId());
090    }
091    
092    /**
093     * Retrieves the id of  the property's type
094     * @return the id of  the property's type
095     */
096    protected abstract String _getTypeId();
097}