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.runtime.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.runtime.model.ItemParserHelper.ConfigurationAndPluginName;
023import org.ametys.runtime.model.disableconditions.DisableConditions;
024
025/**
026 * Parser for categorized {@link ElementDefinition}
027 */
028public class CategorizedElementDefinitionParser
029{
030    private AbstractElementDefinitionParser<? extends DisableConditions> _parser;
031    
032    /**
033     * Creates a categorized element definition parser.
034     * @param parser the element definition parser
035     */
036    public CategorizedElementDefinitionParser(AbstractElementDefinitionParser<? extends DisableConditions> parser)
037    {
038        _parser = parser;
039    }
040
041    /**
042     * Parses an element definition from a XML configuration.
043     * @param serviceManager the service manager
044     * @param pluginName the plugin name declaring this item.
045     * @param definitionConfig the XML configuration of the model item.
046     * @param model the model which defines the model item
047     * @param parent the parent of the model item to create. Can be null if the model item to parse has no parent
048     * @return the parsed model item.
049     * @throws ConfigurationException if the configuration is not valid.
050     */
051    @SuppressWarnings("unchecked")
052    public CategorizedElementDefinitionWrapper parse(ServiceManager serviceManager, String pluginName, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
053    {
054        ElementDefinition definition = _parser.parse(serviceManager, pluginName, definitionConfig, model, parent);
055        CategorizedElementDefinitionWrapper elementDefinitionWrapper = _createElementDefinitionWrapper();
056        
057        elementDefinitionWrapper.setDefinition(definition);
058        ConfigurationAndPluginName definitionConfigAndPluginName = new ConfigurationAndPluginName(definitionConfig, pluginName);
059        elementDefinitionWrapper.setDisplayCategory(ItemParserHelper.parseI18nizableText(definitionConfigAndPluginName, "category"));
060        elementDefinitionWrapper.setDisplayGroup(ItemParserHelper.parseI18nizableText(definitionConfigAndPluginName, "group"));
061        elementDefinitionWrapper.setPosition(definitionConfig.getChild("order").getValueAsLong(-1));
062        
063        return elementDefinitionWrapper;
064    }
065    
066    /**
067     * Create the element definition wrapper to populate it.
068     * @return the wrapper instantiated.
069     */
070    protected CategorizedElementDefinitionWrapper _createElementDefinitionWrapper()
071    {
072        return new CategorizedElementDefinitionWrapper<>();
073    }
074}