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