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.Model;
026import org.ametys.runtime.model.ModelItem;
027import org.ametys.runtime.model.ModelItemGroup;
028import org.ametys.runtime.model.type.ModelItemType;
029import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
030
031/**
032 * This class parses the repeater definition
033 */
034public class RepeaterDefinitionParser extends AbstractModelItemParser
035{
036    /**
037     * Creates a repeater definition parser.
038     * @param modelItemTypeExtensionPoint the extension point to use to get available model items types
039     */
040    public RepeaterDefinitionParser(AbstractThreadSafeComponentExtensionPoint<? extends ModelItemType> modelItemTypeExtensionPoint)
041    {
042        super(modelItemTypeExtensionPoint);
043    }
044    
045    @Override
046    @SuppressWarnings("unchecked")
047    public <T extends ModelItem> T parse(ServiceManager serviceManager, String pluginName, String catalog, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
048    {
049        RepeaterDefinition definition = (RepeaterDefinition) super.parse(serviceManager, pluginName, catalog, definitionConfig, model, parent);
050        
051        definition.setAddLabel(_parseI18nizableText(definitionConfig, catalog, "add-label", "plugin.repository", "PLUGINS_REPOSITORY_REPEATER_DEFAULT_ADD_LABEL"));
052        definition.setDeleteLabel(_parseI18nizableText(definitionConfig, catalog, "del-label", "plugin.repository", "PLUGINS_REPOSITORY_REPEATER_DEFAULT_DELETE_LABEL"));
053        definition.setHeaderLabel(definitionConfig.getChild("header-label").getValue(null));
054        definition.setInitialSize(definitionConfig.getAttributeAsInteger("initial-size", 0));
055        definition.setMinSize(definitionConfig.getAttributeAsInteger("min-size", 0));
056        definition.setMaxSize(definitionConfig.getAttributeAsInteger("max-size", -1));
057        
058        return (T) definition;
059    }
060
061    @Override
062    protected RepeaterDefinition _createModelItem(Configuration itemConfig) throws ConfigurationException
063    {
064        return new RepeaterDefinition();
065    }
066    
067    @Override
068    protected String _parseName(Configuration itemConfig) throws ConfigurationException
069    {
070        String itemName = itemConfig.getAttribute(_getNameConfigurationAttribute());
071        
072        if (!itemName.matches("^[a-zA-Z]((?!__)[a-zA-Z0-9_-])*$"))
073        {
074            throw new ConfigurationException("Invalid model item name: " + itemName + ". The item name must start with a letter and must contain only letters, digits, underscore or dash characters.", itemConfig);
075        }
076        
077        return itemName;
078    }
079    
080    @Override
081    protected ModelItemType _parseType(Configuration config) throws ConfigurationException
082    {
083        return _modelItemTypeExtensionPoint.getExtension(ModelItemTypeConstants.REPEATER_TYPE_ID);
084    }
085}