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.workflow.modules.tipiforms.web.task;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import javax.servlet.http.HttpServletRequest;
40
41 import org.apache.commons.lang3.StringUtils;
42 import org.apache.commons.lang3.math.NumberUtils;
43
44 import fr.paris.lutece.plugins.forms.business.FormHome;
45 import fr.paris.lutece.plugins.forms.business.Question;
46 import fr.paris.lutece.plugins.forms.business.QuestionHome;
47 import fr.paris.lutece.plugins.forms.business.Step;
48 import fr.paris.lutece.plugins.forms.business.StepHome;
49 import fr.paris.lutece.plugins.workflow.modules.tipiforms.business.task.TaskTipiFormsProviderConfig;
50 import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
51 import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
52 import fr.paris.lutece.portal.service.message.AdminMessage;
53 import fr.paris.lutece.portal.service.message.AdminMessageService;
54 import fr.paris.lutece.portal.service.spring.SpringContextService;
55 import fr.paris.lutece.portal.service.template.AppTemplateService;
56 import fr.paris.lutece.util.ReferenceItem;
57 import fr.paris.lutece.util.ReferenceList;
58 import fr.paris.lutece.util.html.HtmlTemplate;
59
60
61
62
63
64
65 public class TipiFormsProviderTaskConfigController
66 {
67
68 private static final String PARAMETER_APPLY = "apply";
69
70
71 private static final String ACTION_CHANGE_FORM = "changeForm";
72 private static final String ACTION_DEFAULT = "defaultAction";
73
74
75 private static final String SERVICE_CONFIG = "workflow-tipiforms.taskTipiFormsProviderConfigService";
76
77
78 private static final ITaskConfigService _taskConfigService = SpringContextService.getBean( SERVICE_CONFIG );
79
80
81 private static final int ID_UNSET = -1;
82
83 private final ITask _task;
84 private TaskTipiFormsProviderConfig _config;
85
86
87
88
89
90
91
92 public TipiFormsProviderTaskConfigController( ITask task )
93 {
94 _task = task;
95
96 findConfig( );
97 }
98
99
100
101
102 private void findConfig( )
103 {
104 _config = _taskConfigService.findByPrimaryKey( _task.getId( ) );
105
106 if ( _config == null )
107 {
108
109 _config = new TaskTipiFormsProviderConfig( );
110 }
111 }
112
113
114
115
116
117
118
119
120 public HtmlTemplate buildView( HttpServletRequest request )
121 {
122 return new View( request ).build( );
123 }
124
125
126
127
128
129
130
131
132 public String performAction( HttpServletRequest request )
133 {
134 String strErrorUrl = null;
135 Action action = new Action( request );
136 String strAction = findAction( action, request );
137
138 if ( ACTION_CHANGE_FORM.equals( strAction ) )
139 {
140 strErrorUrl = action.changeForm( );
141 }
142
143 if ( ACTION_DEFAULT.equals( strAction ) )
144 {
145 strErrorUrl = action.save( );
146 }
147
148 return strErrorUrl;
149 }
150
151
152
153
154
155
156
157
158
159
160 private String findAction( Action action, HttpServletRequest request )
161 {
162 String strAction = request.getParameter( PARAMETER_APPLY );
163
164 if ( ACTION_CHANGE_FORM.equals( strAction ) || action.isFormChanged( ) )
165 {
166 strAction = ACTION_CHANGE_FORM;
167 }
168
169 if ( StringUtils.isBlank( strAction ) )
170 {
171 strAction = ACTION_DEFAULT;
172 }
173
174 return strAction;
175 }
176
177
178
179
180
181 private final class View
182 {
183
184 private static final String MARK_CONFIG = "config";
185 private static final String MARK_LIST_FORM = "list_form";
186 private static final String MARK_LIST_QUESTION = "list_question";
187
188
189 private static final String TEMPLATE_TASK_CONFIG = "admin/plugins/workflow/modules/tipiforms/tipiformsprovider_task_config.html";
190
191
192 private static final String NAME_SEPARATOR = " - ";
193
194 private final HttpServletRequest _request;
195 private final Map<String, Object> _model;
196 private final ReferenceItem _referenceItemEmpty;
197
198
199
200
201
202
203
204 View( HttpServletRequest request )
205 {
206 _request = request;
207 _model = new HashMap<String, Object>( );
208 _referenceItemEmpty = new ReferenceItem( );
209 _referenceItemEmpty.setCode( Integer.toString( ID_UNSET ) );
210 _referenceItemEmpty.setName( StringUtils.EMPTY );
211 }
212
213
214
215
216
217
218 private HtmlTemplate build( )
219 {
220 _model.put( MARK_CONFIG, _config );
221 _model.put( MARK_LIST_FORM, findForms( ) );
222 _model.put( MARK_LIST_QUESTION, findQuestions( ) );
223
224 return AppTemplateService.getTemplate( TEMPLATE_TASK_CONFIG, _request.getLocale( ), _model );
225 }
226
227
228
229
230
231
232 private ReferenceList findForms( )
233 {
234 ReferenceList referenceList = createReferenceListWithEmptyElement( );
235 referenceList.addAll( FormHome.getFormsReferenceList( ) );
236
237 return referenceList;
238 }
239
240
241
242
243
244
245 private ReferenceList createReferenceListWithEmptyElement( )
246 {
247 ReferenceList referenceList = new ReferenceList( );
248 referenceList.add( _referenceItemEmpty );
249
250 return referenceList;
251 }
252
253
254
255
256
257
258 private ReferenceList findQuestions( )
259 {
260 ReferenceList referenceList = createReferenceListWithEmptyElement( );
261
262 for ( Step step : StepHome.getStepsListByForm( _config.getIdForm( ) ) )
263 {
264 for ( Question question : QuestionHome.getQuestionsListByStep( step.getId( ) ) )
265 {
266 referenceList.addItem( question.getId( ), buildNameForQuestion( step, question ) );
267 }
268 }
269
270 return referenceList;
271 }
272
273
274
275
276
277
278
279
280
281
282 private String buildNameForQuestion( Step step, Question question )
283 {
284 StringBuilder sbName = new StringBuilder( step.getTitle( ) );
285 sbName.append( NAME_SEPARATOR ).append( question.getTitle( ) );
286
287 return sbName.toString( );
288 }
289 }
290
291
292
293
294
295 private final class Action
296 {
297
298 private static final String MESSAGE_MANDATORY_FORM = "module.workflow.tipiforms.task_tipiformsprovider_config.message.mandatory.form";
299 private static final String MESSAGE_MANDATORY_QUESTION_REFDET = "module.workflow.tipiforms.task_tipiformsprovider_config.message.mandatory.question.refdet";
300 private static final String MESSAGE_MANDATORY_QUESTION_AMOUNT = "module.workflow.tipiforms.task_tipiformsprovider_config.message.mandatory.question.amount";
301 private static final String MESSAGE_MANDATORY_QUESTION_EMAIL = "module.workflow.tipiforms.task_tipiformsprovider_config.message.mandatory.question.email";
302
303
304 private static final String PARAMETER_FORM_ID = "idForm";
305 private static final String PARAMETER_QUESTION_REFDET_ID = "idRefDetQuestion";
306 private static final String PARAMETER_QUESTION_AMOUNT_ID = "idAmountQuestion";
307 private static final String PARAMETER_QUESTION_EMAIL_ID = "idEmailQuestion";
308
309 private final HttpServletRequest _request;
310 private final TaskTipiFormsProviderConfig _configFromRequest;
311
312
313
314
315
316
317
318 Action( HttpServletRequest request )
319 {
320 _request = request;
321 _configFromRequest = createConfigFrom( _request );
322
323 }
324
325
326
327
328
329
330
331
332 private TaskTipiFormsProviderConfig createConfigFrom( HttpServletRequest request )
333 {
334 TaskTipiFormsProviderConfig config = new TaskTipiFormsProviderConfig( );
335 config.setIdTask( _task.getId( ) );
336 config.setIdForm( NumberUtils.toInt( _request.getParameter( PARAMETER_FORM_ID ), ID_UNSET ) );
337 config.setIdRefDetQuestion( NumberUtils.toInt( request.getParameter( PARAMETER_QUESTION_REFDET_ID ), ID_UNSET ) );
338 config.setIdAmountQuestion( NumberUtils.toInt( request.getParameter( PARAMETER_QUESTION_AMOUNT_ID ), ID_UNSET ) );
339 config.setIdEmailQuestion( NumberUtils.toInt( request.getParameter( PARAMETER_QUESTION_EMAIL_ID ), ID_UNSET ) );
340
341 return config;
342 }
343
344
345
346
347
348
349 private boolean isFormChanged( )
350 {
351 return _configFromRequest.getIdForm( ) != _config.getIdForm( );
352 }
353
354
355
356
357
358
359 private String changeForm( )
360 {
361 String strErrorUrl = validateFormIsSet( );
362
363 if ( !StringUtils.isEmpty( strErrorUrl ) )
364 {
365 return strErrorUrl;
366 }
367
368 fillConfigForNewForm( );
369 saveConfig( );
370
371 return null;
372 }
373
374
375
376
377
378
379 private String save( )
380 {
381 String strResult = validateAllConfig( );
382
383 if ( !StringUtils.isEmpty( strResult ) )
384 {
385 return strResult;
386 }
387
388 fillAllConfig( );
389 saveConfig( );
390
391 return null;
392
393 }
394
395
396
397
398
399
400 private String validateFormIsSet( )
401 {
402 String strErrorUrl = null;
403
404 if ( _configFromRequest.getIdForm( ) == ID_UNSET )
405 {
406 strErrorUrl = buildUrlForMissingForm( );
407 }
408
409 return strErrorUrl;
410 }
411
412
413
414
415
416
417 private String validateAllConfig( )
418 {
419 String strErrorUrl = validateFormIsSet( );
420
421 if ( !StringUtils.isEmpty( strErrorUrl ) )
422 {
423 return strErrorUrl;
424 }
425
426 strErrorUrl = validateRefDetQuestionIsSet( );
427
428 if ( !StringUtils.isEmpty( strErrorUrl ) )
429 {
430 return strErrorUrl;
431 }
432
433 strErrorUrl = validateAmountQuestionIsSet( );
434
435 if ( !StringUtils.isEmpty( strErrorUrl ) )
436 {
437 return strErrorUrl;
438 }
439
440 strErrorUrl = validateEmailQuestionIsSet( );
441
442 if ( !StringUtils.isEmpty( strErrorUrl ) )
443 {
444 return strErrorUrl;
445 }
446
447 return null;
448 }
449
450
451
452
453
454
455 private String validateRefDetQuestionIsSet( )
456 {
457 String strErrorUrl = null;
458
459 if ( _configFromRequest.getIdRefDetQuestion( ) == ID_UNSET )
460 {
461 strErrorUrl = buildUrlForMissingRefDetQuestion( );
462 }
463
464 return strErrorUrl;
465 }
466
467
468
469
470
471
472 private String validateAmountQuestionIsSet( )
473 {
474 String strErrorUrl = null;
475
476 if ( _configFromRequest.getIdAmountQuestion( ) == ID_UNSET )
477 {
478 strErrorUrl = buildUrlForMissingAmountQuestion( );
479 }
480
481 return strErrorUrl;
482 }
483
484
485
486
487
488
489 private String validateEmailQuestionIsSet( )
490 {
491 String strErrorUrl = null;
492
493 if ( _configFromRequest.getIdEmailQuestion( ) == ID_UNSET )
494 {
495 strErrorUrl = buildUrlForMissingEmailQuestion( );
496 }
497
498 return strErrorUrl;
499 }
500
501
502
503
504
505
506 private String buildUrlForMissingForm( )
507 {
508 return AdminMessageService.getMessageUrl( _request, MESSAGE_MANDATORY_FORM, AdminMessage.TYPE_STOP );
509 }
510
511
512
513
514
515
516 private String buildUrlForMissingRefDetQuestion( )
517 {
518 return AdminMessageService.getMessageUrl( _request, MESSAGE_MANDATORY_QUESTION_REFDET, AdminMessage.TYPE_STOP );
519 }
520
521
522
523
524
525
526 private String buildUrlForMissingAmountQuestion( )
527 {
528 return AdminMessageService.getMessageUrl( _request, MESSAGE_MANDATORY_QUESTION_AMOUNT, AdminMessage.TYPE_STOP );
529 }
530
531
532
533
534
535
536 private String buildUrlForMissingEmailQuestion( )
537 {
538 return AdminMessageService.getMessageUrl( _request, MESSAGE_MANDATORY_QUESTION_EMAIL, AdminMessage.TYPE_STOP );
539 }
540
541
542
543
544 private void fillConfigForNewForm( )
545 {
546 if ( isFormChanged( ) )
547 {
548 fillConfigWithForm( );
549 fillConfigWithUnsetQuestions( );
550 }
551 }
552
553
554
555
556 private void fillConfigWithForm( )
557 {
558 _config.setIdForm( _configFromRequest.getIdForm( ) );
559 }
560
561
562
563
564 private void fillConfigWithUnsetQuestions( )
565 {
566 _config.setIdRefDetQuestion( ID_UNSET );
567 _config.setIdAmountQuestion( ID_UNSET );
568 _config.setIdEmailQuestion( ID_UNSET );
569 }
570
571
572
573
574 private void fillAllConfig( )
575 {
576 _config = _configFromRequest;
577 }
578
579
580
581
582 private void saveConfig( )
583 {
584 boolean bCreate = false;
585
586 if ( _config.getIdTask( ) == 0 )
587 {
588 _config.setIdTask( _task.getId( ) );
589 bCreate = true;
590 }
591
592 if ( bCreate )
593 {
594 _taskConfigService.create( _config );
595 }
596 else
597 {
598 _taskConfigService.update( _config );
599 }
600 }
601 }
602 }