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