View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.calendar.web.portlet;
35  
36  import fr.paris.lutece.plugins.calendar.business.CalendarHome;
37  import fr.paris.lutece.plugins.calendar.business.portlet.CalendarPortlet;
38  import fr.paris.lutece.plugins.calendar.business.portlet.CalendarPortletHome;
39  import fr.paris.lutece.plugins.calendar.service.AgendaResource;
40  import fr.paris.lutece.plugins.calendar.service.Utils;
41  import fr.paris.lutece.plugins.calendar.web.Constants;
42  import fr.paris.lutece.portal.business.portlet.PortletHome;
43  import fr.paris.lutece.portal.service.i18n.I18nService;
44  import fr.paris.lutece.portal.service.message.AdminMessage;
45  import fr.paris.lutece.portal.service.message.AdminMessageService;
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.util.AppPathService;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
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.ReferenceList;
55  import fr.paris.lutece.util.html.HtmlTemplate;
56  
57  import java.util.ArrayList;
58  import java.util.Enumeration;
59  import java.util.HashMap;
60  import java.util.List;
61  import java.util.Map;
62  import java.util.MissingFormatArgumentException;
63  import java.util.StringTokenizer;
64  
65  import javax.servlet.http.HttpServletRequest;
66  
67  
68  /**
69   * This class provides the user interface to manage calendar interval portlets.
70   */
71  public class CalendarPortletJspBean extends PortletJspBean
72  {
73      // Prefix of the properties related to this checkbox
74      public static final String PROPERTY_TIME_INTERVAL_LIST = "calendar.interval.time";
75  
76      private static final long serialVersionUID = 3201646840294844754L;
77  
78      // Prefix used to generate checkbox names
79      private static final String PREFIX_CHECKBOX_NAME = "cbx_agenda_";
80  
81      // Bookmarks
82      private static final String BOOKMARK_PAGE_ID = "@page_id@";
83      private static final String BOOKMARK_PORTLET_ID = "@portlet_id@";
84  
85      // Parameters
86      private static final String PARAMETER_PAGE_ID = "page_id";
87      private static final String PARAMETER_PORTLET_ID = "portlet_id";
88      private static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
89      private static final String PARAMETER_PERIODICITY = "periodicity";
90      private static final String PARAMETER_TEXT_AGENDAS = "text_agendas";
91      private static final String PARAMETER_DATE_BEGIN = "date_begin";
92      private static final String PARAMETER_DATE_END = "date_end";
93      private static final String PARAMETER_REPEATED_DAYS = "number_days";
94  
95      // Templates
96      private static final String MARK_SELECTED_AGENDA_ID_LIST = "selected_agenda_id_list";
97      private static final String MARK_AUTHORIZED_AGENDA_LIST = "authorized_agenda_list";
98      private static final String MARK_FILE_AGENDAS = "text_agendas";
99      private static final String MARK_INTERVAL_LIST = "interval_list";
100     private static final String MARK_DATE_BEGIN = "date_begin";
101     private static final String MARK_DATE_END = "date_end";
102     private static final String MARK_NUMBER_DAYS = "number_days";
103     private static final String MARK_BASE_URL = "base_url";
104     private static final String MARK_INTERVAL_TIME_SPAN = "time_span";
105 
106     /**
107      * Returns the creation form for the portlet
108      * 
109      * @param request the HTML request
110      * @return the HTML code for the page
111      */
112     public String getCreate( HttpServletRequest request )
113     {
114         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
115         String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
116 
117         HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId );
118 
119         return template.getHtml( );
120     }
121 
122     /**
123      * Processes the creation of the portlet
124      * 
125      * @param request the HTML request
126      * @return the URL to redirect to
127      */
128     public String doCreate( HttpServletRequest request )
129     {
130         CalendarPortlet portlet = new CalendarPortlet( );
131 
132         // Standard controls on the creation form
133         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
134         int nIdPage = Integer.parseInt( strIdPage );
135 
136         String strStyleId = request.getParameter( Parameters.STYLE );
137 
138         if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
139         {
140             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
141         }
142 
143         setPortletCommonData( request, portlet );
144 
145         // mandatory field
146         String strName = portlet.getName( );
147 
148         if ( strName.trim( ).equals( "" ) )
149         {
150             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
151         }
152 
153         portlet.setPageId( nIdPage );
154 
155         // Creating portlet
156         CalendarPortletHome.getInstance( ).create( portlet );
157 
158         // Returns page with new created portlet
159         return getPageUrl( portlet.getPageId( ) );
160     }
161 
162     /**
163      * Returns the modification form for the portlet
164      * 
165      * @param request the HTML request
166      * @return the HTML code for the page
167      */
168     public String getModify( HttpServletRequest request )
169     {
170         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
171         int nPortletId = Integer.parseInt( strPortletId );
172         CalendarPortlet portlet = (CalendarPortlet) PortletHome.findByPrimaryKey( nPortletId );
173 
174         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
175         String strBaseUrl = AppPathService.getBaseUrl( request );
176 
177         Map<String, Object> model = new HashMap<String, Object>( );
178         model.put( BOOKMARK_PORTLET_ID, strPortletId );
179         model.put( BOOKMARK_PAGE_ID, strIdPage );
180 
181         // Get the plugin for the portlet
182         Plugin plugin = PluginService.getPlugin( portlet.getPluginName( ) );
183 
184         String strBeginDate = Utils.getDate( CalendarPortletHome.getBeginDate( nPortletId ) );
185         String strEndDate = Utils.getDate( CalendarPortletHome.getEndDate( nPortletId ) );
186         int nDays = CalendarPortletHome.getRepetitionDays( nPortletId );
187 
188         // Get the list of authorized calendars depending on workgroup
189         List<AgendaResource> listCalendar = CalendarHome.findAgendaResourcesList( plugin );
190 
191         List<AgendaResource> listAuthorizedAgendas = (List<AgendaResource>) AdminWorkgroupService
192                 .getAuthorizedCollection( listCalendar, getUser( ) );
193 
194         //Add other agendas
195         List<AgendaResource> listSelectedAgendas = CalendarPortletHome.findAgendasInPortlet( nPortletId );
196 
197         //List all non database agendas
198         String strFileAgendas = "";
199 
200         for ( AgendaResource agenda : listSelectedAgendas )
201         {
202             if ( agenda != null )
203             {
204                 try
205                 {
206                     Integer.parseInt( agenda.getId( ) );
207                 }
208                 catch ( NumberFormatException e )
209                 {
210                     strFileAgendas += ( agenda.getId( ) + "," );
211                 }
212             }
213         }
214 
215         if ( strFileAgendas.endsWith( "," ) )
216         {
217             strFileAgendas = strFileAgendas.substring( 0, strFileAgendas.length( ) - 1 );
218         }
219 
220         List<String> listSelectedAgendaId = new ArrayList<String>( );
221 
222         for ( AgendaResource agenda : listSelectedAgendas )
223         {
224             if ( agenda != null )
225             {
226                 listSelectedAgendaId.add( agenda.getId( ) );
227             }
228         }
229 
230         String strBooleanTimeSpan = "TRUE";
231 
232         if ( nDays <= 0 )
233         {
234             strBooleanTimeSpan = "FALSE";
235         }
236 
237         model.put( MARK_INTERVAL_TIME_SPAN, strBooleanTimeSpan );
238         model.put( MARK_AUTHORIZED_AGENDA_LIST, listAuthorizedAgendas );
239         model.put( MARK_SELECTED_AGENDA_ID_LIST, listSelectedAgendaId );
240         model.put( MARK_FILE_AGENDAS, strFileAgendas );
241         model.put( MARK_INTERVAL_LIST, getIntervalList( request ) );
242         model.put( MARK_DATE_BEGIN, strBeginDate );
243         model.put( MARK_DATE_END, strEndDate );
244         model.put( MARK_NUMBER_DAYS, nDays );
245         model.put( MARK_BASE_URL, strBaseUrl );
246         model.put( Constants.MARK_LOCALE, getLocale( ).getLanguage( ) );
247 
248         // Fill the specific part of the modify form
249         HtmlTemplate template = getModifyTemplate( portlet, model );
250 
251         return template.getHtml( );
252     }
253 
254     /**
255      * Processes the modification of the portlet
256      * 
257      * @param request the HTTP request
258      * @return the URL to redirect to
259      */
260     public String doModify( HttpServletRequest request )
261     {
262         // Use the id in the request to load the portlet
263         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
264         int nPortletId = Integer.parseInt( strPortletId );
265         CalendarPortlet portlet = (CalendarPortlet) PortletHome.findByPrimaryKey( nPortletId );
266 
267         // Standard controls on the creation form
268         String strStyleId = request.getParameter( Parameters.STYLE );
269 
270         if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
271         {
272             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
273         }
274 
275         setPortletCommonData( request, portlet );
276 
277         // mandatory field
278         String strName = portlet.getName( );
279 
280         if ( strName.trim( ).equals( "" ) )
281         {
282             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
283         }
284 
285         portlet.update( );
286 
287         String strDateBegin = request.getParameter( PARAMETER_DATE_BEGIN );
288         String strDateEnd = request.getParameter( PARAMETER_DATE_END );
289         String strDays = request.getParameter( PARAMETER_REPEATED_DAYS );
290 
291         int nDays = Integer.parseInt( strDays );
292         boolean bIntervalPeriodicity = false;
293 
294         if ( Integer.parseInt( request.getParameter( PARAMETER_PERIODICITY ) ) == 1 )
295         {
296             bIntervalPeriodicity = true;
297 
298             try
299             {
300                 //put the date in form yyyMMdd
301                 strDateBegin = String.format( "%3$2s%2$2s%1$2s", (Object[]) strDateBegin.split( "/" ) );
302                 strDateEnd = String.format( "%3$2s%2$2s%1$2s", (Object[]) strDateEnd.split( "/" ) );
303             }
304             catch ( MissingFormatArgumentException e )
305             {
306                 return AdminMessageService.getMessageUrl( request, Constants.PROPERTY_MESSAGE_DATEFORMAT,
307                         AdminMessage.TYPE_STOP );
308             }
309 
310             if ( !Utils.isValid( strDateBegin ) || !Utils.isValid( strDateEnd ) )
311             {
312                 return AdminMessageService.getMessageUrl( request, Constants.PROPERTY_MESSAGE_DATEFORMAT,
313                         AdminMessage.TYPE_STOP );
314             }
315         }
316 
317         modifyCalendar( request, nPortletId, strDateBegin, strDateEnd, nDays, bIntervalPeriodicity );
318 
319         return getPageUrl( portlet.getPageId( ) );
320     }
321 
322     /**
323      * Return the list of time intervals declared in properties file
324      * @return A ReferenceList of time interval
325      * @param request The HttpRequest
326      */
327     public static ReferenceList getIntervalList( HttpServletRequest request )
328     {
329         StringTokenizer st = new StringTokenizer( AppPropertiesService.getProperty( PROPERTY_TIME_INTERVAL_LIST ), "," );
330         ReferenceList timeIntervalList = new ReferenceList( );
331 
332         while ( st.hasMoreElements( ) )
333         {
334             String strIntervalName = st.nextToken( ).trim( );
335             String strDescription = I18nService.getLocalizedString( "calendar.interval." + strIntervalName
336                     + ".description", request.getLocale( ) );
337             int nDays = AppPropertiesService.getPropertyInt( "calendar.interval." + strIntervalName + ".value", 7 );
338             timeIntervalList.addItem( nDays, strDescription );
339         }
340 
341         return timeIntervalList;
342     }
343 
344     /**
345      * Helper method to determine which database agenda were chosen for the
346      * portlet
347      * in the modification form, and update the database accordingly.
348      * @param nPortletId The id of the portlet
349      * @param strDateBegin The start date
350      * @param strDateEnd The end date
351      * @param nDays The number of days calendar will be repeated
352      * @param bIntervalPeriodicity A boolean to to determine which periodicity
353      *            is used
354      * @param request the HTTP request
355      */
356     private static void modifyCalendar( HttpServletRequest request, int nPortletId, String strDateBegin,
357             String strDateEnd, int nDays, boolean bIntervalPeriodicity )
358     {
359         List<String> listChosenAgendas = new ArrayList<String>( );
360 
361         Enumeration enumParameterNames = request.getParameterNames( );
362 
363         while ( enumParameterNames.hasMoreElements( ) )
364         {
365             String strParameterName = (String) enumParameterNames.nextElement( );
366 
367             if ( strParameterName.startsWith( PREFIX_CHECKBOX_NAME ) )
368             {
369                 String strAgendaId = strParameterName.substring( PREFIX_CHECKBOX_NAME.length( ) );
370                 listChosenAgendas.add( strAgendaId );
371             }
372         }
373 
374         String strTextAgendas = request.getParameter( PARAMETER_TEXT_AGENDAS );
375 
376         if ( ( strTextAgendas != null ) && !strTextAgendas.equals( "" ) )
377         {
378             StringTokenizer st = new StringTokenizer( strTextAgendas, "," );
379 
380             while ( st.hasMoreElements( ) )
381             {
382                 String strAgenda = st.nextToken( ).trim( );
383                 listChosenAgendas.add( strAgenda );
384             }
385         }
386 
387         //Delete all agendas in portlet
388         CalendarPortletHome.removeAllAgendas( nPortletId );
389 
390         // Add the chosen agendas
391         for ( String strAgendaId : listChosenAgendas )
392         {
393             if ( bIntervalPeriodicity )
394             {
395                 CalendarPortletHome.insertAgendaInterval( nPortletId, strAgendaId, strDateBegin, strDateEnd );
396             }
397             else
398             {
399                 CalendarPortletHome.insertCalendar( nPortletId, strAgendaId, nDays );
400             }
401         }
402     }
403 }