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.mytasks.web;
35  
36  import fr.paris.lutece.plugins.mytasks.business.MyTask;
37  import fr.paris.lutece.plugins.mytasks.service.MyTasksService;
38  import fr.paris.lutece.plugins.mytasks.service.parameter.MyTasksParameterService;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.message.SiteMessage;
41  import fr.paris.lutece.portal.service.message.SiteMessageException;
42  import fr.paris.lutece.portal.service.message.SiteMessageService;
43  import fr.paris.lutece.portal.service.page.PageNotFoundException;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.security.LuteceUser;
46  import fr.paris.lutece.portal.service.security.SecurityService;
47  import fr.paris.lutece.portal.service.security.UserNotSignedException;
48  import fr.paris.lutece.portal.service.template.AppTemplateService;
49  import fr.paris.lutece.portal.service.util.AppPathService;
50  import fr.paris.lutece.portal.service.util.AppPropertiesService;
51  import fr.paris.lutece.portal.web.constants.Messages;
52  import fr.paris.lutece.portal.web.xpages.XPage;
53  import fr.paris.lutece.portal.web.xpages.XPageApplication;
54  import fr.paris.lutece.util.ReferenceItem;
55  import fr.paris.lutece.util.date.DateUtil;
56  import fr.paris.lutece.util.html.HtmlTemplate;
57  
58  import org.apache.commons.lang.StringUtils;
59  
60  import java.sql.Date;
61  
62  import java.util.HashMap;
63  import java.util.Map;
64  
65  import javax.servlet.http.HttpServletRequest;
66  
67  
68  /**
69   * This class provides a simple implementation of an XPage
70   */
71  public class MyTasksApp implements XPageApplication
72  {
73      // CONSTANTS
74      private static final int ALL_INT = -1;
75  
76      // TEMPLATES
77      private static final String TEMPLATE_MYTASKS_PAGE = "skin/plugins/mytasks/mytasks.html";
78      private static final String TEMPLATE_ADD_MYTASK_PAGE = "skin/plugins/mytasks/add_mytask.html";
79      private static final String TEMPLATE_EDIT_MYTASK_PAGE = "skin/plugins/mytasks/update_mytask.html";
80  
81      // PARAMETERS
82      private static final String PARAMETER_ACTION = "action";
83      private static final String PARAMETER_ID_MYTASK = "id_mytask";
84      private static final String PARAMETER_MYTASK_NAME = "mytask_name";
85      private static final String PARAMETER_MYTASK_DATE = "mytask_date";
86      private static final String PARAMETER_MYTASK_URL_RETURN = "mytasks_url_return";
87      private static final String PARAMETER_NB_MYTASKS_MAX = "nb_mytasks_max";
88  
89      // MARKS
90      private static final String MARK_MYTASKS_LIST = "mytasks_list";
91      private static final String MARK_MYTASK = "mytask";
92      private static final String MARK_LOCALE = "locale";
93      private static final String MARK_MYTASKS_URL_RETURN = "mytasks_url_return";
94  
95      // PROPERTIES
96      private static final String PROPERTY_PAGE_PATH = "mytasks.mytasks.pagePathLabel";
97      private static final String PROPERTY_PAGE_TITLE = "mytasks.mytasks.pageTitle";
98      private static final String PROPERTY_ADD_MYTASK_PAGE_TITLE = "mytasks.add_mytask.pageTitle";
99      private static final String PROPERTY_UPDATE_MYTASK_PAGE_TITLE = "mytasks.update_mytask.pageTitle";
100     private static final String PROPERTY_MAX_LENGTH = "mytasks.maxLength";
101 
102     // ACTIONS
103     private static final String ACTION_ADD_MYTASK = "add_mytask";
104     private static final String ACTION_UPDATE_MYTASK = "update_mytask";
105     private static final String ACTION_DO_ADD_MYTASK = "do_add_mytask";
106     private static final String ACTION_DO_UPDATE_MYTASK = "do_update_mytask";
107     private static final String ACTION_DO_REMOVE_MYTASK = "do_remove_mytask";
108     private static final String ACTION_DO_CHANGE_MYTASKS_STATUS = "do_change_mytasks_status";
109     private static final String ACTION_DO_DELETE_COMPLETED_MYTASKS = "do_delete_completed_mytasks";
110 
111     // MESSAGES
112     private static final String MESSAGE_ERROR_DATEFORMAT = "mytasks.message.error.dateFormat";
113     private static final String MESSAGE_NB_MYTASKS_MAX = "mytasks.message.nbMyTasksMax";
114     private static final String MESSAGE_MAX_LENGTH = "mytasks.message.maxLength";
115 
116     // private fields
117     private MyTasksService _myTasksService = MyTasksService.getInstance(  );
118 
119     /**
120      * Returns the content of the page myportal.
121      * @param request The http request
122      * @param nMode The current mode
123      * @param plugin The plugin object
124      * @return the {@link XPage}
125      * @throws SiteMessageException Message displayed if an exception occurs
126      * @throws UserNotSignedException exception if user not connected
127      */
128     public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
129         throws SiteMessageException, UserNotSignedException
130     {
131         XPage page = null;
132 
133         String strAction = request.getParameter( PARAMETER_ACTION );
134 
135         if ( StringUtils.isNotBlank( strAction ) )
136         {
137             if ( ACTION_ADD_MYTASK.equals( strAction ) )
138             {
139                 page = getAddMyTaskPage( request );
140             }
141             else if ( ACTION_UPDATE_MYTASK.equals( strAction ) )
142             {
143                 page = getUpdateMyTaskPage( request );
144             }
145             else
146             {
147                 doActionMyTask( request );
148             }
149         }
150 
151         if ( page == null )
152         {
153             page = getMyTasksPage( request );
154         }
155 
156         return page;
157     }
158 
159     /**
160      * Get the MyTasks page
161      * @param request {@link HttpServletRequest}
162      * @return the {@link XPage}
163      * @throws UserNotSignedException exception if user not connected
164      */
165     private XPage getMyTasksPage( HttpServletRequest request )
166         throws UserNotSignedException
167     {
168         XPage page = new XPage(  );
169         LuteceUser user = getUser( request );
170 
171         Map<String, Object> model = new HashMap<String, Object>(  );
172         model.put( MARK_MYTASKS_LIST, _myTasksService.getMyTasksList( user ) );
173 
174         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MYTASKS_PAGE, request.getLocale(  ), model );
175 
176         page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGE_TITLE, request.getLocale(  ) ) );
177         page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGE_PATH, request.getLocale(  ) ) );
178         page.setContent( template.getHtml(  ) );
179 
180         return page;
181     }
182 
183     /**
184      * Get the AddMyTask Page
185      * @param request {@link HttpServletRequest}
186      * @return the {@link XPage}
187      * @throws UserNotSignedException exception if user not connected
188      */
189     private XPage getAddMyTaskPage( HttpServletRequest request )
190         throws UserNotSignedException
191     {
192         XPage page = new XPage(  );
193         // Check if the user is indeed connected
194         getUser( request );
195 
196         String strUrlReturn = request.getParameter( PARAMETER_MYTASK_URL_RETURN );
197 
198         if ( StringUtils.isBlank( strUrlReturn ) )
199         {
200             strUrlReturn = StringUtils.EMPTY;
201         }
202 
203         Map<String, Object> model = new HashMap<String, Object>(  );
204         model.put( MARK_LOCALE, request.getLocale(  ) );
205         model.put( MARK_MYTASKS_URL_RETURN, strUrlReturn );
206 
207         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADD_MYTASK_PAGE, request.getLocale(  ), model );
208 
209         page.setTitle( I18nService.getLocalizedString( PROPERTY_ADD_MYTASK_PAGE_TITLE, request.getLocale(  ) ) );
210         page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGE_PATH, request.getLocale(  ) ) );
211         page.setContent( template.getHtml(  ) );
212 
213         return page;
214     }
215 
216     /**
217      * Get the update MyTask page
218      * @param request {@link HttpServletRequest}
219      * @return the {@link XPage}
220      * @throws UserNotSignedException exception if user not connected
221      * @throws SiteMessageException Message displayed if an exception occurs
222      */
223     private XPage getUpdateMyTaskPage( HttpServletRequest request )
224         throws SiteMessageException, UserNotSignedException
225     {
226         XPage page = new XPage(  );
227         // Check if the user is indeed connected
228         getUser( request );
229 
230         String strIdMyTask = request.getParameter( PARAMETER_ID_MYTASK );
231         String strUrlReturn = request.getParameter( PARAMETER_MYTASK_URL_RETURN );
232 
233         if ( StringUtils.isBlank( strUrlReturn ) )
234         {
235             strUrlReturn = StringUtils.EMPTY;
236         }
237 
238         if ( StringUtils.isNotBlank( strIdMyTask ) )
239         {
240             int nIdMyTask = Integer.parseInt( strIdMyTask );
241             MyTask myTask = _myTasksService.getMyTask( nIdMyTask );
242 
243             Map<String, Object> model = new HashMap<String, Object>(  );
244             model.put( MARK_MYTASK, myTask );
245             model.put( MARK_LOCALE, request.getLocale(  ) );
246             model.put( MARK_MYTASKS_URL_RETURN, strUrlReturn );
247 
248             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_EDIT_MYTASK_PAGE, request.getLocale(  ),
249                     model );
250 
251             page.setTitle( I18nService.getLocalizedString( PROPERTY_UPDATE_MYTASK_PAGE_TITLE, request.getLocale(  ) ) );
252             page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGE_PATH, request.getLocale(  ) ) );
253             page.setContent( template.getHtml(  ) );
254         }
255         else
256         {
257             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_ERROR );
258         }
259 
260         return page;
261     }
262 
263     /**
264      * Do action MyTask
265      * @param request {@link HttpServletRequest
266      * @return the url return from the parameter
267      * @throws UserNotSignedException exception if user not connected
268      * @throws SiteMessageException Message displayed if an exception occurs
269      */
270     public String doActionMyTask( HttpServletRequest request )
271         throws SiteMessageException, UserNotSignedException
272     {
273         String strUrlReturn = request.getParameter( PARAMETER_MYTASK_URL_RETURN );
274 
275         if ( StringUtils.isBlank( strUrlReturn ) )
276         {
277             strUrlReturn = AppPathService.getPortalUrl(  );
278         }
279 
280         String strAction = request.getParameter( PARAMETER_ACTION );
281 
282         if ( StringUtils.isNotBlank( strAction ) )
283         {
284             if ( ACTION_DO_ADD_MYTASK.equals( strAction ) )
285             {
286                 doAddMyTask( request );
287             }
288             else if ( ACTION_DO_UPDATE_MYTASK.equals( strAction ) )
289             {
290                 doUpdateMyTask( request );
291             }
292             else if ( ACTION_DO_REMOVE_MYTASK.equals( strAction ) )
293             {
294                 doRemoveMyTask( request );
295             }
296             else if ( ACTION_DO_CHANGE_MYTASKS_STATUS.equals( strAction ) )
297             {
298                 doChangeMyTaskStatus( request );
299             }
300             else if ( ACTION_DO_DELETE_COMPLETED_MYTASKS.equals( strAction ) )
301             {
302                 doDeleteCompletedMyTasks( request );
303             }
304         }
305 
306         return strUrlReturn;
307     }
308 
309     /**
310      * Do add a my task
311      * @param request {@link HttpServletRequest}
312      * @throws UserNotSignedException exception if user not connected
313      * @throws SiteMessageException Message displayed if an exception occurs
314      */
315     public void doAddMyTask( HttpServletRequest request )
316         throws SiteMessageException, UserNotSignedException
317     {
318         LuteceUser user = getUser( request );
319         String strName = request.getParameter( PARAMETER_MYTASK_NAME );
320         String strDate = request.getParameter( PARAMETER_MYTASK_DATE );
321 
322         if ( StringUtils.isNotBlank( strName ) && StringUtils.isNotBlank( strDate ) )
323         {
324             int nMaxLength = AppPropertiesService.getPropertyInt( PROPERTY_MAX_LENGTH, 50 );
325 
326             if ( strName.length(  ) < nMaxLength )
327             {
328                 ReferenceItem nbTasksMax = MyTasksParameterService.getInstance(  )
329                                                                   .getParamDefaultValue( PARAMETER_NB_MYTASKS_MAX );
330                 int nNbMyTasksMax = ALL_INT;
331 
332                 if ( ( nbTasksMax != null ) && StringUtils.isNotBlank( nbTasksMax.getName(  ) ) &&
333                         StringUtils.isNumeric( nbTasksMax.getName(  ) ) )
334                 {
335                     nNbMyTasksMax = Integer.parseInt( nbTasksMax.getName(  ) );
336                 }
337 
338                 int nNbMyTasks = _myTasksService.getNbMyTasks( user );
339 
340                 if ( ( nNbMyTasksMax == ALL_INT ) || ( nNbMyTasks < nNbMyTasksMax ) )
341                 {
342                     Date date = DateUtil.formatDateSql( strDate, request.getLocale(  ) );
343 
344                     if ( date != null )
345                     {
346                         MyTask myTask = new MyTask(  );
347                         myTask.setName( strName );
348                         myTask.setDate( date );
349                         _myTasksService.doAddMyTask( myTask, user );
350                     }
351                     else
352                     {
353                         SiteMessageService.setMessage( request, MESSAGE_ERROR_DATEFORMAT, SiteMessage.TYPE_ERROR );
354                     }
355                 }
356                 else
357                 {
358                     Object[] params = { nNbMyTasksMax };
359                     SiteMessageService.setMessage( request, MESSAGE_NB_MYTASKS_MAX, params, SiteMessage.TYPE_ERROR );
360                 }
361             }
362             else
363             {
364                 Object[] params = { nMaxLength };
365                 SiteMessageService.setMessage( request, MESSAGE_MAX_LENGTH, params, SiteMessage.TYPE_ERROR );
366             }
367         }
368         else
369         {
370             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_ERROR );
371         }
372     }
373 
374     /**
375      * Do update a MyTask
376      * @param request {@link HttpServletRequest}
377      * @throws UserNotSignedException exception if user not connected
378      * @throws SiteMessageException Message displayed if an exception occurs
379      */
380     public void doUpdateMyTask( HttpServletRequest request )
381         throws SiteMessageException, UserNotSignedException
382     {
383         LuteceUser user = getUser( request );
384         String strIdMyTask = request.getParameter( PARAMETER_ID_MYTASK );
385         String strName = request.getParameter( PARAMETER_MYTASK_NAME );
386         String strDate = request.getParameter( PARAMETER_MYTASK_DATE );
387 
388         if ( StringUtils.isNotBlank( strIdMyTask ) && StringUtils.isNotBlank( strName ) &&
389                 StringUtils.isNotBlank( strDate ) )
390         {
391             int nIdMyTask = Integer.parseInt( strIdMyTask );
392             MyTask myTask = _myTasksService.getMyTask( nIdMyTask );
393 
394             if ( myTask != null )
395             {
396                 int nMaxLength = AppPropertiesService.getPropertyInt( PROPERTY_MAX_LENGTH, 50 );
397 
398                 if ( strName.length(  ) < nMaxLength )
399                 {
400                     Date date = DateUtil.formatDateSql( strDate, request.getLocale(  ) );
401 
402                     if ( date != null )
403                     {
404                         myTask.setIdMyTask( nIdMyTask );
405                         myTask.setName( strName );
406                         myTask.setDate( date );
407                         _myTasksService.doUpdateMyTask( myTask, user );
408                     }
409                     else
410                     {
411                         SiteMessageService.setMessage( request, MESSAGE_ERROR_DATEFORMAT, SiteMessage.TYPE_ERROR );
412                     }
413                 }
414                 else
415                 {
416                     Object[] params = { nMaxLength };
417                     SiteMessageService.setMessage( request, MESSAGE_MAX_LENGTH, params, SiteMessage.TYPE_ERROR );
418                 }
419             }
420         }
421         else
422         {
423             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_ERROR );
424         }
425     }
426 
427     /**
428      * Do remove a MyTask
429      * @param request {@link HttpServletRequest}
430      * @throws UserNotSignedException exception if user not connected
431      * @throws SiteMessageException Message displayed if an exception occurs
432      */
433     public void doRemoveMyTask( HttpServletRequest request )
434         throws SiteMessageException, UserNotSignedException
435     {
436         LuteceUser user = getUser( request );
437         String strIdMyTask = request.getParameter( PARAMETER_ID_MYTASK );
438 
439         if ( StringUtils.isNotBlank( strIdMyTask ) )
440         {
441             int nIdMyTask = Integer.parseInt( strIdMyTask );
442             _myTasksService.doRemoveMyTask( nIdMyTask, user );
443         }
444         else
445         {
446             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_ERROR );
447         }
448     }
449 
450     /**
451      * Do change the mytask ids
452      * @param request {@link HttpServletRequest}
453      * @throws UserNotSignedException exception if user not connected
454      */
455     public void doChangeMyTaskStatus( HttpServletRequest request )
456         throws UserNotSignedException
457     {
458         LuteceUser user = getUser( request );
459         String[] strIdsMyTask = request.getParameterValues( PARAMETER_ID_MYTASK );
460         _myTasksService.doUpdateMyTasksStatus( strIdsMyTask, user );
461     }
462 
463     /**
464      * Do delete the completed tasks
465      * @param request {@link HttpServletRequest}
466      * @throws UserNotSignedException exception if user not connected
467      */
468     public void doDeleteCompletedMyTasks( HttpServletRequest request )
469         throws UserNotSignedException
470     {
471         LuteceUser user = getUser( request );
472         _myTasksService.doDeleteCompletedMyTasks( user );
473     }
474 
475     /**
476      * Gets the user from the request
477      * @param request The HTTP user
478      * @return The Lutece User
479      * @throws UserNotSignedException exception if user not connected
480      */
481     public LuteceUser getUser( HttpServletRequest request )
482         throws UserNotSignedException
483     {
484         if ( SecurityService.isAuthenticationEnable(  ) )
485         {
486             LuteceUser user = SecurityService.getInstance(  ).getRemoteUser( request );
487 
488             if ( user == null )
489             {
490                 throw new UserNotSignedException(  );
491             }
492 
493             return user;
494         }
495         else
496         {
497             throw new PageNotFoundException(  );
498         }
499     }
500 }