package org.gcube.dataaccess.algorithms.drmalgorithms; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.UUID; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; import org.gcube.dataaccess.databases.access.DatabasesDiscoverer; import org.gcube.dataaccess.databases.resources.DBResource; import org.gcube.dataaccess.databases.utils.DatabaseManagement; import org.gcube.dataanalysis.ecoengine.datatypes.PrimitiveType; import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType; import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes; import org.gcube.dataanalysis.ecoengine.interfaces.StandardLocalExternalAlgorithm; import org.gcube.resources.discovery.client.api.DiscoveryException; import org.gcube.resources.discovery.client.api.InvalidResultException; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; /** Class that allows to retrieve the tables's names of a chosen database */ public class ListTables extends StandardLocalExternalAlgorithm { private LinkedHashMap map = new LinkedHashMap(); private DatabaseManagement mgt; // variable that keeps track of the database's type private String driverInfo; // database's parameters specified by the user private String resourceName = null; private String databaseName = null; private String schemaName = null; private SessionFactory sf; @Override public void init() throws Exception { mgt = new DatabaseManagement(config.getConfigPath()); AnalysisLogger.getLogger().debug("In ListTables->Initialization"); String scope = config.getGcubeScope(); AnalysisLogger.getLogger().debug( "In ListTables->scope set by config object: " + scope); if (scope == null || scope.length() == 0) { scope = ScopeProvider.instance.get(); AnalysisLogger.getLogger().debug( "In ListTables->scope set by ScopeProvider: " + scope); } else { ScopeProvider.instance.set(scope); } } @Override public String getDescription() { // add a simple description for the algorithm return "Algorithm that allows to view the table names of a chosen database"; } @Override protected void process() throws Exception, IOException, IllegalStateException, DiscoveryException, InvalidResultException, HibernateException { AnalysisLogger.getLogger().debug("In ListTables->Processing"); try { // retrieve information useful for the connection List Info = retrieveInfo(); // create the connection sf = getConnection(Info); // get the table' list List listTables = new ArrayList(); if (driverInfo.toLowerCase().contains("postgres")) { schemaName = getInputParameter("SchemaName").trim(); if ((schemaName == null) || (schemaName.equals(""))) { throw new Exception("Warning: insert the schema name"); } // if (!schemaName.equals("")) { listTables = mgt.getTables(databaseName, schemaName); AnalysisLogger .getLogger() .debug("In ListTables->getting table's name for database postgres"); // } } if (driverInfo.toLowerCase().contains("mysql")) { listTables = mgt.getTables(databaseName, null); AnalysisLogger .getLogger() .debug("In ListTables->getting table's name for database mysql"); } // if (listTables.size()==0){ // // AnalysisLogger.getLogger().debug("In ListTables->Warning: no table available"); // // } // TODO: manage also the oracle type if (listTables == null) { AnalysisLogger.getLogger().debug( "In ListTables->Warning: no tables available"); } else { for (int i = 0; i < listTables.size(); i++) { PrimitiveType val = new PrimitiveType( String.class.getName(), null, PrimitiveTypes.STRING, "Table's name", "Table's name"); val.setContent(listTables.get(i)); map.put(String.valueOf(i), val); // AnalysisLogger.getLogger().debug( // "In ListTables->getting table's name: " // + val.getContent()); } } // close the connection sf.close(); } catch (HibernateException h) { AnalysisLogger.getLogger().debug( "In ListTables-> ERROR " + h.getMessage()); throw h; } catch (IllegalStateException e) { // e.printStackTrace(); AnalysisLogger.getLogger().debug( "In ListTables-> ERROR " + e.getMessage()); throw e; } catch (DiscoveryException e1) { // e1.printStackTrace(); AnalysisLogger.getLogger().debug( "In ListTables-> ERROR " + e1.getMessage()); throw e1; } catch (InvalidResultException e2) { // e2.printStackTrace(); AnalysisLogger.getLogger().debug( "In ListTables-> ERROR " + e2.getMessage()); throw e2; } catch (IOException e3) { // e3.printStackTrace(); AnalysisLogger.getLogger().debug( "In ListTables-> Exception " + e3.getMessage()); throw e3; } catch (Exception e4) { // e4.printStackTrace(); AnalysisLogger.getLogger().debug( "In ListTables-> Exception " + e4.getMessage()); throw e4; } finally { if (sf!=null && sf.isClosed() == false) { mgt.closeConnection(); } } } @Override protected void setInputParameters() { // AnalysisLogger.getLogger().debug("In ListTables->setting inputs"); // 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", ""); } @Override public void shutdown() { AnalysisLogger.getLogger().debug("In ListTables->Shutdown"); } @Override public StatisticalType getOutput() { AnalysisLogger.getLogger().debug("In ListTables->retrieving outputs"); // generate a primitive type for the collection PrimitiveType output = new PrimitiveType(LinkedHashMap.class.getName(), map, PrimitiveTypes.MAP, "ResultsMap"+UUID.randomUUID(), "Results Map"); return output; } private List retrieveInfo() throws Exception, IllegalStateException, DiscoveryException, InvalidResultException { // parameters specified by the user 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 ListTables->number of database resources: " + resources.size()); for (int i = 0; i < resources.size(); i++) { AnalysisLogger.getLogger().debug( "In ListTables->Resource's name: " + resources.get(i).getResourceName()); } // 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() .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 ListTables->username: " + resources.get(i).getAccessPoints() .get(j).getUsername()); info.add(resources.get(i).getAccessPoints().get(j) .getPassword()); AnalysisLogger.getLogger().debug( "In ListTables->password: " + resources.get(i).getAccessPoints() .get(j).getPassword()); info.add(resources.get(i).getAccessPoints().get(j) .getDriver()); driverInfo = resources.get(i).getAccessPoints().get(j) .getDriver(); AnalysisLogger.getLogger().debug( "In ListTables->driver: " + resources.get(i).getAccessPoints() .get(j).getDriver()); // driverInfo = // resources.get(i).getAccessPoints().get(j) // .getDriver(); info.add(resources.get(i).getAccessPoints().get(j) .getDialect()); AnalysisLogger.getLogger().debug( "In ListTables->dialect: " + resources.get(i).getAccessPoints() .get(j).getDialect()); info.add(resources.get(i).getAccessPoints().get(j) .address()); AnalysisLogger.getLogger().debug( "In ListTables->url: " + resources.get(i).getAccessPoints() .get(j).address()); info.add(databaseName); AnalysisLogger.getLogger().debug( "In ListTables->databasename: " + resources.get(i).getAccessPoints() .get(j).getDatabaseName()); break check; } } } } AnalysisLogger.getLogger().debug( "In ListTables->information useful for connection: retrieved"); return info; } private SessionFactory getConnection(List Info) throws IOException { // create the connection Iterator iterator = Info.iterator(); String DatabaseUserName = iterator.next(); String DatabasePassword = iterator.next(); String DatabaseDriver = iterator.next(); String DatabaseDialect = iterator.next(); String DatabaseURL = iterator.next(); String DatabaseName = iterator.next(); SessionFactory sf = mgt.createConnection(DatabaseUserName, DatabasePassword, DatabaseDriver, DatabaseDialect, DatabaseURL, DatabaseName); AnalysisLogger.getLogger().debug( "In ListTables->database " + DatabaseName + ": connected"); return sf; } 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 ListTables->: Error in normalization process" + e.getMessage()); throw e; } } } }