001/* 002 * Copyright 2010 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.newsletter.actions; 017 018import java.util.Date; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Set; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.acting.ServiceableAction; 027import org.apache.cocoon.environment.ObjectModelHelper; 028import org.apache.cocoon.environment.Redirector; 029import org.apache.cocoon.environment.Request; 030import org.apache.cocoon.environment.SourceResolver; 031import org.joda.time.format.ISODateTimeFormat; 032 033import org.ametys.cms.repository.Content; 034import org.ametys.core.util.I18nUtils; 035import org.ametys.plugins.newsletter.category.Category; 036import org.ametys.plugins.newsletter.category.CategoryProvider; 037import org.ametys.plugins.newsletter.category.CategoryProviderExtensionPoint; 038import org.ametys.web.repository.content.jcr.DefaultWebContent; 039import org.ametys.web.repository.site.Site; 040import org.ametys.web.repository.sitemap.Sitemap; 041 042/** 043 * This action export the XSL URLs of the view and template to use 044 */ 045public class GetNewsletterTemplateAction extends ServiceableAction 046{ 047 private CategoryProviderExtensionPoint _categoryProviderEP; 048 049 private I18nUtils _i18nUtils; 050 051 @Override 052 public void service(ServiceManager smanager) throws ServiceException 053 { 054 super.service(smanager); 055 _categoryProviderEP = (CategoryProviderExtensionPoint) smanager.lookup(CategoryProviderExtensionPoint.ROLE); 056 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 057 } 058 059 @Override 060 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 061 { 062 Map<String, String> result = new HashMap<>(); 063 boolean textMode = parameters.getParameterAsBoolean("text-mode", false); 064 065 Request request = ObjectModelHelper.getRequest(objectModel); 066 DefaultWebContent content = (DefaultWebContent) request.getAttribute(Content.class.getName()); 067 068 String categoryID = content.getMetadataHolder().getString("category"); 069 070 Set<String> providerIDs = _categoryProviderEP.getExtensionsIds(); 071 for (String providerID : providerIDs) 072 { 073 CategoryProvider provider = _categoryProviderEP.getExtension(providerID); 074 if (provider.hasCategory(categoryID)) 075 { 076 Category category = provider.getCategory(categoryID); 077 String templateId = category.getTemplate(); 078 079 String templateUrl = "skin://newsletter/" + templateId + "/stylesheets/" + (textMode ? "template-text.xsl" : "template.xsl"); 080 result.put("template", templateUrl); 081 082 String categoryTitle = _i18nUtils.translate(category.getTitle(), category.getLang()); 083 084 result.put("categoryId", categoryID); 085 result.put("categoryTitle", categoryTitle); 086 } 087 } 088 089 long num = content.getMetadataHolder().getLong("newsletter-number", -1); 090 Date date = content.getMetadataHolder().getDate("newsletter-date", null); 091 092 result.put("title", content.getTitle()); 093 if (num > -1) 094 { 095 result.put("number", Long.toString(num)); 096 } 097 if (date != null) 098 { 099 result.put("date", ISODateTimeFormat.date().print(date.getTime())); 100 } 101 102 result.put("contentLanguage", content.getLanguage()); 103 104 Site site = content.getSite(); 105 if (site != null) 106 { 107 request.setAttribute("site", site.getName()); 108 109 Sitemap sitemap = site.getSitemap(content.getLanguage()); 110 if (sitemap != null) 111 { 112 request.setAttribute(Sitemap.class.getName(), sitemap); 113 } 114 } 115 116 return result; 117 } 118}