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.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.repository.AmetysObjectIterable;
029import org.ametys.plugins.repository.AmetysObjectIterator;
030import org.ametys.plugins.repository.AmetysRepositoryException;
031import org.ametys.plugins.repository.CollectionIterable;
032import org.ametys.plugins.repository.RepositoryConstants;
033import org.ametys.plugins.repository.UnknownAmetysObjectException;
034import org.ametys.plugins.repository.data.ametysobject.ModifiableModelLessDataAwareAmetysObject;
035import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
036import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelLessDataHolder;
037import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
038import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
039import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
040import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
041import org.ametys.web.repository.page.MetadataAwarePagesContainer;
042import org.ametys.web.repository.page.Page;
043import org.ametys.web.repository.site.Site;
044
045/**
046 * 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.
047 */
048public final class Sitemap extends DefaultTraversableAmetysObject<SitemapFactory> implements ModifiableModelLessDataAwareAmetysObject, MetadataAwarePagesContainer
049{
050    /** Sitemap node type name. */
051    public static final String NODE_TYPE = RepositoryConstants.NAMESPACE_PREFIX + ":sitemap";
052    
053    private static final Logger __LOGGER = LoggerFactory.getLogger(Sitemap.class);
054    
055    /**
056     * Creates a {@link Sitemap}.
057     * @param node the node backing this {@link AmetysObject}.
058     * @param parentPath the parent path in the Ametys hierarchy.
059     * @param factory the {@link SitemapFactory} which creates the AmetysObject.
060     */
061    public Sitemap(Node node, String parentPath, SitemapFactory factory)
062    {
063        super(node, parentPath, factory);
064    }
065    
066    @Override
067    public String getPathInSitemap() throws AmetysRepositoryException
068    {
069        return "";
070    }
071    
072    @Override
073    public AmetysObjectIterable< ? extends Page> getChildrenPages() throws AmetysRepositoryException
074    {
075        return getChildrenPages(true);
076    }
077    
078    @Override
079    public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePage) throws AmetysRepositoryException
080    {
081        List<Page> childrenPages = new ArrayList<>();
082        for (AmetysObject childObject : getChildren())
083        {
084            if (childObject instanceof Page)
085            {
086                Page page = (Page) childObject;
087                if (includeInvisiblePage || page.isVisible())
088                {
089                    childrenPages.add(page);
090                }
091            }
092        }
093        return new CollectionIterable<>(childrenPages);
094    }
095    
096    @Override
097    public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException
098    {
099        if (index < 0)
100        {
101            throw new AmetysRepositoryException("Child page index cannot be negative");
102        }
103        
104        AmetysObjectIterable< ? extends Page> childPages = getChildrenPages();
105        AmetysObjectIterator< ? extends Page> it = childPages.iterator();
106        
107        try
108        {
109            it.skip(index);
110            return it.next();
111        }
112        catch (NoSuchElementException e)
113        {
114            throw new UnknownAmetysObjectException("There's no child page at index " + index + " for sitemap " + this.getId());
115        }
116    }
117    
118    @Override
119    public Site getSite() throws AmetysRepositoryException
120    {
121        return getParent().getParent();
122    }
123
124    @Override
125    public String getSiteName() throws AmetysRepositoryException
126    {
127        return getSite().getName();
128    }
129
130    @Override
131    public Sitemap getSitemap() throws AmetysRepositoryException
132    {
133        return this;
134    }
135
136    @Override
137    public String getSitemapName() throws AmetysRepositoryException
138    {
139        return getName();
140    }
141    
142    public ModifiableModelLessDataHolder getDataHolder()
143    {
144        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
145        return new DefaultModifiableModelLessDataHolder(_getFactory().getPageDataTypeExtensionPoint(), repositoryData);
146    }
147    
148    @Override
149    public ModifiableCompositeMetadata getMetadataHolder()
150    {
151        __LOGGER.warn("The getMetadataHolder method of Sitemap is deprecated. Use the getDataHolder instead. StackTrace: ", new Exception());
152        return super.getMetadataHolder();
153    }
154}