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