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