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.directory.utils;
35
36 import fr.paris.lutece.plugins.directory.service.upload.DirectoryAsynchronousUploadHandler;
37
38 import net.sf.json.JSONArray;
39 import net.sf.json.JSONObject;
40 import net.sf.json.JSONSerializer;
41
42 import org.apache.commons.fileupload.FileItem;
43 import org.apache.commons.lang.StringUtils;
44
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49
50
51
52
53
54 public final class JSONUtils
55 {
56
57 private static final String JSON_KEY_FORM_ERROR = "directory_error";
58 private static final String JSON_KEY_SUCCESS = "success";
59 private static final String JSON_KEY_FIELD_NAME = "field_name";
60 private static final String JSON_KEY_UPLOADED_FILES = "uploadedFiles";
61 private static final String JSON_KEY_UPLOADED_FILES_SIZE = "uploadedFilesSize";
62 private static final String JSON_KEY_FILE_COUNT = "fileCount";
63 private static final String KEY_USER_ATTRIBUTES = "user-attributes";
64
65
66
67
68 private JSONUtils( )
69 {
70 }
71
72
73
74
75
76
77
78
79 public static JSONObject buildJsonError( String strMessage )
80 {
81 JSONObject json = new JSONObject( );
82 buildJsonError( json, strMessage );
83
84 return json;
85 }
86
87
88
89
90
91
92
93
94
95 public static void buildJsonError( JSONObject json, String strMessage )
96 {
97 if ( json != null )
98 {
99 json.accumulate( JSON_KEY_FORM_ERROR, strMessage );
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 public static JSONObject buildJsonSuccess( String strIdEntry, String strSessionId )
113 {
114 JSONObject json = new JSONObject( );
115 json.accumulateAll( getUploadedFileJSON( DirectoryAsynchronousUploadHandler.getHandler( ).getFileItems( strIdEntry, strSessionId ) ) );
116 buildJsonSuccess( DirectoryAsynchronousUploadHandler.getHandler( ).buildFieldName( strIdEntry ), json );
117
118 return json;
119 }
120
121
122
123
124
125
126
127
128
129 public static void buildJsonSuccess( String strFieldName, JSONObject json )
130 {
131 if ( json != null )
132 {
133
134 json.element( JSON_KEY_SUCCESS, JSONUtils.JSON_KEY_SUCCESS );
135 json.element( JSON_KEY_FIELD_NAME, strFieldName );
136 }
137 }
138
139
140
141
142
143
144
145
146 public static JSONObject getUploadedFileJSON( List<FileItem> listFileItem )
147 {
148 JSONObject json = new JSONObject( );
149
150 if ( listFileItem != null )
151 {
152 for ( FileItem fileItem : listFileItem )
153 {
154 json.accumulate( JSON_KEY_UPLOADED_FILES, fileItem.getName( ) );
155 json.accumulate( JSON_KEY_UPLOADED_FILES_SIZE, fileItem.getSize( ) );
156 }
157
158 json.element( JSON_KEY_FILE_COUNT, listFileItem.size( ) );
159 }
160 else
161 {
162
163 json.element( JSON_KEY_FILE_COUNT, 0 );
164 }
165
166 return json;
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 public static Map<String, String> getUserInfos( String strJSON )
184 {
185 Map<String, String> userInfos = new HashMap<String, String>( );
186
187 if ( StringUtils.isNotBlank( strJSON ) )
188 {
189
190 JSONObject json = (JSONObject) JSONSerializer.toJSON( strJSON );
191
192 if ( json != null )
193 {
194
195 JSONArray arrayUserAttributes = json.getJSONArray( KEY_USER_ATTRIBUTES );
196
197 if ( arrayUserAttributes != null )
198 {
199
200 for ( int i = 0; i < arrayUserAttributes.size( ); i++ )
201 {
202 put( userInfos, arrayUserAttributes.getJSONObject( i ) );
203 }
204 }
205 }
206 }
207
208 return userInfos;
209 }
210
211
212
213
214
215
216
217
218
219 private static void put( Map<String, String> userInfos, JSONObject userAttribute )
220 {
221 if ( userAttribute != null )
222 {
223 JSONArray listCodes = userAttribute.names( );
224
225 for ( int i = 0; i < listCodes.size( ); i++ )
226 {
227 String strCode = listCodes.getString( i );
228 String strValue = userAttribute.getString( strCode );
229 userInfos.put( strCode, strValue );
230 }
231 }
232 }
233 }