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