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