diff --git a/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java b/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java index 573e47a..1df3222 100644 --- a/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; @@ -25,6 +26,7 @@ import net.sf.ehcache.config.MemoryUnit; import net.sf.ehcache.store.MemoryStoreEvictionPolicy; import org.apache.regexp.RE; +import org.apache.xpath.operations.Bool; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.contentmanager.storageclient.model.protocol.smp.SMPURLConnection; @@ -83,6 +85,11 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements private static CacheManager cacheManager; public static List listAlgorithms; + // variables to check thread execution + private static HashMap threadsStarted = new HashMap(); + private static boolean endThread = false; + + public GWTdbManagerServiceImpl() throws Exception { } @@ -184,6 +191,24 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements public void destroy() { super.destroy(); + // set endThread variable + setEndThreadvariable(); + + // set endThread variable + // try { + // setEndThreadvariable(); + // logger.info("Thread successfully stopped"); + // + // } catch (InterruptedException e1) { + // // TODO Auto-generated catch block + // // e1.printStackTrace(); + // logger.error("dbmanager-> ", e1); + // } finally{ + // + // if(t.interrupted()){ + // logger.info("Thread interrupt"); + // } + try { CacheManager cacheManager = CacheManager.getInstance(); @@ -211,12 +236,17 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements e); // e.printStackTrace(); } + // } } - private void initVariables() { + private void initVariables(ASLSession session) { - ASLSession session = SessionUtil.getAslSession(this - .getThreadLocalRequest().getSession()); + // // get scope + // String scope = session.getScope(); + // + // ThreadLoaderData loader = new ThreadLoaderData(scope); + // Thread t = new Thread(loader); + // t.start(); // the result generated in the LoadTables method // List result = new ArrayList(); @@ -280,86 +310,114 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements try { + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); + + // check if the thread is alreday started + Boolean value = getThreadStarted(scope); + if ((value == null) || (value.booleanValue() == false)) { + ThreadLoaderData loader = new ThreadLoaderData(scope); + Thread t = new Thread(loader); + t.start(); + + logger.info("dbmanager-> Thread created in order to load all data"); + } + // initialize variables with application startup - initVariables(); - - // data input - List inputParameters = new ArrayList(); - // data output - List outputParameters = new ArrayList(); - - // get algorithmId - String algorithmId = ConstantsPortlet.ALGID_GETRESOURCE; - - Parameter maxNumber = new Parameter("MaxNumber", "", "String", "-1"); - inputParameters.add(maxNumber); - - // check if the value is in cache. If data does not exist in cache - // the computation is started otherwise data are retrieved from - // cache. - - // get data from cache - // check if data exist considering as key the input parameters - // String key = inputParameters.get(0).getDefaultValue(); - String key = "listResources"; - - // System.out.println("***KEY: " + key); - net.sf.ehcache.Element dataFromCache = getDataFromCache(key); - - Object value = null; - if (dataFromCache != null) { - value = dataFromCache.getObjectValue(); - // System.out.println("***GETTING DATA FROM CACHE"); - } - if (value != null) { - outputParameters = (List) value; - } else { - - // start the computation - // System.out.println("***STARTING THE COMPUTATION"); - // create data structure for data output - ComputationOutput outputData = new ComputationOutput(); - // computationId - String computationId = startComputation(algorithmId, - inputParameters, outputData); - - // print check - // retrieve data - // logger.info("output data retrieved"); - - // data output - LinkedHashMap mapValues = new LinkedHashMap(); - mapValues = outputData.getMapValues(); - - for (int i = 0; i < mapValues.size(); i++) { - FileModel obj = new FileModel(mapValues.get(String - .valueOf(i))); - // obj.setIsLoaded(true); - outputParameters.add(obj); - } - - if (outputParameters != null && outputParameters.size() != 0) { - // put data in cache - net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( - key, outputParameters); - - insertDataIntoCache(dataToCache); - } - } - - return outputParameters; + initVariables(session); + return recoverResources(scope); } catch (Exception e) { - // e.printStackTrace(); - // throw new Exception("Failed to load data. " + e); logger.error("dbmanager-> ", e); - if (!(e instanceof StatisticalManagerException)) { - // GWT can't serialize all exceptions - throw new Exception( - "Error in server while loading data. Exception: " + e); - } throw e; + } + + // try { + // + // // initialize variables with application startup + // initVariables(); + // + // // data input + // List inputParameters = new ArrayList(); + // // data output + // List outputParameters = new ArrayList(); + // + // // get algorithmId + // String algorithmId = ConstantsPortlet.ALGID_GETRESOURCE; + // + // Parameter maxNumber = new Parameter("MaxNumber", "", "String", "-1"); + // inputParameters.add(maxNumber); + // + // // check if the value is in cache. If data does not exist in cache + // // the computation is started otherwise data are retrieved from + // // cache. + // + // // get data from cache + // // check if data exist considering as key the input parameters + // // String key = inputParameters.get(0).getDefaultValue(); + // String key = "listResources"; + // + // // System.out.println("***KEY: " + key); + // net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + // + // Object value = null; + // if (dataFromCache != null) { + // value = dataFromCache.getObjectValue(); + // // System.out.println("***GETTING DATA FROM CACHE"); + // } + // if (value != null) { + // outputParameters = (List) value; + // } else { + // + // // start the computation + // // System.out.println("***STARTING THE COMPUTATION"); + // // create data structure for data output + // ComputationOutput outputData = new ComputationOutput(); + // // computationId + // String computationId = startComputation(algorithmId, + // inputParameters, outputData, ""); + // + // // print check + // // retrieve data + // // logger.info("output data retrieved"); + // + // // data output + // LinkedHashMap mapValues = new LinkedHashMap(); + // mapValues = outputData.getMapValues(); + // + // for (int i = 0; i < mapValues.size(); i++) { + // FileModel obj = new FileModel(mapValues.get(String + // .valueOf(i))); + // // obj.setIsLoaded(true); + // outputParameters.add(obj); + // } + // + // if (outputParameters != null && outputParameters.size() != 0) { + // // put data in cache + // net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + // key, outputParameters); + // + // insertDataIntoCache(dataToCache); + // } + // } + // + // return outputParameters; + // + // } catch (Exception e) { + // // e.printStackTrace(); + // // throw new Exception("Failed to load data. " + e); + // logger.error("dbmanager-> ", e); + // if (!(e instanceof StatisticalManagerException)) { + // // GWT can't serialize all exceptions + // throw new Exception( + // "Error in server while loading data. Exception: " + e); + // } + // throw e; + // } } // to get information about databases of a resource @@ -372,97 +430,112 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements throw new SessionExpiredException(); try { - // data input - List inputParameters = new ArrayList(); - // data output - LinkedHashMap outputParameters = new LinkedHashMap(); - - String algorithmId = ConstantsPortlet.ALGID_GETDBINFO; - - // print check - logger.info("dbmanager-> ResourceName: " + resourceName); - - if ((resourceName == null) || (resourceName.equals(""))) { - throw new Exception("Unable to load data"); - } - - Parameter resource = new Parameter("ResourceName", "", "String", ""); - inputParameters.add(resource); - inputParameters.get(0).setValue(resourceName); - - // get data from cache - // check if data exist considering as key the input parameters - String key = inputParameters.get(0).getValue(); - - // System.out.println("***KEY: " + key); - net.sf.ehcache.Element dataFromCache = getDataFromCache(key); - - Object value = null; - if (dataFromCache != null) { - value = dataFromCache.getObjectValue(); - } - if (value != null) { - outputParameters = (LinkedHashMap) value; - // System.out.println("***GETTING DATA FROM CACHE"); - } else { - - // start the computation - // System.out.println("***STARTING THE COMPUTATION"); - // create data structure - ComputationOutput outputData = new ComputationOutput(); - // computation id - String computationId = startComputation(algorithmId, - inputParameters, outputData); - - // print check - // retrieve data - // logger.info("output data retrieved"); - - // data output values - LinkedHashMap mapValues = new LinkedHashMap(); - // data output keys - LinkedHashMap mapKeys = new LinkedHashMap(); - - mapValues = outputData.getMapValues(); - mapKeys = outputData.getmapKeys(); - - for (int i = 0; i < mapValues.size(); i++) { - FileModel obj = new FileModel(mapValues.get(String - .valueOf(i))); - // obj.setIsLoaded(true); - - // print check - // logger.info("value: " + - // mapValues.get(String.valueOf(i))); - // logger.info("key: " + - // mapKeys.get(String.valueOf(i))); - outputParameters.put(mapKeys.get(String.valueOf(i)), obj); - } - - // write data in cache - if (outputParameters != null && outputParameters.size() != 0) { - // put data in cache - net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( - inputParameters.get(0).getValue(), outputParameters); - - insertDataIntoCache(dataToCache); - } - } - - return outputParameters; + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); + return recoverDatabases(scope, resourceName); } catch (Exception e) { - // e.printStackTrace(); - // throw new Exception("Failed to load data " + ); logger.error("dbmanager-> ", e); - - if (!(e instanceof StatisticalManagerException)) { - // GWT can't serialize all exceptions - throw new Exception( - "Error in server while loading data. Exception: " + e); - } throw e; } + + // try { + // // data input + // List inputParameters = new ArrayList(); + // // data output + // LinkedHashMap outputParameters = new + // LinkedHashMap(); + // + // String algorithmId = ConstantsPortlet.ALGID_GETDBINFO; + // + // // print check + // logger.info("dbmanager-> ResourceName: " + resourceName); + // + // if ((resourceName == null) || (resourceName.equals(""))) { + // throw new Exception("Unable to load data"); + // } + // + // Parameter resource = new Parameter("ResourceName", "", "String", ""); + // inputParameters.add(resource); + // inputParameters.get(0).setValue(resourceName); + // + // // get data from cache + // // check if data exist considering as key the input parameters + // String key = inputParameters.get(0).getValue(); + // + // // System.out.println("***KEY: " + key); + // net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + // + // Object value = null; + // if (dataFromCache != null) { + // value = dataFromCache.getObjectValue(); + // } + // if (value != null) { + // outputParameters = (LinkedHashMap) value; + // // System.out.println("***GETTING DATA FROM CACHE"); + // } else { + // + // // start the computation + // // System.out.println("***STARTING THE COMPUTATION"); + // // create data structure + // ComputationOutput outputData = new ComputationOutput(); + // // computation id + // String computationId = startComputation(algorithmId, + // inputParameters, outputData); + // + // // print check + // // retrieve data + // // logger.info("output data retrieved"); + // + // // data output values + // LinkedHashMap mapValues = new LinkedHashMap(); + // // data output keys + // LinkedHashMap mapKeys = new LinkedHashMap(); + // + // mapValues = outputData.getMapValues(); + // mapKeys = outputData.getmapKeys(); + // + // for (int i = 0; i < mapValues.size(); i++) { + // FileModel obj = new FileModel(mapValues.get(String + // .valueOf(i))); + // // obj.setIsLoaded(true); + // + // // print check + // // logger.info("value: " + + // // mapValues.get(String.valueOf(i))); + // // logger.info("key: " + + // // mapKeys.get(String.valueOf(i))); + // outputParameters.put(mapKeys.get(String.valueOf(i)), obj); + // } + // + // // write data in cache + // if (outputParameters != null && outputParameters.size() != 0) { + // // put data in cache + // net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + // inputParameters.get(0).getValue(), outputParameters); + // + // insertDataIntoCache(dataToCache); + // } + // } + // + // return outputParameters; + // + // } catch (Exception e) { + // // e.printStackTrace(); + // // throw new Exception("Failed to load data " + ); + // logger.error("dbmanager-> ", e); + // + // if (!(e instanceof StatisticalManagerException)) { + // // GWT can't serialize all exceptions + // throw new Exception( + // "Error in server while loading data. Exception: " + e); + // } + // throw e; + // } } // to get schema for a database @@ -476,109 +549,123 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements try { - // data input - List inputParameters = new ArrayList(); - // data output - List outputParameters = new ArrayList(); - - String algorithmId = ConstantsPortlet.ALGID_GETDBSCHEMA; - // print check - String rs = dataInput.get("ResourceName"); - String db = dataInput.get("DatabaseName"); - - logger.info("dbmanager-> ResourceName: " + rs); - logger.info("dbmanager-> DatabaseName: " + db); - - if ((rs == null) || (rs.equals(""))) { - throw new Exception("Unable to load data"); - } - if ((db == null) || (db.equals(""))) { - throw new Exception("Unable to load data"); - } - - // set input parameters - Parameter resource = new Parameter("ResourceName", "", "String", ""); - Parameter database = new Parameter("DatabaseName", "", "String", ""); - inputParameters.add(resource); - inputParameters.add(database); - - inputParameters.get(0).setValue(rs); - inputParameters.get(1).setValue(db); - - // print check algorithm input parameters - // for (int i = 0; i < inputParameters.size(); i++) { - // logger.info(inputParameters.get(i).getName()); - // } - - // get data from cache - // check if data exist considering as key the input parameters - String key = inputParameters.get(0).getValue() - + inputParameters.get(1).getValue(); - - // System.out.println("key in GETSCHEMA: " + key); - net.sf.ehcache.Element dataFromCache = getDataFromCache(key); - - Object value = null; - if (dataFromCache != null) { - value = dataFromCache.getObjectValue(); - } - if (value != null) { - outputParameters = (List) value; - } else { - - // start the computation - // create data structure - ComputationOutput outputData = new ComputationOutput(); - // computation id - String computationId = startComputation(algorithmId, - inputParameters, outputData); - - // print check - // retrieve data - // logger.info("dbmanager-> output data retrieved"); - - // data output values - LinkedHashMap mapValues = new LinkedHashMap(); - // data output keys - LinkedHashMap mapKeys = new LinkedHashMap(); - - mapValues = outputData.getMapValues(); - mapKeys = outputData.getmapKeys(); - - for (int i = 0; i < mapValues.size(); i++) { - FileModel obj = new FileModel(mapValues.get(String - .valueOf(i))); - // obj.setIsSchema(true); - // obj.setIsLoaded(true); - outputParameters.add(obj); - } - - // write data in cache - if (outputParameters != null && outputParameters.size() != 0) { - // put data in cache - net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( - inputParameters.get(0).getValue() - + inputParameters.get(1).getValue(), - outputParameters); - - insertDataIntoCache(dataToCache); - // DBCache.put(dataToCache); - } - } - - return outputParameters; - + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); + return recoverSchema(scope, dataInput); } catch (Exception e) { - // e.printStackTrace(); - // throw new Exception("Failed to load data. " + e); logger.error("dbmanager-> ", e); - if (!(e instanceof StatisticalManagerException)) { - // GWT can't serialize all exceptions - throw new Exception( - "Error in server while loading data. Exception: " + e); - } throw e; } + + // try { + // + // // data input + // List inputParameters = new ArrayList(); + // // data output + // List outputParameters = new ArrayList(); + // + // String algorithmId = ConstantsPortlet.ALGID_GETDBSCHEMA; + // // print check + // String rs = dataInput.get("ResourceName"); + // String db = dataInput.get("DatabaseName"); + // + // logger.info("dbmanager-> ResourceName: " + rs); + // logger.info("dbmanager-> DatabaseName: " + db); + // + // if ((rs == null) || (rs.equals(""))) { + // throw new Exception("Unable to load data"); + // } + // if ((db == null) || (db.equals(""))) { + // throw new Exception("Unable to load data"); + // } + // + // // set input parameters + // Parameter resource = new Parameter("ResourceName", "", "String", ""); + // Parameter database = new Parameter("DatabaseName", "", "String", ""); + // inputParameters.add(resource); + // inputParameters.add(database); + // + // inputParameters.get(0).setValue(rs); + // inputParameters.get(1).setValue(db); + // + // // print check algorithm input parameters + // // for (int i = 0; i < inputParameters.size(); i++) { + // // logger.info(inputParameters.get(i).getName()); + // // } + // + // // get data from cache + // // check if data exist considering as key the input parameters + // String key = inputParameters.get(0).getValue() + // + inputParameters.get(1).getValue(); + // + // // System.out.println("key in GETSCHEMA: " + key); + // net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + // + // Object value = null; + // if (dataFromCache != null) { + // value = dataFromCache.getObjectValue(); + // } + // if (value != null) { + // outputParameters = (List) value; + // } else { + // + // // start the computation + // // create data structure + // ComputationOutput outputData = new ComputationOutput(); + // // computation id + // String computationId = startComputation(algorithmId, + // inputParameters, outputData, scope); + // + // // print check + // // retrieve data + // // logger.info("dbmanager-> output data retrieved"); + // + // // data output values + // LinkedHashMap mapValues = new LinkedHashMap(); + // // data output keys + // LinkedHashMap mapKeys = new LinkedHashMap(); + // + // mapValues = outputData.getMapValues(); + // mapKeys = outputData.getmapKeys(); + // + // for (int i = 0; i < mapValues.size(); i++) { + // FileModel obj = new FileModel(mapValues.get(String + // .valueOf(i))); + // // obj.setIsSchema(true); + // // obj.setIsLoaded(true); + // outputParameters.add(obj); + // } + // + // // write data in cache + // if (outputParameters != null && outputParameters.size() != 0) { + // // put data in cache + // net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + // inputParameters.get(0).getValue() + // + inputParameters.get(1).getValue(), + // outputParameters); + // + // insertDataIntoCache(dataToCache); + // // DBCache.put(dataToCache); + // } + // } + // + // return outputParameters; + // + // } catch (Exception e) { + // // e.printStackTrace(); + // // throw new Exception("Failed to load data. " + e); + // logger.error("dbmanager-> ", e); + // if (!(e instanceof StatisticalManagerException)) { + // // GWT can't serialize all exceptions + // throw new Exception( + // "Error in server while loading data. Exception: " + e); + // } + // throw e; + // } } // to get tables @@ -587,126 +674,140 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements try { - // data input - List inputParameters = new ArrayList(); - // data output - List outputParameters = new ArrayList(); - - String algorithmId = ConstantsPortlet.ALGID_GETTABLES; - - String rs = dataInput.get("ResourceName"); - String db = dataInput.get("DatabaseName"); - String scm = dataInput.get("SchemaName"); - - // print check - logger.info("dbmanager-> ResourceName: " + rs); - logger.info("dbmanager-> DatabaseName: " + db); - logger.info("dbmanager-> SchemaName: " + scm); - - if ((elementType != null) - && (elementType.equals(ConstantsPortlet.SCHEMA))) { - if ((rs == null) || (rs.equals(""))) { - throw new Exception("Unable to load data"); - } - if ((db == null) || (db.equals(""))) { - throw new Exception("Unable to load data"); - } - if ((scm == null) || (scm.equals(""))) { - throw new Exception("Unable to load data"); - } - } - if ((elementType != null) - && (elementType.equals(ConstantsPortlet.DATABASE))) { - if ((rs == null) || (rs.equals(""))) { - throw new Exception("Unable to load data"); - } - if ((db == null) || (db.equals(""))) { - throw new Exception("Unable to load data"); - } - } - - // set input parameters - Parameter resource = new Parameter("ResourceName", "", "String", ""); - Parameter database = new Parameter("DatabaseName", "", "String", ""); - Parameter schema = new Parameter("SchemaName", "", "String", ""); - inputParameters.add(resource); - inputParameters.add(database); - inputParameters.add(schema); - - inputParameters.get(0).setValue(rs); - inputParameters.get(1).setValue(db); - inputParameters.get(2).setValue(scm); - - // get data from cache - // check if data exist considering as key the input parameters - String key = inputParameters.get(0).getValue() - + inputParameters.get(1).getValue() - + inputParameters.get(2).getValue(); - - // System.out.println("***KEY: " + key); - net.sf.ehcache.Element dataFromCache = getDataFromCache(key); - - Object value = null; - if (dataFromCache != null) { - value = dataFromCache.getObjectValue(); - // System.out.println("***GETTING DATA FROM CACHE"); - } - if (value != null) { - outputParameters = (List) value; - - } else { - - // start computation - // create data structure - ComputationOutput outputData = new ComputationOutput(); - // computation id - String computationId = startComputation(algorithmId, - inputParameters, outputData); - - // print check on retrieving data - // logger.info("output data retrieved"); - - // data output values - LinkedHashMap mapValues = new LinkedHashMap(); - // data output keys - LinkedHashMap mapKeys = new LinkedHashMap(); - - mapValues = outputData.getMapValues(); - mapKeys = outputData.getmapKeys(); - - for (int i = 0; i < mapValues.size(); i++) { - Result row = new Result(String.valueOf(i), - mapValues.get(String.valueOf(i))); - outputParameters.add(row); - } - - // write data in cache - if (outputParameters != null && outputParameters.size() != 0) { - // put data in cache - net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( - inputParameters.get(0).getValue() - + inputParameters.get(1).getValue() - + inputParameters.get(2).getValue(), - outputParameters); - - insertDataIntoCache(dataToCache); - } - - } - return outputParameters; - + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); + return recoverTables(scope, dataInput, elementType); } catch (Exception e) { - // e.printStackTrace(); - // throw new Exception("Failed to load data. " + e); - // logger.error("dbmanager-> ", e); - - if (!(e instanceof StatisticalManagerException)) { - // GWT can't serialize all exceptions - throw new Exception( - "Error in server while loading data. Exception: " + e); - } + logger.error("dbmanager-> ", e); throw e; } + + // try { + // + // // data input + // List inputParameters = new ArrayList(); + // // data output + // List outputParameters = new ArrayList(); + // + // String algorithmId = ConstantsPortlet.ALGID_GETTABLES; + // + // String rs = dataInput.get("ResourceName"); + // String db = dataInput.get("DatabaseName"); + // String scm = dataInput.get("SchemaName"); + // + // // print check + // logger.info("dbmanager-> ResourceName: " + rs); + // logger.info("dbmanager-> DatabaseName: " + db); + // logger.info("dbmanager-> SchemaName: " + scm); + // + // if ((elementType != null) + // && (elementType.equals(ConstantsPortlet.SCHEMA))) { + // if ((rs == null) || (rs.equals(""))) { + // throw new Exception("Unable to load data"); + // } + // if ((db == null) || (db.equals(""))) { + // throw new Exception("Unable to load data"); + // } + // if ((scm == null) || (scm.equals(""))) { + // throw new Exception("Unable to load data"); + // } + // } + // if ((elementType != null) + // && (elementType.equals(ConstantsPortlet.DATABASE))) { + // if ((rs == null) || (rs.equals(""))) { + // throw new Exception("Unable to load data"); + // } + // if ((db == null) || (db.equals(""))) { + // throw new Exception("Unable to load data"); + // } + // } + // + // // set input parameters + // Parameter resource = new Parameter("ResourceName", "", "String", ""); + // Parameter database = new Parameter("DatabaseName", "", "String", ""); + // Parameter schema = new Parameter("SchemaName", "", "String", ""); + // inputParameters.add(resource); + // inputParameters.add(database); + // inputParameters.add(schema); + // + // inputParameters.get(0).setValue(rs); + // inputParameters.get(1).setValue(db); + // inputParameters.get(2).setValue(scm); + // + // // get data from cache + // // check if data exist considering as key the input parameters + // String key = inputParameters.get(0).getValue() + // + inputParameters.get(1).getValue() + // + inputParameters.get(2).getValue(); + // + // // System.out.println("***KEY: " + key); + // net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + // + // Object value = null; + // if (dataFromCache != null) { + // value = dataFromCache.getObjectValue(); + // // System.out.println("***GETTING DATA FROM CACHE"); + // } + // if (value != null) { + // outputParameters = (List) value; + // + // } else { + // + // // start computation + // // create data structure + // ComputationOutput outputData = new ComputationOutput(); + // // computation id + // String computationId = startComputation(algorithmId, + // inputParameters, outputData, scope); + // + // // print check on retrieving data + // // logger.info("output data retrieved"); + // + // // data output values + // LinkedHashMap mapValues = new LinkedHashMap(); + // // data output keys + // LinkedHashMap mapKeys = new LinkedHashMap(); + // + // mapValues = outputData.getMapValues(); + // mapKeys = outputData.getmapKeys(); + // + // for (int i = 0; i < mapValues.size(); i++) { + // Result row = new Result(String.valueOf(i), + // mapValues.get(String.valueOf(i))); + // outputParameters.add(row); + // } + // + // // write data in cache + // if (outputParameters != null && outputParameters.size() != 0) { + // // put data in cache + // net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + // inputParameters.get(0).getValue() + // + inputParameters.get(1).getValue() + // + inputParameters.get(2).getValue(), + // outputParameters); + // + // insertDataIntoCache(dataToCache); + // } + // + // } + // return outputParameters; + // + // } catch (Exception e) { + // // e.printStackTrace(); + // // throw new Exception("Failed to load data. " + e); + // // logger.error("dbmanager-> ", e); + // + // if (!(e instanceof StatisticalManagerException)) { + // // GWT can't serialize all exceptions + // throw new Exception( + // "Error in server while loading data. Exception: " + e); + // } + // throw e; + // } } // to load tables @@ -843,6 +944,11 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements throw new SessionExpiredException(); try { + + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); logger.info("dbmanager-> Dialect used for smart correction: " + language); @@ -914,7 +1020,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements // parse the query in order to remove spaces String queryParsed = parseQuery(inputParameters.get(5).getValue()); // get data sent to client calling the submitQuery - String keyData = algorithmId + inputParameters.get(0).getValue() + String keyData = scope+algorithmId + inputParameters.get(0).getValue() + inputParameters.get(1).getValue() + inputParameters.get(2).getValue() + inputParameters.get(3).getValue() @@ -956,7 +1062,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements // computation id String computationId = startComputation(algorithmId, - inputParameters, outputData, UID); + inputParameters, outputData, scope, UID); // get JobID if (checkJob(UID)) { // if the computation has not been removed @@ -1080,7 +1186,15 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements if (isSessionExpired()) throw new SessionExpiredException(); + + try { + + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); + // data input List inputParameters = new ArrayList(); // output sample result @@ -1146,12 +1260,12 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements // get data from cache // check if data exist considering as key the input parameters - String key = algorithmId + inputParameters.get(0).getValue() + String key = scope+algorithmId + inputParameters.get(0).getValue() + inputParameters.get(1).getValue() + inputParameters.get(2).getValue() + inputParameters.get(3).getValue(); - // System.out.println("sampling KEY: " + key); +// System.out.println("sampling KEY: " + key); net.sf.ehcache.Element dataFromCache = getDataFromCache(key); Object value = null; @@ -1170,7 +1284,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements ComputationOutput outputData = new ComputationOutput(); // computation id String computationId = startComputation(algorithmId, - inputParameters, outputData); + inputParameters, outputData, scope); // print check on retrieving data // logger.info("output data retrieved"); @@ -1229,6 +1343,11 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements throw new SessionExpiredException(); try { + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); + // data input List inputParameters = new ArrayList(); // output sample result @@ -1294,7 +1413,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements // get data from cache // check if data exist considering as key the input parameters - String key = algorithmId + inputParameters.get(0).getValue() + String key = scope+algorithmId + inputParameters.get(0).getValue() + inputParameters.get(1).getValue() + inputParameters.get(2).getValue() + inputParameters.get(3).getValue(); @@ -1315,7 +1434,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements ComputationOutput outputData = new ComputationOutput(); // computation id String computationId = startComputation(algorithmId, - inputParameters, outputData); + inputParameters, outputData, scope); // print check on retrieving data // logger.info("dbmanager-> output data retrieved"); @@ -1375,6 +1494,11 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements throw new SessionExpiredException(); try { + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); + // data input List inputParameters = new ArrayList(); // output sample result @@ -1441,7 +1565,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements ComputationOutput outputData = new ComputationOutput(); // computation id String computationId = startComputation(algorithmId, - inputParameters, outputData); + inputParameters, outputData, scope); // print check on retrieving data // logger.info("dbmanager-> output data retrieved"); @@ -1491,8 +1615,14 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements if (isSessionExpired()) throw new SessionExpiredException(); - try { + + try { + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); + // data input List inputParameters = new ArrayList(); // data ouptut @@ -1531,7 +1661,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements ComputationOutput outputData = new ComputationOutput(); // computation id String computationId = startComputation(algorithmId, - inputParameters, outputData); + inputParameters, outputData, scope); // print check on retrieving data // logger.info("output data retrieved"); @@ -2033,14 +2163,15 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } private String startComputation(String algorithmName, - List parameters, ComputationOutput outputData) - throws Exception { - return startComputation(algorithmName, parameters, outputData, null); + List parameters, ComputationOutput outputData, + String scope) throws Exception { + return startComputation(algorithmName, parameters, outputData, scope, + null); } private String startComputation(String algorithmName, List parameters, ComputationOutput outputData, - String jobID) throws Exception { + String scopeValue, String jobID) throws Exception { SMComputationConfig config = new SMComputationConfig(); SMInputEntry[] list = new SMInputEntry[parameters.size()]; @@ -2057,11 +2188,13 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements request.config(config); try { - StatisticalManagerFactory factory = getFactory(); + + StatisticalManagerFactory factory = getFactory(scopeValue); String computationId = factory.executeComputation(request); float percentage = 0; - String scope = getScope(); + // String scope = getScope(); + String scope = scopeValue; String username = getUsername(); updateJobStatus(jobID, "computation started"); @@ -2231,9 +2364,9 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements return u.openConnection().getInputStream(); } - private StatisticalManagerFactory getFactory() { - HttpSession httpSession = this.getThreadLocalRequest().getSession(); - return SessionUtil.getFactory(httpSession); + private StatisticalManagerFactory getFactory(String scope) { + // HttpSession httpSession = this.getThreadLocalRequest().getSession(); + return SessionUtil.getFactory(scope); } private String getUsername() { @@ -2287,7 +2420,9 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } if (computationId != null) { - StatisticalManagerFactory factory = getFactory(); + + // TODO ******* TO MODIFY + StatisticalManagerFactory factory = getFactory(""); try { factory.removeComputation(computationId); // remove submit query result @@ -2347,12 +2482,20 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } private synchronized void insertDataIntoCache(net.sf.ehcache.Element data) { - DBCache.put(data); + if (DBCache != null) { + DBCache.put(data); + } // System.out.println("data inserted"); } private synchronized net.sf.ehcache.Element getDataFromCache(String key) { - net.sf.ehcache.Element data = DBCache.get(key); + +// System.out.println("***get item with KEY: " + key); + + net.sf.ehcache.Element data = null; + if (DBCache != null) { + data = DBCache.get(key); + } return data; } @@ -2364,8 +2507,15 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements // session check if (isSessionExpired()) throw new SessionExpiredException(); + + try { + + ASLSession session = SessionUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + // get scope + String scope = session.getScope(); // // call the method related to the element selected // @@ -2374,6 +2524,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements GeneralOutputFromServlet result = null; + //build key if (inputData != null && inputData.size() != 0) { // System.out.println("server-> input size not null"); String key = ""; @@ -2386,10 +2537,10 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements // System.out.println("server-> element type not null"); switch (ElementType) { case "listResources": - key = inputData.get("listResources"); + key = scope+inputData.get("listResources"); break; case "resource": - key = inputData.get("ResourceName"); + key = scope+inputData.get("ResourceName"); break; case "database": if (element.getDatabaseType().equals( @@ -2399,7 +2550,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements + inputData.get("DatabaseName"); // refresh submitted queries - keyUsedForQueryRefresh = ConstantsPortlet.ALGID_SUBMITQUERY + keyUsedForQueryRefresh = scope+ConstantsPortlet.ALGID_SUBMITQUERY + key; refreshSubmittedQueryInCache(keyUsedForQueryRefresh); @@ -2411,14 +2562,16 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements + inputData.get("DatabaseName") + inputData.get("SchemaName"); - keyUsedForQueryRefresh = ConstantsPortlet.ALGID_SUBMITQUERY + keyUsedForQueryRefresh = scope+ConstantsPortlet.ALGID_SUBMITQUERY + key; refreshSubmittedQueryInCache(keyUsedForQueryRefresh); - keyUsedForSamplingsRefresh = ConstantsPortlet.ALGID_SAMPLEONTABLE + keyUsedForSamplingsRefresh = scope+ConstantsPortlet.ALGID_SAMPLEONTABLE + key; - keyUsedForSmartSamplingRefresh = ConstantsPortlet.ALGID_SMARTSAMPLEONTABLE + keyUsedForSmartSamplingRefresh = scope+ConstantsPortlet.ALGID_SMARTSAMPLEONTABLE + key; + + key = scope + key; refreshSamplingsInCache(keyUsedForSamplingsRefresh, keyUsedForSmartSamplingRefresh); @@ -2426,24 +2579,26 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements break; case "schema": - + key = inputData.get("ResourceName") + inputData.get("DatabaseName") + inputData.get("SchemaName"); - // refresh submitted query and samplings - keyUsedForQueryRefresh = ConstantsPortlet.ALGID_SUBMITQUERY + // refresh submitted query and samplings and tables list + keyUsedForQueryRefresh = scope+ConstantsPortlet.ALGID_SUBMITQUERY + inputData.get("ResourceName") + inputData.get("DatabaseName"); refreshSubmittedQueryInCache(keyUsedForQueryRefresh); - keyUsedForSamplingsRefresh = ConstantsPortlet.ALGID_SAMPLEONTABLE + keyUsedForSamplingsRefresh = scope+ConstantsPortlet.ALGID_SAMPLEONTABLE + key; - keyUsedForSmartSamplingRefresh = ConstantsPortlet.ALGID_SMARTSAMPLEONTABLE + keyUsedForSmartSamplingRefresh = scope+ConstantsPortlet.ALGID_SMARTSAMPLEONTABLE + key; + + key = scope+key; refreshSamplingsInCache(keyUsedForSamplingsRefresh, keyUsedForSmartSamplingRefresh); - + break; } } @@ -2560,9 +2715,9 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } // remove keys DBCache.removeAll(keysToBeRemoved); -// System.out.println("Samplings refreshed with keys: " -// + keyUsedForSamplingsRefresh + " " -// + keyUsedForSmartSamplingRefresh); + // System.out.println("Samplings refreshed with keys: " + // + keyUsedForSamplingsRefresh + " " + // + keyUsedForSmartSamplingRefresh); } private String storeResultIntoCSVFile(List result, String n) @@ -2627,4 +2782,631 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements return queryParsed; } + private synchronized void setEndThreadvariable() { + endThread = true; + logger.info("dbmanager-> variable EndThread set in order to stop the thread execution"); + + } + + private synchronized boolean getEndThreadvariable() { + return endThread; + } + + private List recoverResources(String scope) throws Exception { + + try { + + // initialize variables with application startup + // initVariables(); + + // data input + List inputParameters = new ArrayList(); + // data output + List outputParameters = new ArrayList(); + + // get algorithmId + String algorithmId = ConstantsPortlet.ALGID_GETRESOURCE; + + Parameter maxNumber = new Parameter("MaxNumber", "", "String", "-1"); + inputParameters.add(maxNumber); + + // check if the value is in cache. If data does not exist in cache + // the computation is started otherwise data are retrieved from + // cache. + + // get data from cache + // check if data exist considering as key the input parameters + // String key = inputParameters.get(0).getDefaultValue(); + String key = scope+"listResources"; + +// System.out.println("***GetResource KEY: " + key); + net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + + Object value = null; + if (dataFromCache != null) { + value = dataFromCache.getObjectValue(); + // System.out.println("***GETTING DATA FROM CACHE"); + } + if (value != null) { +// System.out.println("***GETTING DATA FROM CACHE"); + outputParameters = (List) value; + } else { + + // start the computation + // System.out.println("***STARTING THE COMPUTATION"); + // create data structure for data output + ComputationOutput outputData = new ComputationOutput(); + // computationId + String computationId = startComputation(algorithmId, + inputParameters, outputData, scope); + + // print check + // retrieve data + // logger.info("output data retrieved"); + + // data output + LinkedHashMap mapValues = new LinkedHashMap(); + mapValues = outputData.getMapValues(); + + for (int i = 0; i < mapValues.size(); i++) { + FileModel obj = new FileModel(mapValues.get(String + .valueOf(i))); + // obj.setIsLoaded(true); + outputParameters.add(obj); + } + + if (outputParameters != null && outputParameters.size() != 0) { + // put data in cache + net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + key, outputParameters); + + insertDataIntoCache(dataToCache); + } + } + + return outputParameters; + + } catch (Exception e) { + // e.printStackTrace(); + // throw new Exception("Failed to load data. " + e); + logger.error("dbmanager-> ", e); + if (!(e instanceof StatisticalManagerException)) { + // GWT can't serialize all exceptions + throw new Exception( + "Error in server while loading data. Exception: " + e); + } + throw e; + } + } + + private LinkedHashMap recoverDatabases(String scope, + String resourceName) throws Exception { + try { + // data input + List inputParameters = new ArrayList(); + // data output + LinkedHashMap outputParameters = new LinkedHashMap(); + + String algorithmId = ConstantsPortlet.ALGID_GETDBINFO; + + // print check + logger.info("dbmanager-> ResourceName: " + resourceName); + + if ((resourceName == null) || (resourceName.equals(""))) { + throw new Exception("Unable to load data"); + } + + Parameter resource = new Parameter("ResourceName", "", "String", ""); + inputParameters.add(resource); + inputParameters.get(0).setValue(resourceName); + + // get data from cache + // check if data exist considering as key the input parameters + String key = scope+inputParameters.get(0).getValue(); + + // System.out.println("***KEY: " + key); + net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + + Object value = null; + if (dataFromCache != null) { + value = dataFromCache.getObjectValue(); + } + if (value != null) { + outputParameters = (LinkedHashMap) value; + // System.out.println("***GETTING DATA FROM CACHE"); + } else { + + // start the computation + // System.out.println("***STARTING THE COMPUTATION"); + // create data structure + ComputationOutput outputData = new ComputationOutput(); + // computation id + String computationId = startComputation(algorithmId, + inputParameters, outputData, scope); + + // print check + // retrieve data + // logger.info("output data retrieved"); + + // data output values + LinkedHashMap mapValues = new LinkedHashMap(); + // data output keys + LinkedHashMap mapKeys = new LinkedHashMap(); + + mapValues = outputData.getMapValues(); + mapKeys = outputData.getmapKeys(); + + for (int i = 0; i < mapValues.size(); i++) { + FileModel obj = new FileModel(mapValues.get(String + .valueOf(i))); + // obj.setIsLoaded(true); + + // print check + // logger.info("value: " + + // mapValues.get(String.valueOf(i))); + // logger.info("key: " + + // mapKeys.get(String.valueOf(i))); + outputParameters.put(mapKeys.get(String.valueOf(i)), obj); + } + + // write data in cache + if (outputParameters != null && outputParameters.size() != 0) { + // put data in cache +// net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( +// inputParameters.get(0).getValue(), outputParameters); + net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + key, outputParameters); + + insertDataIntoCache(dataToCache); + } + } + + return outputParameters; + + } catch (Exception e) { + // e.printStackTrace(); + // throw new Exception("Failed to load data " + ); + logger.error("dbmanager-> ", e); + + if (!(e instanceof StatisticalManagerException)) { + // GWT can't serialize all exceptions + throw new Exception( + "Error in server while loading data. Exception: " + e); + } + throw e; + } + } + + private List recoverSchema(String scope, + LinkedHashMap dataInput) throws Exception { + + try { + + // data input + List inputParameters = new ArrayList(); + // data output + List outputParameters = new ArrayList(); + + String algorithmId = ConstantsPortlet.ALGID_GETDBSCHEMA; + // print check + String rs = dataInput.get("ResourceName"); + String db = dataInput.get("DatabaseName"); + + logger.info("dbmanager-> ResourceName: " + rs); + logger.info("dbmanager-> DatabaseName: " + db); + + if ((rs == null) || (rs.equals(""))) { + throw new Exception("Unable to load data"); + } + if ((db == null) || (db.equals(""))) { + throw new Exception("Unable to load data"); + } + + // set input parameters + Parameter resource = new Parameter("ResourceName", "", "String", ""); + Parameter database = new Parameter("DatabaseName", "", "String", ""); + inputParameters.add(resource); + inputParameters.add(database); + + inputParameters.get(0).setValue(rs); + inputParameters.get(1).setValue(db); + + // print check algorithm input parameters + // for (int i = 0; i < inputParameters.size(); i++) { + // logger.info(inputParameters.get(i).getName()); + // } + + // get data from cache + // check if data exist considering as key the input parameters + String key = scope+inputParameters.get(0).getValue() + + inputParameters.get(1).getValue(); + + // System.out.println("key in GETSCHEMA: " + key); + net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + + Object value = null; + if (dataFromCache != null) { + value = dataFromCache.getObjectValue(); + } + if (value != null) { + outputParameters = (List) value; + } else { + + // start the computation + // create data structure + ComputationOutput outputData = new ComputationOutput(); + // computation id + String computationId = startComputation(algorithmId, + inputParameters, outputData, scope); + + // print check + // retrieve data + // logger.info("dbmanager-> output data retrieved"); + + // data output values + LinkedHashMap mapValues = new LinkedHashMap(); + // data output keys + LinkedHashMap mapKeys = new LinkedHashMap(); + + mapValues = outputData.getMapValues(); + mapKeys = outputData.getmapKeys(); + + for (int i = 0; i < mapValues.size(); i++) { + FileModel obj = new FileModel(mapValues.get(String + .valueOf(i))); + // obj.setIsSchema(true); + // obj.setIsLoaded(true); + outputParameters.add(obj); + } + + // write data in cache + if (outputParameters != null && outputParameters.size() != 0) { + // put data in cache +// net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( +// inputParameters.get(0).getValue() +// + inputParameters.get(1).getValue(), +// outputParameters); + + net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + key,outputParameters); + + insertDataIntoCache(dataToCache); + // DBCache.put(dataToCache); + } + } + + return outputParameters; + + } catch (Exception e) { + // e.printStackTrace(); + // throw new Exception("Failed to load data. " + e); + logger.error("dbmanager-> ", e); + if (!(e instanceof StatisticalManagerException)) { + // GWT can't serialize all exceptions + throw new Exception( + "Error in server while loading data. Exception: " + e); + } + throw e; + } + + } + + private List recoverTables(String scope, + LinkedHashMap dataInput, String elementType) + throws Exception { + try { + + // data input + List inputParameters = new ArrayList(); + // data output + List outputParameters = new ArrayList(); + + String algorithmId = ConstantsPortlet.ALGID_GETTABLES; + + String rs = dataInput.get("ResourceName"); + String db = dataInput.get("DatabaseName"); + String scm = dataInput.get("SchemaName"); + + // print check + logger.info("dbmanager-> ResourceName: " + rs); + logger.info("dbmanager-> DatabaseName: " + db); + logger.info("dbmanager-> SchemaName: " + scm); + + if ((elementType != null) + && (elementType.equals(ConstantsPortlet.SCHEMA))) { + if ((rs == null) || (rs.equals(""))) { + throw new Exception("Unable to load data"); + } + if ((db == null) || (db.equals(""))) { + throw new Exception("Unable to load data"); + } + if ((scm == null) || (scm.equals(""))) { + throw new Exception("Unable to load data"); + } + } + if ((elementType != null) + && (elementType.equals(ConstantsPortlet.DATABASE))) { + if ((rs == null) || (rs.equals(""))) { + throw new Exception("Unable to load data"); + } + if ((db == null) || (db.equals(""))) { + throw new Exception("Unable to load data"); + } + } + + // set input parameters + Parameter resource = new Parameter("ResourceName", "", "String", ""); + Parameter database = new Parameter("DatabaseName", "", "String", ""); + Parameter schema = new Parameter("SchemaName", "", "String", ""); + inputParameters.add(resource); + inputParameters.add(database); + inputParameters.add(schema); + + inputParameters.get(0).setValue(rs); + inputParameters.get(1).setValue(db); + inputParameters.get(2).setValue(scm); + + // get data from cache + // check if data exist considering as key the input parameters + String key = scope+inputParameters.get(0).getValue() + + inputParameters.get(1).getValue() + + inputParameters.get(2).getValue(); + + // System.out.println("***KEY: " + key); + net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + + Object value = null; + if (dataFromCache != null) { + value = dataFromCache.getObjectValue(); + // System.out.println("***GETTING DATA FROM CACHE"); + } + if (value != null) { + outputParameters = (List) value; + + } else { + + // start computation + // create data structure + ComputationOutput outputData = new ComputationOutput(); + // computation id + String computationId = startComputation(algorithmId, + inputParameters, outputData, scope); + + // print check on retrieving data + // logger.info("output data retrieved"); + + // data output values + LinkedHashMap mapValues = new LinkedHashMap(); + // data output keys + LinkedHashMap mapKeys = new LinkedHashMap(); + + mapValues = outputData.getMapValues(); + mapKeys = outputData.getmapKeys(); + + for (int i = 0; i < mapValues.size(); i++) { + Result row = new Result(String.valueOf(i), + mapValues.get(String.valueOf(i))); + outputParameters.add(row); + } + + // write data in cache + if (outputParameters != null && outputParameters.size() != 0) { + // put data in cache +// net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( +// inputParameters.get(0).getValue() +// + inputParameters.get(1).getValue() +// + inputParameters.get(2).getValue(), +// outputParameters); + net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + key, outputParameters); + + insertDataIntoCache(dataToCache); + } + + } + return outputParameters; + + } catch (Exception e) { + // e.printStackTrace(); + // throw new Exception("Failed to load data. " + e); + // logger.error("dbmanager-> ", e); + + if (!(e instanceof StatisticalManagerException)) { + // GWT can't serialize all exceptions + throw new Exception( + "Error in server while loading data. Exception: " + e); + } + throw e; + } + + } + + private synchronized void updateThreadsStarted(String scope, Boolean value) { + threadsStarted.put(scope, value); + } + + private synchronized Boolean getThreadStarted(String scope) { + Boolean value = threadsStarted.get(scope); + return value; + } + + // thread that loads data on the resources + private class ThreadLoaderData implements Runnable { + private String scope; + + public ThreadLoaderData(String s) { + scope = s; + } + + @Override + public void run() { + try { + + // add an element related to the thread + updateThreadsStarted(scope, true); + + if (!getEndThreadvariable()) { + List resources = recoverResources(scope); + + int i = 0; + while ((!getEndThreadvariable()) && (i < resources.size())) { + getDatabase(resources.get(i).getName()); + i++; + logger.error("dbmanager-> ***inside the while"); + } + + logger.error("dbmanager-> ***outside the while"); + } + + logger.info("dbmanager-> ***Thread execution terminated"); + + // List resources = recoverResources(scope); + // + // for (int i = 0; i < resources.size(); i++) { + // getDatabase(resources.get(i).getName()); + // } + + } catch (Exception e) { + logger.error("dbmanager-> ", e); + } + + } + + private void getDatabase(String resourceName) { + + try { + + LinkedHashMap DBdata = recoverDatabases( + scope, resourceName); + if (DBdata != null) { + Set keys = DBdata.keySet(); + Object[] array = keys.toArray(); + + int numIterations = (DBdata.size()) / 5; + int i = 0; + int j = 0; + for (i = 0; i < numIterations; i++) { + // for (i = 0; i < 1; i++) { + + // String DBName = ""; + // for (j = (i * 5); j < (i + 1) * 5; j++) { + String DBName = ""; + j = (i * 5); + while ((!getEndThreadvariable()) && (j < ((i + 1) * 5))) { + + if (array[j].toString().contains("Database Name")) { + DBName = DBdata.get(array[j].toString()) + .getName(); + } + + if (array[j].toString().contains("Driver Name")) { + String driver = DBdata.get(array[j].toString()) + .getName(); + + if (driver.toUpperCase().contains( + ConstantsPortlet.POSTGRES)) { + // get schema + List schemaList = getSchema( + resourceName, DBName); + // recover tables + if (schemaList != null) { + int z = 0; + while ((!getEndThreadvariable()) + && (z < schemaList.size())) { + // for (int i = 0; i < + // schemaList.size(); i++) { + String schemaName = schemaList.get( + z).getName(); +// System.out.println("SchemaName: " +// + schemaName); + + // LinkedHashMap + // dataInputForTables = new + // LinkedHashMap(); + // dataInputForTables.put("ResourceName", + // resourceName); + // dataInputForTables.put("DatabaseName", + // DBName); + // dataInputForTables.put("SchemaName", + // schemaName); + getTables(resourceName, DBName, + schemaName, + ConstantsPortlet.SCHEMA); + z++; + } + } + + } + + if (driver.toUpperCase().contains( + ConstantsPortlet.MYSQL)) { + // get tables + getTables(resourceName, DBName, "", + ConstantsPortlet.DATABASE); + } + } + j++; + } + } + } + } catch (Exception e) { + logger.error("dbmanager-> ", e); + } + } + + public List getSchema(String resourceName, + String databaseName) { + + List schemaList = null; + try { + + LinkedHashMap dataInputForSchema = new LinkedHashMap(); + dataInputForSchema.put("ResourceName", resourceName); + dataInputForSchema.put("DatabaseName", databaseName); + + schemaList = recoverSchema(scope, dataInputForSchema); + + // if (schemaList != null) { + // int i=0; + // while((!getEndThreadvariable())&&( i < schemaList.size())){ + // // for (int i = 0; i < schemaList.size(); i++) { + // String schemaName = schemaList.get(i).getName(); + // System.out.println("SchemaName: " + schemaName); + // + // LinkedHashMap dataInputForTables = new + // LinkedHashMap(); + // dataInputForTables.put("ResourceName", resourceName); + // dataInputForTables.put("DatabaseName", databaseName); + // dataInputForTables.put("SchemaName", schemaName); + // recoverTables(scope, dataInputForTables, + // ConstantsPortlet.SCHEMA); + // i++; + // } + // } + + } catch (Exception e) { + logger.error("dbmanager-> ", e); + } + return schemaList; + } + + public void getTables(String resourceName, String databaseName, + String schemaName, String elementType) { + try { + + LinkedHashMap dataInputForTables = new LinkedHashMap(); + dataInputForTables.put("ResourceName", resourceName); + dataInputForTables.put("DatabaseName", databaseName); + dataInputForTables.put("SchemaName", schemaName); + + recoverTables(scope, dataInputForTables, + ConstantsPortlet.DATABASE); + } catch (Exception e) { + logger.error("dbmanager-> ", e); + } + + } + + } + }