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.Map;
019
020import org.ametys.plugins.repository.AmetysObject;
021import org.ametys.runtime.model.DefaultElementDefinition;
022import org.ametys.web.repository.page.Page;
023import org.ametys.web.repository.page.SitemapElement;
024
025/**
026 * Object representing a model item with it inheritance if exist
027 * @param <T> Type of the element value
028 */
029public class ViewParameter<T> extends DefaultElementDefinition<T>             
030{
031    private Map<String, String> _inheritances;
032    
033    /**
034     * Get inheritance map
035     * @return the inheritance map
036     */
037    public Map<String, String> getInheritances()
038    {
039        return this._inheritances;
040    }
041    
042    /**
043     * Set inheritance map
044     * @param inheritances the inheritance map
045     */
046    public void setInheritances(Map<String, String> inheritances)
047    {
048        this._inheritances = inheritances;
049    }
050    
051    /**
052     * Determine if this parameter inherits another one
053     * @return true if a inheritance is declared.
054     */
055    public boolean hasInheritances()
056    {
057        return _inheritances != null;
058    }
059    
060    /**
061     * True if the parameter has inheritance from the page
062     * @param page the page
063     * @return true if the parameter has inheritance
064     */
065    public boolean hasInheritance(SitemapElement page)
066    {
067        if (!hasInheritances())
068        {
069            // no inheritance
070            return false;
071        }
072        
073        AmetysObject parent = page.getParent();
074        if (!(parent instanceof Page))
075        {
076            // no parent page
077            return false;
078        }
079        
080        Page parentPage = (Page) parent;
081        Map<String, String> inheritances = getInheritances();
082        for (String template : inheritances.keySet())
083        {
084            // All template match || The parent page match
085            if (template == null || template.equals(parentPage.getTemplate()))
086            {
087                String context = inheritances.get(template);
088                // If context is null, it means the template is excluded from inheritance
089                if (context != null)
090                {
091                    return true;
092                }
093            }
094        }
095        
096        return false;
097    }
098}