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