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;
024
025/**
026 * Simple group of view items
027 */
028public class SimpleViewItemGroup extends AbstractViewItemGroup
029{
030    private String _name;
031    
032    public String getName()
033    {
034        return _name;
035    }
036
037    public void setName(String name)
038    {
039        _name = name;
040    }
041    
042    public Map<String, Object> toJSON(DefinitionContext context) throws ProcessingException
043    {
044        Map<String, Object> result = new HashMap<>();
045        
046        result.put("name", getName());
047        result.put("label", getLabel());
048        result.put("description", getDescription());
049        result.put("role", getRole());
050
051        List<ViewItem> viewItems = getViewItems();
052        if (!viewItems.isEmpty())
053        {
054            result.put("elements", ViewHelper.viewItemsToJSON(viewItems, context));
055        }
056        
057        return result;
058    }
059    
060    /**
061     * Include the given view to the group.
062     * Add the items of the view to include if they are not already present in the group or the reference view
063     * @param viewToInclude the view to include
064     * @param referenceView the reference view
065     */
066    public void includeView(View viewToInclude, View referenceView)
067    {
068        ViewHelper.addViewAccessorItems(this, viewToInclude, referenceView);
069    }
070    
071    @Override
072    public void copyTo(ViewItem item)
073    {
074        super.copyTo(item);
075        
076        assert item instanceof SimpleViewItemGroup;
077        ((SimpleViewItemGroup) item).setName(getName());
078        ((SimpleViewItemGroup) item).setLabel(getLabel());
079        ((SimpleViewItemGroup) item).setDescription(getDescription());
080    }
081    
082    public ViewItem createInstance()
083    {
084        return new SimpleViewItemGroup();
085    }
086
087    @Override
088    public int hashCode()
089    {
090        final int prime = 31;
091        int result = super.hashCode();
092        result = prime * result + Objects.hash(_name);
093        return result;
094    }
095
096    @Override
097    public boolean equals(Object obj)
098    {
099        if (this == obj)
100        {
101            return true;
102        }
103        if (!super.equals(obj))
104        {
105            return false;
106        }
107        if (getClass() != obj.getClass())
108        {
109            return false;
110        }
111        SimpleViewItemGroup other = (SimpleViewItemGroup) obj;
112        return Objects.equals(_name, other._name);
113    }
114}