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.service.ServiceException;
019
020import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
021import org.ametys.cms.data.type.indexing.IndexableElementType;
022import org.ametys.runtime.model.DefaultElementDefinition;
023import org.ametys.runtime.model.exception.UnknownTypeException;
024import org.ametys.runtime.model.type.ModelItemType;
025import org.ametys.runtime.plugin.ExtensionPoint;
026import org.ametys.runtime.plugin.component.PluginAware;
027
028/**
029 * Abstract class for single property
030 * @param <T> type of the property values
031 * @param <X> type of ametys object supported by this property
032 */
033public abstract class AbstractProperty<T, X extends ModelAwareDataAwareAmetysObject> extends DefaultElementDefinition<T> implements Property<T, X>, PluginAware
034{
035    @SuppressWarnings("unchecked")
036    public void init(String availableTypesRole) throws Exception
037    {
038        try
039        {
040            ExtensionPoint<ModelItemType> availableTypesExtensionPoint = (ExtensionPoint<ModelItemType>) __serviceManager.lookup(availableTypesRole);
041            String typeId = getTypeId();
042            if (availableTypesExtensionPoint.hasExtension(typeId))
043            {
044                setType(availableTypesExtensionPoint.getExtension(typeId));
045            }
046            else
047            {
048                throw new UnknownTypeException("Type " + typeId + " is not available on extension point " + availableTypesRole);
049            }
050        }
051        catch (ServiceException e)
052        {
053            throw new RuntimeException("Unable to lookup after the extension point " + availableTypesRole, e);
054        }
055    }
056    
057    @Override
058    public void setPluginInfo(String pluginName, String featureName, String id)
059    {
060        setPluginName(pluginName);
061    }
062    
063    public boolean isEditable()
064    {
065        return false;
066    }
067    
068    @Override
069    public IndexableElementType<T> getType()
070    {
071        return (IndexableElementType<T>) super.getType();
072    }
073    
074    /**
075     * Retrieves the id of the property's type
076     * @return the id of the property's type
077     */
078    protected abstract String getTypeId();
079}