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.web.repository.dom;
018
019import java.util.Iterator;
020
021import org.w3c.dom.Element;
022import org.w3c.dom.Node;
023
024import org.ametys.core.right.RightManager;
025import org.ametys.core.user.UserIdentity;
026import org.ametys.plugins.repository.AmetysObjectIterable;
027import org.ametys.plugins.repository.dom.AmetysObjectElement;
028import org.ametys.web.pageaccess.RestrictedPagePolicy;
029import org.ametys.web.renderingcontext.RenderingContext;
030import org.ametys.web.renderingcontext.RenderingContextHandler;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.page.PagesContainer;
033
034/**
035 * DOM {@link Element} wrapping a {@link PagesContainer}.
036 * @param <A> the actual type of the wrapped {@link PagesContainer}.
037 */
038public abstract class AbstractPagesContainerElement<A extends PagesContainer> extends AmetysObjectElement<A>
039{
040    /** Prefix for sitemap namespace. */
041    public static final String NAMESPACE_PREFIX = "sitemap";
042    /** URI for sitemap namespace. */
043    public static final String NAMESPACE_URI = "http://www.ametys.org/inputdata/sitemap/3.0";
044
045    /** The right manager */
046    protected RightManager _rightManager;
047    /** The rendering context handler. */
048    protected RenderingContextHandler _renderingContextHandler;
049    /** The path of the wrapped page. */
050    protected String _currentPagePath;
051    /** The identity of the current user. */
052    protected UserIdentity _userIdentity;
053    /** the {@link RestrictedPagePolicy} of the current site. */
054    protected RestrictedPagePolicy _policy;
055    
056    private AmetysObjectIterable<? extends Page> _children;
057    
058    /**
059     * Constructor.
060     * @param pagesContainer the wrapped {@link PagesContainer}.
061     * @param rightManager the right manager
062     * @param currentPagePath the path of the current page, or null if none.
063     * @param renderingContextHandler the {@link RenderingContextHandler}.
064     * @param userIdentity the identity of the current user, or null if none.
065     */
066    public AbstractPagesContainerElement(A pagesContainer, RightManager rightManager, RenderingContextHandler renderingContextHandler, String currentPagePath, UserIdentity userIdentity)
067    {
068        this(pagesContainer, null, rightManager, renderingContextHandler, currentPagePath, userIdentity);
069    }
070    
071    /**
072     * Constructor.
073     * @param pagesContainer the wrapped {@link PagesContainer}.
074     * @param parent the parent container.
075     * @param rightManager the right manager
076     * @param currentPagePath the path of the current page, or null if none.
077     * @param renderingContextHandler the {@link RenderingContextHandler}.
078     * @param userIdentity the identity of the current user, or null if none.
079     */
080    public AbstractPagesContainerElement(A pagesContainer, AbstractPagesContainerElement<PagesContainer> parent, RightManager rightManager, RenderingContextHandler renderingContextHandler, String currentPagePath, UserIdentity userIdentity)
081    {
082        super(pagesContainer, parent);
083        _rightManager = rightManager;
084        _renderingContextHandler = renderingContextHandler;
085        _currentPagePath = currentPagePath;
086        _userIdentity = userIdentity;
087        _policy = _object.getSite().getRestrictedPagePolicy();
088    }
089    
090    @Override
091    public String getNamespaceURI()
092    {
093        //return "http://www.ametys.org/inputdata/sitemap/3.0";
094        return null;
095    }
096    
097    AmetysObjectIterable<? extends Page> getChildren()
098    {
099        if (_children == null)
100        {
101            _children = _object.getChildrenPages(false);
102        }
103        
104        return _children;
105    }
106    
107    @Override
108    public boolean hasChildNodes()
109    {
110        if (_children == null)
111        {
112            _children = _object.getChildrenPages(false);
113        }
114        
115        return _children.iterator().hasNext();
116    }
117    
118    @SuppressWarnings("unchecked")
119    @Override
120    public Node getFirstChild()
121    {
122        if (_children == null)
123        {
124            _children = _object.getChildrenPages(false);
125        }
126        
127        Iterator<? extends Page> it = _children.iterator();
128        
129        if (!it.hasNext())
130        {
131            return null;
132        }
133        
134        RenderingContext renderingContext = _renderingContextHandler.getRenderingContext();
135        boolean inBackOffice = renderingContext == RenderingContext.BACK || renderingContext == RenderingContext.PREVIEW;
136        
137        Page child = null;
138        
139        while (child == null && it.hasNext())
140        {
141            Page page = it.next();
142            if (inBackOffice || _policy == RestrictedPagePolicy.DISPLAYED || _rightManager.hasReadAccess(_userIdentity, page))
143            {
144                child = page;
145            }
146        }
147        
148        return child == null ? null : new PageElement(child, (AbstractPagesContainerElement<PagesContainer>) this, _rightManager, _renderingContextHandler, _currentPagePath, _userIdentity);
149    }
150    
151    @SuppressWarnings("unchecked")
152    @Override
153    public Node getNextSibling()
154    {
155        if (_parent == null)
156        {
157            return null;
158        }
159        
160        AmetysObjectIterable<? extends Page> children = ((AbstractPagesContainerElement<PagesContainer>) _parent).getChildren();
161        
162        RenderingContext renderingContext = _renderingContextHandler.getRenderingContext();
163        boolean inBackOffice = renderingContext == RenderingContext.BACK || renderingContext == RenderingContext.PREVIEW;
164        
165        boolean isNext = false;
166        Page nextSibling = null;
167        Iterator<? extends Page> it = children.iterator();
168        
169        while (nextSibling == null && it.hasNext())
170        {
171            Page child = it.next();
172            
173            if (isNext && (inBackOffice || _policy == RestrictedPagePolicy.DISPLAYED || _rightManager.hasReadAccess(_userIdentity, child)))
174            {
175                nextSibling = child;
176            }
177            else if (_object.getId().equals(child.getId()))
178            {
179                isNext = true;
180            }
181        }
182        
183        return nextSibling == null ? null : new PageElement(nextSibling, (AbstractPagesContainerElement<PagesContainer>) _parent, _rightManager, _renderingContextHandler, _currentPagePath, _userIdentity);
184    }
185}