001/*
002 *  Copyright 2023 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.Map;
021import java.util.Set;
022
023import org.apache.commons.lang3.LocaleUtils;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.plugins.repository.AmetysObject;
027import org.ametys.plugins.repository.AmetysObjectIterable;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.repository.CollectionIterable;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.page.virtual.AbstractConfigurableVirtualPage;
033import org.ametys.web.repository.page.virtual.VirtualPageConfiguration;
034
035/**
036 * Page representing an UGC page.
037 */
038public class UGCPage extends AbstractConfigurableVirtualPage<UGCPageFactory>
039{
040    private String _title;
041    private String _path;
042    private Content _ugcContent;
043    
044    /**
045     * Constructor.
046     * @param root the root page.
047     * @param syncContent the synchronized content
048     * @param path the path
049     * @param configuration The abstract virtual page's configuration
050     * @param scheme The scheme
051     * @param factory The UGC page factory
052     */
053    public UGCPage(Page root, VirtualPageConfiguration configuration, String scheme, UGCPageFactory factory, Content syncContent, String path)
054    {
055        super(root, configuration, scheme, factory);
056        
057        _path = path;
058        _ugcContent = syncContent;
059        _title = _ugcContent.getTitle(LocaleUtils.toLocale(root.getSitemapName()));
060    }
061    
062    /**
063     * Returns the associated UGC {@link Content}.
064     * @return the associated UGC {@link Content}.
065     */
066    @SuppressWarnings("unchecked")
067    @Override
068    public Content getContent()
069    {
070        return _ugcContent;
071    }
072    
073    public int getDepth() throws AmetysRepositoryException
074    {
075        return _root.getDepth() + (_path.equals("_root") ? 1 : 2);
076    }
077
078    @Override
079    public Set<String> getReferers() throws AmetysRepositoryException
080    {
081        return null;
082    }
083
084    public String getTitle() throws AmetysRepositoryException
085    {
086        return _title;
087    }
088    
089    public String getLongTitle() throws AmetysRepositoryException
090    {
091        return _title;
092    }
093
094    public AmetysObjectIterable<? extends Page> getChildrenPages() throws AmetysRepositoryException
095    {
096        List<Page> children = new ArrayList<>();
097        return new CollectionIterable<>(children);
098    }
099
100    public String getPathInSitemap() throws AmetysRepositoryException
101    {
102        if (_path.equals("_root"))
103        {
104            return _root.getPathInSitemap() + "/" + getName();
105        }
106        else
107        {
108            return _root.getPathInSitemap() + "/" + _path + "/" + getName();
109        }
110    }
111
112    public <A extends AmetysObject> A getChild(String path) throws AmetysRepositoryException, UnknownAmetysObjectException
113    {
114        if (path.isEmpty())
115        {
116            throw new AmetysRepositoryException("path must be non empty");
117        }
118        
119        return null;
120    }
121
122    @SuppressWarnings("unchecked")
123    public AmetysObjectIterable<? extends AmetysObject> getChildren() throws AmetysRepositoryException
124    {
125        return getChildrenPages();
126    }
127
128    public boolean hasChild(String name) throws AmetysRepositoryException
129    {
130        return false;
131    }
132    
133    public String getId() throws AmetysRepositoryException
134    {
135        return _factory.getUgcPageHandler().computePageId(_path, _root, _ugcContent);
136    }
137
138    public String getName() throws AmetysRepositoryException
139    {
140        return _ugcContent.getName();
141    }
142    
143    @SuppressWarnings("unchecked")
144    public Page getParent() throws AmetysRepositoryException
145    {
146        if (!_path.equals("_root"))
147        {
148            String name = _path;
149            Map<String, Map<String, String>> transitionalPageName = _factory.getUgcPageHandler().getTransitionalPage(_root);
150            Map<String, String> attributes = transitionalPageName.get(name);
151            String title = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE);
152            String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE);
153            
154            return _factory.getTransitionalPageFactory().createUGCTransitionalPage(_root, title, metadataValue, name);
155        }
156        else
157        {
158            return _root;
159        }
160    }
161
162    public String getParentPath() throws AmetysRepositoryException
163    {
164        if (!_path.equals("_root"))
165        {
166            return _root.getPath() + "/" + _path;
167        }
168        else
169        {
170            return _root.getPath();
171        }
172    }
173
174    public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePage) throws AmetysRepositoryException
175    {
176        List<Page> children = new ArrayList<>();
177        return new CollectionIterable<>(children);
178    }
179
180    public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException
181    {
182        throw new UnknownAmetysObjectException("There is no child for ugc page");
183    }
184}