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.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.Objects;
022
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.commons.lang3.StringUtils;
026import org.xml.sax.ContentHandler;
027import org.xml.sax.SAXException;
028
029import org.ametys.core.util.XMLUtils;
030
031/**
032 * Simple group of view items
033 */
034public class SimpleViewItemGroup extends AbstractViewItemGroup
035{
036    private String _name;
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 Map<String, Object> toJSON(DefinitionContext context) throws ProcessingException
049    {
050        Map<String, Object> result = new HashMap<>();
051        
052        result.put("name", getName());
053        result.put("label", getLabel());
054        result.put("description", getDescription());
055        result.put("role", getRole());
056
057        List<ViewItem> viewItems = getViewItems();
058        if (!viewItems.isEmpty())
059        {
060            result.put("elements", ViewHelper.viewItemsToJSON(viewItems, context));
061        }
062        
063        return result;
064    }
065    
066    @SuppressWarnings("static-access")
067    public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException
068    {
069        AttributesImpl attributes = new AttributesImpl();
070        attributes.addCDATAAttribute("name", getName());
071        attributes.addCDATAAttribute("role", getRole());
072        
073        XMLUtils.startElement(contentHandler, "group", attributes);
074        
075        XMLUtils.createI18nElementIfNotNull(contentHandler, "label", getLabel());
076        XMLUtils.createI18nElementIfNotNull(contentHandler, "description", getDescription());
077        
078        for (ViewItem viewItem : getViewItems())
079        {
080            viewItem.toSAX(contentHandler, context);
081        }
082        
083        XMLUtils.endElement(contentHandler, "group");
084    }
085    
086    /**
087     * Include the given view to the group.
088     * Add the items of the view to include if they are not already present in the group or the reference view
089     * @param viewToInclude the view to include
090     * @param referenceView the reference view
091     */
092    public void includeView(View viewToInclude, View referenceView)
093    {
094        ViewHelper.addViewAccessorItems(this, viewToInclude, referenceView, StringUtils.EMPTY);
095    }
096    
097    @Override
098    public void copyTo(ViewItem item)
099    {
100        super.copyTo(item);
101        
102        assert item instanceof SimpleViewItemGroup;
103        ((SimpleViewItemGroup) item).setName(getName());
104        ((SimpleViewItemGroup) item).setLabel(getLabel());
105        ((SimpleViewItemGroup) item).setDescription(getDescription());
106    }
107    
108    public ViewItem createInstance()
109    {
110        return new SimpleViewItemGroup();
111    }
112
113    @Override
114    public int hashCode()
115    {
116        final int prime = 31;
117        int result = super.hashCode();
118        result = prime * result + Objects.hash(_name);
119        return result;
120    }
121
122    @Override
123    public boolean equals(Object obj)
124    {
125        if (this == obj)
126        {
127            return true;
128        }
129        if (!super.equals(obj))
130        {
131            return false;
132        }
133        if (getClass() != obj.getClass())
134        {
135            return false;
136        }
137        SimpleViewItemGroup other = (SimpleViewItemGroup) obj;
138        return Objects.equals(_name, other._name);
139    }
140    
141    @Override
142    public String toString()
143    {
144        return _name + ": " + _children.toString();
145    }
146}