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.web;
35  
36  import fr.paris.lutece.plugins.newsletter.business.NewsLetter;
37  import fr.paris.lutece.plugins.newsletter.business.NewsLetterHome;
38  import fr.paris.lutece.plugins.newsletter.business.NewsLetterProperties;
39  import fr.paris.lutece.plugins.newsletter.business.NewsletterPropertiesHome;
40  import fr.paris.lutece.plugins.newsletter.service.NewsLetterArchiveService;
41  import fr.paris.lutece.plugins.newsletter.service.NewsLetterRegistrationService;
42  import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants;
43  import fr.paris.lutece.portal.service.captcha.CaptchaSecurityService;
44  import fr.paris.lutece.portal.service.i18n.I18nService;
45  import fr.paris.lutece.portal.service.message.SiteMessageException;
46  import fr.paris.lutece.portal.service.plugin.Plugin;
47  import fr.paris.lutece.portal.service.plugin.PluginService;
48  import fr.paris.lutece.portal.service.template.AppTemplateService;
49  import fr.paris.lutece.portal.web.xpages.XPage;
50  import fr.paris.lutece.portal.web.xpages.XPageApplication;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  
53  import java.util.Collection;
54  import java.util.HashMap;
55  import java.util.Locale;
56  import java.util.Map;
57  
58  import javax.servlet.http.HttpServletRequest;
59  
60  import static fr.paris.lutece.portal.service.admin.AdminUserService.getLocale;
61  
62  /**
63   * This XPage handles the newsletter subscription, and the newsletter archives.
64   */
65  public class NewsLetterApp implements XPageApplication
66  {
67      // Templates used to generate the HTML code
68      private static final String TEMPLATE_XPAGE_NEWSLETTER = "skin/plugins/newsletter/page_newsletter.html";
69      private static final String TEMPLATE_XPAGE_TOS = "skin/plugins/newsletter/tos.html";
70  
71      // Properties
72      private static final String PROPERTY_PATHLABEL = "newsletter.pagePathLabel";
73      private static final String PROPERTY_PAGETITLE = "newsletter.pageTitle";
74  
75      // Bookmarks
76      private static final String MARK_NEWSLETTERS_LIST = "newsletters_list";
77      private static final String MARK_PLUGIN = "plugin";
78      private static final String MARK_TOS = "tos";
79      private static final String MARK_CAPTCHA = "captcha";
80      private static final String MARK_IS_ACTIVE_CAPTCHA = "is_active_captcha";
81      private static final String MARK_PROPERTIES = "properties";
82  
83      // parameters
84      private static final String PARAMETER_VIEW_REQUIREMENT = "view_requirement";
85  
86      // Constants
87      private static final String JCAPTCHA_PLUGIN = "jcaptcha";
88  
89      // Captcha
90      private CaptchaSecurityService _captchaService;
91  
92      /**
93       * 
94       * @param request
95       *            The HTTP request
96       * @param plugin
97       *            The plugin
98       */
99      public void init( HttpServletRequest request, Plugin plugin )
100     {
101     }
102 
103     /**
104      * Returns the Newsletter XPage content depending on the request parameters and the current mode.
105      * 
106      * @param request
107      *            The HTTP request.
108      * @param nMode
109      *            The current mode.
110      * @param plugin
111      *            The Plugin
112      * @return The page content.
113      * @throws fr.paris.lutece.portal.service.message.SiteMessageException
114      *             Throws a message interpreted by the front office
115      */
116     public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws SiteMessageException
117     {
118         String strAction = request.getParameter( NewsLetterConstants.PARAMETER_ACTION );
119         init( request, plugin );
120 
121         XPage resultPage = null;
122 
123         if ( strAction == null )
124         {
125             return getPage( request, plugin );
126         }
127 
128         if ( strAction.equals( NewsLetterConstants.ACTION_REGISTER ) )
129         {
130             NewsLetterRegistrationService.getInstance( ).doSubscription( request );
131         }
132         else
133             if ( strAction.equals( NewsLetterConstants.ACTION_CONFIRM_UNSUBSCRIBE ) )
134             {
135                 NewsLetterRegistrationService.getInstance( ).doConfirmUnSubscribe( request );
136             }
137             else
138                 if ( strAction.equals( NewsLetterConstants.ACTION_UNSUBSCRIBE ) )
139                 {
140                     NewsLetterRegistrationService.getInstance( ).doUnSubscribe( request );
141                 }
142                 else
143                     if ( strAction.equals( NewsLetterConstants.ACTION_CONFIRM_SUBSCRIBE ) )
144                     {
145                         NewsLetterRegistrationService.getInstance( ).doConfirmSubscribe( request );
146                     }
147 
148         resultPage = getPage( request, plugin );
149 
150         return resultPage;
151     }
152 
153     /**
154      * Get the main page of this app.
155      * 
156      * @param request
157      *            The request
158      * @param plugin
159      *            The newsletter plugin
160      * @return The Html content to display
161      */
162     private XPage getPage( HttpServletRequest request, Plugin plugin )
163     {
164         XPage page = new XPage( );
165 
166         if ( request.getParameter( PARAMETER_VIEW_REQUIREMENT ) != null )
167         {
168             // See conditional use
169             page.setTitle( I18nService.getLocalizedString( PROPERTY_PATHLABEL, getLocale( request ) ) );
170             page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGETITLE, getLocale( request ) ) );
171             page.setContent( getRequirement( request, plugin ) );
172         }
173         else
174         {
175             page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PATHLABEL, getLocale( request ) ) );
176             page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGETITLE, getLocale( request ) ) );
177 
178             HashMap<String, Object> model = new HashMap<String, Object>( );
179             Collection<NewsLetter> list = NewsLetterHome.findAll( plugin );
180             NewsLetterProperties properties = NewsletterPropertiesHome.find( plugin );
181             model.put( MARK_PROPERTIES, properties );
182             model.put( MARK_NEWSLETTERS_LIST, list );
183             model.put( MARK_TOS, properties.getTOS( ) );
184 
185             model.put( MARK_PLUGIN, plugin );
186 
187             boolean bIsCaptchaEnabled = PluginService.isPluginEnable( JCAPTCHA_PLUGIN );
188             model.put( MARK_IS_ACTIVE_CAPTCHA, bIsCaptchaEnabled );
189 
190             if ( bIsCaptchaEnabled )
191             {
192                 _captchaService = new CaptchaSecurityService( );
193                 model.put( MARK_CAPTCHA, _captchaService.getHtmlCode( ) );
194             }
195             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_NEWSLETTER, getLocale( request ), model );
196             page.setContent( template.getHtml( ) );
197         }
198 
199         return page;
200     }
201 
202     /**
203      * Returns the Newsletter archive XPage content depending on the request parameters and the current mode.
204      * 
205      * @return The page content.
206      * @param request
207      *            The HTTP request.
208      * @throws SiteMessageException
209      *             A site message exception
210      */
211     public String getShowArchivePage( HttpServletRequest request ) throws SiteMessageException
212     {
213         return NewsLetterArchiveService.getInstance( ).getShowArchivePage( request );
214     }
215 
216     /**
217      * Performs the subscription process
218      * 
219      * @param request
220      *            The Http request
221      * @throws fr.paris.lutece.portal.service.message.SiteMessageException
222      *             The error message thrown to the user
223      */
224     public void doSubscription( HttpServletRequest request ) throws SiteMessageException
225     {
226         NewsLetterRegistrationService.getInstance( ).doSubscription( request );
227     }
228 
229     /**
230      * Performs unsubscription process
231      * 
232      * @param request
233      *            The http request
234      * @throws fr.paris.lutece.portal.service.message.SiteMessageException
235      *             The error message handled by the front office
236      */
237     public void doUnSubscribe( HttpServletRequest request ) throws SiteMessageException
238     {
239         NewsLetterRegistrationService.getInstance( ).doUnSubscribe( request );
240     }
241 
242     /**
243      * Confirm a subscribtion
244      * 
245      * @param request
246      *            The request
247      * @throws SiteMessageException
248      *             A site message exception
249      */
250     public void doConfirmSubscribe( HttpServletRequest request ) throws SiteMessageException
251     {
252         NewsLetterRegistrationService.getInstance( ).doConfirmSubscribe( request );
253     }
254 
255     /**
256      * Return the newsletter requirement
257      * 
258      * @param request
259      *            The HTTP request
260      * @param plugin
261      *            The Plugin
262      * @return the form recap
263      **/
264     private String getRequirement( HttpServletRequest request, Plugin plugin )
265     {
266         Map<String, Object> model = new HashMap<String, Object>( );
267         Locale locale = getLocale( request );
268 
269         NewsLetterProperties properties = NewsletterPropertiesHome.find( plugin );
270         model.put( MARK_TOS, properties.getTOS( ) );
271 
272         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_TOS, locale, model );
273 
274         return template.getHtml( );
275     }
276 
277 }