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.ArrayList;
019import java.util.List;
020
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.tag.TagProviderExtensionPoint;
027import org.ametys.core.util.I18nUtils;
028import org.ametys.plugins.blog.BlogCacheManager;
029import org.ametys.plugins.repository.AmetysObjectFactory;
030import org.ametys.plugins.repository.AmetysObjectIterable;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.AmetysRepositoryException;
033import org.ametys.plugins.repository.CollectionIterable;
034import org.ametys.plugins.repository.UnknownAmetysObjectException;
035import org.ametys.plugins.repository.jcr.JCRAmetysObject;
036import org.ametys.plugins.repository.virtual.VirtualAmetysObjectFactory;
037import org.ametys.runtime.i18n.I18nizableText;
038import org.ametys.runtime.plugin.component.PluginAware;
039import org.ametys.web.repository.page.Page;
040import org.ametys.web.repository.page.PagesContainer;
041import org.ametys.web.site.SiteConfigurationExtensionPoint;
042import org.ametys.web.skin.SkinsManager;
043
044/**
045 * {@link AmetysObjectFactory} for handling "virtual" blog root page.
046 */
047public class BlogRootPageFactory extends AbstractLogEnabled implements VirtualAmetysObjectFactory<Page>, Serviceable, PluginAware
048{
049    
050    /** The sub-pages scheme. */
051    public static final String SCHEME = "blog-category";
052    
053    /** The ametys object resolver. */
054    protected AmetysObjectResolver _resolver;
055    
056    /** The blog cache manager. */
057    protected BlogCacheManager _cacheManager;
058    
059    /** The tag provider extension point. */
060    protected TagProviderExtensionPoint _tagProviderEP;
061    
062    /** The i18n cache. */
063    protected I18nUtils _i18nUtils;
064    
065    /** The site configuration. */
066    protected SiteConfigurationExtensionPoint _siteConf;
067    
068    /** The plugin name. */
069    protected String _pluginName;
070    
071    /** The i18n catalogue. */
072    protected String _i18nCatalogue;
073    
074    /** The skins manager */
075    protected SkinsManager _skinsManager;
076    
077    @Override
078    public void service(ServiceManager manager) throws ServiceException
079    {
080        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
081        _cacheManager = (BlogCacheManager) manager.lookup(BlogCacheManager.ROLE);
082        _tagProviderEP = (TagProviderExtensionPoint) manager.lookup(TagProviderExtensionPoint.ROLE);
083        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
084        _siteConf = (SiteConfigurationExtensionPoint) manager.lookup(SiteConfigurationExtensionPoint.ROLE);
085        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
086    }
087    
088    @Override
089    public void setPluginInfo(String pluginName, String featureName, String id)
090    {
091        _pluginName = pluginName;
092        _i18nCatalogue = "plugin." + pluginName;
093    }
094    
095    @Override
096    public String getScheme()
097    {
098        return SCHEME;
099    }
100    
101    @Override
102    public AmetysObjectIterable<Page> getChildren(JCRAmetysObject parent)
103    {
104        if (!(parent instanceof PagesContainer))
105        {
106            throw new IllegalArgumentException("The holder of the blog virtual pages should be either a sitemap or a page.");
107        }
108        
109        PagesContainer parentContainer = (PagesContainer) parent;
110        
111        List<Page> children = new ArrayList<>();
112        
113        String lang = parentContainer.getSitemapName();
114        
115        Page yearsPage = new VirtualYearsPage(_resolver, _cacheManager, _skinsManager, _i18nUtils, _i18nCatalogue, _siteConf, translate(lang, "PLUGINS_BLOG_PAGE_YEARS"), parentContainer);
116        Page tagsPage = new VirtualTagsPage(_resolver, _cacheManager, _skinsManager, _i18nUtils, _tagProviderEP, _siteConf, translate(lang, "PLUGINS_BLOG_PAGE_TAGS"), parentContainer);
117        Page postsPage = new VirtualPostsPage(_resolver, _cacheManager, _skinsManager, translate(lang, "PLUGINS_BLOG_PAGE_POSTS"), parentContainer);
118        
119        children.add(yearsPage);
120        children.add(tagsPage);
121        children.add(postsPage);
122        
123        return new CollectionIterable<>(children);
124    }
125    
126    @Override
127    public boolean hasChild(JCRAmetysObject parent, String childName)
128    {
129        return VirtualYearsPage.NAME.equals(childName)
130            || VirtualTagsPage.NAME.equals(childName)
131            || VirtualPostsPage.NAME.equals(childName);
132    }
133    
134    @Override
135    public Page getChild(JCRAmetysObject parent, String childName)
136    {
137        if (!(parent instanceof PagesContainer))
138        {
139            throw new IllegalArgumentException("The holder of the blog virtual pages should be either a sitemap or a page.");
140        }
141        
142        PagesContainer parentContainer = (PagesContainer) parent;
143        
144        String lang = parentContainer.getSitemapName();
145        
146        Page child = null;
147        if (VirtualYearsPage.NAME.equals(childName))
148        {
149            child = new VirtualYearsPage(_resolver, _cacheManager, _skinsManager, _i18nUtils, _i18nCatalogue, _siteConf, translate(lang, "PLUGINS_BLOG_PAGE_YEARS"), parentContainer);
150        }
151        else if (VirtualTagsPage.NAME.equals(childName))
152        {
153            child = new VirtualTagsPage(_resolver, _cacheManager, _skinsManager, _i18nUtils, _tagProviderEP, _siteConf, translate(lang, "PLUGINS_BLOG_PAGE_TAGS"), parentContainer);
154        }
155        else if (VirtualPostsPage.NAME.equals(childName))
156        {
157            child = new VirtualPostsPage(_resolver, _cacheManager, _skinsManager, translate(lang, "PLUGINS_BLOG_PAGE_POSTS"), parentContainer);
158        }
159        else
160        {
161            throw new UnknownAmetysObjectException("Unknown child " + childName + " for object " + parent.getId());
162        }
163        
164        return child;
165    }
166    
167    @Override
168    public Page getAmetysObjectById(String id) throws AmetysRepositoryException
169    {
170        int i = id.indexOf('?');
171        String childName = id.substring(SCHEME.length() + 3, i);
172        String parentId = id.substring(i + "?rootId=".length());
173        
174        PagesContainer parent = _resolver.resolveById(parentId);
175        
176        String lang = parent.getSitemapName();
177        
178        Page child = null;
179        if (VirtualYearsPage.NAME.equals(childName))
180        {
181            child = new VirtualYearsPage(_resolver, _cacheManager, _skinsManager, _i18nUtils, _i18nCatalogue, _siteConf, translate(lang, "PLUGINS_BLOG_PAGE_YEARS"), parent);
182        }
183        else if (VirtualTagsPage.NAME.equals(childName))
184        {
185            child = new VirtualTagsPage(_resolver, _cacheManager, _skinsManager, _i18nUtils, _tagProviderEP, _siteConf, translate(lang, "PLUGINS_BLOG_PAGE_TAGS"), parent);
186        }
187        else if (VirtualPostsPage.NAME.equals(childName))
188        {
189            child = new VirtualPostsPage(_resolver, _cacheManager, _skinsManager,  translate(lang, "PLUGINS_BLOG_PAGE_POSTS"), parent);
190        }
191        
192        return child;
193    }
194    
195    @Override
196    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
197    {
198        return id.startsWith(SCHEME + "://" + VirtualYearsPage.NAME)
199            || id.startsWith(SCHEME + "://" + VirtualTagsPage.NAME)
200            || id.startsWith(SCHEME + "://" + VirtualPostsPage.NAME);
201    }
202    
203    /**
204     * Get a translated i18n message from the cache.
205     * @param language the language.
206     * @param key the i18n message key.
207     * @return the translated text.
208     */
209    public String translate(String language, String key)
210    {
211        return _i18nUtils.translate(new I18nizableText(_i18nCatalogue, key), language);
212    }
213    
214}