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