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