View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.newsletter.service;
35  
36  import fr.paris.lutece.plugins.newsletter.business.SendingNewsLetter;
37  import fr.paris.lutece.plugins.newsletter.business.SendingNewsLetterHome;
38  import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants;
39  import fr.paris.lutece.portal.service.message.SiteMessage;
40  import fr.paris.lutece.portal.service.message.SiteMessageException;
41  import fr.paris.lutece.portal.service.message.SiteMessageService;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.plugin.PluginService;
44  import fr.paris.lutece.portal.service.template.AppTemplateService;
45  import fr.paris.lutece.portal.service.util.AppPathService;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  import org.apache.commons.lang3.StringUtils;
54  
55  import static fr.paris.lutece.portal.service.admin.AdminUserService.getLocale;
56  
57  /**
58   * The service that renders the archived newsletters
59   */
60  public final class NewsLetterArchiveService
61  {
62      private static final String REGEX_ID = "^[\\d]+$";
63      private static final String TEMPLATE_VIEW_NEWSLETTER_ARCHIVE = "skin/plugins/newsletter/page_newsletter_archive.html";
64      private static NewsLetterArchiveServiceewsLetterArchiveService.html#NewsLetterArchiveService">NewsLetterArchiveService _singleton = new NewsLetterArchiveService( );
65  
66      /**
67       * Constructor
68       */
69      private NewsLetterArchiveService( )
70      {
71          if ( _singleton == null )
72          {
73              _singleton = this;
74          }
75      }
76  
77      /**
78       * Fetches the instance of the class
79       * 
80       * @return The singleton
81       */
82      public static NewsLetterArchiveService getInstance( )
83      {
84          return _singleton;
85      }
86  
87      /**
88       * Returns the Newsletter archive XPage content depending on the request parameters and the current mode.
89       * 
90       * @return The page content.
91       * @param request
92       *            The HTTP request.
93       * @throws SiteMessageException
94       *             If parameters are not correct
95       */
96      public String getShowArchivePage( HttpServletRequest request ) throws SiteMessageException
97      {
98          Plugin plugin = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
99          String strSendingId = request.getParameter( NewsLetterConstants.PARAMETER_SENDING_ID );
100         String strBaseUrl = AppPathService.getBaseUrl( request );
101 
102         if ( ( strSendingId == null ) || !strSendingId.matches( REGEX_ID ) )
103         {
104             SiteMessageService.setMessage( request, NewsLetterConstants.PROPERTY_NO_NEWSLETTER_CHOSEN_TITLE_MESSAGE, SiteMessage.TYPE_ERROR );
105             return StringUtils.EMPTY;
106         }
107 
108         int nSendingId = Integer.parseInt( strSendingId );
109         SendingNewsLetter sending = SendingNewsLetterHome.findByPrimaryKey( nSendingId, plugin );
110 
111         if ( ( sending == null ) || StringUtils.isEmpty( sending.getHtml( ) ) )
112         {
113             SiteMessageService.setMessage( request, NewsLetterConstants.PROPERTY_NO_NEWSLETTER_CHOSEN_TITLE_MESSAGE, SiteMessage.TYPE_ERROR );
114             return StringUtils.EMPTY;
115         }
116 
117         Map<String, Object> model = new HashMap<String, Object>( );
118         model.put( NewsLetterConstants.MARK_SENDING, sending );
119 
120         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_NEWSLETTER_ARCHIVE, getLocale( request ), model );
121         template.substitute( NewsLetterConstants.WEBAPP_PATH_FOR_LINKSERVICE, strBaseUrl );
122 
123         return template.getHtml( );
124     }
125 }