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