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.modules.rest.service.formatters;
35
36 import fr.paris.lutece.plugins.directory.business.Directory;
37 import fr.paris.lutece.plugins.directory.business.Field;
38 import fr.paris.lutece.plugins.directory.business.IEntry;
39 import fr.paris.lutece.plugins.directory.business.Record;
40 import fr.paris.lutece.plugins.directory.business.RecordField;
41 import fr.paris.lutece.plugins.directory.modules.rest.service.IDirectoryRestService;
42 import fr.paris.lutece.plugins.directory.modules.rest.util.constants.DirectoryRestConstants;
43 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
44 import fr.paris.lutece.plugins.rest.business.resourceinfo.IResourceInfo;
45 import fr.paris.lutece.plugins.rest.service.formatters.IFormatter;
46 import fr.paris.lutece.plugins.rest.service.resourceinfo.ResourceInfoManager;
47 import fr.paris.lutece.plugins.rest.util.xml.XMLUtil;
48 import fr.paris.lutece.portal.service.i18n.I18nService;
49 import fr.paris.lutece.portal.service.util.AppLogService;
50 import fr.paris.lutece.portal.service.util.AppPropertiesService;
51 import fr.paris.lutece.util.xml.XmlUtil;
52
53 import freemarker.template.utility.StringUtil;
54
55 import org.apache.commons.lang.StringUtils;
56
57 import java.util.HashMap;
58 import java.util.List;
59 import java.util.Locale;
60 import java.util.Map;
61
62 import javax.inject.Inject;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public class RecordFormatterXml implements IFormatter<Record>
97 {
98 @Inject
99 private IDirectoryRestService _directoryRestService;
100 private Locale _locale = I18nService.getDefaultLocale( );
101
102
103
104
105 @Override
106 public String formatError( String strCode, String strMessage )
107 {
108 return XMLUtil.formatError( strMessage, DirectoryUtils.convertStringToInt( strCode ) );
109 }
110
111
112
113
114 @Override
115 public String format( Record record )
116 {
117 StringBuffer sbXml = new StringBuffer( AppPropertiesService.getProperty( XmlUtil.PROPERTIES_XML_HEADER ) );
118 XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_RESPONSE );
119 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_STATUS, DirectoryRestConstants.STATUS_SUCCESS );
120
121 formatRecord( sbXml, record );
122
123 XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_RESPONSE );
124
125 return sbXml.toString( );
126 }
127
128
129
130
131 @Override
132 public String format( List<Record> listRecords )
133 {
134 StringBuffer sbXml = new StringBuffer( AppPropertiesService.getProperty( XmlUtil.PROPERTIES_XML_HEADER ) );
135 XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_RESPONSE );
136 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_STATUS, DirectoryRestConstants.STATUS_SUCCESS );
137 XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_RECORDS );
138
139 for ( Record record : listRecords )
140 {
141 formatRecord( sbXml, record );
142 }
143
144 XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_RECORDS );
145 XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_RESPONSE );
146
147 return sbXml.toString( );
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 private void formatRecord( StringBuffer sbXml, Record record )
178 {
179 XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_RECORD );
180 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_ID, record.getIdRecord( ) );
181
182
183 formatResourceInfo( sbXml, record );
184
185
186
187
188
189
190
191 List<IEntry> listEntries = _directoryRestService.getEntries( record );
192 Map<String, List<RecordField>> mapIdsEntryListRecordFields = _directoryRestService.getMapIdEntryListRecordField( record );
193
194 XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_RECORD_FIELDS );
195
196 for ( IEntry entry : listEntries )
197 {
198 if ( entry.getEntryType( ).getGroup( ) )
199 {
200 if ( ( entry.getChildren( ) != null ) && !entry.getChildren( ).isEmpty( ) )
201 {
202 String strTag = fr.paris.lutece.util.string.StringUtil.replaceAccent( entry.getTitle( ) );
203 XmlUtil.beginElement( sbXml, strTag );
204
205 for ( IEntry entryChild : entry.getChildren( ) )
206 {
207 sbXml.append( getEntryXml( entryChild, mapIdsEntryListRecordFields ) );
208 }
209
210 XmlUtil.endElement( sbXml, strTag );
211 }
212 }
213 else
214 {
215 sbXml.append( getEntryXml( entry, mapIdsEntryListRecordFields ) );
216 }
217 }
218
219 XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_RECORD_FIELDS );
220 XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_RECORD );
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 private String getEntryXml( IEntry entry, Map<String, List<RecordField>> mapIdsEntryListRecordFields )
244 {
245 StringBuffer sbXml = new StringBuffer( );
246
247 if ( entry != null )
248 {
249 List<RecordField> listRecordFields = mapIdsEntryListRecordFields.get( Integer.toString(
250 entry.getIdEntry( ) ) );
251
252 if ( ( listRecordFields != null ) && !listRecordFields.isEmpty( ) )
253 {
254 String strTag = fr.paris.lutece.util.string.StringUtil.replaceAccent( entry.getTitle( ) );
255
256 if ( entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeCheckBox ||
257 entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeSelect )
258 {
259
260 for ( RecordField recordField : listRecordFields )
261 {
262 XmlUtil.beginElement( sbXml, strTag );
263 sbXml.append( getRecordFieldXml( entry, recordField ) );
264 XmlUtil.endElement( sbXml, strTag );
265 }
266 }
267 else
268 {
269 XmlUtil.beginElement( sbXml, strTag );
270
271 for ( RecordField recordField : listRecordFields )
272 {
273 sbXml.append( getRecordFieldXml( entry, recordField ) );
274 }
275
276 XmlUtil.endElement( sbXml, strTag );
277 }
278 }
279 }
280
281 return sbXml.toString( );
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 private String getRecordFieldXml( IEntry entry, RecordField recordField )
310 {
311 StringBuffer sbXml = new StringBuffer( );
312
313 if ( recordField != null )
314 {
315 if ( entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeImg )
316 {
317 Field field = recordField.getField( );
318
319 if ( field != null )
320 {
321 XmlUtil.beginElement( sbXml, field.getValue( ) );
322
323
324 if ( recordField.getFile( ) != null )
325 {
326 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_ID, recordField.getFile( ).getIdFile( ) );
327 }
328
329 if ( ( field.getWidth( ) != 0 ) && ( field.getHeight( ) != 0 ) )
330 {
331 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_WIDTH, field.getWidth( ) );
332 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_HEIGHT, field.getHeight( ) );
333 }
334
335 XmlUtil.endElement( sbXml, field.getValue( ) );
336 }
337 }
338 else if ( entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeFile )
339 {
340
341 if ( recordField.getFile( ) != null )
342 {
343 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_ID, recordField.getFile( ).getIdFile( ) );
344 }
345 }
346 else if ( entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeCheckBox ||
347 entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeSelect ||
348 entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeRadioButton )
349 {
350 sbXml.append( StringUtil.XMLEnc( entry.convertRecordFieldTitleToString( recordField, _locale, false ) ) );
351 }
352 else if ( entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeGeolocation )
353 {
354 Field field = recordField.getField( );
355
356 if ( ( field != null ) && StringUtils.isNotBlank( field.getTitle( ) ) )
357 {
358 XmlUtil.addElement( sbXml, field.getTitle( ),
359 entry.convertRecordFieldValueToString( recordField, _locale, false, false ) );
360 }
361 }
362 else
363 {
364 sbXml.append( StringUtil.XMLEnc( entry.convertRecordFieldValueToString( recordField, _locale, false,
365 false ) ) );
366 }
367 }
368 else
369 {
370 if ( AppLogService.isDebugEnabled( ) )
371 {
372 AppLogService.debug( "Record field is null for entry " + entry.getTitle( ) + " (" +
373 entry.getIdEntry( ) + ")" );
374 }
375 }
376
377 return sbXml.toString( );
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402 private void formatResourceInfo( StringBuffer sbXml, Record record )
403 {
404 Directory directory = _directoryRestService.getDirectory( record.getDirectory( ).getIdDirectory( ) );
405 Map<String, String> mapParams = new HashMap<String, String>( );
406 mapParams.put( DirectoryRestConstants.PARAMETER_ID_RESOURCE, Integer.toString( record.getIdRecord( ) ) );
407 mapParams.put( DirectoryRestConstants.PARAMETER_RESOURCE_TYPE, Record.WORKFLOW_RESOURCE_TYPE );
408 mapParams.put( DirectoryRestConstants.PARAMETER_ID_WORKFLOW, Integer.toString( directory.getIdWorkflow( ) ) );
409
410 List<IResourceInfo> listResourceInfos = ResourceInfoManager.getResourceInfo( mapParams );
411
412 if ( ( listResourceInfos != null ) && !listResourceInfos.isEmpty( ) )
413 {
414 for ( IResourceInfo resourceInfo : listResourceInfos )
415 {
416 formatResourceInfo( sbXml, resourceInfo );
417 }
418 }
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438 private void formatResourceInfo( StringBuffer sbXml, IResourceInfo resourceInfo )
439 {
440 if ( resourceInfo != null )
441 {
442 if ( resourceInfo.hasChildren( ) )
443 {
444 XmlUtil.beginElement( sbXml, resourceInfo.getKey( ) );
445
446 for ( IResourceInfo child : resourceInfo.getListChildren( ) )
447 {
448 formatResourceInfo( sbXml, child );
449 }
450
451 XmlUtil.endElement( sbXml, resourceInfo.getKey( ) );
452 }
453 else
454 {
455 XmlUtil.addElement( sbXml, resourceInfo.getKey( ), resourceInfo.getValue( ) );
456 }
457 }
458 }
459 }