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
026/**
027 * View reference to a group of model items 
028 */
029public class ModelViewItemGroup extends AbstractViewItemGroup implements ModelViewItem<ModelItemGroup>
030{
031    private ModelItemGroup _definition;
032
033    public ModelItemGroup getDefinition()
034    {
035        return _definition;
036    }
037    
038    public void setDefinition(ModelItemGroup definition)
039    {
040        if (definition == null)
041        {
042            throw new IllegalArgumentException("Try to set a null definition to the model view item group");
043        }
044        _definition = definition;
045    }
046    
047    public String getName()
048    {
049        if (_definition != null)
050        {
051            return _definition.getName();
052        }
053        else
054        {
055            return null;
056        }
057    }
058    
059    public Map<String, Object> toJSON() throws ProcessingException
060    {
061        Map<String, Object> result = new HashMap<>();
062        
063        result.put("role", getRole());
064        
065        if (!_getChildrenForJSON().isEmpty())
066        {
067            Map<String, Object> elements = new LinkedHashMap<>();
068            List<Map<String, Object>> groups = new ArrayList<>();
069            for (ViewItem item : _getChildrenForJSON())
070            {
071                if (item instanceof ViewElement)
072                {
073                    elements.putAll(item.toJSON());
074                }
075                else
076                {
077                    groups.add(item.toJSON());
078                }
079            }
080            
081            if (!groups.isEmpty())
082            {
083                elements.put("groups", groups);
084            }
085            
086            result.put("elements", elements);
087        }
088        
089        if (getDefinition() != null)
090        {
091            result.putAll(getDefinition().toJSON(false));
092        }
093        
094        return result;
095    }
096    
097    private List<ViewItem> _getChildrenForJSON()
098    {
099        ElementDefinition switcher = getDefinition().getSwitcher();
100        if (switcher == null)
101        {
102            return getViewItems();
103        }
104        
105        List<ViewItem> childrenWithoutSwitcher = new ArrayList<>();
106        for (ViewItem child : getViewItems())
107        {
108            if (child instanceof ViewElement)
109            {
110                ElementDefinition childDefinitonReference = ((ViewElement) child).getDefinition();
111                if (!switcher.equals(childDefinitonReference))
112                {
113                    childrenWithoutSwitcher.add(child);
114                }
115            }
116        }
117        
118        return childrenWithoutSwitcher;
119    }
120}