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.Collections;
019import java.util.NoSuchElementException;
020import java.util.Set;
021
022import org.ametys.plugins.explorer.resources.ResourceCollection;
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.plugins.repository.AmetysObjectIterable;
025import org.ametys.plugins.repository.AmetysObjectIterator;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.UnknownAmetysObjectException;
028import org.ametys.web.repository.page.Page;
029
030/**
031 * Common class for ODF virtual pages
032 *
033 */
034public abstract class AbstractOdfPage implements Page
035{
036    @Override
037    public Set<String> getReferers() throws AmetysRepositoryException
038    {
039        throw new UnsupportedOperationException("getReferers not supported on virtual odf pages");
040    }
041
042    @Override
043    public ResourceCollection getRootAttachments() throws AmetysRepositoryException
044    {
045        return null;
046    }
047    
048    @Override
049    public PageType getType() throws AmetysRepositoryException
050    {
051        return PageType.CONTAINER;
052    }
053
054    @Override
055    public String getURL() throws AmetysRepositoryException
056    {
057        throw new UnsupportedOperationException("getURL not supported on virtual odf pages");
058    }
059
060    @Override
061    public LinkType getURLType() throws AmetysRepositoryException
062    {
063        throw new UnsupportedOperationException("getURLType not supported on virtual odf pages");
064    }
065    
066    @Override
067    public boolean hasZone(String name) throws AmetysRepositoryException
068    {
069        return "default".equals(name);
070    }
071    
072    @Override
073    public Set<String> getTags() throws AmetysRepositoryException
074    {
075        return Collections.emptySet();
076    }
077    
078    @Override
079    public boolean isVisible() throws AmetysRepositoryException
080    {
081        return true;
082    }
083    
084    @Override
085    public String getPath() throws AmetysRepositoryException
086    {
087        return getParentPath() + "/" + getName();
088    }
089    
090    @SuppressWarnings("unchecked")
091    @Override
092    public AmetysObjectIterable<? extends AmetysObject> getChildren() throws AmetysRepositoryException
093    {
094        return getChildrenPages();
095    }
096    
097    @Override
098    public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePages) throws AmetysRepositoryException
099    {
100        return getChildrenPages();
101    }
102    
103    @Override
104    public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException
105    {
106        if (index < 0)
107        {
108            throw new AmetysRepositoryException("Child page index cannot be negative");
109        }
110        
111        AmetysObjectIterator<? extends Page> childPages = getChildrenPages().iterator();
112        
113        try
114        {
115            childPages.skip(index);
116            return childPages.next();
117        }
118        catch (NoSuchElementException e)
119        {
120            throw new UnknownAmetysObjectException("There's no child page at index " + index + " for page " + this.getId());
121        }
122    }
123}