001/*
002 *  Copyright 2010 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.web.repository.sitemap;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.NoSuchElementException;
021
022import javax.jcr.Node;
023
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.AmetysObjectIterator;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.CollectionIterable;
029import org.ametys.plugins.repository.RepositoryConstants;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
032import org.ametys.web.repository.page.MetadataAwarePagesContainer;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.repository.site.Site;
035
036/**
037 * Sitemap of a web site. A sitemap is a hierarchical view of the site. It is composed by <code>Page</code>s A single web site may have many sitemaps.
038 */
039public final class Sitemap extends DefaultTraversableAmetysObject<SitemapFactory> implements MetadataAwarePagesContainer
040{
041    /** Sitemap node type name. */
042    public static final String NODE_TYPE = RepositoryConstants.NAMESPACE_PREFIX + ":sitemap";
043    
044    /**
045     * Creates a {@link Sitemap}.
046     * @param node the node backing this {@link AmetysObject}.
047     * @param parentPath the parent path in the Ametys hierarchy.
048     * @param factory the {@link SitemapFactory} which creates the AmetysObject.
049     */
050    public Sitemap(Node node, String parentPath, SitemapFactory factory)
051    {
052        super(node, parentPath, factory);
053    }
054    
055    @Override
056    public String getPathInSitemap() throws AmetysRepositoryException
057    {
058        return "";
059    }
060    
061    @Override
062    public AmetysObjectIterable< ? extends Page> getChildrenPages() throws AmetysRepositoryException
063    {
064        return getChildrenPages(true);
065    }
066    
067    @Override
068    public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePage) throws AmetysRepositoryException
069    {
070        List<Page> childrenPages = new ArrayList<>();
071        for (AmetysObject childObject : getChildren())
072        {
073            if (childObject instanceof Page)
074            {
075                Page page = (Page) childObject;
076                if (includeInvisiblePage || page.isVisible())
077                {
078                    childrenPages.add(page);
079                }
080            }
081        }
082        return new CollectionIterable<>(childrenPages);
083    }
084    
085    @Override
086    public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException
087    {
088        if (index < 0)
089        {
090            throw new AmetysRepositoryException("Child page index cannot be negative");
091        }
092        
093        AmetysObjectIterable< ? extends Page> childPages = getChildrenPages();
094        AmetysObjectIterator< ? extends Page> it = childPages.iterator();
095        
096        try
097        {
098            it.skip(index);
099            return it.next();
100        }
101        catch (NoSuchElementException e)
102        {
103            throw new UnknownAmetysObjectException("There's no child page at index " + index + " for sitemap " + this.getId());
104        }
105    }
106    
107    @Override
108    public Site getSite() throws AmetysRepositoryException
109    {
110        return getParent().getParent();
111    }
112
113    @Override
114    public String getSiteName() throws AmetysRepositoryException
115    {
116        return getSite().getName();
117    }
118
119    @Override
120    public Sitemap getSitemap() throws AmetysRepositoryException
121    {
122        return this;
123    }
124
125    @Override
126    public String getSitemapName() throws AmetysRepositoryException
127    {
128        return getName();
129    }
130}