- method submitQuery modified in class DatabaseManagement in order to return a file and a map as output. The map is truncated to 1000 rows if the result has more than 1000 rows.

method writeTableIntoFile reintroduced that performs the file writing and the map generation. 
- Version in pom file changed in 1.3.0
- method submitQuery modified in class DatabaseManagement adding the UUID string to the submitQuery result file QueryResult file.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@101653 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Loredana Liccardo 2014-11-19 14:38:18 +00:00
parent 4e18e1cd99
commit 72b8e87e81
1 changed files with 284 additions and 204 deletions

View File

@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.UUID;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataaccess.databases.converter.SqlDialectConverter; import org.gcube.dataaccess.databases.converter.SqlDialectConverter;
@ -344,12 +345,12 @@ public class DatabaseManagement {
*/ */
// method that allows to submit a query // method that allows to submit a query
public List<Object> submitQuery(String query, Connection conn) throws Exception { public List<Object> submitQuery(String query, Connection conn, String pathFile) throws Exception {
List<Object> results = new ArrayList<Object>(); List<Object> results = new ArrayList<Object>();
results = connection.executeQueryJDBC(query, conn); results = connection.executeQueryJDBC(query, conn);
if (results != null) { if (results != null) {
AnalysisLogger.getLogger().debug( AnalysisLogger.getLogger().debug(
"DatabaseManagement-> Query result retrieved"); "DatabaseManagement-> Query result retrieved");
@ -365,22 +366,66 @@ public class DatabaseManagement {
// to retrieve datatype columns of a table // to retrieve datatype columns of a table
List<String> DataTypeColumns = null; List<String> DataTypeColumns = null;
// // store table in a file // store table in a file and build the result Map
// String FileName = pathFile + "QueryResult.csv"; String FileName = pathFile + "QueryResult"+UUID.randomUUID()+".csv";
// // write the result in the file and in the map // write the result in the file and in the map
// AnalysisLogger.getLogger().debug( AnalysisLogger.getLogger().debug(
// "In DatabaseManagement->writing the result in the file: " "In DatabaseManagement-> writing the result in the file: "
// + FileName); + FileName);
// file = new File(FileName);
// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( try{
// file), "UTF-8")); file = new File(FileName);
// writeTableIntoFile(results, DataTypeColumns); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
file), "UTF-8"));
writeTableIntoFile(results, DataTypeColumns);
// //truncate the result list to 10000 rows
// if ((mapResult!=null)&&(mapResult.size()!=0)){
// //result size without header
// int mapSize = mapResult.size() - 1;
// AnalysisLogger.getLogger().debug(
// "DatabaseManagement-> size of the map without header obtained from the submit operation: "
// + mapSize);
// if (mapSize>10000){
// int numElemToDelete = mapSize - 10000;
// AnalysisLogger.getLogger().debug(
// "DatabaseManagement-> The result size is greater than 10000 rows. Rows number to delete: "
// + numElemToDelete);
// AnalysisLogger.getLogger().debug(
// "DatabaseManagement-> Starting to remove elements");
// int i=10000;
// if (numElemToDelete != 0){
// while ((i < mapSize)) {
// mapResult.remove(String.valueOf(i));
// i++;
//
// }
//
// AnalysisLogger.getLogger().debug(
// "DatabaseManagement-> size of the truncated map without header: "
// + (mapResult.size()-1));
// }
//
// }
// }
}catch (Throwable e) {
throw e;
}finally{
//close the file
if (out!=null){
out.close();
out = null;
AnalysisLogger.getLogger().debug(
"In DatabaseManagement-> File closed");
}
}
// build the Map of Result // build the Map of Result
buildMapResult(results, DataTypeColumns); // buildMapResult(results, DataTypeColumns);
return results; return results;
} }
/** /**
@ -902,6 +947,7 @@ public class DatabaseManagement {
// "DatabaseManagement->columns names: " + listKeys); // "DatabaseManagement->columns names: " + listKeys);
if (result != null && result.size() != 0) { if (result != null && result.size() != 0) {
// // write operation in the file // // write operation in the file
for (int i = 0; i < result.size(); i++) { for (int i = 0; i < result.size(); i++) {
String RowString = ""; String RowString = "";
@ -985,6 +1031,8 @@ public class DatabaseManagement {
// mapResult.put(Integer.valueOf(i), RowString); // mapResult.put(Integer.valueOf(i), RowString);
mapResult.put(String.valueOf(i), RowString); mapResult.put(String.valueOf(i), RowString);
} }
} }
} }
@ -1028,190 +1076,222 @@ public class DatabaseManagement {
// } // }
} }
} }
//print check
// AnalysisLogger.getLogger().debug(
// "mapResult size with header: " + mapResult.size());
} }
// // write the table result in the file and build the map of results // write the table result in the file and build the map of results
// private void writeTableIntoFile(List<Object> result, private void writeTableIntoFile(List<Object> result,
// List<String> DataTypeColumns) throws Exception { List<String> DataTypeColumns) throws Exception {
//
// // // file that will contain result // // file that will contain result
// // BufferedWriter out; // BufferedWriter out;
// // // String fileName; // // String fileName;
// // // fileName = "./cfg/" + "table.txt"; // // fileName = "./cfg/" + "table.txt";
// // // fileName = "./files/" + "table.txt"; // // fileName = "./files/" + "table.txt";
// // // fileName = // // fileName =
// // // // //
// // "/home/loredana/workspace/DatabasesResourcesManagerAlgorithms/cfg/" // "/home/loredana/workspace/DatabasesResourcesManagerAlgorithms/cfg/"
// // // + "SampleOnTable.txt"; // // + "SampleOnTable.txt";
// // // fileName = "./files/" + "SmartSampleOnTable.txt"; // // fileName = "./files/" + "SmartSampleOnTable.txt";
// // // File file = new File(fileName); // // File file = new File(fileName);
// // file = new File(FileName); // file = new File(FileName);
// // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
// // file), "UTF-8")); // file), "UTF-8"));
//
// // to get columns names and result
// // write headers in the file try{
// // to recover columns names // to get columns names and result
// if (header.equals("")) { // write headers in the file
// ArrayList<String> listKeys = new ArrayList<String>( // to recover columns names
// ((LinkedHashMap<String, Object>) (result.get(0))).keySet()); if (header.equals("")) {
// ArrayList<String> listKeys = new ArrayList<String>(
// for (int i = 0; i < listKeys.size(); i++) { ((LinkedHashMap<String, Object>) (result.get(0))).keySet());
// if (i != listKeys.size() - 1) {
// header = header + listKeys.get(i) + ", "; for (int i = 0; i < listKeys.size(); i++) {
// } else { if (i != listKeys.size() - 1) {
// header = header + listKeys.get(i); header = header + listKeys.get(i) + ", ";
// } } else {
// } header = header + listKeys.get(i);
// } }
// }
// // // print check }
// // AnalysisLogger.getLogger().debug(
// // "DatabaseManagement->HEADERS: " + header); // // print check
// // AnalysisLogger.getLogger().debug(
// out.write(header); // "DatabaseManagement->HEADERS: " + header);
// out.newLine();
// mapResult.put("HEADERS", header); out.write(header);
// out.newLine();
// // //print check values mapResult.put("HEADERS", header);
// // AnalysisLogger.getLogger().debug(
// // "DatabaseManagement->columns names: " + listKeys); // //print check values
// // AnalysisLogger.getLogger().debug(
// if (result != null && result.size() != 0) { // "DatabaseManagement->columns names: " + listKeys);
// // // write operation in the file
// for (int i = 0; i < result.size(); i++) { if (result != null && result.size() != 0) {
// String RowString = ""; // // write operation in the file
// Object element = result.get(i); for (int i = 0; i < result.size(); i++) {
// String RowString = "";
// // arraylist in which each element is a row result Object element = result.get(i);
// ArrayList<Object> listvalues = new ArrayList<Object>(
// ((LinkedHashMap<String, Object>) element).values()); // arraylist in which each element is a row result
// ArrayList<Object> listvalues = new ArrayList<Object>(
// // // print check ((LinkedHashMap<String, Object>) element).values());
// // AnalysisLogger.getLogger().debug(
// // "DatabaseManagement->values: " + listvalues); // // print check
// // AnalysisLogger.getLogger().debug(
// // each row could have several column values // "DatabaseManagement->values: " + listvalues);
// Object[] row = listvalues.toArray();
// if (row.length >= 1) { // each row could have several column values
// for (int j = 0; j < row.length; j++) { Object[] row = listvalues.toArray();
// if (row[j] == null) { if (row.length >= 1) {
// row[j] = ""; for (int j = 0; j < row.length; j++) {
// } if (row[j] == null) {
// // to parse the obtained results in order to align row[j] = "";
// // number }
// // values with those of postgres // to parse the obtained results in order to align
// String original = row[j].toString(); // number
// // values with those of postgres
// // // check value String original = row[j].toString();
// // AnalysisLogger.getLogger().debug(
// // "In DatabaseManagement->original value: " // // check value
// // + original); // AnalysisLogger.getLogger().debug(
// // "In DatabaseManagement->original value: "
// String parsed = "" + row[j]; // + original);
// if (original != "") {
// // convert database datatypes to Java datatypes String parsed = "" + row[j];
// if (DataTypeColumns == null if (original != "") {
// || DataTypeColumns.size() == 0) // convert database datatypes to Java datatypes
// parsed = convertToJavaType(row[j].getClass() if (DataTypeColumns == null
// .getName(), parsed); || DataTypeColumns.size() == 0)
// else parsed = convertToJavaType(row[j].getClass()
// parsed = convertToJavaType( .getName(), parsed);
// DataTypeColumns.get(j), parsed); else
// } parsed = convertToJavaType(
// DataTypeColumns.get(j), parsed);
// // // check value }
// // AnalysisLogger.getLogger().debug(
// // "In DatabaseManagement->row: " + (i + 1) // // check value
// // + " column: " + (j + 1) + " value= " // AnalysisLogger.getLogger().debug(
// // + parsed); // "In DatabaseManagement->row: " + (i + 1)
// // + " column: " + (j + 1) + " value= "
// // write in a file // + parsed);
// if (j != row.length - 1) {
// // out.write("\"" + parsed + "\""); // write in a file
// // out.write(","); if (j != row.length - 1) {
// out.write(parsed); // out.write("\"" + parsed + "\"");
// out.write(","); // out.write(",");
// out.write(parsed);
// // System.out.println("write column : " + j); out.write(",");
// // RowString = RowString + parsed + " ";
// if (j == 0) { // System.out.println("write column : " + j);
// RowString = parsed; // RowString = RowString + parsed + " ";
// } else { if (j == 0) {
// RowString = RowString + "," + parsed; RowString = parsed;
// } } else {
// } RowString = RowString + "," + parsed;
// if (j == row.length - 1) { }
// // out.write("\"" + parsed + "\""); }
// // out.newLine(); if (j == row.length - 1) {
// out.write(parsed); // out.write("\"" + parsed + "\"");
// out.newLine(); // out.newLine();
// out.write(parsed);
// // to add a row to the map out.newLine();
// if (row.length == 1) {
// RowString = parsed; // to add a row to the map
// } else { if (row.length == 1) {
// RowString = RowString + "," + parsed; RowString = parsed;
// } } else {
// RowString = RowString + "," + parsed;
// // to add a row to the map }
// // RowString = RowString + "," + parsed;
// // mapSampleTableResult.put(String.valueOf(i), // to add a row to the map
// // RowString); // RowString = RowString + "," + parsed;
// // mapSampleTableResult.put(String.valueOf(i),
// // // check value row // RowString);
// // AnalysisLogger.getLogger().debug(
// // "writing the value: " + RowString + " key: " // // check value row
// // + String.valueOf(i)); // AnalysisLogger.getLogger().debug(
// // "writing the value: " + RowString + " key: "
// // mapResult.put(Integer.valueOf(i), RowString); // + String.valueOf(i));
// mapResult.put(String.valueOf(i), RowString);
// } // mapResult.put(Integer.valueOf(i), RowString);
// }
// } //add in the map only the first 1000 rows if the result list size is greater than 1000
// // else if (result.size() == 1) { if (result.size()>1000){
// // if(i<1000){
// // // Object RowElement = (Object) result.get(0); mapResult.put(String.valueOf(i), RowString);
// // }
// // if (row[0] == null) {
// // row[0] = ""; }else{
// // } mapResult.put(String.valueOf(i), RowString);
// // }
// // // to parse the obtained results in order to align }
// // // number }
// // // values with those of postgres }
// // String original = row[0].toString(); // else if (result.size() == 1) {
// // //
// // // // check value // // Object RowElement = (Object) result.get(0);
// // // AnalysisLogger.getLogger().debug( //
// // // "In DatabaseManagement->original value: " // if (row[0] == null) {
// // // + original); // row[0] = "";
// // // }
// // String parsed = "" + row[0]; //
// // // // to parse the obtained results in order to align
// // if (original != "") { // // number
// // // convert database datatypes to Java datatypes // // values with those of postgres
// // if (DataTypeColumns == null // String original = row[0].toString();
// // || DataTypeColumns.size() == 0) //
// // parsed = convertToJavaType(row[0].getClass() // // // check value
// // .getName(), parsed); // // AnalysisLogger.getLogger().debug(
// // else // // "In DatabaseManagement->original value: "
// // parsed = convertToJavaType(DataTypeColumns.get(0), // // + original);
// // parsed); //
// // } // String parsed = "" + row[0];
// // //
// // out.write(row[0].toString()); // if (original != "") {
// // out.newLine(); // // convert database datatypes to Java datatypes
// // // if (DataTypeColumns == null
// // // to add a row to the map // || DataTypeColumns.size() == 0)
// // mapResult.put(String.valueOf(i), row[0].toString()); // parsed = convertToJavaType(row[0].getClass()
// // // .getName(), parsed);
// // } // else
// } // parsed = convertToJavaType(DataTypeColumns.get(0),
// } // parsed);
// // close the file // }
// out.close(); //
// } // out.write(row[0].toString());
// out.newLine();
//
// // to add a row to the map
// mapResult.put(String.valueOf(i), row[0].toString());
//
// }
}
}
AnalysisLogger.getLogger().debug(
"In DatabaseManagement-> map size without header: " + (mapResult.size()-1));
AnalysisLogger.getLogger().debug(
"In DatabaseManagement-> Writing File and Result Map creation operations terminated");
}catch (Throwable e) {
throw e;
}finally{
// close the file
if (out!=null){
out.close();
out=null;
AnalysisLogger.getLogger().debug(
"In DatabaseManagement-> File closed");
}
}
}
// to retrieve datatype columns of a table // to retrieve datatype columns of a table
private List<String> getDataTypeColumns(String tableName, String schemaName) private List<String> getDataTypeColumns(String tableName, String schemaName)
@ -1395,14 +1475,14 @@ public class DatabaseManagement {
} }
// // to return the file in which the query result (originated from a submit // to return the file in which the query result (originated from a submit
// // query) is stored // query) is stored
// public File getFileQueryResult() { public File getFileQueryResult() {
//
// // return fileQueryResult; // return fileQueryResult;
// return file; return file;
//
// } }
// // to return the file in which the table result (originated from a submit // // to return the file in which the table result (originated from a submit
// // query and sample operations) is stored // // query and sample operations) is stored