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