001/*
002 *  Copyright 2020 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.parameters.view;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang.StringUtils;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028import org.ametys.cms.model.ElementDefinitionParser;
029import org.ametys.runtime.model.Enumerator;
030import org.ametys.runtime.model.Model;
031import org.ametys.runtime.model.ModelItem;
032import org.ametys.runtime.model.ModelItemGroup;
033import org.ametys.runtime.parameter.Validator;
034import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
035import org.ametys.web.data.type.ModelItemTypeExtensionPoint;
036
037/**
038 * This class parses the view parameters definition
039 */
040public class ViewParameterDefinitionParser extends ElementDefinitionParser
041{
042    /** The logger. */
043    protected static final Logger _LOGGER = LoggerFactory.getLogger(ViewParameterDefinitionParser.class);
044    
045    /**
046     * Creates a view parameters definition parser.
047     * @param viewParameterTypeExtensionPoint the extension point to use to get available element types
048     * @param enumeratorManager the enumerator component manager.
049     * @param validatorManager the validator component manager.
050     */
051    public ViewParameterDefinitionParser(ModelItemTypeExtensionPoint viewParameterTypeExtensionPoint, ThreadSafeComponentManager<Enumerator> enumeratorManager, ThreadSafeComponentManager<Validator> validatorManager)
052    {
053        super(viewParameterTypeExtensionPoint, enumeratorManager, validatorManager);
054    }
055    
056    @Override
057    @SuppressWarnings("unchecked")
058    public <T extends ModelItem> T parse(ServiceManager serviceManager, String pluginName, String catalog, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
059    {
060        ViewParameter viewParameter = (ViewParameter) super.parse(serviceManager, pluginName, catalog, definitionConfig, model, parent);
061        
062        String inheritancesAsString = definitionConfig.getAttribute("inherit", null);
063        Map<String, String> inheritances = _parseInheritance(inheritancesAsString);
064        viewParameter.setInheritances(inheritances);
065        
066        return (T) viewParameter;
067    }
068
069    @Override
070    protected ViewParameter _createModelItem(Configuration definitionConfig) throws ConfigurationException
071    {
072        return new ViewParameter();
073    }
074    
075    /**
076     * Parse the inheritance argument for view parameters
077     * Inheritance works only for template view parameters and zone view parameters
078     * @param inheritanceString the representation of the inheritance context for the templates
079     * @return the inheritance mapping between templates and zones
080     */
081    private Map<String, String> _parseInheritance(String inheritanceString)
082    {
083        if (inheritanceString == null)
084        {
085            return null;
086        }
087
088        Map<String, String> inheritance = new HashMap<>();
089        if (StringUtils.isNotEmpty(inheritanceString))
090        {
091            String[] inheritenaceParts = inheritanceString.split("[, ]");
092            for (String inheritenacePart : inheritenaceParts)
093            {
094                if (StringUtils.isNotEmpty(inheritenacePart))
095                {
096                    String templateName = null;
097                    String zoneName = "";
098
099                    int i = inheritenacePart.indexOf("->");
100                    if (i == -1)
101                    {
102                        zoneName = inheritenacePart.trim();
103                    }
104                    else
105                    {
106                        templateName = inheritenacePart.substring(0, i).trim();
107                        if ("*".equals(templateName))
108                        {
109                            templateName = null;
110                        }
111                        
112                        zoneName = inheritenacePart.substring(i + 2).trim();
113                        if (StringUtils.isEmpty(zoneName))
114                        {
115                            zoneName = null;
116                        }
117                    }
118
119                    if (inheritance.get(templateName) != null)
120                    {
121                        _LOGGER.warn("The inheritance is declared as '" 
122                                    + inheritanceString + "', but this declares twice the same template (" 
123                                    + (templateName != null ? templateName : "*") 
124                                    + "). The first declaration is kept intact.");
125                    }
126                    else
127                    {
128                        inheritance.put(templateName, zoneName);
129                    }
130                }
131            }
132        }
133        
134        return inheritance;
135    }
136}