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;
020
021import org.w3c.dom.Element;
022import org.w3c.dom.Node;
023
024import org.ametys.core.util.dom.AbstractWrappingAmetysElement;
025import org.ametys.plugins.explorer.resources.Resource;
026import org.ametys.plugins.explorer.resources.ResourceCollection;
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.repository.AmetysObjectIterable;
029import org.ametys.plugins.repository.dom.AmetysObjectElement;
030
031/**
032 * DOM {@link Element} wrapping a {@link Resource} or a {@link ResourceCollection}.
033 * @param <A> the actual type of the wrapped object.
034 */
035public abstract class AbstractResourceElement<A extends AmetysObject> extends AmetysObjectElement<A>
036{
037    /**
038     * Constructor.
039     * @param object the wrapped object.
040     */
041    public AbstractResourceElement(A object)
042    {
043        super(object);
044    }
045    
046    /**
047     * Constructor.
048     * @param object the wrapped object.
049     * @param parent the parent collection.
050     */
051    public AbstractResourceElement(A object, ResourceCollectionElement parent)
052    {
053        super(object, parent);
054    }
055    
056    @Override
057    public Node getNextSibling()
058    {
059        if (_parent == null)
060        {
061            return null;
062        }
063        
064        ResourceCollection parent = (ResourceCollection) ((AbstractWrappingAmetysElement) _parent).getWrappedObject();
065        
066        AmetysObjectIterable<AmetysObject> children = parent.getChildren();
067        
068        boolean isNext = false;
069        AmetysObject nextSibling = null;
070        Iterator<AmetysObject> it = children.iterator();
071        
072        while (nextSibling == null && it.hasNext())
073        {
074            AmetysObject child = it.next();
075            
076            if (isNext && (child instanceof Resource || child instanceof ResourceCollection))
077            {
078                nextSibling = child;
079            }
080            else if (_object.getId().equals(child.getId()))
081            {
082                isNext = true;
083            }
084        }
085        
086        if (nextSibling == null)
087        {
088            return null;
089        }
090        else if (nextSibling instanceof Resource)
091        {
092            return new ResourceElement((Resource) nextSibling, (ResourceCollectionElement) _parent);
093        }
094        else
095        {
096            return new ResourceCollectionElement((ResourceCollection) nextSibling, (ResourceCollectionElement) _parent);
097        }
098    }
099}