001/*
002 *  Copyright 2011 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 */
016
017package org.ametys.plugins.explorer.resources.dom;
018
019import java.util.Iterator;
020import java.util.Map;
021
022import org.w3c.dom.Element;
023import org.w3c.dom.Node;
024
025import org.ametys.core.util.dom.AmetysAttribute;
026import org.ametys.plugins.explorer.resources.Resource;
027import org.ametys.plugins.explorer.resources.ResourceCollection;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030
031/**
032 * DOM {@link Element} wrapping a {@link ResourceCollection}.
033 */
034public class ResourceCollectionElement extends AbstractResourceElement<ResourceCollection>
035{
036    /**
037     * Constructor.
038     * @param collection the wrapped object.
039     */
040    public ResourceCollectionElement(ResourceCollection collection)
041    {
042        super(collection);
043    }
044    
045    /**
046     * Constructor.
047     * @param collection the wrapped object.
048     * @param parent the parent collection.
049     */
050    public ResourceCollectionElement(ResourceCollection collection, ResourceCollectionElement parent)
051    {
052        super(collection, parent);
053    }
054    
055    @Override
056    public String getTagName()
057    {
058        return "collection";
059    }
060    
061    @Override
062    protected Map<String, AmetysAttribute> _lookupAttributes()
063    {
064        Map<String, AmetysAttribute> atts = super._lookupAttributes();
065        atts.put("name", new AmetysAttribute("name", "name", null, _object.getName(), this));
066        atts.put("id", new AmetysAttribute("id", "id", null, _object.getId(), this));
067        
068        return atts;
069    }
070    
071    @Override
072    public Node getFirstChild()
073    {
074        AmetysObjectIterable<AmetysObject> children = _object.getChildren();
075        Iterator<AmetysObject> it = children.iterator();
076        
077        if (!it.hasNext())
078        {
079            return null;
080        }
081        
082        AmetysObject object = it.next();
083        
084        if (object instanceof ResourceCollection)
085        {
086            return new ResourceCollectionElement((ResourceCollection) object, this);
087        }
088        else if (object instanceof Resource)
089        {
090            return new ResourceElement((Resource) object, this);
091        }
092        
093        return null;
094    }
095}