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.phraseanet.service.parsers;
35
36 import fr.paris.lutece.plugins.phraseanet.business.record.Metadata;
37 import fr.paris.lutece.plugins.phraseanet.business.record.MetadataValue;
38 import fr.paris.lutece.plugins.phraseanet.service.Constants;
39 import fr.paris.lutece.plugins.phraseanet.service.api.PhraseanetApiCallException;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41
42 import net.sf.json.JSONException;
43 import net.sf.json.JSONObject;
44 import net.sf.json.JSONArray;
45
46 import java.util.ArrayList;
47 import java.util.Iterator;
48 import java.util.List;
49
50 import org.apache.log4j.Logger;
51
52
53
54
55
56 public final class MetadatasJsonParser
57 {
58 private static Logger _logger = Logger.getLogger( Constants.LOGGER );
59
60
61 private MetadatasJsonParser( )
62 {
63 _logger.debug( "MetadatasJsonParser" );
64 }
65
66
67
68
69
70
71
72
73 public static List<Metadata> parse( JSONObject jsonResponse )
74 throws PhraseanetApiCallException
75 {
76 try
77 {
78 List<Metadata> listMetadatas = new ArrayList<Metadata>( );
79
80 JSONArray jsonMetadatasList = jsonResponse.getJSONArray( "record_metadatas" );
81 JSONObject jsonMetadatas = jsonMetadatasList.toJSONObject( jsonMetadatasList ) ;
82 Iterator i = jsonMetadatas.keys( );
83 while ( i.hasNext( ) )
84 {
85 String strKey = (String) i.next( );
86 JSONObject jsonMetadata = jsonMetadatas.getJSONObject( strKey );
87 Metadata metadata = new Metadata( );
88 metadata.setMetaId( jsonMetadata.getInt( "meta_id" ) );
89 metadata.setMetaStructureId( jsonMetadata.getInt( "meta_structure_id" ) );
90 metadata.setName( jsonMetadata.getString( "name" ) );
91 metadata.setValue( jsonMetadata.getString( "value" ) );
92 listMetadatas.add( metadata );
93 }
94
95 return listMetadatas;
96 }
97 catch ( JSONException e )
98 {
99 AppLogService.error( "Error parsing metadatas " + e.getMessage()+ " - JSON : " + jsonResponse.toString( 4 ) );
100 return null;
101 }
102 }
103
104
105
106
107
108
109
110 public static List<Metadata> parseByDataboxe( JSONObject jsonResponse )
111 throws PhraseanetApiCallException
112 {
113 try
114 {
115 List<Metadata> listMetadatas = new ArrayList<Metadata>( );
116 JSONArray jsonMetadatasList = jsonResponse.getJSONArray( "document_metadatas" );
117 _logger.debug("Liste des metadatas : " + jsonMetadatasList ) ;
118 JSONObject jsonMetadatas = jsonMetadatasList.toJSONObject(jsonMetadatasList);
119 _logger.debug("JSONObject : " + jsonMetadatas ) ;
120 Iterator i = jsonMetadatas.keys( );
121
122 while ( i.hasNext( ) )
123 {
124 String strKey = (String) i.next( );
125 JSONObject jsonMetadata = jsonMetadatas.getJSONObject( strKey );
126 Metadata metadata = new Metadata( );
127 metadata.setName( jsonMetadata.getString( "name" ) );
128 listMetadatas.add( metadata );
129 }
130
131
132 return listMetadatas;
133 }
134 catch ( JSONException e )
135 {
136 throw new PhraseanetApiCallException( "Error parsing metadatas : " + e.getMessage( ) + " - JSON : " +
137 jsonResponse.toString( 4 ) );
138 }
139 }
140 }