001/*
002 *  Copyright 2011 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.blog.repository;
017
018import java.util.Collections;
019import java.util.Set;
020
021import org.ametys.plugins.blog.BlogCacheManager;
022import org.ametys.plugins.blog.BlogConstants;
023import org.ametys.plugins.explorer.resources.ResourceCollection;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.EmptyIterable;
029import org.ametys.plugins.repository.UnknownAmetysObjectException;
030import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
031import org.ametys.plugins.repository.data.holder.ModelLessDataHolder;
032import org.ametys.plugins.repository.data.holder.impl.DefaultModelLessDataHolder;
033import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
034import org.ametys.plugins.repository.data.repositorydata.impl.MemoryRepositoryData;
035import org.ametys.web.data.type.ModelItemTypeExtensionPoint;
036import org.ametys.web.repository.page.Page;
037import org.ametys.web.repository.page.SitemapElement;
038import org.ametys.web.repository.page.Zone;
039import org.ametys.web.repository.site.Site;
040import org.ametys.web.repository.sitemap.Sitemap;
041import org.ametys.web.service.ServiceExtensionPoint;
042import org.ametys.web.skin.Skin;
043import org.ametys.web.skin.SkinsManager;
044
045/**
046 * Virtual page representing a month page.
047 */
048public class VirtualMonthPage extends AbstractBlogPage
049{
050    private SitemapElement _root;
051    private SkinsManager _skinsManager;
052    private int _year;
053    private int _month;
054    private String _title;
055    
056    private ModelItemTypeExtensionPoint _pageDataTypeExtensionPoint;
057    private ModelItemTypeExtensionPoint _zoneDataTypeExtensionPoint;
058    private ServiceExtensionPoint _serviceExtensionPoint;
059    private ModelItemTypeExtensionPoint _zoneItemDataTypeExtensionPoint;
060    
061    /**
062     * Constructor.
063     * @param resolver the {@link AmetysObjectResolver}.
064     * @param cacheManager the {@link AmetysObjectResolver}.
065     * @param skinsManager the {@link SkinsManager}
066     * @param year the year
067     * @param month the month 
068     * @param title the page's title.
069     * @param root the blog root page.
070     * @param pageDataTypeExtensionPoint the extension point with available data types for pages
071     * @param zoneDataTypeExtensionPoint the extension point with available data types for zones
072     * @param serviceExtensionPoint The service extension point
073     * @param zoneItemDataTypeExtensionPoint the extension point with available data types for zone items
074     */
075    public VirtualMonthPage(AmetysObjectResolver resolver, BlogCacheManager cacheManager, SkinsManager skinsManager, int year, int month, String title, SitemapElement root, ModelItemTypeExtensionPoint pageDataTypeExtensionPoint, ModelItemTypeExtensionPoint zoneDataTypeExtensionPoint, ServiceExtensionPoint serviceExtensionPoint, ModelItemTypeExtensionPoint zoneItemDataTypeExtensionPoint)
076    {
077        _skinsManager = skinsManager;
078        _root = root;
079        _year = year;
080        _month = month;
081        _title = title;
082        
083        _pageDataTypeExtensionPoint = pageDataTypeExtensionPoint;
084        _zoneDataTypeExtensionPoint = zoneDataTypeExtensionPoint;
085        _serviceExtensionPoint = serviceExtensionPoint;
086        _zoneItemDataTypeExtensionPoint = zoneItemDataTypeExtensionPoint;
087    }
088    
089    /**
090     * Get the page's year.
091     * @return the year.
092     */
093    public int getYear()
094    {
095        return _year;
096    }
097    
098    /**
099     * Get the page's month.
100     * @return the month.
101     */
102    public int getMonth()
103    {
104        return _month;
105    }
106    
107    @Override
108    public int getDepth() throws AmetysRepositoryException
109    {
110        int depth = 0;
111        if (_root instanceof Page)
112        {
113            depth = ((Page) _root).getDepth();
114        }
115        
116        return depth + 3;
117    }
118
119    @Override
120    public Set<String> getReferers() throws AmetysRepositoryException
121    {
122        return null;
123    }
124
125    @Override
126    public ResourceCollection getRootAttachments() throws AmetysRepositoryException
127    {
128        return null;
129    }
130    
131    @Override
132    public String getTitle() throws AmetysRepositoryException
133    {
134        return _title;
135    }
136    
137    @Override
138    public String getLongTitle() throws AmetysRepositoryException
139    {
140        return _title;
141    }
142
143    @Override
144    public String getURL() throws AmetysRepositoryException
145    {
146        throw new UnsupportedOperationException("getURL not supported on virtual blog pages");
147    }
148
149    @Override
150    public LinkType getURLType() throws AmetysRepositoryException
151    {
152        throw new UnsupportedOperationException("getURLType not supported on virtual blog pages");
153    }
154
155    @Override
156    protected Zone getDefaultZone()
157    {
158        Long maxCount = getSite().getValue("posts-service-max-count", false, Long.valueOf(BlogConstants.DEFAULT_POST_COUNT_PER_PAGE));
159        return new PostListZone(this, maxCount.intValue(), _zoneDataTypeExtensionPoint, _serviceExtensionPoint, _zoneItemDataTypeExtensionPoint);
160    }
161    
162    @Override
163    public AmetysObjectIterable<? extends Page> getChildrenPages() throws AmetysRepositoryException
164    {
165        return new EmptyIterable<>();
166    }
167
168    @Override
169    public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePages) throws AmetysRepositoryException
170    {
171        return getChildrenPages();
172    }
173    
174    @Override
175    public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException
176    {
177        throw new UnknownAmetysObjectException("There's no child page at index " + index + " for page " + this.getId());
178    }
179    
180    @Override
181    public String getPathInSitemap() throws AmetysRepositoryException
182    {
183        String rootPath = _root.getPathInSitemap();
184        
185        StringBuilder buff = new StringBuilder(rootPath);
186        if (!rootPath.isEmpty())
187        {
188            buff.append('/');
189        }
190        buff.append(VirtualYearsPage.NAME)
191            .append('/').append(_year)
192            .append('/').append(String.format("%02d", _month));
193        
194        return buff.toString();
195    }
196
197    @Override
198    public Site getSite() throws AmetysRepositoryException
199    {
200        return _root.getSite();
201    }
202
203    @Override
204    public String getSiteName() throws AmetysRepositoryException
205    {
206        return _root.getSiteName();
207    }
208
209    @Override
210    public Sitemap getSitemap() throws AmetysRepositoryException
211    {
212        return _root.getSitemap();
213    }
214
215    @Override
216    public String getSitemapName() throws AmetysRepositoryException
217    {
218        return _root.getSitemapName();
219    }
220
221    @Override
222    public <A extends AmetysObject> A getChild(String path) throws AmetysRepositoryException, UnknownAmetysObjectException
223    {
224        return null;
225    }
226
227    @SuppressWarnings("unchecked")
228    @Override
229    public AmetysObjectIterable<? extends AmetysObject> getChildren() throws AmetysRepositoryException
230    {
231        return getChildrenPages();
232    }
233
234    @Override
235    public boolean hasChild(String name) throws AmetysRepositoryException
236    {
237        return false;
238    }
239    
240    @Override
241    public String getId() throws AmetysRepositoryException
242    {
243        return "blog-month://" + _year + "/" + String.format("%02d", _month) + "?rootId=" + _root.getId();
244    }
245
246    @Override
247    public String getName() throws AmetysRepositoryException
248    {
249        return String.format("%02d", _month);
250    }
251
252    @SuppressWarnings("unchecked")
253    @Override
254    public SitemapElement getParent() throws AmetysRepositoryException
255    {
256        return _root;
257    }
258
259    @Override
260    public String getParentPath() throws AmetysRepositoryException
261    {
262        StringBuilder path = new StringBuilder(_root.getPath());
263        if (path.length() > 0)
264        {
265            path.append('/');
266        }
267        
268        path.append(VirtualYearsPage.NAME).append('/').append(_year);
269        
270        return path.toString();
271    }
272
273    @Override
274    public String getPath() throws AmetysRepositoryException
275    {
276        return getParentPath() + "/" + String.format("%02d", _month);
277    }
278
279    public ModelLessDataHolder getDataHolder()
280    {
281        RepositoryData repositoryData = new MemoryRepositoryData(getName());
282        return new DefaultModelLessDataHolder(_pageDataTypeExtensionPoint, repositoryData);
283    }
284
285    @Override
286    public Set<String> getTags() throws AmetysRepositoryException
287    {
288        return Collections.emptySet();
289    }
290    
291    @Override
292    public String getTemplate() throws AmetysRepositoryException
293    {
294        Skin skin = _skinsManager.getSkin(getSite().getSkinId());
295        
296        if (skin.getTemplate(BLOG_MONTH_TEMPLATE) != null)
297        {
298            return BLOG_MONTH_TEMPLATE;
299        }
300        
301        return super.getTemplate(); 
302    }
303
304    public boolean isVisible() throws AmetysRepositoryException
305    {
306        return true;
307    }
308
309    public ModelAwareDataHolder getTemplateParametersHolder() throws AmetysRepositoryException
310    {
311        return null;
312    }
313}