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