001/*
002 *  Copyright 2010 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.ArrayList;
019import java.util.List;
020import java.util.Optional;
021import java.util.stream.Collectors;
022
023import org.ametys.cms.FilterNameHelper;
024import org.ametys.odf.program.Program;
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.UnknownZoneException;
036import org.ametys.web.repository.page.Zone;
037import org.ametys.web.repository.site.Site;
038import org.ametys.web.repository.sitemap.Sitemap;
039
040/**
041 * Page representing a second-level page.
042 */
043public class SecondLevelPage extends AbstractLevelPage
044{
045    private Page _root;
046    private String _name;
047    private String _title;
048    private String _level1;
049    private FirstLevelPage _level1Page;
050    
051    private SecondLevelPageFactory _factory;
052    
053    /**
054     * Constructor.
055     * @param factory the factory
056     * @param root the odf root page.
057     * @param level1Key the parent first level code (name)
058     * @param name the page's name.
059     * @param title the page's title.
060     */
061    public SecondLevelPage(SecondLevelPageFactory factory, Page root, String level1Key, String name, String title)
062    {
063        this(factory, root, level1Key, name, title, null);
064    }
065    
066    /**
067     * Constructor.
068     * @param factory the factory
069     * @param root the odf root page.
070     * @param level1Key the parent first level code (name)
071     * @param name the page's name.
072     * @param title the page's title.
073     * @param firstLevelPage The parent first level page.
074     */
075    public SecondLevelPage(SecondLevelPageFactory factory, Page root, String level1Key, String name, String title, FirstLevelPage firstLevelPage)
076    {
077        _factory = factory;
078        _root = root;
079        _name = name;
080        _title = title;
081        _level1 = level1Key;
082        _level1Page = firstLevelPage;
083    }
084    
085    @Override
086    public int getDepth() throws AmetysRepositoryException
087    {
088        return _root.getDepth() + 2;
089    }
090
091    @Override
092    public String getTitle() throws AmetysRepositoryException
093    {
094        return _title;
095    }
096    
097    @Override
098    public String getLongTitle() throws AmetysRepositoryException
099    {
100        return getParent().getTitle() + " - "  + _title;
101    }
102
103    @Override
104    public Zone getZone(String name) throws UnknownZoneException, AmetysRepositoryException
105    {
106        if (!"default".equals(name))
107        {
108            throw new IllegalArgumentException("Only the zone named 'default' is actually supported on virtual program pages.");
109        }
110        
111        return new SecondLevelZone(this, _factory.getZoneDataTypeEP(), _factory.getServiceEP(), _factory.getZoneItemDataTypeEP());
112    }
113
114    @Override
115    public AmetysObjectIterable<? extends Zone> getZones() throws AmetysRepositoryException
116    {
117        ArrayList<Zone> zones = new ArrayList<>();
118        zones.add(new SecondLevelZone(this, _factory.getZoneDataTypeEP(), _factory.getServiceEP(), _factory.getZoneItemDataTypeEP()));
119        return new CollectionIterable<>(zones);
120    }
121
122    @Override
123    public AmetysObjectIterable<? extends Page> getChildrenPages() throws AmetysRepositoryException
124    {
125        List<ProgramPage> pages = _getChildrenFromCache()
126            .orElseGet(() -> _factory.getODFPageHandler().getProgramsWithRestrictions(_root, _level1, _name, null, null))
127            .stream()
128            .map(this::_toProgramPage)
129            .collect(Collectors.toList());
130        
131        return new CollectionIterable<>(pages);
132    }
133    
134    private Optional<AmetysObjectIterable<Program>> _getChildrenFromCache()
135    {
136        return Optional.ofNullable(_factory.getODFPageCache().getProgramCache(_root, false))
137            .map(firstLevelCache -> firstLevelCache.get(_level1))
138            .map(secondLevelCache -> secondLevelCache.get(_name))
139            .map(CollectionIterable<Program>::new);
140    }
141    
142    private ProgramPage _toProgramPage(Program program)
143    {
144        return new ProgramPage(_factory.getProgramPageFactory(), _root, program, null, null, this);
145    }
146    
147    @Override
148    public String getPathInSitemap() throws AmetysRepositoryException
149    {
150        return _root.getPathInSitemap() + "/" + getParent().getName() + "/" + getName();
151    }
152
153    @Override
154    public Site getSite() throws AmetysRepositoryException
155    {
156        return _root.getSite();
157    }
158
159    @Override
160    public String getSiteName() throws AmetysRepositoryException
161    {
162        return _root.getSiteName();
163    }
164
165    @Override
166    public Sitemap getSitemap() throws AmetysRepositoryException
167    {
168        return _root.getSitemap();
169    }
170
171    @Override
172    public String getSitemapName() throws AmetysRepositoryException
173    {
174        return _root.getSitemapName();
175    }
176
177    @SuppressWarnings("unchecked")
178    @Override
179    public <A extends AmetysObject> A getChild(String path) throws AmetysRepositoryException, UnknownAmetysObjectException
180    {
181        return (A) _factory.getODFPageCache().getChildProgramPage(_root, this, _level1, _name, path);
182    }
183
184    @Override
185    public boolean hasChild(String name) throws AmetysRepositoryException
186    {
187        return _factory.getODFPageCache().getProgramFromPageName(_root, _level1, _name, name).isPresent();
188    }
189    
190    @Override
191    public String getId() throws AmetysRepositoryException
192    {
193        // E.g: odfLevel2://XA/ALL?rootId=...
194        return "odfLevel2://" + _factory.getODFPageHandler().encodeLevelValue(_level1) + "/" + _factory.getODFPageHandler().encodeLevelValue(_name) + "?rootId=" + _root.getId();
195    }
196
197    @Override
198    public String getName() throws AmetysRepositoryException
199    {
200        // E.g: arts-lettres-langues-ALL
201        return FilterNameHelper.filterName(_title) + "-" + _factory.getODFPageHandler().encodeLevelValue(_name);
202    }
203    
204    @SuppressWarnings("unchecked")
205    @Override
206    public Page getParent() throws AmetysRepositoryException
207    {
208        return _level1Page != null ? _level1Page : (Page) _root.getChild(_factory.getODFPageHandler().encodeLevelValue(_level1));
209    }
210
211    @Override
212    public String getParentPath() throws AmetysRepositoryException
213    {
214        return _root.getPath() + "/" + getParent().getName();
215    }
216
217    public ModelLessDataHolder getDataHolder()
218    {
219        RepositoryData repositoryData = new MemoryRepositoryData(getName());
220        return new DefaultModelLessDataHolder(_factory.getPageDataTypeEP(), repositoryData);
221    }
222
223}