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
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.LinkedHashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.cocoon.ProcessingException;
025
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.runtime.util.ModifiableLabelable;
028
029/**
030 * Simple group of view items
031 */
032public class SimpleViewItemGroup extends AbstractViewItemGroup implements ModifiableLabelable
033{
034    private String _name;
035    private I18nizableText _label;
036    private I18nizableText _description;
037    
038    public String getName()
039    {
040        return _name;
041    }
042
043    public void setName(String name)
044    {
045        _name = name;
046    }
047    
048    public I18nizableText getLabel()
049    {
050        return _label;
051    }
052
053    public void setLabel(I18nizableText label)
054    {
055        _label = label;
056    }
057    
058    public I18nizableText getDescription()
059    {
060        return _description;
061    }
062    
063    public void setDescription(I18nizableText description)
064    {
065        _description = description;
066    }
067    
068    public Map<String, Object> toJSON() throws ProcessingException
069    {
070        Map<String, Object> result = new HashMap<>();
071        
072        result.put("name", getName());
073        result.put("label", getLabel());
074        result.put("description", getDescription());
075        result.put("role", getRole());
076
077        if (!getViewItems().isEmpty())
078        {
079            Map<String, Object> elements = new LinkedHashMap<>();
080            List<Map<String, Object>> groups = new ArrayList<>();
081            for (ViewItem item : getViewItems())
082            {
083                if (item instanceof ViewElement)
084                {
085                    elements.putAll(item.toJSON());
086                }
087                else
088                {
089                    groups.add(item.toJSON());
090                }
091            }
092            
093            if (!groups.isEmpty())
094            {
095                elements.put("groups", groups);    
096            }
097            
098            result.put("elements", elements);
099        }
100        
101        return result;
102    }
103    
104    /**
105     * Include the given view to the group.
106     * Add the items of the view to include if they are not already present in the group or the reference view
107     * @param viewToInclude the view to include
108     * @param referenceView the reference view
109     */
110    public void includeView(View viewToInclude, View referenceView)
111    {
112        ViewHelper.addViewContainerItems(this, viewToInclude, referenceView);
113    }
114    
115    /**
116     * Copy the given group to the current one.
117     * Add the items of the group to copy if they are not already present in the group or the reference view
118     * @param groupToCopy the group to copy
119     * @param referenceView the reference view
120     */
121    public void copyGroupItem(SimpleViewItemGroup groupToCopy, View referenceView)
122    {
123        setRole(groupToCopy.getRole());
124        setName(groupToCopy.getName());
125        setLabel(groupToCopy.getLabel());
126        setDescription(groupToCopy.getDescription());
127        
128        ViewHelper.addViewContainerItems(this, groupToCopy, referenceView);
129    }
130}