001/*
002 *  Copyright 2019 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.plugins.odfweb.repository;
017
018import java.util.NoSuchElementException;
019
020import org.ametys.plugins.repository.AmetysObject;
021import org.ametys.plugins.repository.AmetysObjectIterable;
022import org.ametys.plugins.repository.AmetysObjectIterator;
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.plugins.repository.UnknownAmetysObjectException;
025import org.ametys.web.repository.page.Page;
026import org.ametys.web.repository.page.virtual.AbstractConfigurableVirtualPage;
027import org.ametys.web.repository.page.virtual.VirtualPageConfiguration;
028
029/**
030 * Common class for ODF virtual pages
031 * @param <T> The type of factory to use
032 *
033 */
034public abstract class AbstractOdfPage<T extends AbstractOdfPageFactory> extends AbstractConfigurableVirtualPage<T>
035{
036    /** Indexable children attribute name. */
037    public static final String INDEXABLE_CHILDREN = "indexableChildren";
038    
039    /**
040     * The constructor of abstract odf pages
041     * @param root the odf root page.
042     * @param configuration The abstract virtual page configuration
043     * @param scheme The scheme
044     * @param factory The factory
045     */
046    public AbstractOdfPage(Page root, VirtualPageConfiguration configuration, String scheme, T factory)
047    {
048        super(root, configuration, scheme, factory);
049    }
050    
051    @SuppressWarnings("unchecked")
052    @Override
053    public AmetysObjectIterable<? extends AmetysObject> getChildren() throws AmetysRepositoryException
054    {
055        return getChildrenPages();
056    }
057    
058    @Override
059    public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePages) throws AmetysRepositoryException
060    {
061        return getChildrenPages();
062    }
063    
064    @Override
065    public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException
066    {
067        if (index < 0)
068        {
069            throw new AmetysRepositoryException("Child page index cannot be negative");
070        }
071        
072        AmetysObjectIterator<? extends Page> childPages = getChildrenPages().iterator();
073        
074        try
075        {
076            childPages.skip(index);
077            return childPages.next();
078        }
079        catch (NoSuchElementException e)
080        {
081            throw new UnknownAmetysObjectException("There's no child page at index " + index + " for page " + this.getId());
082        }
083    }
084    
085    public boolean isIndexable()
086    {
087        return _factory.getODFPageCache().areChildrenIndexable(_root);
088    }
089}