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 // TODO NEWATTRIBUTEAPI_CONTENT: There is no attribute category on newsletter content. See how to deal with this metadata 071 String categoryID = content.getMetadataHolder().getString("category"); 072 073 Set<String> providerIDs = _categoryProviderEP.getExtensionsIds(); 074 for (String providerID : providerIDs) 075 { 076 CategoryProvider provider = _categoryProviderEP.getExtension(providerID); 077 if (provider.hasCategory(categoryID)) 078 { 079 Category category = provider.getCategory(categoryID); 080 String templateId = category.getTemplate(); 081 082 String templateUrl = "skin://newsletter/" + templateId + "/stylesheets/" + (textMode ? "template-text.xsl" : "template.xsl"); 083 result.put("template", templateUrl); 084 085 String categoryTitle = _i18nUtils.translate(category.getTitle(), category.getLang()); 086 087 result.put("categoryId", categoryID); 088 result.put("categoryTitle", categoryTitle); 089 } 090 } 091 092 long num = content.getValue("newsletter-number", false, -1L); 093 LocalDate date = content.getValue("newsletter-date"); 094 095 result.put("title", content.getTitle()); 096 if (num > -1) 097 { 098 result.put("number", Long.toString(num)); 099 } 100 if (date != null) 101 { 102 result.put("date", DateTimeFormatter.ISO_LOCAL_DATE.format(date.atStartOfDay(ZoneId.systemDefault()))); 103 } 104 105 result.put("contentLanguage", content.getLanguage()); 106 107 Site site = content.getSite(); 108 if (site != null) 109 { 110 request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, site.getName()); 111 112 Sitemap sitemap = site.getSitemap(content.getLanguage()); 113 if (sitemap != null) 114 { 115 request.setAttribute(WebConstants.REQUEST_ATTR_SITEMAP, sitemap); 116 } 117 } 118 119 return result; 120 } 121}