1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
70
71 public class MyTasksApp implements XPageApplication
72 {
73
74 private static final int ALL_INT = -1;
75
76
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
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
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
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
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
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
117 private MyTasksService _myTasksService = MyTasksService.getInstance( );
118
119
120
121
122
123
124
125
126
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
161
162
163
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
185
186
187
188
189 private XPage getAddMyTaskPage( HttpServletRequest request )
190 throws UserNotSignedException
191 {
192 XPage page = new XPage( );
193
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
218
219
220
221
222
223 private XPage getUpdateMyTaskPage( HttpServletRequest request )
224 throws SiteMessageException, UserNotSignedException
225 {
226 XPage page = new XPage( );
227
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
265
266
267
268
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
311
312
313
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
376
377
378
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
429
430
431
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
452
453
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
465
466
467
468 public void doDeleteCompletedMyTasks( HttpServletRequest request )
469 throws UserNotSignedException
470 {
471 LuteceUser user = getUser( request );
472 _myTasksService.doDeleteCompletedMyTasks( user );
473 }
474
475
476
477
478
479
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 }