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 */
016
017package org.ametys.plugins.blog.repository;
018
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022
023import org.ametys.core.util.I18nUtils;
024import org.ametys.plugins.blog.BlogCacheManager;
025import org.ametys.plugins.repository.AmetysObjectFactory;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.plugin.component.PluginAware;
031import org.ametys.web.repository.page.PageDataTypeExtensionPoint;
032import org.ametys.web.repository.page.PagesContainer;
033import org.ametys.web.service.ServiceExtensionPoint;
034import org.ametys.web.skin.SkinsManager;
035
036/**
037 * {@link AmetysObjectFactory} handling {@link VirtualMonthPage}.
038 */
039public class VirtualMonthPageFactory implements AmetysObjectFactory<VirtualMonthPage>, Serviceable, PluginAware
040{
041    /** The i18n cache. */
042    protected I18nUtils _i18nUtils;
043    
044    /** The plugin name. */
045    protected String _pluginName;
046    
047    /** The i18n catalogue. */
048    protected String _i18nCatalogue;
049    
050    private AmetysObjectResolver _resolver;
051    private BlogCacheManager _cacheManager;
052    private SkinsManager _skinsManager;
053    
054    private PageDataTypeExtensionPoint _pageDataTypeExtensionPoint;
055    private ServiceExtensionPoint _serviceExtensionPoint;
056    
057    @Override
058    public void service(ServiceManager manager) throws ServiceException
059    {
060        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
061        _cacheManager = (BlogCacheManager) manager.lookup(BlogCacheManager.ROLE);
062        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
063        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
064        _pageDataTypeExtensionPoint = (PageDataTypeExtensionPoint) manager.lookup(PageDataTypeExtensionPoint.ROLE);
065        _serviceExtensionPoint = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE);
066    }
067    
068    @Override
069    public void setPluginInfo(String pluginName, String featureName, String id)
070    {
071        _pluginName = pluginName;
072        _i18nCatalogue = "plugin." + pluginName;
073    }
074    
075    @Override
076    public String getScheme()
077    {
078        return "blog-month";
079    }
080    
081    @Override
082    public VirtualMonthPage getAmetysObjectById(String id) throws AmetysRepositoryException
083    {
084        // id is like blog-month://year/month?rootId=...
085        int rootPos = id.indexOf('?');
086        int monthPos = id.lastIndexOf('/', rootPos);
087        String yearStr = id.substring(getScheme().length() + 3, monthPos);
088        String monthStr = id.substring(monthPos + 1, rootPos);
089        String rootId = id.substring(rootPos + "?rootId=".length());
090        
091        Integer year;
092        Integer month;
093        try
094        {
095            year = Integer.parseInt(yearStr);
096            month = Integer.parseInt(monthStr);
097        }
098        catch (NumberFormatException e)
099        {
100            throw new AmetysRepositoryException("Invalid year or month", e);
101        }
102        
103        PagesContainer root = _resolver.resolveById(rootId);
104        
105        if (!_cacheManager.hasMonth(root.getSiteName(), root.getSitemapName(), year, month))
106        {
107            throw new UnknownAmetysObjectException("There's no virtual child page named " + yearStr + " for parent " + rootId); 
108        }
109        
110        String title = translate(root.getSitemapName(), "PLUGINS_BLOG_PAGE_MONTH_" + monthStr);
111        
112        return new VirtualMonthPage(_resolver, _cacheManager, _skinsManager, year, month, title, root, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint);
113    }
114
115    @Override
116    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
117    {
118        // id is like blog-month://year/month?rootId=...
119        int rootPos = id.indexOf('?');
120        int monthPos = id.lastIndexOf('/', rootPos);
121        String yearStr = id.substring(getScheme().length() + 3, monthPos);
122        String monthStr = id.substring(monthPos + 1, rootPos);
123        String rootId = id.substring(rootPos + "?rootId=".length());
124        
125        Integer year = null;
126        Integer month = null;
127        PagesContainer root = null;
128        try
129        {
130            year = Integer.parseInt(yearStr);
131            month = Integer.parseInt(monthStr);
132        }
133        catch (NumberFormatException e)
134        {
135            throw new AmetysRepositoryException("Invalid year or month", e);
136        }
137        
138        try
139        {
140            root = _resolver.resolveById(rootId);
141        }
142        catch (UnknownAmetysObjectException e)
143        {
144            // Ignore.
145        }
146        
147        return root != null && _cacheManager.hasMonth(root.getSiteName(), root.getSitemapName(), year, month);
148    }
149    
150    /**
151     * Get a translated i18n message from the cache.
152     * @param language the language.
153     * @param key the i18n message key.
154     * @return the translated text.
155     */
156    public String translate(String language, String key)
157    {
158        return _i18nUtils.translate(new I18nizableText(_i18nCatalogue, key), language);
159    }
160    
161}