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 fr.paris.lutece.plugins.formengine.service.FormsRegistrationService;
37 import fr.paris.lutece.plugins.formengine.util.JSONUtils;
38 import fr.paris.lutece.portal.service.i18n.I18nService;
39 import fr.paris.lutece.portal.service.util.AppLogService;
40 import fr.paris.lutece.portal.web.upload.IAsynchronousUploadHandler;
41
42 import net.sf.json.JSONObject;
43
44 import org.apache.commons.fileupload.FileItem;
45 import org.apache.commons.lang.StringUtils;
46
47 import java.util.List;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
52
53
54
55
56
57
58
59
60
61 public class FormengineAsynchronousUploadHandler implements IAsynchronousUploadHandler
62 {
63 private static final String PARAMETER_PAGE = "page";
64 private static final String PARAMETER_FORM = "form";
65 private static final String PARAMETER_SUBFORM = "subform";
66 private static final String PARAMETER_FIELD_NAME = "fieldname";
67 private static final String PARAMETER_JSESSION_ID = "jsessionid";
68 private static final String PROPERTY_MESSAGE_ERROR_UPLOAD = "";
69
70
71
72
73 public boolean isInvoked( HttpServletRequest request )
74 {
75 return SharedConstants.PLUGIN_NAME.equals( request.getParameter( PARAMETER_PAGE ) );
76 }
77
78
79
80
81
82
83 public void process( HttpServletRequest request, HttpServletResponse response, JSONObject mainObject,
84 List<FileItem> listFileItems )
85 {
86 String strFormName = request.getParameter( PARAMETER_FORM );
87 String strSubformName = request.getParameter( PARAMETER_SUBFORM );
88 String strFieldName = request.getParameter( PARAMETER_FIELD_NAME );
89 Form form = FormsRegistrationService.getForm( strFormName );
90 String strSessionId = request.getParameter( PARAMETER_JSESSION_ID );
91
92
93 if ( strSessionId == null )
94 {
95 AppLogService.error( "Session does not exists" );
96 mainObject.accumulate( JSONUtils.JSON_KEY_ERROR,
97 I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_UPLOAD, request.getLocale( ) ) );
98 }
99 else if ( form == null )
100 {
101 mainObject.accumulate( JSONUtils.JSON_KEY_ERROR,
102 I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_UPLOAD, request.getLocale( ) ) );
103 }
104 else
105 {
106 SubForm subForm = null;
107
108 if ( StringUtils.isNotBlank( strSubformName ) )
109 {
110 subForm = form.getSubForm( strSubformName );
111 }
112
113 if ( subForm == null )
114 {
115
116 subForm = form.getFirstSubForm( );
117 }
118
119
120 for ( FileItem fileItem : listFileItems )
121 {
122
123
124 subForm.addFileItem( strSessionId, strFieldName, fileItem );
125 }
126
127
128 List<FileItem> listSessionFileItems = subForm.getFileItems( strSessionId, strFieldName );
129 JSONObject jsonListFileItems = JSONUtils.getUploadedFileJSON( listSessionFileItems );
130 mainObject.accumulateAll( jsonListFileItems );
131
132 mainObject.element( JSONUtils.JSON_KEY_FIELD, strFieldName );
133 }
134 }
135 }