diff --git a/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseManagement.java b/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseManagement.java index 270c64a..16ef154 100644 --- a/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseManagement.java +++ b/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseManagement.java @@ -339,9 +339,9 @@ public class DatabaseManagement { "DatabaseManagement-> Query result retrieved"); } - // store table in a file - AnalysisLogger.getLogger().debug( - "In DatabaseManagement->store table in a file"); + // // store table in a file + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->store table in a file"); // List listColumnNames = getColumnNamesTable(tableName, // schemaName); @@ -360,6 +360,9 @@ public class DatabaseManagement { // file), "UTF-8")); // writeTableIntoFile(results, DataTypeColumns); + // build the Map of Result + buildMapResult(results, DataTypeColumns); + return results; } @@ -655,9 +658,9 @@ public class DatabaseManagement { "DatabaseManagement-> rows retrieved"); } - // store table in a file - AnalysisLogger.getLogger().debug( - "In DatabaseManagement->store table in a file"); + // // store table in a file + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->store table in a file"); // store table in a file @@ -692,18 +695,17 @@ public class DatabaseManagement { // AnalysisLogger.getLogger().debug( // "In DatabaseManagement->header: " + header); - // write the result in the file and in the map + // // write the result in the file and in the map + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->writing the result in the file: " + // + FileName); + // file = new File(FileName); + // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( + // file), "UTF-8")); + // // writeTableIntoFile(resultSet, DataTypeColumns); - AnalysisLogger.getLogger().debug( - "In DatabaseManagement->writing the result in the file: " - + FileName); - - file = new File(FileName); - - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( - file), "UTF-8")); - - // writeTableIntoFile(resultSet, DataTypeColumns); + // build the Map of Result + buildMapResult(resultSet, DataTypeColumns); } /** @@ -742,9 +744,9 @@ public class DatabaseManagement { "DatabaseManagement-> rows retrieved"); } - // store table in a file - AnalysisLogger.getLogger().debug( - "In DatabaseManagement->store table in a file"); + // // store table in a file + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->store table in a file"); // to recover columns names list List listColumns = sampler.getListColumns(); @@ -768,6 +770,9 @@ public class DatabaseManagement { // file), "UTF-8")); // writeTableIntoFile(resultSet, DataTypeColumns); + // build the Map of Result + buildMapResult(resultSet, DataTypeColumns); + } /** @@ -810,9 +815,9 @@ public class DatabaseManagement { "DatabaseManagement-> rows retrieved"); } - // store table in a file - AnalysisLogger.getLogger().debug( - "In DatabaseManagement->store table in a file"); + // // store table in a file + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->store table in a file"); // to recover columns names list List listColumns = sampler.getListColumns(); @@ -833,23 +838,178 @@ public class DatabaseManagement { } } -// // store table in a file -// String FileName = pathFile + "SampleResult.csv"; -// // write the result in the file and in the map -// AnalysisLogger.getLogger().debug( -// "In DatabaseManagement->writing the result in the file: " -// + FileName); -// file = new File(FileName); -// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( -// file), "UTF-8")); -// writeTableIntoFile(resultSet, DataTypeColumns); + // // store table in a file + // String FileName = pathFile + "SampleResult.csv"; + // // write the result in the file and in the map + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->writing the result in the file: " + // + FileName); + // file = new File(FileName); + // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( + // file), "UTF-8")); + // writeTableIntoFile(resultSet, DataTypeColumns); + + // build the Map of Result + buildMapResult(resultSet, DataTypeColumns); } - // // write the table result in the file and build the map of results - // private void writeTableIntoFile(List result, - // List DataTypeColumns, String tableName, String schemaName, - // String FileName) throws Exception { + // build the map of results + private void buildMapResult(List result, + List DataTypeColumns) throws Exception { + + // to get columns names and result + + // to recover columns names + if (header.equals("")) { + ArrayList listKeys = new ArrayList( + ((LinkedHashMap) (result.get(0))).keySet()); + + for (int i = 0; i < listKeys.size(); i++) { + if (i != listKeys.size() - 1) { + header = header + listKeys.get(i) + ", "; + } else { + header = header + listKeys.get(i); + } + } + } + + // // print check + // AnalysisLogger.getLogger().debug( + // "DatabaseManagement->HEADERS: " + header); + + // add headers + mapResult.put("HEADERS", header); + + // //print check values + // AnalysisLogger.getLogger().debug( + // "DatabaseManagement->columns names: " + listKeys); + + if (result != null && result.size() != 0) { + // // write operation in the file + for (int i = 0; i < result.size(); i++) { + String RowString = ""; + Object element = result.get(i); + + // arraylist in which each element is a row result + ArrayList listvalues = new ArrayList( + ((LinkedHashMap) element).values()); + + // // print check + // AnalysisLogger.getLogger().debug( + // "DatabaseManagement->values: " + listvalues); + + // each row could have several column values + Object[] row = listvalues.toArray(); + if (row.length >= 1) { + for (int j = 0; j < row.length; j++) { + if (row[j] == null) { + row[j] = ""; + } + // to parse the obtained results in order to align + // number + // values with those of postgres + String original = row[j].toString(); + + // // check value + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->original value: " + // + original); + + String parsed = "" + row[j]; + if (original != "") { + // convert database datatypes to Java datatypes + if (DataTypeColumns == null + || DataTypeColumns.size() == 0) + parsed = convertToJavaType(row[j].getClass() + .getName(), parsed); + else + parsed = convertToJavaType( + DataTypeColumns.get(j), parsed); + } + + // // check value + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->row: " + (i + 1) + // + " column: " + (j + 1) + " value= " + // + parsed); + + // write in a file + if (j != row.length - 1) { + + // System.out.println("write column : " + j); + // RowString = RowString + parsed + " "; + if (j == 0) { + RowString = parsed; + } else { + RowString = RowString + "," + parsed; + } + } + if (j == row.length - 1) { + + // to add a row to the map + if (row.length == 1) { + RowString = parsed; + } else { + RowString = RowString + "," + parsed; + } + + // to add a row to the map + // RowString = RowString + "," + parsed; + // mapSampleTableResult.put(String.valueOf(i), + // RowString); + + // check value row + // AnalysisLogger.getLogger().debug( + // "writing the value: " + RowString + " key: " + // + String.valueOf(i)); + + // mapResult.put(Integer.valueOf(i), RowString); + mapResult.put(String.valueOf(i), RowString); + } + } + } + // else if (result.size() == 1) { + // + // // Object RowElement = (Object) result.get(0); + // + // if (row[0] == null) { + // row[0] = ""; + // } + // + // // to parse the obtained results in order to align + // // number + // // values with those of postgres + // String original = row[0].toString(); + // + // // // check value + // // AnalysisLogger.getLogger().debug( + // // "In DatabaseManagement->original value: " + // // + original); + // + // String parsed = "" + row[0]; + // + // if (original != "") { + // // convert database datatypes to Java datatypes + // if (DataTypeColumns == null + // || DataTypeColumns.size() == 0) + // parsed = convertToJavaType(row[0].getClass() + // .getName(), parsed); + // else + // parsed = convertToJavaType(DataTypeColumns.get(0), + // parsed); + // } + // + // out.write(row[0].toString()); + // out.newLine(); + // + // // to add a row to the map + // mapResult.put(String.valueOf(i), row[0].toString()); + // + // } + } + } + } // // write the table result in the file and build the map of results // private void writeTableIntoFile(List result, @@ -1206,8 +1366,6 @@ public class DatabaseManagement { return listColumnNamesTable; } - - // to return the map which contains the rows that constitute the table // result public HashMap getMapSampleTableResult() { @@ -1218,23 +1376,23 @@ public class DatabaseManagement { } -// // to return the file in which the query result (originated from a submit -// // query) is stored -// public File getFileQueryResult() { -// -// // return fileQueryResult; -// return file; -// -// } - -// // to return the file in which the table result (originated from a submit -// // query and sample operations) is stored -// public File getFileSampleTableResult() { -// -// // return fileSample; -// return file; -// -// } + // // to return the file in which the query result (originated from a submit + // // query) is stored + // public File getFileQueryResult() { + // + // // return fileQueryResult; + // return file; + // + // } + + // // to return the file in which the table result (originated from a submit + // // query and sample operations) is stored + // public File getFileSampleTableResult() { + // + // // return fileSample; + // return file; + // + // } // to return the map which contains the rows of the query result public HashMap getMapQueryResult() {