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.plugins.repository.model.parsing;
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.plugins.repository.data.type.ModelItemTypeConstants;
023import org.ametys.plugins.repository.model.RepeaterDefinition;
024import org.ametys.runtime.model.AbstractModelItemParser;
025import org.ametys.runtime.model.ItemParserHelper;
026import org.ametys.runtime.model.Model;
027import org.ametys.runtime.model.ModelItem;
028import org.ametys.runtime.model.ModelItemGroup;
029import org.ametys.runtime.model.disableconditions.DisableConditions;
030import org.ametys.runtime.model.type.ModelItemType;
031import org.ametys.runtime.model.type.ModelItemTypeExtensionPoint;
032import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
033
034/**
035 * This class parses the repeater definition
036 */
037public abstract class AbstractRepeaterDefinitionParser extends AbstractModelItemParser
038{
039    /**
040     * Creates a repeater definition parser.
041     * @param modelItemTypeExtensionPoint the extension point to use to get available model items types
042     * @param disableConditionsManager the disable conditions component manager
043     */
044    public AbstractRepeaterDefinitionParser(ModelItemTypeExtensionPoint modelItemTypeExtensionPoint, ThreadSafeComponentManager<DisableConditions> disableConditionsManager)
045    {
046        super(modelItemTypeExtensionPoint, disableConditionsManager);
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        RepeaterDefinition definition = (RepeaterDefinition) super.parse(serviceManager, pluginName, catalog, definitionConfig, model, parent);
054        
055        definition.setAddLabel(_parseI18nizableText(definitionConfig, catalog, "add-label", "plugin.repository", "PLUGINS_REPOSITORY_REPEATER_DEFAULT_ADD_LABEL"));
056        definition.setDeleteLabel(_parseI18nizableText(definitionConfig, catalog, "del-label", "plugin.repository", "PLUGINS_REPOSITORY_REPEATER_DEFAULT_DELETE_LABEL"));
057        definition.setHeaderLabel(definitionConfig.getChild("header-label").getValue(null));
058        definition.setInitialSize(definitionConfig.getAttributeAsInteger("initial-size", 0));
059        definition.setMinSize(definitionConfig.getAttributeAsInteger("min-size", 0));
060        definition.setMaxSize(definitionConfig.getAttributeAsInteger("max-size", -1));
061        
062        return (T) definition;
063    }
064
065    @Override
066    protected RepeaterDefinition _createModelItem(Configuration itemConfig) throws ConfigurationException
067    {
068        return new RepeaterDefinition();
069    }
070    
071    @Override
072    protected String _parseName(Configuration itemConfig) throws ConfigurationException
073    {
074        return ItemParserHelper.parseName(itemConfig, _getNameConfigurationAttribute(), false);
075    }
076    
077    @Override
078    protected ModelItemType _parseType(Configuration config) throws ConfigurationException
079    {
080        return _modelItemTypeExtensionPoint.getExtension(ModelItemTypeConstants.REPEATER_TYPE_ID);
081    }
082}