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.formengine.web;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.StringWriter;
39 import java.text.MessageFormat;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Enumeration;
43 import java.util.HashMap;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.Locale;
47 import java.util.Map;
48 import java.util.Properties;
49
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpSession;
52 import javax.xml.bind.JAXBException;
53 import javax.xml.bind.Marshaller;
54 import javax.xml.transform.stream.StreamSource;
55
56 import org.apache.commons.fileupload.FileItem;
57 import org.apache.commons.lang.StringUtils;
58
59 import fr.paris.lutece.plugins.blobstore.service.BlobStoreFileItem;
60 import fr.paris.lutece.plugins.blobstore.service.IBlobStoreService;
61 import fr.paris.lutece.plugins.blobstore.service.NoSuchBlobException;
62 import fr.paris.lutece.plugins.formengine.business.Notice;
63 import fr.paris.lutece.plugins.formengine.business.NoticeFilter;
64 import fr.paris.lutece.plugins.formengine.business.NoticeGroup;
65 import fr.paris.lutece.plugins.formengine.business.NoticeGroupFilter;
66 import fr.paris.lutece.plugins.formengine.business.NoticeGroupHome;
67 import fr.paris.lutece.plugins.formengine.business.NoticeHome;
68 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.CheckFieldRules;
69 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.CheckRule;
70 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.CheckSubFormRules;
71 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.Choice;
72 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.ChoiceList;
73 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.Field;
74 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.Fields;
75 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.FileName;
76 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.FormElements;
77 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.Notices;
78 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.ObjectFactory;
79 import fr.paris.lutece.plugins.formengine.service.FormsRegistrationService;
80 import fr.paris.lutece.plugins.formengine.service.XslOutputPropertiesLoader;
81 import fr.paris.lutece.plugins.formengine.service.validator.FieldValidator;
82 import fr.paris.lutece.plugins.formengine.service.validator.SubFormValidator;
83 import fr.paris.lutece.plugins.formengine.service.validator.ValidatorFactory;
84 import fr.paris.lutece.portal.service.content.PageData;
85 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
86 import fr.paris.lutece.portal.service.html.XmlTransformerService;
87 import fr.paris.lutece.portal.service.includes.PageInclude;
88 import fr.paris.lutece.portal.service.includes.PageIncludeService;
89 import fr.paris.lutece.portal.service.plugin.Plugin;
90 import fr.paris.lutece.portal.service.plugin.PluginService;
91 import fr.paris.lutece.portal.service.security.UserNotSignedException;
92 import fr.paris.lutece.portal.service.template.AppTemplateService;
93 import fr.paris.lutece.portal.service.util.AppException;
94 import fr.paris.lutece.portal.service.util.AppHTTPSService;
95 import fr.paris.lutece.portal.service.util.AppLogService;
96 import fr.paris.lutece.portal.service.util.AppPathService;
97 import fr.paris.lutece.portal.service.util.AppPropertiesService;
98 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
99 import fr.paris.lutece.util.ReferenceItem;
100 import fr.paris.lutece.util.ReferenceList;
101 import fr.paris.lutece.util.filesystem.UploadUtil;
102 import fr.paris.lutece.util.html.HtmlTemplate;
103
104
105
106
107
108
109 public abstract class SubForm
110 {
111 public static final String PARAMETER_XSL_BASE_URL = "baseUrl";
112 private static final String SESSION_ATTRIBUTE_SUBFORM_ALLOWED = "SUBFORM_ALLOWED";
113 private static final String SESSION_ATTRIBUTE_FORM_ELEMENTS = "FORM_ELEMENTS";
114 private static final String TEMPLATE_MAIN = "/skin/plugins/formengine/subform_main_template.html";
115 private static final String TEMPLATE_HEADER = "/skin/plugins/formengine/subform_template_header.html";
116 private static final String TEMPLATE_ERROR = "/skin/plugins/formengine/error.html";
117 private static final String TEMPLATE_ACCESS_FORBIDDEN = "/skin/plugins/formengine/access_forbidden.html";
118 private static final String MARK_FORM_NAME = "form_name";
119 private static final String MARK_WARNING_MESSAGE_WS = "warning_message_ws";
120 private static final String BOOKMARK_ERROR_MESSAGE = "@error_message@";
121 private static final String BOOKMARK_ERROR_LIST = "@error_list@";
122 private static final String BOOKMARK_HEADER = "@subform_header@";
123 private static final String BOOKMARK_SUMMARY = "@selection_summary@";
124 private static final String BOOKMARK_SUBFORM_CONTENT = "@subform_content@";
125 private static final String BOOKMARK_SUBFORM_TITLE = "@subform_title@";
126 private static final String BOOKMARK_FORM_TITLE = "@form_title@";
127 private static final String BOOKMARK_PORTAL_URL = "@portal_url@";
128 private static final String PROPERTY_FRAGMENT_URL_FOR_LAST = ".url.exit";
129 private static final String PROPERTY_MESSAGE_EXISTING_FILE = "formengine.validator.message.existingFile";
130 private static final String PARAMETER_XSL_FORM_NAME = "formName";
131 private static final String PARAMETER_XSL_SUBFORM_NAME = "subFormName";
132
133
134
135
136
137 private static final String PARAMETER_XSL_PORTAL_URI = "portalURI";
138 private static final String PARAMETER_XSL_URL_EXIT = "defaultUrlToExit";
139 private static final String PARAMETER_XSL_UPLOAD_SUBMIT_PREFIX = "uploadSubmitPrefix";
140 private static final String PARAMETER_XSL_UPLOAD_DELETE_PREFIX = "uploadDeletePrefix";
141 private static final String PARAMETER_XSL_UPLOAD_CHECKBOX_PREFIX = "uploadCheckboxPrefix";
142 private static final String PLUGIN_NAME = "formengine";
143 private String _strTitle;
144 private String _strName;
145 private String _strPortalURI;
146 private Form _formParent;
147 private SubForm _subFormNext;
148 private SubForm _subFormPrevious;
149 private FormElements _initialFormElements;
150
151
152
153
154 public SubForm( )
155 {
156 }
157
158
159
160
161
162
163
164
165
166 protected abstract String getXslFormElementsFileName( );
167
168
169
170
171
172
173
174
175
176 public abstract String doAction( String strActionName, HttpServletRequest request );
177
178
179
180
181
182
183 protected abstract String displaySummary( HttpServletRequest request );
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 protected String displayForm( HttpServletRequest request )
199 {
200 FormElements formElements = this.getFormElements( request );
201
202
203 String strForm = "";
204
205
206 String strBaseUrl;
207 Map formParams = new HashMap( );
208
209 if ( AppHTTPSService.isHTTPSSupportEnabled( ) )
210 {
211 strBaseUrl = AppHTTPSService.getHTTPSUrl( request );
212
213 HttpSession session = request.getSession( );
214
215 if ( session != null )
216 {
217 session.setAttribute( AppPathService.SESSION_BASE_URL, AppPathService.getBaseUrl( request ) );
218 }
219 }
220 else
221 {
222 strBaseUrl = AppPathService.getBaseUrl( request );
223 }
224
225 formParams.put( PARAMETER_XSL_BASE_URL, strBaseUrl );
226
227
228 for ( Field field : getUploadFields( request ) )
229 {
230 mergeAsynchronousUploadedFiles( request, field.getName( ) );
231 }
232
233 strForm = this.buildHtmlForm( formElements, formParams );
234
235 return strForm;
236 }
237
238
239
240
241
242
243 protected String displayHeader( HttpServletRequest request )
244 {
245 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_HEADER );
246 template.substitute( BOOKMARK_FORM_TITLE, this.getParentForm( ).getTitle( ) );
247 template.substitute( BOOKMARK_SUBFORM_TITLE, this.getTitle( ) );
248
249
250 String strPortalUrl = request.getRequestURI( );
251
252 if ( AppHTTPSService.isHTTPSSupportEnabled( ) )
253 {
254 strPortalUrl = AppPathService.getBaseUrl( request ) + strPortalUrl;
255 }
256
257 template.substitute( BOOKMARK_PORTAL_URL, strPortalUrl );
258
259 return template.getHtml( );
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274 public String display( HttpServletRequest request ) throws UserNotSignedException
275 {
276 if ( !this.getParentForm( ).checkIsAccessToSubFormAllowed( request, this.getName( ) ) )
277 {
278 HtmlTemplate templateForbidden = AppTemplateService.getTemplate( TEMPLATE_ACCESS_FORBIDDEN );
279
280 return templateForbidden.getHtml( );
281 }
282
283 HashMap<String, Object> model = new HashMap<String, Object>( );
284
285 List<PageInclude> listIncludes = PageIncludeService.getIncludes( );
286 PageData data = new PageData( );
287
288 for ( PageInclude pic : listIncludes )
289 {
290 pic.fillTemplate( model, data, 0, request );
291
292
293
294 }
295
296 model.put( MARK_FORM_NAME, this.getName( ) );
297
298 if ( StringUtils.isNotBlank( (String) request.getSession( ).getAttribute(
299 SharedConstants.SESSION_ATTRIBUTE_WARNING_ERROR_WS ) ) )
300 {
301 model.put( MARK_WARNING_MESSAGE_WS,
302 request.getSession( ).getAttribute( SharedConstants.SESSION_ATTRIBUTE_WARNING_ERROR_WS ) );
303 }
304
305 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIN, Locale.getDefault( ), model );
306
307
308 template.substitute( BOOKMARK_HEADER, this.displayHeader( request ) );
309
310
311 String strPortalUrl = request.getRequestURI( );
312 template.substitute( BOOKMARK_PORTAL_URL, strPortalUrl );
313
314
315 this.setPortalURI( request.getRequestURI( ) );
316
317
318 template.substitute( BOOKMARK_SUMMARY, this.displaySummary( request ) );
319
320
321 template.substitute( BOOKMARK_ERROR_LIST, this.getFormattedErrors( request ) );
322
323
324 template.substitute( BOOKMARK_SUBFORM_CONTENT, this.displayForm( request ) );
325
326 request.getSession( ).removeAttribute( SharedConstants.SESSION_ATTRIBUTE_WARNING_ERROR_WS );
327
328 return template.getHtml( );
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344 public String displayForMail( HttpServletRequest request, String strMailTemplate ) throws UserNotSignedException
345 {
346 if ( !this.getParentForm( ).checkIsAccessToSubFormAllowed( request, this.getName( ) ) )
347 {
348 HtmlTemplate templateForbidden = AppTemplateService.getTemplate( TEMPLATE_ACCESS_FORBIDDEN );
349
350 return templateForbidden.getHtml( );
351 }
352
353 HtmlTemplate template = AppTemplateService.getTemplate( strMailTemplate );
354
355
356
357
358
359
360
361
362
363 this.setPortalURI( request.getRequestURI( ) );
364
365
366 template.substitute( BOOKMARK_SUMMARY, this.displaySummaryForMail( request ) );
367
368
369
370
371
372
373 return template.getHtml( );
374 }
375
376
377
378
379
380
381 protected String displaySummaryForMail( HttpServletRequest request )
382 {
383 return null;
384 }
385
386
387
388
389
390 public String getTitle( )
391 {
392 return _strTitle;
393 }
394
395
396
397
398
399 public void setTitle( String strTitle )
400 {
401 _strTitle = strTitle;
402 }
403
404
405
406
407
408 public String getName( )
409 {
410 return _strName;
411 }
412
413
414
415
416
417 public void setName( String strName )
418 {
419 _strName = strName;
420 }
421
422
423
424
425
426 public String getPortalURI( )
427 {
428 return _strPortalURI;
429 }
430
431
432
433
434
435 public void setPortalURI( String strPortalURI )
436 {
437 _strPortalURI = strPortalURI;
438 }
439
440
441
442
443
444 public void setParentForm( Form formParent )
445 {
446 _formParent = formParent;
447 }
448
449
450
451
452
453 public Form getParentForm( )
454 {
455 return _formParent;
456 }
457
458
459
460
461
462 public void setNextSubForm( SubForm subFormNext )
463 {
464 _subFormNext = subFormNext;
465 }
466
467
468
469
470
471 public SubForm getNextSubForm( )
472 {
473 return _subFormNext;
474 }
475
476
477
478
479
480 public void setPreviousSubForm( SubForm subFormPrevious )
481 {
482 _subFormPrevious = subFormPrevious;
483 }
484
485
486
487
488
489 public SubForm getPreviousSubForm( )
490 {
491 return _subFormPrevious;
492 }
493
494
495
496
497
498
499
500
501
502
503
504
505 protected String getHtml( String strXmlCode, String strXslFileName, Map params )
506 {
507 String strHtml;
508 StreamSource sourceStyleSheet = null;
509
510 String strXslDirectory = this.getParentForm( ).getXslDirectoryPath( );
511 String strStyleSheetId;
512
513 String strXslPath = strXslDirectory + File.separator + strXslFileName;
514
515 File fileXsl = new File( strXslPath );
516 sourceStyleSheet = new StreamSource( fileXsl );
517 strStyleSheetId = strXslPath;
518
519 Properties outputProperties = XslOutputPropertiesLoader.getOutputProperties( );
520
521 try
522 {
523 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
524 strHtml = xmlTransformerService.transformBySourceWithXslCache( strXmlCode, sourceStyleSheet,
525 strStyleSheetId, params, outputProperties );
526 }
527 catch ( Exception e )
528 {
529 throw new AppException( "Formengine : an error occurred during XSL transformation - form : "
530 + this.getParentForm( ).getName( ) + " - subform " + this.getName( ), e );
531 }
532
533 return strHtml;
534 }
535
536
537
538
539
540
541
542
543
544 protected String getHtml( String strXmlCode, String strXslFileName )
545 {
546 return getHtml( strXmlCode, strXslFileName, null );
547 }
548
549
550
551
552
553
554 protected String getFormattedErrors( HttpServletRequest request )
555 {
556 String[] errors = getParentForm( ).getErrors( request );
557 StringBuilder strErrors = new StringBuilder( );
558
559 if ( ( errors != null ) && ( errors.length > 0 ) )
560 {
561 for ( int i = 0; i < errors.length; i++ )
562 {
563 HtmlTemplate tError = AppTemplateService.getTemplate( TEMPLATE_ERROR );
564
565
566
567
568
569 tError.substitute( BOOKMARK_ERROR_MESSAGE, errors[i] );
570 strErrors.append( tError.getHtml( ) );
571 }
572 }
573 else
574 {
575 strErrors.append( "" );
576 }
577
578 return strErrors.toString( );
579 }
580
581
582
583
584
585
586
587
588
589 protected boolean validate( Object objectToValidate )
590 {
591 boolean bValid = true;
592
593 try
594 {
595 bValid = this.getParentForm( ).getXmlValidator( ).validate( objectToValidate );
596 }
597 catch ( JAXBException e )
598 {
599 AppLogService.error( e.getMessage( ), e );
600 }
601
602 return bValid;
603 }
604
605
606
607
608
609
610
611 public void setIsSubFormAllowed( HttpServletRequest request, boolean bIsAllowed )
612 {
613
614 this.setSessionAttribute( request, SESSION_ATTRIBUTE_SUBFORM_ALLOWED, Boolean.valueOf( bIsAllowed ) );
615 }
616
617
618
619
620
621
622
623 public boolean getIsSubFormAllowed( HttpServletRequest request )
624 {
625 Boolean bIsAllowed = (Boolean) this.getSessionAttribute( request, SESSION_ATTRIBUTE_SUBFORM_ALLOWED );
626
627 if ( bIsAllowed == null )
628 {
629 return false;
630 }
631 else
632 {
633 return bIsAllowed.booleanValue( );
634 }
635 }
636
637
638
639
640
641
642
643
644 public void fillFields( HttpServletRequest request )
645 {
646 FormElements formElements = getFormElements( request );
647 Fields fields = formElements.getFields( );
648 Collection<Field> colFields = fields.getField( );
649
650 for ( Field field : colFields )
651 {
652 String strName = field.getName( );
653
654 if ( request.getParameter( strName ) != null )
655 {
656 field.setValue( request.getParameter( strName ) );
657 }
658 else
659 {
660 field.setValue( "" );
661 }
662
663 if ( SharedConstants.FIELD_TYPE_UPLOAD.equals( field.getType( ) ) )
664 {
665
666 mergeAsynchronousUploadedFiles( request, field.getName( ) );
667 }
668 }
669 }
670
671
672
673
674
675
676 public void mergeAsynchronousUploadedFiles( HttpServletRequest request, String strFieldName )
677 {
678 mergeAsynchronousUploadedFiles( request, strFieldName, null );
679 }
680
681
682
683
684
685
686
687 public void mergeAsynchronousUploadedFiles( HttpServletRequest request, String strFieldName,
688 FormErrorsList formErrorsList )
689 {
690 Field field = getFieldFromName( request, strFieldName );
691
692
693 List<FileItem> listAsynchronousFileItem = getFileItems( request.getSession( ).getId( ), field.getName( ) );
694
695 if ( listAsynchronousFileItem != null )
696 {
697 Iterator<FileItem> itFileItems = listAsynchronousFileItem.iterator( );
698
699 while ( itFileItems.hasNext( ) )
700 {
701 FileItem fileItem = itFileItems.next( );
702
703 addFileItemToUploadedFiles( request, field, fileItem, formErrorsList );
704
705
706 itFileItems.remove( );
707 }
708 }
709 }
710
711
712
713
714
715
716
717
718
719
720
721
722 public boolean validateFields( HttpServletRequest request )
723 {
724 boolean bValidate = true;
725
726 FormElements formElements = this.getFormElements( request );
727 Fields fields = formElements.getFields( );
728
729
730 Collection<Field> colFields = fields.getField( );
731
732
733 FormErrorsList listErrors = new FormErrorsList( );
734
735 for ( Field field : colFields )
736 {
737 CheckFieldRules checkFieldRules = field.getCheckFieldRules( );
738
739 if ( checkFieldRules != null )
740 {
741 Collection<CheckRule> colRules = checkFieldRules.getCheckRule( );
742
743 for ( CheckRule rule : colRules )
744 {
745 FieldValidator validator = (FieldValidator) ValidatorFactory.getValidator( rule );
746 bValidate &= validator.validate( field, listErrors );
747 }
748 }
749 }
750
751
752 CheckSubFormRules checkSubFormRules = formElements.getCheckSubFormRules( );
753
754
755 if ( ( bValidate ) && ( checkSubFormRules != null ) )
756 {
757 Collection<CheckRule> colRules = checkSubFormRules.getCheckRule( );
758
759 for ( CheckRule rule : colRules )
760 {
761 SubFormValidator validator = (SubFormValidator) ValidatorFactory.getValidator( rule );
762 bValidate &= validator.validate( this, request, listErrors );
763 }
764 }
765
766
767 listErrors.convertAnchorsToFullUrl( request, this.getName( ) );
768
769
770 for ( int k = 0; k < listErrors.size( ); k++ )
771 {
772 getParentForm( ).addErrorMessage( request, listErrors.get( k ) );
773 }
774
775 return bValidate;
776 }
777
778
779
780
781
782
783
784
785
786
787
788
789 private boolean validateUploadField( HttpServletRequest request, Field field, FormErrorsList listFormErrors )
790 {
791 boolean bValidate = true;
792
793
794 FormErrorsList listErrors;
795
796 if ( listFormErrors != null )
797 {
798 listErrors = listFormErrors;
799 }
800 else
801 {
802 listErrors = new FormErrorsList( );
803 }
804
805 CheckFieldRules checkFieldRules = field.getCheckFieldRules( );
806
807 if ( checkFieldRules != null )
808 {
809 Collection<CheckRule> colRules = checkFieldRules.getCheckRule( );
810
811 for ( CheckRule rule : colRules )
812 {
813 FieldValidator validator = (FieldValidator) ValidatorFactory.getValidator( rule );
814 bValidate &= validator.validate( field, listErrors );
815 }
816 }
817
818
819 listErrors.convertAnchorsToFullUrl( request, this.getName( ) );
820
821
822 for ( int k = 0; k < listErrors.size( ); k++ )
823 {
824 getParentForm( ).addErrorMessage( request, listErrors.get( k ) );
825 }
826
827 return bValidate;
828 }
829
830
831
832
833
834
835
836 private FormErrorsList validateUploadField2( HttpServletRequest request, Field field )
837 {
838 return null;
839 }
840
841
842
843
844
845
846
847
848
849
850 public FormElements getFormElements( HttpServletRequest request )
851 {
852
853 FormElements formElements = (FormElements) this.getSessionAttribute( request, SESSION_ATTRIBUTE_FORM_ELEMENTS );
854
855 if ( formElements == null )
856 {
857 formElements = getinitializedFormElements( );
858 setFormElements( request, formElements );
859 }
860
861 return formElements;
862 }
863
864
865
866
867
868
869 public void setFormElements( HttpServletRequest request, FormElements formElements )
870 {
871
872 this.setSessionAttribute( request, SESSION_ATTRIBUTE_FORM_ELEMENTS, formElements );
873 }
874
875
876
877
878
879
880 public void clearFormElements( HttpServletRequest request )
881 {
882
883 this.removeSessionAttribute( request, SESSION_ATTRIBUTE_FORM_ELEMENTS );
884 }
885
886
887
888
889
890
891 public void initFormElements( FormElements formElements )
892 {
893 _initialFormElements = formElements;
894 }
895
896
897
898
899
900
901 protected FormElements getinitializedFormElements( )
902 {
903 FormElements formElements = null;
904
905 ObjectFactory fieldFactory = new ObjectFactory( );
906 formElements = fieldFactory.createFormElements( );
907
908 if ( _initialFormElements != null )
909 {
910
911 formElements.setCheckSubFormRules( _initialFormElements.getCheckSubFormRules( ) );
912
913
914 formElements.setButtons( _initialFormElements.getButtons( ) );
915
916
917 formElements.setNotices( _initialFormElements.getNotices( ) );
918
919 Fields initialFields = _initialFormElements.getFields( );
920 List<Field> initialFieldList = initialFields.getField( );
921 Fields newFields = fieldFactory.createFields( );
922
923 for ( Field initialField : initialFieldList )
924 {
925 Field newField = fieldFactory.createField( );
926
927 newField.setLabel( initialField.getLabel( ) );
928 newField.setName( initialField.getName( ) );
929 newField.setType( initialField.getType( ) );
930 newField.setValue( initialField.getValue( ) );
931 newField.setCheckboxValue( initialField.getCheckboxValue( ) );
932 newField.setAdditionalInfo( initialField.getAdditionalInfo( ) );
933 newField.setRows( initialField.getRows( ) );
934 newField.setCols( initialField.getCols( ) );
935
936
937 newField.setCheckFieldRules( initialField.getCheckFieldRules( ) );
938
939 ChoiceList initialChoices = initialField.getChoiceList( );
940 ChoiceList newChoiceList = fieldFactory.createChoiceList( );
941
942 if ( initialChoices != null )
943 {
944 List<Choice> initialChoicesList = initialChoices.getChoice( );
945
946 for ( Choice initialChoice : initialChoicesList )
947 {
948 Choice newChoice = fieldFactory.createChoice( );
949
950 newChoice.setLabel( initialChoice.getLabel( ) );
951 newChoice.setValue( initialChoice.getValue( ) );
952
953 newChoiceList.getChoice( ).add( newChoice );
954 }
955 }
956
957 newField.setChoiceList( newChoiceList );
958
959 newFields.getField( ).add( newField );
960 }
961
962 formElements.setFields( newFields );
963 }
964
965 return formElements;
966 }
967
968
969
970
971
972
973
974
975 public Field getFieldFromName( HttpServletRequest request, String strName )
976 {
977 FormElements formElements = getFormElements( request );
978 Fields fields = formElements.getFields( );
979
980
981 if ( ( strName == null ) || ( fields == null ) || ( fields.getField( ) == null ) )
982 {
983 return null;
984 }
985
986
987 List<Field> fieldsList = fields.getField( );
988
989 for ( Field field : fieldsList )
990 {
991 if ( field.getName( ).equals( strName.trim( ) ) )
992 {
993 return field;
994 }
995 }
996
997
998 return null;
999 }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 public void setChoiceList( Field field, ReferenceList referenceList )
1010 {
1011 ObjectFactory formFactory = new ObjectFactory( );
1012 ChoiceList choices = formFactory.createChoiceList( );
1013 addElementsToChoiceList( choices, referenceList );
1014 field.setChoiceList( choices );
1015 }
1016
1017
1018
1019
1020
1021
1022
1023 public void appendToChoiceList( Field field, ReferenceList referenceList )
1024 {
1025 ChoiceList choices = field.getChoiceList( );
1026
1027 if ( choices == null )
1028 {
1029 setChoiceList( field, referenceList );
1030 }
1031 else
1032 {
1033 addElementsToChoiceList( choices, referenceList );
1034 }
1035 }
1036
1037
1038
1039
1040
1041
1042
1043 private void addElementsToChoiceList( ChoiceList choices, ReferenceList referenceList )
1044 {
1045 ObjectFactory formFactory = new ObjectFactory( );
1046
1047 if ( referenceList != null )
1048 {
1049 List<Choice> choiceList = choices.getChoice( );
1050
1051 for ( ReferenceItem item : referenceList )
1052 {
1053 String strValue = item.getCode( );
1054 String strLabel = item.getName( );
1055 Choice choice = formFactory.createChoice( );
1056 choice.setLabel( strLabel );
1057 choice.setValue( strValue );
1058 choiceList.add( choice );
1059 }
1060 }
1061 }
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076 public String buildHtmlForm( FormElements formElements, Map params )
1077 {
1078 String strXml = "";
1079 Plugin plugin = PluginService.getPlugin( PLUGIN_NAME );
1080
1081 try
1082 {
1083
1084 StringWriter writer = new StringWriter( );
1085 Marshaller marshaller = getParentForm( ).getFormElementsMarshaller( );
1086 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
1087
1088 addNotices( formElements );
1089
1090 marshaller.marshal( formElements, writer );
1091 strXml = writer.toString( );
1092 }
1093 catch ( JAXBException e )
1094 {
1095 throw new AppException( "Formengine : an error occurred during marshalling - form : "
1096 + this.getParentForm( ).getName( ) + " - subform " + this.getName( ), e );
1097 }
1098
1099 Map<String, String> formParams = new HashMap<String, String>( );
1100
1101 if ( params != null )
1102 {
1103 formParams.putAll( params );
1104 }
1105
1106
1107 formParams.put( PARAMETER_XSL_FORM_NAME, this.getParentForm( ).getName( ) );
1108 formParams.put( PARAMETER_XSL_SUBFORM_NAME, this.getName( ) );
1109
1110
1111 formParams.put( PARAMETER_XSL_PORTAL_URI, this.getPortalURI( ) );
1112
1113
1114
1115
1116 formParams.put( PARAMETER_XSL_UPLOAD_SUBMIT_PREFIX, SharedConstants.UPLOAD_SUBMIT_PREFIX );
1117 formParams.put( PARAMETER_XSL_UPLOAD_DELETE_PREFIX, SharedConstants.UPLOAD_DELETE_PREFIX );
1118 formParams.put( PARAMETER_XSL_UPLOAD_CHECKBOX_PREFIX, SharedConstants.UPLOAD_CHECKBOX_PREFIX );
1119
1120 if ( this.getNextSubForm( ) == null )
1121 {
1122 String strLastUrl = getDefaultUrlToExit( );
1123 formParams.put( PARAMETER_XSL_URL_EXIT, strLastUrl );
1124 }
1125
1126 String strForm = this.getHtml( strXml, this.getXslFormElementsFileName( ), formParams );
1127
1128 return strForm;
1129 }
1130
1131
1132
1133
1134
1135 protected void addNotices( FormElements formElements )
1136 {
1137 Plugin plugin = PluginService.getPlugin( PLUGIN_NAME );
1138
1139 String strFormName = getParentForm( ).getName( ) + "-" + getName( );
1140
1141 String strIdForm = FormsRegistrationService.getIdForm( strFormName );
1142
1143 NoticeGroupFilter filter = new NoticeGroupFilter( );
1144 filter.setForm( strIdForm );
1145 filter.setIsEnbled( 1 );
1146
1147 List<NoticeGroup> listNoticesGroup = NoticeGroupHome.findByFilter( filter, plugin );
1148
1149 Notices notices = new Notices( );
1150 formElements.setNotices( notices );
1151
1152 for ( NoticeGroup noticeGroup : listNoticesGroup )
1153 {
1154
1155
1156
1157 List<String> strNoticeValue = new ArrayList<String>( );
1158 NoticeFilter noticeFilter = new NoticeFilter( );
1159 noticeFilter.setIdNoticeGroup( noticeGroup.getId( ) );
1160 noticeFilter.setIsEnabled( 1 );
1161 List<Notice> listNotice = NoticeHome.getNoticeList( noticeFilter, plugin );
1162
1163 for ( Notice notice : listNotice )
1164 {
1165 if ( notice.hasValidDate( ) )
1166 {
1167 strNoticeValue.add( notice.getMessage( ) );
1168 }
1169 }
1170
1171 fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.NoticeGroup xmlNoticeGroup = new fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.NoticeGroup( );
1172
1173
1174 xmlNoticeGroup.setName( Integer.toString( noticeGroup.getId( ) ) );
1175 xmlNoticeGroup.getNotice( ).addAll( strNoticeValue );
1176
1177
1178 notices.getNoticeGroup( ).add( xmlNoticeGroup );
1179 }
1180
1181 }
1182
1183
1184
1185
1186
1187 protected String getDefaultUrlToExit( )
1188 {
1189 return AppPropertiesService.getProperty( this.getParentForm( ).getName( ) + PROPERTY_FRAGMENT_URL_FOR_LAST );
1190 }
1191
1192
1193
1194
1195
1196
1197 public final List<Field> getUploadFields( HttpServletRequest request )
1198 {
1199 List<Field> listUploadFields = new ArrayList<Field>( );
1200
1201 for ( Field field : getFormElements( request ).getFields( ).getField( ) )
1202 {
1203 if ( SharedConstants.FIELD_TYPE_UPLOAD.equals( field.getType( ) ) )
1204 {
1205 listUploadFields.add( field );
1206 }
1207 }
1208
1209 return listUploadFields;
1210 }
1211
1212
1213
1214
1215
1216
1217
1218
1219 public void setSessionAttribute( HttpServletRequest request, String strAttributeName, Object attribute )
1220 {
1221 HttpSession session = request.getSession( true );
1222 String strPrefix = SharedConstants.SESSION_PREFIX + "_" + this.getParentForm( ).getName( ) + "_"
1223 + this.getName( ) + "_";
1224 String strSessionAttribute = strPrefix + strAttributeName.trim( );
1225 strSessionAttribute = strSessionAttribute.toUpperCase( );
1226 session.setAttribute( strSessionAttribute, attribute );
1227 }
1228
1229
1230
1231
1232
1233
1234
1235
1236 public Object getSessionAttribute( HttpServletRequest request, String strAttributeName )
1237 {
1238 HttpSession session = request.getSession( true );
1239 String strPrefix = SharedConstants.SESSION_PREFIX + "_" + this.getParentForm( ).getName( ) + "_"
1240 + this.getName( ) + "_";
1241 String strSessionAttribute = strPrefix + strAttributeName.trim( );
1242 strSessionAttribute = strSessionAttribute.toUpperCase( );
1243
1244 return session.getAttribute( strSessionAttribute );
1245 }
1246
1247
1248
1249
1250
1251
1252
1253 public void removeSessionAttribute( HttpServletRequest request, String strAttributeName )
1254 {
1255 HttpSession session = request.getSession( true );
1256 String strPrefix = SharedConstants.SESSION_PREFIX + "_" + this.getParentForm( ).getName( ) + "_"
1257 + this.getName( ) + "_";
1258 String strSessionAttribute = strPrefix + strAttributeName.trim( );
1259 strSessionAttribute = strSessionAttribute.toUpperCase( );
1260 session.removeAttribute( strSessionAttribute );
1261 }
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271 public String doUploadAction( HttpServletRequest request, String strUploadAction )
1272 {
1273
1274 this.fillFields( request );
1275
1276
1277 String strFieldName = ( strUploadAction.startsWith( SharedConstants.UPLOAD_SUBMIT_PREFIX ) ? strUploadAction
1278 .substring( SharedConstants.UPLOAD_SUBMIT_PREFIX.length( ) ) : strUploadAction
1279 .substring( SharedConstants.UPLOAD_DELETE_PREFIX.length( ) ) );
1280
1281
1282 Field field = null;
1283 Iterator iterFields = this.getFormElements( request ).getFields( ).getField( ).iterator( );
1284 boolean bFound = false;
1285
1286 while ( !bFound && iterFields.hasNext( ) )
1287 {
1288 Field aField = (Field) iterFields.next( );
1289
1290 if ( strFieldName.equals( aField.getName( ) ) )
1291 {
1292 bFound = true;
1293 field = aField;
1294 }
1295 }
1296
1297 if ( strUploadAction.startsWith( SharedConstants.UPLOAD_SUBMIT_PREFIX ) )
1298 {
1299
1300 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
1301
1302 FileItem fileItem = multipartRequest.getFile( strFieldName );
1303
1304 if ( fileItem != null )
1305 {
1306
1307
1308 String strFileName = UploadUtil.cleanFileName( FileUploadService.getFileNameOnly( fileItem ) );
1309
1310
1311 List<FileItem> uploadedFiles = this.getFileItems( request, strFieldName );
1312
1313 if ( uploadedFiles != null )
1314 {
1315 Iterator<FileItem> iterUploadedFiles = uploadedFiles.iterator( );
1316 boolean bNew = true;
1317
1318 while ( bNew && iterUploadedFiles.hasNext( ) )
1319 {
1320 FileItem uploadedFile = iterUploadedFiles.next( );
1321 String strUploadedFileName = UploadUtil.cleanFileName( FileUploadService
1322 .getFileNameOnly( uploadedFile ) );
1323
1324
1325
1326 bNew = !( strUploadedFileName.equals( strFileName ) && ( uploadedFile.getSize( ) == fileItem
1327 .getSize( ) ) );
1328 }
1329
1330 if ( !bNew )
1331 {
1332
1333
1334
1335
1336 String[] messageParams = new String[2];
1337 messageParams[0] = field.getName( );
1338 messageParams[1] = field.getLabel( );
1339
1340 String strError = MessageFormat.format(
1341 AppPropertiesService.getProperty( PROPERTY_MESSAGE_EXISTING_FILE ),
1342 (Object[]) messageParams );
1343 this.getParentForm( ).addErrorMessage( request, strError );
1344
1345 return this.getName( );
1346 }
1347 }
1348
1349 addFileItemToUploadedFiles( request, field, fileItem, null );
1350 }
1351 }
1352 else if ( strUploadAction.startsWith( SharedConstants.UPLOAD_DELETE_PREFIX ) )
1353 {
1354
1355
1356 String strPrefix = SharedConstants.UPLOAD_CHECKBOX_PREFIX + strFieldName;
1357
1358
1359 Enumeration enumParamNames = request.getParameterNames( );
1360
1361 while ( enumParamNames.hasMoreElements( ) )
1362 {
1363 String strParamName = (String) enumParamNames.nextElement( );
1364
1365 if ( strParamName.startsWith( strPrefix ) )
1366 {
1367
1368 int iIndex = Integer.parseInt( strParamName.substring( strPrefix.length( ) ) );
1369
1370
1371 field.getFileNames( ).getFileName( ).remove( iIndex );
1372
1373
1374
1375 this.removeFileItem( request, strFieldName, iIndex );
1376 }
1377 }
1378 }
1379
1380
1381 return this.getName( );
1382 }
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395 public final void addFileItemToUploadedFiles( HttpServletRequest request, Field field, FileItem fileItem,
1396 FormErrorsList formErrorsList )
1397 {
1398 ObjectFactory objectFactory = new ObjectFactory( );
1399
1400 if ( field.getFileNames( ) == null )
1401 {
1402 field.setFileNames( objectFactory.createFileNames( ) );
1403 }
1404
1405 FileName fileName = objectFactory.createFileName( );
1406 String strFileName = UploadUtil.cleanFileName( FileUploadService.getFileNameOnly( fileItem ) );
1407 fileName.setValue( strFileName );
1408 fileName.setSize( Long.toString( fileItem.getSize( ) ) );
1409 field.getFileNames( ).getFileName( ).add( fileName );
1410
1411
1412 boolean bValid = validateUploadField( request, field, formErrorsList );
1413
1414 if ( bValid )
1415 {
1416
1417 this.addFileItem( request, field.getName( ), fileItem );
1418 }
1419 else
1420 {
1421
1422
1423
1424
1425
1426 List fileNames = field.getFileNames( ).getFileName( );
1427 fileNames.remove( fileNames.size( ) - 1 );
1428 }
1429 }
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439 public final void addFileItem( HttpServletRequest request, String strFieldName, FileItem fileItem )
1440 {
1441
1442 String strAttributeName = SharedConstants.FILELIST_PREFIX + strFieldName;
1443
1444 if ( this.getSessionAttribute( request, strAttributeName ) == null )
1445 {
1446 this.setSessionAttribute( request, strAttributeName, new ArrayList<FileItem>( ) );
1447 }
1448
1449
1450 List<FileItem> listFiles = (List) this.getSessionAttribute( request, strAttributeName );
1451 listFiles.add( fileItem );
1452 }
1453
1454
1455
1456
1457
1458
1459
1460 public final void addFileItem( String strIdSession, String strFieldName, FileItem fileItem )
1461 {
1462 getParentForm( ).addFileItem( this.getName( ), strIdSession, strFieldName, fileItem );
1463 }
1464
1465
1466
1467
1468
1469
1470
1471 public final List<FileItem> getFileItems( String strIdSession, String strFieldName )
1472 {
1473 return getParentForm( ).getAsynchronousUploadedFiles( this.getName( ), strIdSession, strFieldName );
1474 }
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484 public final synchronized boolean removeUploadedFileItem( HttpServletRequest request, String strFieldName,
1485 int nIndex )
1486 {
1487 boolean bRemoved = false;
1488
1489 List<FileItem> listFiles = getFileItems( request, strFieldName );
1490
1491 if ( ( listFiles != null ) && ( listFiles.size( ) > nIndex ) )
1492 {
1493 FileItem fileItem = listFiles.remove( nIndex );
1494
1495 if ( fileItem != null )
1496 {
1497 fileItem.delete( );
1498 bRemoved = true;
1499 }
1500 else
1501 {
1502 bRemoved = false;
1503 }
1504
1505 getFieldFromName( request, strFieldName ).getFileNames( ).getFileName( ).remove( nIndex );
1506 }
1507
1508 return bRemoved;
1509 }
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520 public final void removeFileItem( HttpServletRequest request, String strFieldName, int iIndex )
1521 {
1522
1523 String strAttributeName = SharedConstants.FILELIST_PREFIX + strFieldName;
1524 List<FileItem> listFiles = (List) this.getSessionAttribute( request, strAttributeName );
1525
1526 if ( ( listFiles != null ) && ( listFiles.size( ) > iIndex ) )
1527 {
1528
1529 FileItem fileItem = listFiles.remove( iIndex );
1530 fileItem.delete( );
1531 }
1532 }
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545 public final List<FileItem> getFileItems( HttpServletRequest request, String strFieldName )
1546 {
1547
1548 String strAttributeName = SharedConstants.FILELIST_PREFIX + strFieldName;
1549
1550 return (List<FileItem>) this.getSessionAttribute( request, strAttributeName );
1551 }
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563 protected void setBlobStoreFileItemsURL( HttpServletRequest request, String strFieldName,
1564 List<String> listFilesUrl, IBlobStoreService IBlobStoreService )
1565 {
1566 List<FileItem> listUploadedFileItems = getFileItems( request, strFieldName );
1567
1568 if ( listUploadedFileItems != null )
1569 {
1570 listFilesUrl.clear( );
1571
1572 List<BlobStoreFileItem> listBlobStoreFileItems = buildListBlobStoreFileItem( listUploadedFileItems,
1573 IBlobStoreService );
1574
1575 for ( BlobStoreFileItem fileItem : listBlobStoreFileItems )
1576 {
1577 listFilesUrl.add( IBlobStoreService.getFileUrl( fileItem.getBlobId( ) ) );
1578 }
1579
1580
1581 listUploadedFileItems.clear( );
1582 listUploadedFileItems.addAll( listBlobStoreFileItems );
1583 }
1584 }
1585
1586
1587
1588
1589
1590
1591
1592 private List<BlobStoreFileItem> buildListBlobStoreFileItem( List<FileItem> fileItems,
1593 IBlobStoreService IBlobStoreService )
1594 {
1595 List<BlobStoreFileItem> listBlobStoreFileItem = new ArrayList<BlobStoreFileItem>( );
1596
1597 for ( FileItem fileItem : fileItems )
1598 {
1599
1600 BlobStoreFileItem blobFileItem;
1601
1602 if ( !( fileItem instanceof BlobStoreFileItem ) )
1603 {
1604 if ( AppLogService.isDebugEnabled( ) )
1605 {
1606 AppLogService.debug( "Forcing upload of " + fileItem.getName( ) + " to blobstore" );
1607 }
1608
1609 blobFileItem = uploadFileToBlobStore( fileItem, IBlobStoreService );
1610 }
1611 else
1612 {
1613 blobFileItem = (BlobStoreFileItem) fileItem;
1614 }
1615
1616 listBlobStoreFileItem.add( blobFileItem );
1617 }
1618
1619 return listBlobStoreFileItem;
1620 }
1621
1622
1623
1624
1625
1626
1627
1628 private BlobStoreFileItem uploadFileToBlobStore( FileItem fileItem, IBlobStoreService service )
1629 {
1630 String strFileBlobId;
1631
1632 try
1633 {
1634 strFileBlobId = service.storeInputStream( fileItem.getInputStream( ) );
1635 }
1636 catch ( IOException e )
1637 {
1638 AppLogService.error( e.getMessage( ), e );
1639
1640 return null;
1641 }
1642
1643 String strJSONMetada = BlobStoreFileItem.buildFileMetadata(
1644 UploadUtil.cleanFileName( FileUploadService.getFileNameOnly( fileItem ) ), fileItem.getSize( ),
1645 strFileBlobId, fileItem.getContentType( ) );
1646 String strKeyMetadata = service.store( strJSONMetada.getBytes( ) );
1647
1648 try
1649 {
1650 return new BlobStoreFileItem( strKeyMetadata, service );
1651 }
1652 catch ( NoSuchBlobException e )
1653 {
1654 AppLogService.error( e.getMessage( ), e );
1655
1656 return null;
1657 }
1658 }
1659
1660
1661
1662
1663
1664
1665
1666
1667 public String[] getXslFilesNames( )
1668 {
1669 return new String[] { getXslFormElementsFileName( ), };
1670 }
1671 }