001/*
002 *  Copyright 2018 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.runtime.model;
017
018/**
019 * Helper class for views
020 */
021public final class ViewHelper
022{
023    private ViewHelper()
024    {
025        // Empty constructor
026    }
027    
028    /**
029     * Checks if there is a view item with the given name in the view item container
030     * @param container the view item container
031     * @param itemName the name of the item
032     * @return <code>true</code> if the container contains an item with the given name, <code>false</code> otherwise 
033     */
034    public static boolean hasViewItem(ViewItemContainer container, String itemName)
035    {
036        if (itemName == null)
037        {
038            return false;
039        }
040        else
041        {
042            for (ViewItem item : container.getViewItems())
043            {
044                if (itemName.equals(item.getName()))
045                {
046                    return true;
047                }
048                
049                if (item instanceof SimpleViewItemGroup)
050                {
051                    if (((ViewItemContainer) item).hasViewItem(itemName))
052                    {
053                        return true;
054                    }
055                }
056            }
057        
058            // No item found
059            return false;
060        }
061    }
062    
063    /**
064     * Add the items of the view item container to include if they are not already present in the current view item container or  in the reference view
065     * @param currentContainer the current container
066     * @param containerToInclude the container to include
067     * @param referenceView the reference view
068     */
069    public static void addViewContainerItems(ViewItemContainer currentContainer, ViewItemContainer containerToInclude, View referenceView)
070    {
071        for (ViewItem itemToCopy : containerToInclude.getViewItems())
072        {
073            if (itemToCopy instanceof SimpleViewItemGroup)
074            {
075                SimpleViewItemGroup groupToCopy = (SimpleViewItemGroup) itemToCopy;
076                SimpleViewItemGroup group = new SimpleViewItemGroup();
077                group.copyGroupItem(groupToCopy, referenceView);
078                
079                if (!group.getViewItems().isEmpty())
080                {
081                    currentContainer.addViewItem(group);
082                }
083            }
084            else
085            {
086                String itemName = itemToCopy.getName();
087                if (!currentContainer.hasViewItem(itemName) && !referenceView.hasViewItem(itemName))
088                {
089                    currentContainer.addViewItem(itemToCopy);
090                }
091            }
092        }
093    }
094}