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.cms.model;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.avalon.framework.service.ServiceManager;
021
022import org.ametys.cms.data.type.ModelItemTypeConstants;
023import org.ametys.runtime.model.ElementDefinition;
024import org.ametys.runtime.model.Enumerator;
025import org.ametys.runtime.model.Model;
026import org.ametys.runtime.model.ModelItem;
027import org.ametys.runtime.model.ModelItemGroup;
028import org.ametys.runtime.model.disableconditions.DisableConditions;
029import org.ametys.runtime.model.type.ModelItemType;
030import org.ametys.runtime.model.type.ModelItemTypeExtensionPoint;
031import org.ametys.runtime.parameter.Validator;
032import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
033
034/**
035 * {@link ElementDefinition} parser from an XML configuration.
036 */
037public abstract class AbstractElementDefinitionParser extends org.ametys.runtime.model.AbstractElementDefinitionParser
038{
039    /**
040     * Creates an element definition parser.
041     * @param modelItemTypeExtensionPoint the extension point to use to get available element types
042     * @param disableConditionsManager the disable conditions component manager
043     * @param enumeratorManager the enumerator component manager.
044     * @param validatorManager the validator component manager.
045     */
046    public AbstractElementDefinitionParser(ModelItemTypeExtensionPoint modelItemTypeExtensionPoint, ThreadSafeComponentManager<DisableConditions> disableConditionsManager, ThreadSafeComponentManager<Enumerator> enumeratorManager, ThreadSafeComponentManager<Validator> validatorManager)
047    {
048        super(modelItemTypeExtensionPoint, disableConditionsManager, enumeratorManager, validatorManager);
049    }
050    
051    @Override
052    @SuppressWarnings("unchecked")
053    public <T extends ModelItem> T parse(ServiceManager serviceManager, String pluginName, String catalog, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
054    {
055        ElementDefinition definition = (ElementDefinition) super.parse(serviceManager, pluginName, catalog, definitionConfig, model, parent);
056        
057        if (definition instanceof ContentElementDefinition contentElementDefinition)
058        {
059            contentElementDefinition.setContentTypeId(_parseContentTypeId(definitionConfig));
060        }
061        
062        return (T) definition;
063    }
064    
065    @Override
066    protected ElementDefinition _createModelItem(Configuration definitionConfig) throws ConfigurationException
067    {
068        ModelItemType type = _parseType(definitionConfig);
069        if (ModelItemTypeConstants.CONTENT_ELEMENT_TYPE_ID.equals(type.getId()))
070        {
071            return new DefaultContentElementDefinition();
072        }
073        else
074        {
075            return super._createModelItem(definitionConfig);            
076        }
077    }
078    
079    /**
080     * Parses the content type identifier attribute.
081     * @param itemConfig the item configuration to use.
082     * @return the identifier of the content type or <code>null</code> if none defined.
083     * @throws ConfigurationException if the defined content type des not exist
084     */
085    protected String _parseContentTypeId(Configuration itemConfig) throws ConfigurationException
086    {
087        return itemConfig.getAttribute("contentType", null);
088    }
089}