From c57e373d222c0c1bfd0cc1fbf64d5c75e585421c Mon Sep 17 00:00:00 2001 From: Loredana Liccardo Date: Tue, 29 Jul 2014 09:22:18 +0000 Subject: [PATCH] classes SubmitQuery, SampleOnTable, SmartSampleOnTable, RandomSampleOnTable modified in order to not make available the file as result. Further some not useful comments on code have been removed. git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManagerAlgorithms@99053 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../drmalgorithms/RandomSampleOnTable.java | 215 +---------- .../drmalgorithms/SampleOnTable.java | 232 +----------- .../drmalgorithms/SmartSampleOnTable.java | 220 +---------- .../algorithms/drmalgorithms/SubmitQuery.java | 350 ++++-------------- 4 files changed, 90 insertions(+), 927 deletions(-) diff --git a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/RandomSampleOnTable.java b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/RandomSampleOnTable.java index ebc4725..171532d 100644 --- a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/RandomSampleOnTable.java +++ b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/RandomSampleOnTable.java @@ -33,11 +33,6 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { // object that allows to manage some operations on a database private DatabaseManagement mgt; - - // file in which information is written - // private BufferedWriter out; - // private String fileName; - // database's parameters specified by the user private String resourceName = null; private String databaseName = null; @@ -45,7 +40,6 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { private String tableName = null; private SessionFactory sf; - // variable that keeps track of the driver information private String driverInfo; @@ -53,30 +47,22 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { public void init() throws Exception { mgt = new DatabaseManagement(config.getConfigPath()); - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable->Initialization"); String scope = config.getGcubeScope(); - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable->scope set by config object: " + scope); if (scope == null || scope.length() == 0) { - scope = ScopeProvider.instance.get(); - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable->scope set by ScopeProvider: " + scope); - } else { - ScopeProvider.instance.set(scope); - } - } @Override @@ -90,36 +76,20 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { "In RandomSmartSampleOnTable->Processing"); try { - - // // file that will contain the result - // String property = "java.io.tmpdir"; - // String tempDir = System.getProperty(property); - // tempDir = tempDir + "/"; - // fileName = "./cfg/" + "RandomSampleOnTable.txt"; - // out = new BufferedWriter(new FileWriter(fileName)); - // retrieve information List Info = retrieveInfo(); // check on table name field tableName = getInputParameter("TableName").trim(); - if ((tableName == null) || (tableName.equals(""))) { - throw new Exception("Warning: insert the table name"); - } // check on schema name field - if (driverInfo.toLowerCase().contains("postgres")) { - schemaName = getInputParameter("SchemaName").trim(); - if ((schemaName == null) || (schemaName.equals(""))) { - throw new Exception("Warning: insert the schema name"); - } } @@ -132,7 +102,6 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { // close the connection sf.close(); } catch (HibernateException h) { - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable-> ERROR " + h.getMessage()); throw h; @@ -140,41 +109,34 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { catch (IllegalStateException e) { // e.printStackTrace(); - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable-> ERROR " + e.getMessage()); throw e; } catch (DiscoveryException e1) { // e1.printStackTrace(); - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable-> ERROR " + e1.getMessage()); throw e1; } catch (InvalidResultException e2) { // e2.printStackTrace(); - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable-> ERROR " + e2.getMessage()); throw e2; } catch (IOException e3) { // e3.printStackTrace(); - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable-> ERROR " + e3.getMessage()); - throw e3; } catch (Exception e4) { // e4.printStackTrace(); - AnalysisLogger.getLogger().debug( "In RandomSmartSampleOnTable-> Exception " + e4.getMessage()); - throw e4; } finally { @@ -182,39 +144,31 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { if (sf.isClosed() == false) { mgt.closeConnection(); } - } - } @Override protected void setInputParameters() { // parameters specified by the user - addStringInput("ResourceName", "The name of the resource", ""); addStringInput("DatabaseName", "The name of the database", ""); addStringInput("SchemaName", "The name of the schema", ""); addStringInput("TableName", "The name of the table", ""); - } public StatisticalType getOutput() { AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->retrieving outputs"); - // generate a primitive type for the collection PrimitiveType output = new PrimitiveType(LinkedHashMap.class.getName(), map, PrimitiveTypes.MAP, "ResultsMap", "Results Map"); - return output; - } @Override public void shutdown() { AnalysisLogger.getLogger().debug("In RandomSampleOnTable->Shutdown"); - } // Method that recovers the info useful for the connection @@ -222,26 +176,18 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { IllegalStateException, DiscoveryException, InvalidResultException { resourceName = getInputParameter("ResourceName").trim(); - if ((resourceName == null) || (resourceName.equals(""))) { - throw new Exception("Warning: insert the resource name"); - } databaseName = getInputParameter("DatabaseName").trim(); - if ((databaseName == null) || (databaseName.equals(""))) { - throw new Exception("Warning: insert the database name"); - } // retrieve the chosen resource DatabasesDiscoverer discovery = new DatabasesDiscoverer(); - List resources = discovery.discover(); - AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->number of elements: " + resources.size()); @@ -250,58 +196,17 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { List info = new ArrayList(); check: for (int i = 0; i < resources.size(); i++) { - if (resources.get(i).getResourceName().toLowerCase() .equals(resourceName.toLowerCase())) { - normalizeDBInfo(resources.get(i)); - for (int j = 0; j < resources.get(i).getAccessPoints().size(); j++) { - // if (resources.get(i).getAccessPoints().get(j) - // .getDatabaseName().equals(databaseName)) { - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getUsername()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getPassword()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getDriver()); - // - // // driverInfo = - // // resources.get(i).getAccessPoints().get(j) - // // .getDriver(); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getDialect()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .address()); - // - // info.add(databaseName); - // - // break check; - // - // } - - // if (resources.get(i).getAccessPoints().get(j) - // .address().equals(url)){ - // - // System.out.println("url selezionato"); - // - // - // - // } - if (resources.get(i).getAccessPoints().get(j) .getDatabaseName().toLowerCase() .equals(databaseName.toLowerCase())) { info.add(resources.get(i).getAccessPoints().get(j) .getUsername()); - AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->username: " + resources.get(i).getAccessPoints() @@ -309,7 +214,6 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getPassword()); - AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->password: " + resources.get(i).getAccessPoints() @@ -317,7 +221,6 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getDriver()); - driverInfo = resources.get(i).getAccessPoints().get(j) .getDriver(); @@ -332,7 +235,6 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getDialect()); - AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->dialect: " + resources.get(i).getAccessPoints() @@ -340,58 +242,41 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .address()); - AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->url: " + resources.get(i).getAccessPoints() .get(j).address()); info.add(databaseName); - AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->databasename: " + resources.get(i).getAccessPoints() .get(j).getDatabaseName()); - break check; - } - } - } - } AnalysisLogger .getLogger() .debug("In RandomSampleOnTable->information useful for connection: retrieved"); - return info; - } // to normalize the information related to a database private void normalizeDBInfo(DBResource resource) throws IOException { - int ap = resource.getAccessPoints().size(); - for (int i = 0; i < ap; i++) { - try { resource.normalize(i); } catch (IOException e) { - // e.printStackTrace(); - AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->: Error in normalization process" + e.getMessage()); - throw e; - } - } } @@ -425,7 +310,6 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { throws Exception { LinkedHashMap mapResults = new LinkedHashMap(); - AnalysisLogger .getLogger() .debug("In RandomSampleOnTable->starting the sample operation on table randomly"); @@ -434,13 +318,11 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { // List resultSet = null; if (driverInfo.toLowerCase().contains("postgres")) { - // for a postgres database the second parameter is the schema name // resultSet = mgt.randomSampleOnTable(tableName, schemaName, // config.getPersistencePath()); mgt.randomSampleOnTable(tableName, schemaName, config.getPersistencePath()); - } if (driverInfo.toLowerCase().contains("mysql")) { @@ -449,101 +331,18 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { // config.getPersistencePath()); mgt.randomSampleOnTable(tableName, databaseName, config.getPersistencePath()); - } AnalysisLogger.getLogger().debug( "In RandomSampleOnTable->result retrieved"); - // // processing the information and adding it to the map - // - // for (int i = 0; i < resultSet.size(); i++) { - // - // if (resultSet.get(i).getClass().isArray()) { - // - // Object[] row = (Object[]) resultSet.get(i); - // - // String RowString = ""; - // - // for (int j = 0; j < row.length; j++) { - // - // AnalysisLogger.getLogger().debug( - // "In RandomSampleOnTable->row: " + (i + 1) - // + " column: " + (j + 1) + " value= " - // + row[j].toString()); - // // System.out.print("\"" + row[j] + "\"; "); - // - // // write in a file - // - // if (j != row.length - 1) { - // - // out.write(row[j].toString()); - // out.write(","); - // RowString = RowString + row[j].toString() + " "; - // - // // AnalysisLogger.getLogger().debug( - // // "In SubmitQuery->row " + (j+1) + ": " + - // // row[j].toString()); - // - // } - // if (j == row.length - 1) { - // - // out.write(row[j].toString()); - // out.newLine(); - // - // RowString = RowString + row[j].toString(); - // - // PrimitiveType valRow = new PrimitiveType( - // String.class.getName(), RowString, - // PrimitiveTypes.STRING, "Row", "Row"); - // - // mapResults.put(String.valueOf(i), valRow); - // - // } - // - // } - // - // } else { - // // String RowElement=""; - // - // Object RowElement = (Object) resultSet.get(i); - // - // // AnalysisLogger.getLogger().debug( - // // "In SubmitQuery->Query's Result: "); - // - // AnalysisLogger.getLogger().debug( - // "In RandomSampleOnTable->row: " + (i + 1) + " value= " - // + RowElement.toString()); - // - // // write in a file - // - // out.write(RowElement.toString()); - // out.newLine(); - // - // } - // - // } - // // adding the file - // PrimitiveType valfile = new PrimitiveType(File.class.getName(), - // new File(fileName), PrimitiveTypes.FILE, "ResultQuery", - // "ResultQuery"); - // - // mapResults.put("ResultQuery", valfile); - // - // // close the file - // out.close(); - // - // return mapResults; - // to add the results to the variable map - // to add the map HashMap mapResult = new HashMap(); mapResult = mgt.getMapSampleTableResult(); String encoded = null; - encoded = new String(mapResult.get("HEADERS").getBytes(), "UTF-8"); // // check the encoded value @@ -552,11 +351,9 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { PrimitiveType val = new PrimitiveType(String.class.getName(), encoded, PrimitiveTypes.STRING, "Row", "Row"); - mapResults.put("HEADERS", val); for (int i = 0; i < mapResult.size() - 1; i++) { - encoded = new String(mapResult.get(String.valueOf(i)).getBytes(), "UTF-8"); @@ -566,23 +363,19 @@ public class RandomSampleOnTable extends StandardLocalExternalAlgorithm { PrimitiveType val1 = new PrimitiveType(String.class.getName(), encoded, PrimitiveTypes.STRING, "Row", "Row"); - mapResults.put(String.valueOf(i), val1); // //check value contained in map // String value = (String) val1.getContent(); // // AnalysisLogger.getLogger().debug( // "In RandomSampleOnTable->value: " + value); - } // to add the file - - PrimitiveType fileResult = new PrimitiveType(File.class.getName(), - mgt.getFileSampleTableResult(), PrimitiveTypes.FILE, "File", - "File"); - - mapResults.put("File", fileResult); +// PrimitiveType fileResult = new PrimitiveType(File.class.getName(), +// mgt.getFileSampleTableResult(), PrimitiveTypes.FILE, "File", +// "File"); +// mapResults.put("File", fileResult); return mapResults; diff --git a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SampleOnTable.java b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SampleOnTable.java index 37482c1..4a902b5 100644 --- a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SampleOnTable.java +++ b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SampleOnTable.java @@ -35,11 +35,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { // object that allows to manage some operations on a database private DatabaseManagement mgt; - - // file in which information is written - // private BufferedWriter out; - // private String fileName; - // database's parameters specified by the user private String resourceName = null; private String databaseName = null; @@ -47,7 +42,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { private String tableName = null; private SessionFactory sf; - // variable that keeps track of the driver information private String driverInfo; @@ -55,27 +49,19 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { public void init() throws Exception { mgt = new DatabaseManagement(config.getConfigPath()); - AnalysisLogger.getLogger().debug("In SampleOnTable->Initialization"); String scope = config.getGcubeScope(); - AnalysisLogger.getLogger().debug( "In SampleOnTable->scope set by config object: " + scope); if (scope == null || scope.length() == 0) { - scope = ScopeProvider.instance.get(); - AnalysisLogger.getLogger().debug( "In SampleOnTable->scope set by ScopeProvider: " + scope); - } else { - ScopeProvider.instance.set(scope); - } - } @Override @@ -88,36 +74,20 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { AnalysisLogger.getLogger().debug("In SampleOnTable->Processing"); try { - - // // file that will contain the result - // String property = "java.io.tmpdir"; - // String tempDir = System.getProperty(property); - // tempDir = tempDir + "/"; - // fileName = "./cfg/" + "SampleOnTable.txt"; - // out = new BufferedWriter(new FileWriter(fileName)); - // retrieve information List Info = retrieveInfo(); // check on table name field tableName = getInputParameter("TableName").trim(); - if ((tableName == null) || (tableName.equals(""))) { - throw new Exception("Warning: insert the table name"); - } // check on schema name field - if (driverInfo.toLowerCase().contains("postgres")) { - schemaName = getInputParameter("SchemaName").trim(); - if ((schemaName == null) || (schemaName.equals(""))) { - throw new Exception("Warning: insert the schema name"); - } } @@ -131,7 +101,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { sf.close(); } catch (HibernateException h) { - AnalysisLogger.getLogger().debug( "In SampleOnTable-> ERROR " + h.getMessage()); throw h; @@ -139,40 +108,33 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { catch (IllegalStateException e) { // e.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SampleOnTable-> ERROR " + e.getMessage()); throw e; } catch (DiscoveryException e1) { // e1.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SampleOnTable-> ERROR " + e1.getMessage()); throw e1; } catch (InvalidResultException e2) { // e2.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SampleOnTable-> ERROR " + e2.getMessage()); throw e2; } catch (IOException e3) { // e3.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SampleOnTable-> ERROR " + e3.getMessage()); - throw e3; } catch (Exception e4) { // e4.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SampleOnTable-> Exception " + e4.getMessage()); - throw e4; } finally { @@ -180,9 +142,7 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { if (sf.isClosed() == false) { mgt.closeConnection(); } - } - } @Override @@ -192,7 +152,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { addStringInput("DatabaseName", "The name of the database", ""); addStringInput("SchemaName", "The name of the schema", ""); addStringInput("TableName", "The name of the table", ""); - } public StatisticalType getOutput() { @@ -204,29 +163,7 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { PrimitiveType output = new PrimitiveType(LinkedHashMap.class.getName(), map, PrimitiveTypes.MAP, "ResultsMap", "Results Map"); - // LinkedHashMap result = (LinkedHashMap) output.getContent(); - // - // for (int i=0; i<100;i++){ - // - // PrimitiveType obj = result.get(String.valueOf(i)); - // - // String val = (String) obj.getContent(); - // - // try { - // String decoded = URLDecoder.decode(val, "UTF-8"); - // - // AnalysisLogger.getLogger().debug("value decoded: " + decoded); - // - // } catch (UnsupportedEncodingException e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } - // - // } - return output; - } @Override @@ -240,24 +177,17 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { IllegalStateException, DiscoveryException, InvalidResultException { resourceName = getInputParameter("ResourceName").trim(); - if ((resourceName == null) || (resourceName.equals(""))) { - throw new Exception("Warning: insert the resource name"); - } databaseName = getInputParameter("DatabaseName").trim(); - if ((databaseName == null) || (databaseName.equals(""))) { - throw new Exception("Warning: insert the database name"); - } // retrieve the chosen resource DatabasesDiscoverer discovery = new DatabasesDiscoverer(); - List resources = discovery.discover(); AnalysisLogger.getLogger().debug( @@ -267,58 +197,17 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { List info = new ArrayList(); check: for (int i = 0; i < resources.size(); i++) { - if (resources.get(i).getResourceName().toLowerCase() .equals(resourceName.toLowerCase())) { - normalizeDBInfo(resources.get(i)); - for (int j = 0; j < resources.get(i).getAccessPoints().size(); j++) { - // if (resources.get(i).getAccessPoints().get(j) - // .getDatabaseName().equals(databaseName)) { - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getUsername()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getPassword()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getDriver()); - // - // // driverInfo = - // // resources.get(i).getAccessPoints().get(j) - // // .getDriver(); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getDialect()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .address()); - // - // info.add(databaseName); - // - // break check; - // - // } - - // if (resources.get(i).getAccessPoints().get(j) - // .address().equals(url)){ - // - // System.out.println("url selezionato"); - // - // - // - // } - if (resources.get(i).getAccessPoints().get(j) .getDatabaseName().toLowerCase() .equals(databaseName.toLowerCase())) { info.add(resources.get(i).getAccessPoints().get(j) .getUsername()); - AnalysisLogger.getLogger().debug( "In SampleOnTable->username: " + resources.get(i).getAccessPoints() @@ -326,7 +215,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getPassword()); - AnalysisLogger.getLogger().debug( "In SampleOnTable->password: " + resources.get(i).getAccessPoints() @@ -334,10 +222,8 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getDriver()); - driverInfo = resources.get(i).getAccessPoints().get(j) .getDriver(); - AnalysisLogger.getLogger().debug( "In SampleOnTable->driver: " + resources.get(i).getAccessPoints() @@ -349,7 +235,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getDialect()); - AnalysisLogger.getLogger().debug( "In SampleOnTable->dialect: " + resources.get(i).getAccessPoints() @@ -357,27 +242,20 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .address()); - AnalysisLogger.getLogger().debug( "In SampleOnTable->url: " + resources.get(i).getAccessPoints() .get(j).address()); info.add(databaseName); - AnalysisLogger.getLogger().debug( "In SampleOnTable->databasename: " + resources.get(i).getAccessPoints() .get(j).getDatabaseName()); - break check; - } - } - } - } AnalysisLogger @@ -385,29 +263,21 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { .debug("In SampleOnTable->information useful for connection: retrieved"); return info; - } // to normalize the information related to a database private void normalizeDBInfo(DBResource resource) throws IOException { - int ap = resource.getAccessPoints().size(); - for (int i = 0; i < ap; i++) { - try { resource.normalize(i); } catch (IOException e) { // e.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SampleOnTable->: Error in normalization process" + e.getMessage()); - throw e; - } - } } @@ -432,7 +302,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { "In SampleOnTable->database " + DatabaseName + ": connected"); return sf; - } // to perform the sample operation on the table @@ -440,7 +309,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { throws Exception { LinkedHashMap mapResults = new LinkedHashMap(); - AnalysisLogger.getLogger().debug( "In SampleOnTable->starting the sample operation on table"); @@ -448,113 +316,30 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { // List resultSet = null; if (driverInfo.toLowerCase().contains("postgres")) { - // for a postgres database the second parameter is the schema name // resultSet = mgt.sampleOnTable(tableName, schemaName, // config.getPersistencePath()); - mgt.sampleOnTable(tableName, schemaName, config.getPersistencePath()); - } if (driverInfo.toLowerCase().contains("mysql")) { // for a mysql database the second parameter is the database name // resultSet = mgt.sampleOnTable(tableName, databaseName, // config.getPersistencePath()); - mgt.sampleOnTable(tableName, databaseName, config.getPersistencePath()); - } AnalysisLogger.getLogger().debug("In SampleOnTable->result retrieved"); - // //processing the information and adding it to the map - // - // for (int i = 0; i < resultSet.size(); i++) { - // - // if (resultSet.get(i).getClass().isArray()) { - // - // Object[] row = (Object[]) resultSet.get(i); - // - // String RowString = ""; - // - // for (int j = 0; j < row.length; j++) { - // - // AnalysisLogger.getLogger().debug( - // "In SampleOnTable->row: " + (i + 1) + " column: " - // + (j + 1) + " value= " + row[j].toString()); - // // System.out.print("\"" + row[j] + "\"; "); - // - // // write in a file - // - // if (j != row.length - 1) { - // - // out.write(row[j].toString()); - // out.write(","); - // RowString = RowString + row[j].toString() + " "; - // - // // AnalysisLogger.getLogger().debug( - // // "In SubmitQuery->row " + (j+1) + ": " + - // // row[j].toString()); - // - // } - // if (j == row.length - 1) { - // - // out.write(row[j].toString()); - // out.newLine(); - // - // RowString = RowString + row[j].toString(); - // - // PrimitiveType valRow = new PrimitiveType( - // String.class.getName(), RowString, - // PrimitiveTypes.STRING, "Row", "Row"); - // - // mapResults.put(String.valueOf(i), valRow); - // - // } - // - // } - // - // } else { - // // String RowElement=""; - // - // Object RowElement = (Object) resultSet.get(i); - // - // // AnalysisLogger.getLogger().debug( - // // "In SubmitQuery->Query's Result: "); - // - // AnalysisLogger.getLogger().debug( - // "In SampleOnTable->row: " + (i + 1) + " value= " - // + RowElement.toString()); - // - // // write in a file - // - // out.write(RowElement.toString()); - // out.newLine(); - // - // } - // - // } - // adding the file - // PrimitiveType valfile = new PrimitiveType(File.class.getName(), - // new File(fileName), PrimitiveTypes.FILE, "ResultQuery", - // "ResultQuery"); - - // // close the file - // out.close(); - // to add the results to the variable map - // to add the map - - // HashMap mapResult = new HashMap(); + HashMap mapResult = new HashMap(); mapResult = mgt.getMapSampleTableResult(); String encoded = null; - encoded = new String(mapResult.get("HEADERS").getBytes(), "UTF-8"); // // check encoded value @@ -564,11 +349,9 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { PrimitiveType val = new PrimitiveType(String.class.getName(), encoded, PrimitiveTypes.STRING, "Row", "Row"); - mapResults.put("HEADERS", val); for (int i = 0; i < mapResult.size() - 1; i++) { - encoded = new String(mapResult.get(String.valueOf(i)).getBytes(), "UTF-8"); @@ -578,7 +361,6 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { PrimitiveType val1 = new PrimitiveType(String.class.getName(), encoded, PrimitiveTypes.STRING, "Row", "Row"); - mapResults.put(String.valueOf(i), val1); // //check value contained in the map // String value = (String) val1.getContent(); @@ -588,13 +370,11 @@ public class SampleOnTable extends StandardLocalExternalAlgorithm { } - // to add the file - - PrimitiveType fileResult = new PrimitiveType(File.class.getName(), - mgt.getFileSampleTableResult(), PrimitiveTypes.FILE, "File", - "File"); - - mapResults.put("File", fileResult); +// // to add the file +// PrimitiveType fileResult = new PrimitiveType(File.class.getName(), +// mgt.getFileSampleTableResult(), PrimitiveTypes.FILE, "File", +// "File"); +// mapResults.put("File", fileResult); return mapResults; diff --git a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SmartSampleOnTable.java b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SmartSampleOnTable.java index 7c22bc8..ea412cb 100644 --- a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SmartSampleOnTable.java +++ b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SmartSampleOnTable.java @@ -34,11 +34,6 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { // object that allows to manage some operations on a database private DatabaseManagement mgt; - - // file in which information is written - // private BufferedWriter out; - // private String fileName; - // database's parameters specified by the user private String resourceName = null; private String databaseName = null; @@ -46,7 +41,6 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { private String tableName = null; private SessionFactory sf; - // variable that keeps track of the driver information private String driverInfo; @@ -54,73 +48,48 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { public void init() throws Exception { mgt = new DatabaseManagement(config.getConfigPath()); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->Initialization"); String scope = config.getGcubeScope(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->scope set by config object: " + scope); if (scope == null || scope.length() == 0) { - scope = ScopeProvider.instance.get(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->scope set by ScopeProvider: " + scope); - } else { - ScopeProvider.instance.set(scope); - } - } @Override public String getDescription() { - return "Algorithm that allows to perform a smart sample operation on a table"; - } @Override protected void process() throws Exception { - AnalysisLogger.getLogger().debug("In SmartSampleOnTable->Processing"); try { - // // file that will contain the result - // String property = "java.io.tmpdir"; - // String tempDir = System.getProperty(property); - // tempDir = tempDir + "/"; - // fileName = "./cfg/" + "SmartSampleOnTable.txt"; - // out = new BufferedWriter(new FileWriter(fileName)); - + // retrieve information List Info = retrieveInfo(); // check on table name field tableName = getInputParameter("TableName").trim(); - if ((tableName == null) || (tableName.equals(""))) { - throw new Exception("Warning: insert the table name"); - } // check on schema name field - if (driverInfo.toLowerCase().contains("postgres")) { - schemaName = getInputParameter("SchemaName").trim(); - if ((schemaName == null) || (schemaName.equals(""))) { - throw new Exception("Warning: insert the schema name"); - } } @@ -133,7 +102,6 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { // close the connection sf.close(); } catch (HibernateException h) { - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable-> ERROR " + h.getMessage()); throw h; @@ -141,40 +109,33 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { catch (IllegalStateException e) { // e.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable-> ERROR " + e.getMessage()); throw e; } catch (DiscoveryException e1) { // e1.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable-> ERROR " + e1.getMessage()); throw e1; } catch (InvalidResultException e2) { // e2.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable-> ERROR " + e2.getMessage()); throw e2; } catch (IOException e3) { // e3.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable-> ERROR " + e3.getMessage()); - throw e3; } catch (Exception e4) { // e4.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable-> Exception " + e4.getMessage()); - throw e4; } finally { @@ -182,9 +143,7 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { if (sf.isClosed() == false) { mgt.closeConnection(); } - } - } @Override @@ -194,26 +153,21 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { addStringInput("DatabaseName", "The name of the database", ""); addStringInput("SchemaName", "The name of the schema", ""); addStringInput("TableName", "The name of the table", ""); - } public StatisticalType getOutput() { - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->retrieving outputs"); // generate a primitive type for the collection PrimitiveType output = new PrimitiveType(LinkedHashMap.class.getName(), map, PrimitiveTypes.MAP, "ResultsMap", "Results Map"); - return output; - } @Override public void shutdown() { AnalysisLogger.getLogger().debug("In SmartSampleOnTable->Shutdown"); - } // Method that recovers the info useful for the connection @@ -221,24 +175,17 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { IllegalStateException, DiscoveryException, InvalidResultException { resourceName = getInputParameter("ResourceName").trim(); - if ((resourceName == null) || (resourceName.equals(""))) { - throw new Exception("Warning: insert the resource name"); - } databaseName = getInputParameter("DatabaseName").trim(); - if ((databaseName == null) || (databaseName.equals(""))) { - throw new Exception("Warning: insert the database name"); - } // retrieve the chosen resource DatabasesDiscoverer discovery = new DatabasesDiscoverer(); - List resources = discovery.discover(); AnalysisLogger.getLogger().debug( @@ -247,7 +194,7 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { // list that contains information useful for the connection List info = new ArrayList(); - + check: for (int i = 0; i < resources.size(); i++) { if (resources.get(i).getResourceName().toLowerCase() @@ -257,50 +204,12 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { for (int j = 0; j < resources.get(i).getAccessPoints().size(); j++) { - // if (resources.get(i).getAccessPoints().get(j) - // .getDatabaseName().equals(databaseName)) { - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getUsername()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getPassword()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getDriver()); - // - // // driverInfo = - // // resources.get(i).getAccessPoints().get(j) - // // .getDriver(); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getDialect()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .address()); - // - // info.add(databaseName); - // - // break check; - // - // } - - // if (resources.get(i).getAccessPoints().get(j) - // .address().equals(url)){ - // - // System.out.println("url selezionato"); - // - // - // - // } - if (resources.get(i).getAccessPoints().get(j) .getDatabaseName().toLowerCase() .equals(databaseName.toLowerCase())) { info.add(resources.get(i).getAccessPoints().get(j) .getUsername()); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->username: " + resources.get(i).getAccessPoints() @@ -308,7 +217,6 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getPassword()); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->password: " + resources.get(i).getAccessPoints() @@ -316,10 +224,8 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getDriver()); - driverInfo = resources.get(i).getAccessPoints().get(j) .getDriver(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->driver: " + resources.get(i).getAccessPoints() @@ -331,7 +237,6 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getDialect()); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->dialect: " + resources.get(i).getAccessPoints() @@ -339,58 +244,41 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .address()); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->url: " + resources.get(i).getAccessPoints() .get(j).address()); info.add(databaseName); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->databasename: " + resources.get(i).getAccessPoints() .get(j).getDatabaseName()); - break check; - } - } - } - } AnalysisLogger .getLogger() .debug("In SmartSampleOnTable->information useful for connection: retrieved"); - return info; - } // to normalize the information related to a database private void normalizeDBInfo(DBResource resource) throws IOException { - int ap = resource.getAccessPoints().size(); - for (int i = 0; i < ap; i++) { - try { resource.normalize(i); } catch (IOException e) { - // e.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->: Error in normalization process" + e.getMessage()); - throw e; - } - } } @@ -414,9 +302,7 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->database " + DatabaseName + ": connected"); - return sf; - } // to perform the sample operation on the table @@ -433,14 +319,11 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { // List resultSet = null; if (driverInfo.toLowerCase().contains("postgres")) { - // for a postgres database the second parameter is the schema name // resultSet = mgt.smartSampleOnTable(tableName, schemaName, // config.getPersistencePath()); - mgt.smartSampleOnTable(tableName, schemaName, config.getPersistencePath()); - } if (driverInfo.toLowerCase().contains("mysql")) { @@ -449,101 +332,17 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { // config.getPersistencePath()); mgt.smartSampleOnTable(tableName, databaseName, config.getPersistencePath()); - } AnalysisLogger.getLogger().debug( "In SmartSampleOnTable->result retrieved"); - // // processing the information and adding it to the map - // - // for (int i = 0; i < resultSet.size(); i++) { - // - // if (resultSet.get(i).getClass().isArray()) { - // - // Object[] row = (Object[]) resultSet.get(i); - // - // String RowString = ""; - // - // for (int j = 0; j < row.length; j++) { - // - // AnalysisLogger.getLogger().debug( - // "In SmartSampleOnTable->row: " + (i + 1) - // + " column: " + (j + 1) + " value= " - // + row[j].toString()); - // // System.out.print("\"" + row[j] + "\"; "); - // - // // write in a file - // - // if (j != row.length - 1) { - // - // out.write(row[j].toString()); - // out.write(","); - // RowString = RowString + row[j].toString() + " "; - // - // // AnalysisLogger.getLogger().debug( - // // "In SubmitQuery->row " + (j+1) + ": " + - // // row[j].toString()); - // - // } - // if (j == row.length - 1) { - // - // out.write(row[j].toString()); - // out.newLine(); - // - // RowString = RowString + row[j].toString(); - // - // PrimitiveType valRow = new PrimitiveType( - // String.class.getName(), RowString, - // PrimitiveTypes.STRING, "Row", "Row"); - // - // mapResults.put(String.valueOf(i), valRow); - // - // } - // - // } - // - // } else { - // // String RowElement=""; - // - // Object RowElement = (Object) resultSet.get(i); - // - // // AnalysisLogger.getLogger().debug( - // // "In SubmitQuery->Query's Result: "); - // - // AnalysisLogger.getLogger().debug( - // "In SmartSampleOnTable->row: " + (i + 1) + " value= " - // + RowElement.toString()); - // - // // write in a file - // - // out.write(RowElement.toString()); - // out.newLine(); - // - // } - // - // } - // // adding the file - // PrimitiveType valfile = new PrimitiveType(File.class.getName(), - // new File(fileName), PrimitiveTypes.FILE, "ResultQuery", - // "ResultQuery"); - // - // mapResults.put("ResultQuery", valfile); - // - // // close the file - // out.close(); - // - // return mapResults; - // to add the results to the variable map - // to add the map - HashMap mapResult = new HashMap(); mapResult = mgt.getMapSampleTableResult(); String encoded = null; - encoded = new String(mapResult.get("HEADERS").getBytes(), "UTF-8"); // //check encoded value @@ -552,11 +351,9 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { PrimitiveType val = new PrimitiveType(String.class.getName(), encoded, PrimitiveTypes.STRING, "Row", "Row"); - mapResults.put("HEADERS", val); for (int i = 0; i < mapResult.size() - 1; i++) { - encoded = new String(mapResult.get(String.valueOf(i)).getBytes(), "UTF-8"); @@ -566,7 +363,6 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { PrimitiveType val1 = new PrimitiveType(String.class.getName(), encoded, PrimitiveTypes.STRING, "Row", "Row"); - mapResults.put(String.valueOf(i), val1); // //check value // String value = (String) val1.getContent(); @@ -577,15 +373,11 @@ public class SmartSampleOnTable extends StandardLocalExternalAlgorithm { } // to add the file - - PrimitiveType fileResult = new PrimitiveType(File.class.getName(), - mgt.getFileSampleTableResult(), PrimitiveTypes.FILE, "File", - "File"); - - mapResults.put("File", fileResult); +// PrimitiveType fileResult = new PrimitiveType(File.class.getName(), +// mgt.getFileSampleTableResult(), PrimitiveTypes.FILE, "File", +// "File"); +// mapResults.put("File", fileResult); return mapResults; - } - } diff --git a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SubmitQuery.java b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SubmitQuery.java index f7bac7a..e019817 100644 --- a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SubmitQuery.java +++ b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SubmitQuery.java @@ -39,12 +39,7 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // variable that keeps track of the database's type private String driverInfo; - // file that will contain query's result - // private BufferedWriter out; - // private String fileName; - private SessionFactory sf; - // database's parameters specified by the user private String resourceName = null; private String databaseName = null; @@ -55,11 +50,10 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { private String valueReadOnly = "Read-Only Query"; private String smartCorrection = "Apply Smart Correction"; private String dialect = "Language"; - - String valueRO; - String valueSC; - String valueDialect=""; - + + private String valueRO; + private String valueSC; + private String valueDialect = ""; // variable used to filter the disallowed queries private boolean NotAllowedQuery = false; @@ -68,30 +62,22 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { public void init() throws Exception { mgt = new DatabaseManagement(config.getConfigPath()); - AnalysisLogger.getLogger().debug("In SubmitQuery->Initialization"); String scope = config.getGcubeScope(); - AnalysisLogger.getLogger().debug( "In SubmitQuery->scope set by config object: " + scope); if (scope == null || scope.length() == 0) { - scope = ScopeProvider.instance.get(); - AnalysisLogger.getLogger().debug( "In SubmitQuery->scope set by ScopeProvider: " + scope); - } else { - ScopeProvider.instance.set(scope); - } valueRO = config.getParam(valueReadOnly); valueSC = config.getParam(smartCorrection); - } @Override @@ -107,55 +93,12 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { AnalysisLogger.getLogger().debug("In SubmitQuery->Processing"); try { - - // // file that will contain query's result - // String property = "java.io.tmpdir"; - // String tempDir = System.getProperty(property); - // - // // File f = File.createTempFile("results", ".txt", new - // // File(tempDir)); - // tempDir = tempDir + "/"; - // - // // BufferedWriter out = new BufferedWriter(new - // // FileWriter(tempDir+"results.txt")); - // - // // fileName = "./cfg/" + "results.csv"; - // - // fileName = "./cfg/" + "results.txt"; - // - // // remove comment - // // fileName = tempDir + "results.csv"; - // - // out = new BufferedWriter(new FileWriter(fileName)); - // retrieve information List Info = retrieveInfo(); // create the connection sf = getConnection(Info); - // // check on table name field - // tableName = getInputParameter("TableName"); - // - // if ((tableName == null) || (tableName.equals(""))) { - // - // throw new Exception("Warning: insert the table name"); - // - // } - // - // // check on schema name field - // - // if (driverInfo.toLowerCase().contains("postgres")) { - // - // schemaName = getInputParameter("SchemaName"); - // - // if ((schemaName == null) || (schemaName.equals(""))) { - // - // throw new Exception("Warning: insert the schema name"); - // - // } - // } - // submit a query map = submitQuery(); @@ -164,105 +107,86 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { } catch (HibernateException h) { -// AnalysisLogger.getLogger().debug( -// "In SubmitQuery-> ERROR " + h.getMessage()); - + // AnalysisLogger.getLogger().debug( + // "In SubmitQuery-> ERROR " + h.getMessage()); AnalysisLogger.getLogger().debug( "In SubmitQuery-> " + h.getMessage()); - throw h; } catch (IllegalStateException e) { // e.printStackTrace(); -// AnalysisLogger.getLogger().debug( -// "In SubmitQuery-> ERROR " + e.getMessage()); - + // AnalysisLogger.getLogger().debug( + // "In SubmitQuery-> ERROR " + e.getMessage()); AnalysisLogger.getLogger().debug( "In SubmitQuery-> " + e.getMessage()); - throw e; } catch (DiscoveryException e1) { // e1.printStackTrace(); -// AnalysisLogger.getLogger().debug( -// "In SubmitQuery-> ERROR " + e1.getMessage()); - + // AnalysisLogger.getLogger().debug( + // "In SubmitQuery-> ERROR " + e1.getMessage()); AnalysisLogger.getLogger().debug( "In SubmitQuery-> " + e1.getMessage()); - throw e1; } catch (InvalidResultException e2) { // e2.printStackTrace(); -// AnalysisLogger.getLogger().debug( -// "In SubmitQuery-> ERROR " + e2.getMessage()); - + // AnalysisLogger.getLogger().debug( + // "In SubmitQuery-> ERROR " + e2.getMessage()); AnalysisLogger.getLogger().debug( "In SubmitQuery-> " + e2.getMessage()); - throw e2; } catch (IOException e3) { // e3.printStackTrace(); -// AnalysisLogger.getLogger().debug( -// "In SubmitQuery-> ERROR " + e3.getMessage()); - + // AnalysisLogger.getLogger().debug( + // "In SubmitQuery-> ERROR " + e3.getMessage()); AnalysisLogger.getLogger().debug( "In SubmitQuery-> " + e3.getMessage()); - throw e3; } catch (Exception e4) { // e4.printStackTrace(); -// AnalysisLogger.getLogger().debug( -// "In SubmitQuery-> Exception " + e4.getMessage()); - + // AnalysisLogger.getLogger().debug( + // "In SubmitQuery-> Exception " + e4.getMessage()); AnalysisLogger.getLogger().debug( - "In SubmitQuery-> " + e4.getMessage()); - + "In SubmitQuery-> " + e4.getMessage()); throw e4; } finally { - if (sf.isClosed() == false) { mgt.closeConnection(); } - } - } public StatisticalType getOutput() { AnalysisLogger.getLogger().debug("In SubmitQuery->retrieving outputs"); - // generate a primitive type for the collection PrimitiveType output = new PrimitiveType(LinkedHashMap.class.getName(), map, PrimitiveTypes.MAP, "ResultsMap", "Results Map"); - return output; - } public List getInputParameters() { List parameters = new ArrayList(); - - // parameters specified by the user - PrimitiveType p0= new PrimitiveType(String.class.getName(), - "", PrimitiveTypes.STRING, "ResourceName", "The name of the resource"); - - - PrimitiveType p1=new PrimitiveType(String.class.getName(), - "", PrimitiveTypes.STRING, "DatabaseName", "The name of the database"); + PrimitiveType p0 = new PrimitiveType(String.class.getName(), "", + PrimitiveTypes.STRING, "ResourceName", + "The name of the resource"); + + PrimitiveType p1 = new PrimitiveType(String.class.getName(), "", + PrimitiveTypes.STRING, "DatabaseName", + "The name of the database"); PrimitiveType p2 = new PrimitiveType(Boolean.class.getName(), null, PrimitiveTypes.BOOLEAN, valueReadOnly, @@ -272,24 +196,22 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { PrimitiveTypes.BOOLEAN, smartCorrection, "Check the box for smart correction", "true"); - - - - -// addEnumerateInput(SmartCorrectionEnum.values(), dialect, "Language", ""); - PrimitiveType p4 = new PrimitiveType(Enum.class.getName(),SmartCorrectionEnum.values(),PrimitiveTypes.ENUMERATED,dialect,"Language", SmartCorrectionEnum.NONE.name()); - - PrimitiveType p5 = new PrimitiveType(String.class.getName(), - "", PrimitiveTypes.STRING, "Query", "query"); - + // addEnumerateInput(SmartCorrectionEnum.values(), dialect, "Language", + // ""); + PrimitiveType p4 = new PrimitiveType(Enum.class.getName(), + SmartCorrectionEnum.values(), PrimitiveTypes.ENUMERATED, + dialect, "Language", SmartCorrectionEnum.NONE.name()); + + PrimitiveType p5 = new PrimitiveType(String.class.getName(), "", + PrimitiveTypes.STRING, "Query", "query"); + parameters.add(p0); parameters.add(p1); parameters.add(p2); parameters.add(p3); parameters.add(p4); parameters.add(p5); - - + return parameters; } @@ -297,11 +219,11 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { @Override protected void setInputParameters() { -// AnalysisLogger.getLogger().debug("In SubmitQuery->setting inputs"); + // AnalysisLogger.getLogger().debug("In SubmitQuery->setting inputs"); // parameters specified by the user -// addStringInput("ResourceName", "The name of the resource", ""); -// addStringInput("DatabaseName", "The name of the database", ""); + // addStringInput("ResourceName", "The name of the resource", ""); + // addStringInput("DatabaseName", "The name of the database", ""); // PrimitiveType p2 = new PrimitiveType(Boolean.class.getName(), null, // PrimitiveTypes.BOOLEAN, valueReadOnly, @@ -329,19 +251,18 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // addEnumerateInput(SmartCorrectionEnum.values(), dialect, "Language)", // SmartCorrectionEnum.POSTGRES.name()); -// addEnumerateInput(SmartCorrectionEnum.values(), dialect, "Language", ""); + // addEnumerateInput(SmartCorrectionEnum.values(), dialect, "Language", + // ""); // addEnumerateInput(SmartCorrectionEnum.values(), dialect, "Language", // ""); -// addStringInput("Query", "query", ""); + // addStringInput("Query", "query", ""); } @Override public void shutdown() { - AnalysisLogger.getLogger().debug("In SubmitQuery->Shutdown"); - } // Method that recovers the info useful for the connection @@ -349,26 +270,18 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { IllegalStateException, DiscoveryException, InvalidResultException { resourceName = getInputParameter("ResourceName").trim(); - if ((resourceName == null) || (resourceName.equals(""))) { - throw new Exception("Warning: insert the resource name"); - } databaseName = getInputParameter("DatabaseName").trim(); - if ((databaseName == null) || (databaseName.equals(""))) { - throw new Exception("Warning: insert the database name"); - } // retrieve the chosen resource DatabasesDiscoverer discovery = new DatabasesDiscoverer(); - List resources = discovery.discover(); - AnalysisLogger.getLogger().debug( "In SubmitQuery->number of elements: " + resources.size()); @@ -376,58 +289,17 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { List info = new ArrayList(); check: for (int i = 0; i < resources.size(); i++) { - if (resources.get(i).getResourceName().toLowerCase() .equals(resourceName.toLowerCase())) { - normalizeDBInfo(resources.get(i)); - for (int j = 0; j < resources.get(i).getAccessPoints().size(); j++) { - - // if (resources.get(i).getAccessPoints().get(j) - // .getDatabaseName().equals(databaseName)) { - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getUsername()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getPassword()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getDriver()); - // - // // driverInfo = - // // resources.get(i).getAccessPoints().get(j) - // // .getDriver(); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .getDialect()); - // - // info.add(resources.get(i).getAccessPoints().get(j) - // .address()); - // - // info.add(databaseName); - // - // break check; - // - // } - - // if (resources.get(i).getAccessPoints().get(j) - // .address().equals(url)){ - // - // System.out.println("url selezionato"); - // - // - // - // } - + if (resources.get(i).getAccessPoints().get(j) .getDatabaseName().toLowerCase() .equals(databaseName.toLowerCase())) { info.add(resources.get(i).getAccessPoints().get(j) .getUsername()); - AnalysisLogger.getLogger().debug( "In SubmitQuery->username: " + resources.get(i).getAccessPoints() @@ -435,7 +307,6 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getPassword()); - AnalysisLogger.getLogger().debug( "In SubmitQuery->password: " + resources.get(i).getAccessPoints() @@ -443,10 +314,8 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getDriver()); - driverInfo = resources.get(i).getAccessPoints().get(j) .getDriver(); - AnalysisLogger.getLogger().debug( "In SubmitQuery->driver: " + resources.get(i).getAccessPoints() @@ -458,7 +327,6 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .getDialect()); - AnalysisLogger.getLogger().debug( "In SubmitQuery->dialect: " + resources.get(i).getAccessPoints() @@ -466,34 +334,26 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { info.add(resources.get(i).getAccessPoints().get(j) .address()); - AnalysisLogger.getLogger().debug( "In SubmitQuery->url: " + resources.get(i).getAccessPoints() .get(j).address()); info.add(databaseName); - AnalysisLogger.getLogger().debug( "In SubmitQuery->databasename: " + resources.get(i).getAccessPoints() .get(j).getDatabaseName()); break check; - } - } - } - } AnalysisLogger.getLogger().debug( "In SubmitQuery->information useful for connection: retrieved"); - return info; - } // create the database's connection @@ -512,12 +372,9 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { SessionFactory sf = mgt.createConnection(DatabaseUserName, DatabasePassword, DatabaseDriver, DatabaseDialect, DatabaseURL, DatabaseName); - AnalysisLogger.getLogger().debug( "In SubmitQuery->database " + DatabaseName + ": connected"); - return sf; - } // Method that allows to submit a query @@ -530,33 +387,24 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { LinkedHashMap mapResults = new LinkedHashMap(); query = getInputParameter("Query"); - if ((query == null) || (query.equals(""))) { - throw new Exception("Warning: insert the query"); - } - - // analyze the query to filter it if it is not read-only compliant // String valueRO = getInputParameter(valueReadOnly); // //print check - AnalysisLogger.getLogger().debug("In SubmitQuery->valueRO: " + - valueRO); + AnalysisLogger.getLogger().debug("In SubmitQuery->valueRO: " + valueRO); if (valueRO.equals("true")) { - AnalysisLogger.getLogger().debug( "In SubmitQuery->Analyzing the query: " + query); - NotAllowedQuery = analyzeQuery(query); // //print check // AnalysisLogger.getLogger().debug( // "In SubmitQuery->NotAllowedQuery valueRO: " + NotAllowedQuery); - } if (NotAllowedQuery == false) { @@ -566,27 +414,26 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // submit query List result = new ArrayList(); - - - AnalysisLogger.getLogger() - .debug("In SubmitQuery->path file: " - + config.getPersistencePath()); + // path file + // AnalysisLogger.getLogger() + // .debug("In SubmitQuery->path file: " + // + config.getPersistencePath()); // if user specifies to use the smart correction a translation in // applied on the query // String valueSC = getInputParameter(smartCorrection); // //print check - AnalysisLogger.getLogger().debug( - "In SubmitQuery->valueSC: " + valueSC); + AnalysisLogger.getLogger().debug( + "In SubmitQuery->valueSC: " + valueSC); // dialect to which a query is converted -// String valueDialect = getInputParameter(dialect); + // String valueDialect = getInputParameter(dialect); valueDialect = getInputParameter(dialect); // //print check - AnalysisLogger.getLogger().debug( - "In SubmitQuery->valueDialect: " + valueDialect); + AnalysisLogger.getLogger().debug( + "In SubmitQuery->valueDialect: " + valueDialect); if ((valueSC.equals("true")) && (!(valueDialect.equals("NONE")))) { String smartCorrectedQuery = ""; @@ -608,42 +455,36 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // call the SwisSQL library functionality smartCorrectedQuery = mgt.smartCorrectionOnQuery(query, DatabaseManagement.POSTGRESQLDialect); - } if (valueDialect.equals("MYSQL")) { - // call the SwisSQL library functionality smartCorrectedQuery = mgt.smartCorrectionOnQuery(query, DatabaseManagement.MYSQLDialect); - } AnalysisLogger.getLogger().debug( "In SubmitQuery-> query converted: " + smartCorrectedQuery); - query = smartCorrectedQuery; if (!(smartCorrectedQuery.equals(""))) { - PrimitiveType valQuery = new PrimitiveType( String.class.getName(), smartCorrectedQuery, PrimitiveTypes.STRING, "Converted Query", "Query Converted"); mapResults.put("Query Converted", valQuery); - } + } + + // else if ((valueSC.equals("true")) && + // (valueDialect.equals("NONE"))) { + // + // throw new Exception("Warning: specify the language"); + // + // } - } - -// else if ((valueSC.equals("true")) && (valueDialect.equals("NONE"))) { -// -// throw new Exception("Warning: specify the language"); -// -// } - AnalysisLogger.getLogger().debug( "In SubmitQuery->Submitting the query: " + query); @@ -658,7 +499,6 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { result = mgt .submitQuery(query, sf, config.getPersistencePath()); - } if (driverInfo.toLowerCase().contains("mysql")) { @@ -670,13 +510,10 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { result = mgt .submitQuery(query, sf, config.getPersistencePath()); - } if (result == null) { - throw new Exception("Warning: the table has not rows"); - } AnalysisLogger.getLogger().debug( @@ -686,7 +523,6 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { mapResult = mgt.getMapQueryResult(); String encoded = null; - encoded = new String(mapResult.get("HEADERS").getBytes(), "UTF-8"); // // check the encoded value @@ -700,7 +536,6 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // to add the rows (result of the query) for (int i = 0; i < mapResult.size() - 1; i++) { - encoded = new String(mapResult.get(String.valueOf(i)) .getBytes(), "UTF-8"); @@ -710,28 +545,22 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { PrimitiveType val1 = new PrimitiveType(String.class.getName(), encoded, PrimitiveTypes.STRING, "Row", "Row"); - mapResults.put(String.valueOf(i), val1); // //check value contained in map // String value = (String) val1.getContent(); // // AnalysisLogger.getLogger().debug( // "In RandomSampleOnTable->value: " + value); - } // to add the file - - PrimitiveType fileResult = new PrimitiveType(File.class.getName(), - mgt.getFileQueryResult(), PrimitiveTypes.FILE, "File", - "File"); - - mapResults.put("File", fileResult); - + // PrimitiveType fileResult = new + // PrimitiveType(File.class.getName(), + // mgt.getFileQueryResult(), PrimitiveTypes.FILE, "File", + // "File"); + // mapResults.put("File", fileResult); } - return mapResults; - } // method that allows to analyze the query in order to filter it if it is @@ -742,73 +571,42 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // TODO: check also the oracle case if (driverInfo.toLowerCase().contains("postgres")) { - PostgresLexicalAnalyzer obj = new PostgresLexicalAnalyzer(); - NotAllowed = obj.analyze(query); - } if (driverInfo.toLowerCase().contains("mysql")) { - MySQLLexicalAnalyzer obj = new MySQLLexicalAnalyzer(); - NotAllowed = obj.analyze(query); - } AnalysisLogger.getLogger().debug( "In SubmitQuery->Warning: query filtered: " + NotAllowed); - return NotAllowed; - } private void normalizeDBInfo(DBResource resource) throws IOException { - int ap = resource.getAccessPoints().size(); - for (int i = 0; i < ap; i++) { - try { resource.normalize(i); } catch (IOException e) { - // e.printStackTrace(); - AnalysisLogger.getLogger().debug( "In SubmitQuery->: Error in normalization process" + e.getMessage()); - throw e; - } - } } - // private void formatWithQuotes(String Query){ - // - // if (driverInfo.toLowerCase().contains("postgres")){ - // - // if (Query.contains(tableName)) - // { - // - // query=Query.replaceAll(tableName, "\""+tableName+"\""); - // - // } - // - // if (driverInfo.toLowerCase().contains("mysql")){ - // - // - // query=Query.replaceAll(tableName, "\""+tableName+"\""); - // - // - // } - // - // - // } - // - // - // } - +// private void formatWithQuotes(String Query) { +// if (driverInfo.toLowerCase().contains("postgres")) { +// if (Query.contains(tableName)) { +// query = Query.replaceAll(tableName, "\"" + tableName + "\""); +// } +// if (driverInfo.toLowerCase().contains("mysql")) { +// query = Query.replaceAll(tableName, "\"" + tableName + "\""); +// } +// } +// } }