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.portlet;
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.portlet.NewsLetterSubscriptionPortlet;
39  import fr.paris.lutece.plugins.newsletter.business.portlet.NewsLetterSubscriptionPortletHome;
40  import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants;
41  import fr.paris.lutece.portal.business.portlet.PortletHome;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.message.SiteMessage;
45  import fr.paris.lutece.portal.service.message.SiteMessageException;
46  import fr.paris.lutece.portal.service.message.SiteMessageService;
47  import fr.paris.lutece.portal.service.plugin.Plugin;
48  import fr.paris.lutece.portal.service.plugin.PluginService;
49  import fr.paris.lutece.portal.service.template.AppTemplateService;
50  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
51  import fr.paris.lutece.portal.web.constants.Messages;
52  import fr.paris.lutece.portal.web.constants.Parameters;
53  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
54  import fr.paris.lutece.util.html.HtmlTemplate;
55  
56  import java.util.Collection;
57  import java.util.Enumeration;
58  import java.util.HashMap;
59  import java.util.HashSet;
60  import java.util.Set;
61  
62  import javax.servlet.http.HttpServletRequest;
63  
64  import org.apache.commons.lang3.StringUtils;
65  
66  /**
67   * This class provides the user interface to manage newsletter subscription portlets.
68   */
69  public class NewsLetterSubscriptionPortletJspBean extends PortletJspBean
70  {
71      /**
72       * Serial version UID
73       */
74      private static final long serialVersionUID = -2321192141005152468L;
75  
76      /**
77       * Prefix of the properties related to this checkbox
78       */
79      private static final String PROPERTIES_PREFIX = "portlet.newsletter_subscription";
80  
81      /**
82       * Prefix used to generate checkbox names
83       */
84      private static final String PREFIX_CHECKBOX_NAME = "cbx_snd_";
85  
86      // Bookmarks
87      private static final String BOOKMARK_PAGE_ID = "@page_id@"; // Todo remove the @
88      private static final String BOOKMARK_PORTLET_ID = "@portlet_id@"; // Todo remove the @
89  
90      // Templates
91      private static final String MARK_NEWSLETTER_LIST = "subscribed_newsletter_list";
92      private static final String MARK_SELECTED_NEWSLETTER_LIST = "selected_newsletter_list";
93      private static final String MARK_NEWSLETTER_SUBCRIPTION_LIST = "newsletter_subscription_list";
94  
95      /**
96       * Returns the creation form for the portlet
97       * 
98       * @param request
99       *            the HTML request
100      * @return the HTML code for the page
101      */
102     public String getCreate( HttpServletRequest request )
103     {
104         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
105         String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
106         Plugin plugin = PluginService.getPlugin( NewsLetterConstants.PLUGIN_NAME );
107         // get the list of newsletter
108         Collection<NewsLetter> colNewsLetter = NewsLetterHome.findAll( plugin );
109         colNewsLetter = AdminWorkgroupService.getAuthorizedCollection( colNewsLetter, getUser( ) );
110         Set<Integer> selectedNewsletterList = new HashSet<Integer>( );
111         HashMap<String, Object> model = new HashMap<String, Object>( );
112         model.put( MARK_NEWSLETTER_LIST, colNewsLetter );
113         model.put( MARK_SELECTED_NEWSLETTER_LIST, selectedNewsletterList );
114         HtmlTemplate templateNewsletterList = AppTemplateService.getTemplate(NewsLetterConstants.TEMPLATE_NEWSLETTER_SUBSCRIPTION_LIST, this.getLocale(), model);
115         model.put( MARK_NEWSLETTER_SUBCRIPTION_LIST, templateNewsletterList.getHtml( ) );
116         HtmlTemplate templateCreate = getCreateTemplate( strPageId, strPortletTypeId , model );
117          return  templateCreate.getHtml( );
118     }
119 
120     /**
121      * Processes the creation of the portlet
122      * 
123      * @param request
124      *            the HTML request
125      * @return the URL to redirect to
126      */
127     public String doCreate( HttpServletRequest request )
128     {
129         NewsLetterSubscriptionPortlet/portlet/NewsLetterSubscriptionPortlet.html#NewsLetterSubscriptionPortlet">NewsLetterSubscriptionPortlet portlet = new NewsLetterSubscriptionPortlet( );
130 
131         // Standard controls on the creation form
132         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
133         int nIdPage = Integer.parseInt( strIdPage );
134 
135         String strStyleId = request.getParameter( Parameters.STYLE );
136 
137         if ( StringUtils.isEmpty( strStyleId ) )
138         {
139             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
140         }
141 
142         setPortletCommonData( request, portlet );
143 
144         // mandatory field
145         String strName = portlet.getName( );
146 
147         if ( StringUtils.isBlank( strName ) )
148         {
149             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
150         }
151 
152         portlet.setPageId( nIdPage );
153 
154         // Creating portlet
155         NewsLetterSubscriptionPortletHome.getInstance( ).create( portlet );
156         // get the new portlet id
157 
158         modifySubscriptions( request,  portlet );
159         // Displays the page with the new Portlet
160         return getPageUrl( nIdPage );
161     }
162 
163     /**
164      * Returns the modification form for the portlet
165      * 
166      * @param request
167      *            the HTML request
168      * @return the HTML code for the page
169      */
170     public String getModify( HttpServletRequest request )
171     {
172         // Use the id in the request to load the portlet
173         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
174         int nPortletId = Integer.parseInt( strPortletId );
175         NewsLetterSubscriptionPortletparis/lutece/plugins/newsletter/business/portlet/NewsLetterSubscriptionPortlet.html#NewsLetterSubscriptionPortlet">NewsLetterSubscriptionPortlet portlet = (NewsLetterSubscriptionPortlet) PortletHome.findByPrimaryKey( nPortletId );
176 
177         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
178 
179         HashMap<String, Object> model = new HashMap<String, Object>( );
180         model.put( BOOKMARK_PORTLET_ID, strPortletId );
181         model.put( BOOKMARK_PAGE_ID, strIdPage );
182 
183         Plugin plugin = PluginService.getPlugin( portlet.getPluginName( ) );
184 
185         Collection<NewsLetter> colNewsLetter = NewsLetterHome.findAll( plugin );
186         colNewsLetter = AdminWorkgroupService.getAuthorizedCollection( colNewsLetter, getUser( ) );
187 
188         Set<Integer> selectedNewsletterList = NewsLetterSubscriptionPortletHome.findSelectedNewsletters( nPortletId );
189         model.put( MARK_NEWSLETTER_LIST, colNewsLetter );
190         model.put( MARK_SELECTED_NEWSLETTER_LIST, selectedNewsletterList );
191        // Fill the specific part of the modify form
192         HtmlTemplate template = getModifyTemplate( portlet, model );
193 
194         return template.getHtml( );
195     }
196 
197     /**
198      * Processes the modification of the portlet
199      * 
200      * @param request
201      *            the HTTP request
202      * @return the URL to redirect to
203      */
204     public String doModify( HttpServletRequest request )
205     {
206         // Use the id in the request to load the portlet
207         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
208         int nPortletId = Integer.parseInt( strPortletId );
209         NewsLetterSubscriptionPortletparis/lutece/plugins/newsletter/business/portlet/NewsLetterSubscriptionPortlet.html#NewsLetterSubscriptionPortlet">NewsLetterSubscriptionPortlet portlet = (NewsLetterSubscriptionPortlet) PortletHome.findByPrimaryKey( nPortletId );
210 
211         // Standard controls on the creation form
212         String strStyleId = request.getParameter( Parameters.STYLE );
213 
214         if ( StringUtils.isEmpty( strStyleId ) )
215         {
216             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
217         }
218 
219         setPortletCommonData( request, portlet );
220 
221         // mandatory field
222         String strName = portlet.getName( );
223 
224         if ( StringUtils.isBlank( strName ) )
225         {
226             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
227         }
228 
229         // Update generic values
230         portlet.update( );
231 
232         // Update the selected subscriptions
233         modifySubscriptions( request, portlet );
234 
235         // displays the page with the potlet updated
236         return getPageUrl( portlet.getPageId( ) );
237     }
238 
239     /**
240      * Returns portlet's properties prefix
241      * 
242      * @return prefix
243      */
244     public String getPropertiesPrefix( )
245     {
246         return PROPERTIES_PREFIX;
247     }
248 
249     /**
250      * Helper method to determine which subscriptions were checked in the portlet modification form, and update the database accordingly.
251      * 
252      * @param request
253      *            the HTTP request
254      * @param portlet
255      *            the portlet
256      */
257     private static void modifySubscriptions( HttpServletRequest request, NewsLetterSubscriptionPortlet portlet )
258     {
259         // Build the set of the subscriptions that were checked in the page
260         Set<Integer> checkedSubscriptions = new HashSet<Integer>( );
261 
262         // Read all request parameters
263         @SuppressWarnings( "unchecked" )
264         Enumeration<String> enumParameterNames = request.getParameterNames( );
265 
266         while ( enumParameterNames.hasMoreElements( ) )
267         {
268             String strParameterName = enumParameterNames.nextElement( );
269 
270             // If parameter is a subscription checkbox
271             if ( strParameterName.startsWith( PREFIX_CHECKBOX_NAME ) )
272             {
273                 // Extract the int value concatenated to the prefix
274                 String strSubscriptionId = strParameterName.substring( PREFIX_CHECKBOX_NAME.length( ) );
275 
276                 // Add the Integer object to the set
277                 checkedSubscriptions.add( new Integer( strSubscriptionId ) );
278             }
279         }
280 
281         // Build the set of the subscriptions that were previously associated to the
282         // portlet
283         Set<Integer> previousSubscriptions = NewsLetterSubscriptionPortletHome.findSelectedNewsletters( portlet.getId( ) );
284 
285         // Add the subscriptions that are checked now but were not present before
286         for ( Integer newSubscription : checkedSubscriptions )
287         {
288             if ( !previousSubscriptions.contains( newSubscription ) )
289             {
290                 NewsLetterSubscriptionPortletHome.insertSubscription( portlet.getId( ), newSubscription.intValue( ) );
291             }
292         }
293 
294         // Remove the subscriptions that were present before but are unchecked now
295         for ( Integer oldSubscription : previousSubscriptions )
296         {
297             if ( !checkedSubscriptions.contains( oldSubscription ) )
298             {
299                 NewsLetterSubscriptionPortletHome.removeNewsletter( portlet.getId( ), oldSubscription.intValue( ) );
300             }
301         }
302     }
303 
304     /**
305      * Performs the registration action
306      * 
307      * @param request
308      *            The http request
309      * @throws fr.paris.lutece.portal.service.message.SiteMessageException
310      *             A message handled by the front office
311      * @return An error message
312      */
313     public static String doRegister( HttpServletRequest request ) throws SiteMessageException
314     {
315         SiteMessageService.setMessage( request, NewsLetterConstants.PROPERTY_SUBSCRIPTION_OK_ALERT_MESSAGE, null,
316                 NewsLetterConstants.PROPERTY_SUBSCRIPTION_OK_TITLE_MESSAGE, null, null, SiteMessage.TYPE_CONFIRMATION );
317 
318         return StringUtils.EMPTY;
319     }
320 }