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