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