001/*
002 *  Copyright 2018 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.ugc.page;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Set;
021import java.util.stream.Collectors;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.CollectionIterable;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.page.virtual.AbstractConfigurableVirtualPage;
031import org.ametys.web.repository.page.virtual.VirtualPageConfiguration;
032
033
034/**
035 * Page representing a transitional page.
036 */
037public class UGCTransitionalPage extends AbstractConfigurableVirtualPage<UGCTransitionalPageFactory>
038{
039    private String _path;
040    private String _title;
041    private String _metadataValue;
042    
043    /**
044     * Constructor.
045     * @param root the user directory root page.
046     * @param title the page's title.
047     * @param metadataValue the metadata value
048     * @param path the path
049     * @param configuration The abstract virtual page's configuration
050     * @param scheme The scheme
051     * @param factory The UGC transitional page factory
052     */
053    public UGCTransitionalPage(Page root, VirtualPageConfiguration configuration, String scheme, UGCTransitionalPageFactory factory, String title, String metadataValue, String path)
054    {
055        super(root, configuration, scheme, factory);
056       
057        _title = title;
058        _metadataValue = metadataValue;
059        _path = path;
060        _factory = factory;
061    }
062    
063    @Override
064    public int getDepth() throws AmetysRepositoryException
065    {
066        return _root.getDepth() + 1;
067    }
068
069    @Override
070    public Set<String> getReferers() throws AmetysRepositoryException
071    {
072        return null;
073    }
074
075    @Override
076    public String getTitle() throws AmetysRepositoryException
077    {
078        return _title;
079    }
080    
081    @Override
082    public String getLongTitle() throws AmetysRepositoryException
083    {
084        return _title;
085    }
086
087    @Override
088    public AmetysObjectIterable<? extends Page> getChildrenPages() throws AmetysRepositoryException
089    {
090        List<Page> children = new ArrayList<>();
091        
092        AmetysObjectIterable<Content> contentsForTransitionalPage = _factory.getUgcPageHandler().getContentsForTransitionalPage(_root, _metadataValue);
093        for (Content content : contentsForTransitionalPage)
094        {
095            children.add(_factory.getUGCPageFactory().createUGCPage(_root, content, _path));
096        }
097        
098        return new CollectionIterable<>(children);
099    }
100
101    @Override
102    public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePage) throws AmetysRepositoryException
103    {
104        if (includeInvisiblePage)
105        {
106            return getChildrenPages();
107        }
108        else
109        {
110            List<Page> children = new ArrayList<>();
111            return new CollectionIterable<>(children);
112        }
113    }
114    
115    @Override
116    public String getPathInSitemap() throws AmetysRepositoryException
117    {
118        return _root.getPathInSitemap() + "/" + _path;
119    }
120
121    @SuppressWarnings("unchecked")
122    @Override
123    public <A extends AmetysObject> A getChild(String path) throws AmetysRepositoryException, UnknownAmetysObjectException
124    {
125        if (path.isEmpty())
126        {
127            throw new AmetysRepositoryException("path must be non empty");
128        }
129        
130        String name = path;
131        AmetysObjectIterable<Content> contentsForTransitionalPage = _factory.getUgcPageHandler().getContentsForTransitionalPage(_root, _metadataValue);
132        List<Content> contentFilters = contentsForTransitionalPage.stream().filter(c -> c.getName().equals(name)).collect(Collectors.toList());
133        if (!contentFilters.isEmpty())
134        {
135            Content content = contentFilters.get(0);
136            return (A) _factory.getUGCPageFactory().createUGCPage(_root, content, _path);
137        }
138        else
139        {
140            throw new UnknownAmetysObjectException("No ugc page for path " + path);
141        }
142    }
143
144    @SuppressWarnings("unchecked")
145    @Override
146    public AmetysObjectIterable<? extends AmetysObject> getChildren() throws AmetysRepositoryException
147    {
148        return getChildrenPages();
149    }
150
151    @Override
152    public boolean hasChild(String name) throws AmetysRepositoryException
153    {
154        AmetysObjectIterable<Content> contentsForTransitionalPage = _factory.getUgcPageHandler().getContentsForTransitionalPage(_root, _metadataValue);
155        List<Content> contentFilters = contentsForTransitionalPage.stream().filter(c -> c.getName().equals(name)).collect(Collectors.toList());
156        return !contentFilters.isEmpty();
157    }
158    
159    @Override
160    public String getId() throws AmetysRepositoryException
161    {
162        // E.g: ugctransitional://path?rootId=...
163        return "ugctransitional://" + _path + "?rootId=" + _root.getId();
164    }
165
166    @Override
167    public String getName() throws AmetysRepositoryException
168    {
169        return _path;
170    }
171    
172    @SuppressWarnings("unchecked")
173    @Override
174    public Page getParent() throws AmetysRepositoryException
175    {
176        return _root;
177    }
178
179    @Override
180    public String getParentPath() throws AmetysRepositoryException
181    {
182        return _root.getPath();
183    }
184
185    @Override
186    public boolean isVisible() throws AmetysRepositoryException
187    {
188        return _factory.getUgcPageHandler().isClassificationPagesVisible(_root);
189    }
190
191    @Override
192    public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException
193    {
194        return getChildrenPages().stream().collect(Collectors.toList()).get(index);
195    }
196}