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.PagesContainer; 032import org.ametys.web.site.SiteConfigurationExtensionPoint; 033import org.ametys.web.skin.SkinsManager; 034 035/** 036 * {@link AmetysObjectFactory} handling {@link VirtualYearPage}. 037 */ 038public class VirtualYearPageFactory implements AmetysObjectFactory<VirtualYearPage>, Serviceable, PluginAware 039{ 040 /** The i18n cache. */ 041 protected I18nUtils _i18nUtils; 042 043 /** The plugin name. */ 044 protected String _pluginName; 045 046 /** The i18n catalogue. */ 047 protected String _i18nCatalogue; 048 049 private AmetysObjectResolver _resolver; 050 private BlogCacheManager _cacheManager; 051 private SiteConfigurationExtensionPoint _siteConf; 052 private SkinsManager _skinsManager; 053 054 @Override 055 public void service(ServiceManager manager) throws ServiceException 056 { 057 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 058 _cacheManager = (BlogCacheManager) manager.lookup(BlogCacheManager.ROLE); 059 _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE); 060 _siteConf = (SiteConfigurationExtensionPoint) manager.lookup(SiteConfigurationExtensionPoint.ROLE); 061 _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE); 062 } 063 064 @Override 065 public void setPluginInfo(String pluginName, String featureName, String id) 066 { 067 _pluginName = pluginName; 068 _i18nCatalogue = "plugin." + pluginName; 069 } 070 071 @Override 072 public String getScheme() 073 { 074 return "blog-year"; 075 } 076 077 @Override 078 public VirtualYearPage getAmetysObjectById(String id) throws AmetysRepositoryException 079 { 080 // id is like blog-year://year?rootId=... 081 int i = id.indexOf('?'); 082 String yearStr = id.substring(getScheme().length() + 3, i); 083 String rootId = id.substring(i + "?rootId=".length()); 084 085 Integer year; 086 try 087 { 088 year = Integer.parseInt(yearStr); 089 } 090 catch (NumberFormatException e) 091 { 092 throw new AmetysRepositoryException("Invalid year", e); 093 } 094 095 PagesContainer root = _resolver.resolveById(rootId); 096 097 if (!_cacheManager.hasYear(root.getSiteName(), root.getSitemapName(), year)) 098 { 099 throw new UnknownAmetysObjectException("There's no virtual child page named " + yearStr + " for parent " + rootId); 100 } 101 102 return new VirtualYearPage(_resolver, _cacheManager, _skinsManager, _i18nUtils, _i18nCatalogue, _siteConf, year, yearStr, root); 103 } 104 105 @Override 106 public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException 107 { 108 // id is like blog-year://year?rootId=... 109 int i = id.indexOf('?'); 110 String yearStr = id.substring(getScheme().length() + 3, i); 111 String rootId = id.substring(i + "?rootId=".length()); 112 113 Integer year = null; 114 PagesContainer root = null; 115 try 116 { 117 year = Integer.parseInt(yearStr); 118 } 119 catch (NumberFormatException e) 120 { 121 throw new AmetysRepositoryException("Invalid year", e); 122 } 123 124 try 125 { 126 root = _resolver.resolveById(rootId); 127 } 128 catch (UnknownAmetysObjectException e) 129 { 130 // Ignore. 131 } 132 133 return root != null && _cacheManager.hasYear(root.getSiteName(), root.getSitemapName(), year); 134 } 135 136 /** 137 * Get a translated i18n message from the cache. 138 * @param language the language. 139 * @param key the i18n message key. 140 * @return the translated text. 141 */ 142 public String translate(String language, String key) 143 { 144 return _i18nUtils.translate(new I18nizableText(_i18nCatalogue, key), language); 145 } 146 147}