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 * @param <D> The type of disable conditions managed by the parsed repeater
037 */
038public abstract class AbstractRepeaterDefinitionParser<D extends DisableConditions> extends AbstractModelItemParser<D>
039{
040    /**
041     * Creates a repeater definition parser.
042     * @param modelItemTypeExtensionPoint the extension point to use to get available model items types
043     * @param disableConditionsManager the disable conditions component manager
044     */
045    public AbstractRepeaterDefinitionParser(ModelItemTypeExtensionPoint modelItemTypeExtensionPoint, ThreadSafeComponentManager<DisableConditions> disableConditionsManager)
046    {
047        super(modelItemTypeExtensionPoint, disableConditionsManager);
048    }
049    
050    @Override
051    @SuppressWarnings("unchecked")
052    public <T extends ModelItem> T parse(ServiceManager serviceManager, String pluginName, String catalog, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
053    {
054        RepeaterDefinition definition = (RepeaterDefinition) super.parse(serviceManager, pluginName, catalog, definitionConfig, model, parent);
055        
056        definition.setAddLabel(_parseI18nizableText(definitionConfig, catalog, "add-label", "plugin.repository", "PLUGINS_REPOSITORY_REPEATER_DEFAULT_ADD_LABEL"));
057        definition.setDeleteLabel(_parseI18nizableText(definitionConfig, catalog, "del-label", "plugin.repository", "PLUGINS_REPOSITORY_REPEATER_DEFAULT_DELETE_LABEL"));
058        definition.setHeaderLabel(definitionConfig.getChild("header-label").getValue(null));
059        definition.setInitialSize(definitionConfig.getAttributeAsInteger("initial-size", 0));
060        definition.setMinSize(definitionConfig.getAttributeAsInteger("min-size", 0));
061        definition.setMaxSize(definitionConfig.getAttributeAsInteger("max-size", -1));
062        
063        return (T) definition;
064    }
065
066    @Override
067    protected RepeaterDefinition _createModelItem(Configuration itemConfig) throws ConfigurationException
068    {
069        return new RepeaterDefinition();
070    }
071    
072    @Override
073    protected String _parseName(Configuration itemConfig) throws ConfigurationException
074    {
075        return ItemParserHelper.parseName(itemConfig, _getNameConfigurationAttribute(), false);
076    }
077    
078    @Override
079    protected ModelItemType _parseType(Configuration config) throws ConfigurationException
080    {
081        return _modelItemTypeExtensionPoint.getExtension(ModelItemTypeConstants.REPEATER_TYPE_ID);
082    }
083}