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.SendingNewsLetter;
37  import fr.paris.lutece.plugins.newsletter.business.SendingNewsLetterHome;
38  import fr.paris.lutece.plugins.newsletter.business.portlet.NewsLetterArchivePortlet;
39  import fr.paris.lutece.plugins.newsletter.business.portlet.NewsLetterArchivePortletHome;
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.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.template.AppTemplateService;
47  import fr.paris.lutece.portal.web.constants.Messages;
48  import fr.paris.lutece.portal.web.constants.Parameters;
49  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  
52  import java.util.ArrayList;
53  import java.util.Enumeration;
54  import java.util.HashMap;
55  import java.util.HashSet;
56  import java.util.List;
57  import java.util.Set;
58  
59  import javax.servlet.http.HttpServletRequest;
60  /**
61   * This class provides the user interface to manage newsletter archive portlets.
62   */
63  public class NewsLetterArchivePortletJspBean extends PortletJspBean
64  {
65      /**
66       * Serial version UID
67       */
68      private static final long serialVersionUID = -5219786237834856528L;
69  
70      // Prefix of the properties related to this checkbox
71      private static final String PROPERTIES_PREFIX = "portlet.newsletter_archive";
72  
73      // Prefix used to generate checkbox names
74      private static final String PREFIX_CHECKBOX_NAME = "cbx_snd_";
75  
76      // Bookmarks
77      private static final String BOOKMARK_PAGE_ID = "@page_id@";
78      private static final String BOOKMARK_PORTLET_ID = "@portlet_id@";
79  
80      // Templates
81      private static final String MARK_SENDING_NEWSLETTER_LIST = "sending_newsletter_list";
82      private static final String MARK_SELECTED_SENDING_LIST = "selected_sendings_list";
83      private static final String MARK_NEWSLETTER_SUBCRIPTION_LIST = "newsletter_subscription_list";
84  
85  
86      /**
87       * Returns the creation form for the portlet
88       * 
89       * @param request
90       *            the HTML request
91       * @return the HTML code for the page
92       */
93      public String getCreate( HttpServletRequest request )
94      {
95          String strPageId = request.getParameter( PARAMETER_PAGE_ID );
96          String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
97          Plugin plugin = PluginService.getPlugin( NewsLetterConstants.PLUGIN_NAME );
98          ArrayList<Integer> selectedSendings = new ArrayList<>();
99          List<SendingNewsLetter> sendingNewsletterList = SendingNewsLetterHome.findAllSendings( plugin );
100             HashMap<String, Object> model = new HashMap<String, Object>( );
101             model.put( MARK_SENDING_NEWSLETTER_LIST, sendingNewsletterList );
102             model.put( MARK_SELECTED_SENDING_LIST, selectedSendings );
103         HtmlTemplate templateNewsletterList = AppTemplateService.getTemplate( NewsLetterConstants.TEMPLATE_NEWSLETTER_ARCHIVE_LIST, this.getLocale( ),
104                 model );
105         model.put( MARK_NEWSLETTER_SUBCRIPTION_LIST, templateNewsletterList.getHtml( ) );
106         HtmlTemplate templateCreate = getCreateTemplate( strPageId, strPortletTypeId , model );
107         return  templateCreate.getHtml( );
108     }
109 
110     /**
111      * Processes the creation of the portlet
112      * 
113      * @param request
114      *            the HTML request
115      * @return the URL to redirect to
116      */
117     public String doCreate( HttpServletRequest request )
118     {
119         NewsLetterArchivePortletiness/portlet/NewsLetterArchivePortlet.html#NewsLetterArchivePortlet">NewsLetterArchivePortlet portlet = new NewsLetterArchivePortlet( );
120 
121         // Standard controls on the creation form
122         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
123         int nIdPage = Integer.parseInt( strIdPage );
124 
125         String strStyleId = request.getParameter( Parameters.STYLE );
126 
127         if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
128         {
129             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
130         }
131 
132         setPortletCommonData( request, portlet );
133 
134         // mandatory field
135         String strName = portlet.getName( );
136 
137         if ( strName.trim( ).equals( "" ) )
138         {
139             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
140         }
141 
142         portlet.setPageId( nIdPage );
143 
144         // Creating portlet
145         NewsLetterArchivePortletHome.getInstance( ).create( portlet );
146 
147         modifySendings( request, portlet );
148         // Displays the page with the new Portlet
149         return getPageUrl( nIdPage );
150     }
151 
152     /**
153      * Returns the modification form for the portlet
154      * 
155      * @param request
156      *            the HTML request
157      * @return the HTML code for the page
158      */
159     public String getModify( HttpServletRequest request )
160     {
161         // Use the id in the request to load the portlet
162         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
163         int nPortletId = Integer.parseInt( strPortletId );
164         NewsLetterArchivePortlet./fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterArchivePortlet.html#NewsLetterArchivePortlet">NewsLetterArchivePortlet portlet = (NewsLetterArchivePortlet) PortletHome.findByPrimaryKey( nPortletId );
165 
166         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
167 
168         // Load the modify template and fill in
169         HashMap<String, Object> model = new HashMap<String, Object>( );
170         model.put( BOOKMARK_PORTLET_ID, strPortletId );
171         model.put( BOOKMARK_PAGE_ID, strIdPage );
172 
173         // Get the plugin for the portlet
174         Plugin plugin = PluginService.getPlugin( portlet.getPluginName( ) );
175 
176         ArrayList<Integer> selectedSendings = NewsLetterArchivePortletHome.findSendingsInPortlet( nPortletId, plugin );
177         List<SendingNewsLetter> sendingNewsletterList = SendingNewsLetterHome.findAllSendings( plugin );
178         model.put( MARK_SENDING_NEWSLETTER_LIST, sendingNewsletterList );
179         model.put( MARK_SELECTED_SENDING_LIST, selectedSendings );
180 
181         // Fill the specific part of the modify form
182         HtmlTemplate template = getModifyTemplate( portlet, model );
183 
184         return template.getHtml( );
185     }
186 
187     /**
188      * Processes the modification of the portlet
189      * 
190      * @param request
191      *            the HTTP request
192      * @return the URL to redirect to
193      */
194     public String doModify( HttpServletRequest request )
195     {
196         // Use the id in the request to load the portlet
197         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
198         int nPortletId = Integer.parseInt( strPortletId );
199         NewsLetterArchivePortlet./fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterArchivePortlet.html#NewsLetterArchivePortlet">NewsLetterArchivePortlet portlet = (NewsLetterArchivePortlet) PortletHome.findByPrimaryKey( nPortletId );
200 
201         // Standard controls on the creation form
202         String strStyleId = request.getParameter( Parameters.STYLE );
203 
204         if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
205         {
206             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
207         }
208 
209         setPortletCommonData( request, portlet );
210 
211         // mandatory field
212         String strName = portlet.getName( );
213 
214         if ( strName.trim( ).equals( "" ) )
215         {
216             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
217         }
218 
219         // Update generic values
220         portlet.update( );
221 
222         // Update the selected sendings
223         modifySendings( request, portlet );
224 
225         // displays the page with the potlet updated
226         return getPageUrl( portlet.getPageId( ) );
227     }
228 
229     /**
230      * Returns portlet's properties prefix
231      * 
232      * @return prefix
233      */
234     public String getPropertiesPrefix( )
235     {
236         return PROPERTIES_PREFIX;
237     }
238 
239     /**
240      * Helper method to determine which sendings were checked in the portlet modification form, and update the database accordingly.
241      * 
242      * @param request
243      *            the HTTP request
244      * @param portlet
245      *            the portlet
246      */
247     private static void modifySendings( HttpServletRequest request, NewsLetterArchivePortlet portlet )
248     {
249         // Build the set of the sendings that were checked in the page
250         Set<Integer> checkedSendings = new HashSet<Integer>( );
251 
252         // Read all request parameters
253         @SuppressWarnings( "unchecked" )
254         Enumeration<String> enumParameterNames = request.getParameterNames( );
255 
256         while ( enumParameterNames.hasMoreElements( ) )
257         {
258             String strParameterName = enumParameterNames.nextElement( );
259 
260             // If parameter is a sending checkbox
261             if ( strParameterName.startsWith( PREFIX_CHECKBOX_NAME ) )
262             {
263                 // Extract the int value concatenated to the prefix
264                 String strSendingId = strParameterName.substring( PREFIX_CHECKBOX_NAME.length( ) );
265 
266                 // Add the Integer object to the set
267                 checkedSendings.add( new Integer( strSendingId ) );
268             }
269         }
270 
271         ArrayList<Integer> previousSendings = NewsLetterArchivePortletHome.findSendingsInPortlet( portlet.getId( ),
272                 PluginService.getPlugin( portlet.getPluginName( ) ) );
273 
274         // Add the sendings that are checked now but were not present before
275         for ( Integer newSending : checkedSendings )
276         {
277             if ( !previousSendings.contains( newSending ) )
278             {
279                 NewsLetterArchivePortletHome.insertSending( portlet.getId( ), newSending.intValue( ), PluginService.getPlugin( portlet.getPluginName( ) ) );
280             }
281         }
282 
283         // Remove the sendings that were present before but are unchecked now
284         for ( Integer oldSending : previousSendings )
285         {
286             if ( !checkedSendings.contains( oldSending ) )
287             {
288                 NewsLetterArchivePortletHome.removeSending( portlet.getId( ), oldSending.intValue( ), PluginService.getPlugin( portlet.getPluginName( ) ) );
289             }
290         }
291     }
292 }