001/*
002 *  Copyright 2019 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.web.site;
017
018import java.util.Arrays;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.runtime.model.CategorizedElementDefinitionParser;
028import org.ametys.runtime.model.Model;
029import org.ametys.runtime.model.ModelItemGroup;
030
031/**
032 * Parser for Site parameter wrapper.
033 */
034public class SiteParameterWrapperParser extends CategorizedElementDefinitionParser
035{
036    /**
037     * Creates a site parameter wrapper parser.
038     * @param parser the site parameter parser
039     */
040    public SiteParameterWrapperParser(SiteParameterParser parser)
041    {
042        super(parser);
043    }
044    
045    /**
046     * Parses a site parameter wrapper from a XML configuration.
047     * @param serviceManager the service manager
048     * @param pluginName the plugin name declaring this parameter.
049     * @param parameterConfig the XML configuration of the parameter.
050     * @return the parsed parameter.
051     * @throws ConfigurationException if the configuration is not valid.
052     */
053    public SiteParameterWrapper parse(ServiceManager serviceManager, String pluginName, Configuration parameterConfig) throws ConfigurationException
054    {
055        return parse(serviceManager, pluginName, parameterConfig, null, null);
056    }
057    
058    @Override
059    public SiteParameterWrapper parse(ServiceManager serviceManager, String pluginName, Configuration parameterConfig, Model model, ModelItemGroup parent) throws ConfigurationException
060    {
061        SiteParameterWrapper<?> wrapper = (SiteParameterWrapper) super.parse(serviceManager, pluginName, parameterConfig, model, parent);
062        
063        Set<String> siteTypes = new HashSet<>();
064        String siteTypesStr = parameterConfig.getChild("site-types").getValue("");
065        if (StringUtils.isNotEmpty(siteTypesStr))
066        {
067            siteTypes.addAll(Arrays.asList(StringUtils.split(siteTypesStr, ", ")));
068            wrapper.setSiteTypes(siteTypes);
069        }
070        
071        return wrapper;
072    }
073    
074    @Override
075    protected SiteParameterWrapper _createElementDefinitionWrapper()
076    {
077        return new SiteParameterWrapper();
078    }
079}