bug fixed in class DatabaseManagement: method writeSampleTableIntoFile replaced with method buildMapResult.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@99060 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Loredana Liccardo 2014-07-29 10:09:49 +00:00
parent 78e9c6e288
commit ab97ee8304
1 changed files with 214 additions and 56 deletions

View File

@ -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<String> 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<String> 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<String> 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<Object> result,
// List<String> DataTypeColumns, String tableName, String schemaName,
// String FileName) throws Exception {
// build the map of results
private void buildMapResult(List<Object> result,
List<String> DataTypeColumns) throws Exception {
// to get columns names and result
// to recover columns names
if (header.equals("")) {
ArrayList<String> listKeys = new ArrayList<String>(
((LinkedHashMap<String, Object>) (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<Object> listvalues = new ArrayList<Object>(
((LinkedHashMap<String, Object>) 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<Object> 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<String, String> 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<String, String> getMapQueryResult() {