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 VirtualYearPage}.
038 */
039public class VirtualYearPageFactory implements AmetysObjectFactory<VirtualYearPage>, 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-year";
079    }
080    
081    @Override
082    public VirtualYearPage getAmetysObjectById(String id) throws AmetysRepositoryException
083    {
084        // id is like blog-year://year?rootId=...
085        int i = id.indexOf('?');
086        String yearStr = id.substring(getScheme().length() + 3, i);
087        String rootId = id.substring(i + "?rootId=".length());
088        
089        Integer year;
090        try
091        {
092            year = Integer.parseInt(yearStr);
093        }
094        catch (NumberFormatException e)
095        {
096            throw new AmetysRepositoryException("Invalid year", e);
097        }
098        
099        PagesContainer root = _resolver.resolveById(rootId);
100        
101        if (!_cacheManager.hasYear(root.getSiteName(), root.getSitemapName(), year))
102        {
103            throw new UnknownAmetysObjectException("There's no virtual child page named " + yearStr + " for parent " + rootId); 
104        }
105        
106        return new VirtualYearPage(_resolver, _cacheManager, _skinsManager, _i18nUtils, _i18nCatalogue, year, yearStr, root, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint);
107    }
108
109    @Override
110    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
111    {
112        // id is like blog-year://year?rootId=...
113        int i = id.indexOf('?');
114        String yearStr = id.substring(getScheme().length() + 3, i);
115        String rootId = id.substring(i + "?rootId=".length());
116        
117        Integer year = null;
118        PagesContainer root = null;
119        try
120        {
121            year = Integer.parseInt(yearStr);
122        }
123        catch (NumberFormatException e)
124        {
125            throw new AmetysRepositoryException("Invalid year", e);
126        }
127
128        try
129        {
130            root = _resolver.resolveById(rootId);
131        }
132        catch (UnknownAmetysObjectException e)
133        {
134            // Ignore.
135        }
136        
137        return root != null && _cacheManager.hasYear(root.getSiteName(), root.getSitemapName(), year);
138    }
139    
140    /**
141     * Get a translated i18n message from the cache.
142     * @param language the language.
143     * @param key the i18n message key.
144     * @return the translated text.
145     */
146    public String translate(String language, String key)
147    {
148        return _i18nUtils.translate(new I18nizableText(_i18nCatalogue, key), language);
149    }
150    
151}