1616import org .apache .commons .lang3 .StringUtils ;
1717import org .springframework .stereotype .Component ;
1818
19+ import com .fasterxml .jackson .databind .ObjectMapper ;
20+
1921import fr .insee .arc .core .dataobjects .ArcPreparedStatementBuilder ;
2022import fr .insee .arc .core .dataobjects .ColumnEnum ;
2123import fr .insee .arc .core .dataobjects .DataObjectService ;
@@ -163,21 +165,20 @@ public ResultSet exportFileFilteredOrdered(Statement stmt, int n, List<String> t
163165 * @throws IOException
164166 * @throws SQLException
165167 */
166- public void exportFile (Map <String , List <String >> h , int n , BufferedWriter bw , FileOutputStream fw )
168+ public void exportFile (Map <String , List <String >> exportparameter , int n , BufferedWriter bw , FileOutputStream fw )
167169 throws ArcException , IOException , SQLException {
168- List <String > tablesToExport = h .get ("table_to_export" );
169- List <String > headers = h .get ("headers" );
170- List <String > nulls = h .get ("nulls" );
171- List <String > filterTable = h .get ("filter_table" );
172- List <String > orderTable = h .get ("order_table" );
173- List <String > howToExport = h .get ("nomenclature_export" );
174- List <String > headersToScan = h .get ("columns_array_header" );
175- List <String > valuesToScan = h .get ("columns_array_value" );
170+ List <String > tablesToExport = exportparameter .get ("table_to_export" );
171+ List <String > headers = exportparameter .get ("headers" );
172+ List <String > nulls = exportparameter .get ("nulls" );
173+ List <String > filterTable = exportparameter .get ("filter_table" );
174+ List <String > orderTable = exportparameter .get ("order_table" );
175+ List <String > howToExport = exportparameter .get ("nomenclature_export" );
176+ List <String > jsonKeyValueToConvert = exportparameter .get ("json_key_value" );
176177
177178 Map <String , Integer > pos = new HashMap <>();
178179 List <String > headerLine = new ArrayList <>();
179180
180- h = exportFileRetrieveRules (n , howToExport , tablesToExport , this .dataObjectService .getSandboxSchema ());
181+ Map < String , List < String >> h = exportFileRetrieveRules (n , howToExport , tablesToExport , this .dataObjectService .getSandboxSchema ());
181182
182183 if (h .isEmpty ()) {
183184 throw new ArcException (ArcExceptionMessage .GUI_EXPORT_TABLE_NOT_EXISTS );
@@ -208,33 +209,32 @@ public void exportFile(Map<String, List<String>> h, int n, BufferedWriter bw, Fi
208209 try (ResultSet res = exportFileFilteredOrdered (stmt , n , tablesToExport , filterTable , orderTable , this .dataObjectService .getSandboxSchema ())) {
209210 ResultSetMetaData rsmd = res .getMetaData ();
210211
211- ArrayList <String > output ;
212- String [] tabH ;
213- String [] tabV ;
214212 String colName ;
215213 while (res .next ()) {
216214 // reinitialiser l'arraylist de sortie
217- output = new ArrayList <>();
215+ ArrayList < String > output = new ArrayList <>();
218216 for (int k = 0 ; k < maxPos ; k ++) {
219217 output .add ("" );
220218 }
221-
219+
222220 boolean todo = false ;
223- tabH = null ;
224- tabV = null ;
221+
222+ List <String > tabH =new ArrayList <>();
223+ List <String > tabV =new ArrayList <>();
224+
225+
225226 for (int i = 1 ; i <= rsmd .getColumnCount (); i ++) {
226227 colName = rsmd .getColumnLabel (i ).toLowerCase ();
227228
228229 todo = true ;
229230 // cas ou on est dans un tableau
230- if (todo && colName .equals (headersToScan .get (n ))) {
231- todo = false ;
232- tabH = (String []) res .getArray (i ).getArray ();
233- }
234- if (todo && colName .equals (valuesToScan .get (n ))) {
231+ if (todo && colName .equals (jsonKeyValueToConvert .get (n ))) {
235232 todo = false ;
236- tabV = (String []) res .getArray (i ).getArray ();
233+
234+ Map <String ,String > m = convertKeyValueJsonToArray (res .getString (i ));
235+ m .entrySet ().stream ().forEach (t -> { tabH .add (t .getKey ()); tabV .add (t .getValue ());});
237236 }
237+
238238 if (todo ) {
239239 todo = false ;
240240 if (pos .get (colName ) != null ) {
@@ -247,12 +247,12 @@ public void exportFile(Map<String, List<String>> h, int n, BufferedWriter bw, Fi
247247 }
248248
249249 // traitement des variables tableaux
250- if (tabH != null && tabV != null ) {
251- for (int k = 0 ; k < tabH .length ; k ++) {
252- if (pos .get (tabH [ k ] .toLowerCase ()) != null ) {
250+ if (! tabH . isEmpty () && ! tabV . isEmpty () ) {
251+ for (int k = 0 ; k < tabH .size () ; k ++) {
252+ if (pos .get (tabH . get ( k ) .toLowerCase ()) != null ) {
253253 // if nulls value musn't be quoted as "null" and element is null then don't write
254- if (!(StringUtils .isEmpty (nulls .get (n )) && StringUtils .isEmpty (tabV [ k ] ))) {
255- output .set (pos .get (tabH [ k ] .toLowerCase ()), tabV [ k ] );
254+ if (!(StringUtils .isEmpty (nulls .get (n )) && StringUtils .isEmpty (tabV . get ( k ) ))) {
255+ output .set (pos .get (tabH . get ( k ) .toLowerCase ()), tabV . get ( k ) );
256256 }
257257 }
258258 }
@@ -271,6 +271,20 @@ public void exportFile(Map<String, List<String>> h, int n, BufferedWriter bw, Fi
271271 }
272272
273273
274+ @ SuppressWarnings ("unchecked" )
275+ private Map <String ,String > convertKeyValueJsonToArray (String keyValueJson ) {
276+ ObjectMapper mapper = new ObjectMapper ();
277+ try {
278+ return mapper .readValue (keyValueJson , Map .class );
279+ }
280+ catch (Exception e )
281+ {
282+ System .out .println ("not deserializable" );
283+ }
284+ return new HashMap <>();
285+
286+ }
287+
274288 public VObjectService getvObjectService () {
275289 return vObjectService ;
276290 }
0 commit comments