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