package org.gcube.portlets.user.databasesmanager.server; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; import javax.servlet.http.HttpSession; import org.apache.regexp.RE; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.contentmanager.storageclient.model.protocol.smp.SMPURLConnection; import org.gcube.data.analysis.statisticalmanager.proxies.StatisticalManagerDSL; import org.gcube.data.analysis.statisticalmanager.proxies.StatisticalManagerFactory; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMAlgorithm; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMComputationConfig; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMComputationRequest; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMGroupedAlgorithms; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMListGroupedAlgorithms; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMOperationStatus; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMParameter; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMParameters; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMResourceType; import org.gcube.data.analysis.statisticalmanager.stubs.types.SMTypeParameter; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMAbstractResource; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMComputation; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMEntries; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMFile; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMInputEntry; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMObject; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMOperationInfo; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMResource; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.SMTable; import org.gcube.data.analysis.statisticalmanager.stubs.types.schema.StatisticalServiceType; import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes; import org.gcube.portlets.user.databasesmanager.client.GWTdbManagerService; import org.gcube.portlets.user.databasesmanager.client.datamodel.ComputationOutput; import org.gcube.portlets.user.databasesmanager.client.datamodel.FileModel; import org.gcube.portlets.user.databasesmanager.client.datamodel.Parameter; import org.gcube.portlets.user.databasesmanager.client.datamodel.Result; import org.gcube.portlets.user.databasesmanager.client.datamodel.Row; import org.gcube.portlets.user.databasesmanager.server.util.SessionUtil; import org.gcube.portlets.user.databasesmanager.server.util.WsUtil; import com.extjs.gxt.ui.client.data.BasePagingLoadResult; import com.extjs.gxt.ui.client.data.PagingLoadConfig; import com.extjs.gxt.ui.client.data.PagingLoadResult; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.thoughtworks.xstream.XStream; import org.apache.log4j.Logger; public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements GWTdbManagerService { // // the result generated in the LoadTables method // private List result = null; // information about a database // private String currentDB = ""; // private String previousDB = ""; // information about a schema // private String currentSchema = ""; // private String previousSchema = ""; //Hashmap that contains computationId // HashMap computationIDMap = new HashMap(); // logger private static Logger logger = Logger .getLogger(GWTdbManagerServiceImpl.class); public GWTdbManagerServiceImpl() { } private void initVariables(){ ASLSession session = WsUtil.getAslSession(this.getThreadLocalRequest().getSession()); // the result generated in the LoadTables method List result = new ArrayList(); session.setAttribute("TablesResult", result); // information about a database String currentDB = ""; session.setAttribute("currentDB", currentDB); String previousDB = ""; session.setAttribute("previousDB", previousDB); // information about a schema String currentSchema = ""; session.setAttribute("currentSchema", currentSchema); String previousSchema = ""; session.setAttribute("previousSchema", previousSchema); } // to get resources from IS @Override public List getResource() throws Exception { //initialize variables with application startup initVariables(); // data input List inputParameters = new ArrayList(); // data output List outputParameters = new ArrayList(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("LISTDBNAMES")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm specified by id inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } // // print check input parameters // for (int i = 0; i < inputParameters.size(); i++) { // logger.info(inputParameters.get(i).getName()); // } // 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); } return outputParameters; } // to get information about databases of a resource @Override public LinkedHashMap getDBInfo(String resourceName) throws Exception { // data input List inputParameters = new ArrayList(); // data output LinkedHashMap outputParameters = new LinkedHashMap(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("LISTDBINFO")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm specified by id inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } inputParameters.get(0).setValue(resourceName); // print check // logger.info(inputParameters.get(0).getName()); // 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); } return outputParameters; } // to get schema for a database @Override public List getDBSchema(LinkedHashMap dataInput) throws Exception { // data input List inputParameters = new ArrayList(); // data output List outputParameters = new ArrayList(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("LISTDBSCHEMA")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm specified by id inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } // print check algorithm input parameters // for (int i = 0; i < inputParameters.size(); i++) { // logger.info(inputParameters.get(i).getName()); // } inputParameters.get(0).setValue(dataInput.get("ResourceName")); inputParameters.get(1).setValue(dataInput.get("DatabaseName")); // print check logger.info("ResourceName: " + dataInput.get("ResourceName")); logger.info("DatabaseName: " + dataInput.get("DatabaseName")); // 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.setIsSchema(true); // obj.setIsLoaded(true); outputParameters.add(obj); } return outputParameters; } // to get tables private List getTables(LinkedHashMap dataInput) throws Exception { // data input List inputParameters = new ArrayList(); // data output List outputParameters = new ArrayList(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("LISTTABLES")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } // print check algorithm input parameters // for (int i = 0; i < inputParameters.size(); i++) { // logger.info(inputParameters.get(i).getName()); // } inputParameters.get(0).setValue(dataInput.get("ResourceName")); inputParameters.get(1).setValue(dataInput.get("DatabaseName")); inputParameters.get(2).setValue(dataInput.get("SchemaName")); // print check logger.info("ResourceName: " + dataInput.get("ResourceName")); logger.info("DatabaseName: " + dataInput.get("DatabaseName")); logger.info("SchemaName: " + dataInput.get("SchemaName")); // 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); } return outputParameters; } // to load tables @Override public PagingLoadResult LoadTables(PagingLoadConfig config, LinkedHashMap dataInput, boolean SearchTable, String keyword) throws Exception { ASLSession session = WsUtil.getAslSession(this.getThreadLocalRequest().getSession()); List result = (List) session.getAttribute("TablesResult"); // check on a database String currentDB = ""; currentDB = dataInput.get("DatabaseName"); String previousDB = (String) session.getAttribute("previousDB"); if (!currentDB.equals(previousDB)) { // result = null; result = new ArrayList(); System.gc(); } previousDB = currentDB; session.setAttribute("previousDB", previousDB); // check on a schema String currentSchema = ""; currentSchema = dataInput.get("SchemaName"); String previousSchema = (String) session.getAttribute("previousSchema"); if (!currentSchema.equals(previousSchema)) { // result = null; result = new ArrayList(); System.gc(); } previousSchema = currentSchema; session.setAttribute("previousSchema", previousSchema); // get tables // if (result == null) // result = getTables(dataInput); if (result.size()==0) result = getTables(dataInput); // Create a sublist and add data to list according // to the limit and offset value of the config List sublist = new ArrayList(); BasePagingLoadResult loadResult = null; // print check on the search // logger.info("Searching in the table: " + SearchTable); // logger.info("Keyword to search: " + keyword); int start = config.getOffset(); int limit = result.size(); if (config.getLimit() > 0) { limit = Math.min(start + config.getLimit(), limit); } int totalNumber = result.size(); if ((SearchTable == false) || keyword == null || keyword.length() == 0) { sublist = new ArrayList(result.subList(start, limit)); } else { // print check // logger.info("searching the table"); // search the table for (int i = 0; i < result.size(); i++) { if ((result.get(i).getValue().toLowerCase()).startsWith(keyword .toLowerCase())) { sublist.add(result.get(i)); } } limit = sublist.size(); int sublen = sublist.size(); totalNumber = sublen; if (start < sublen - 1) { limit = Math.min(sublen, limit); totalNumber = sublist.size(); sublist = new ArrayList(sublist.subList(start, limit)); } } // print check // logger.info("result size: " + totalNumber); // logger.info("limit: " + limit); // logger.info("offset: " + config.getOffset()); // logger.info("start: " + start); loadResult = new BasePagingLoadResult(sublist, config.getOffset(), totalNumber); session.setAttribute("TablesResult", result); return loadResult; } // to submit a query @Override public List submitQuery(LinkedHashMap dataDB, String query, boolean valueReadOnlyQuery, boolean smartCorrectionQuery, String language) throws Exception { logger.info("Dialect used for smart correction: " + language); // data input List inputParameters = new ArrayList(); // data output List output = new ArrayList(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("SUBMITQUERY")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } // print check // for (int i = 0; i < inputParameters.size(); i++) { // logger.info(inputParameters.get(i).getName()); // } // print check logger.info("ResourceName: " + dataDB.get("ResourceName")); logger.info("DatabaseName: " + dataDB.get("DatabaseName")); logger.info("Query: " + query); logger.info("SmartCorrections check: " + smartCorrectionQuery); inputParameters.get(0).setValue(dataDB.get("ResourceName")); inputParameters.get(1).setValue(dataDB.get("DatabaseName")); inputParameters.get(2).setValue(String.valueOf(valueReadOnlyQuery)); inputParameters.get(3).setValue(String.valueOf(smartCorrectionQuery)); inputParameters.get(4).setValue(language); inputParameters.get(5).setValue(query); // create data structure ComputationOutput outputData = new ComputationOutput(); // computation id String computationId = startComputation(algorithmId, inputParameters, outputData); // computationIDMap.put(id, computationId); // 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(mapKeys.get(String.valueOf(i)), mapValues.get(String.valueOf(i))); output.add(row); } return output; } // // to submit a query // public String startSubmitQueryComputation( // LinkedHashMap dataDB, String query, // boolean valueReadOnlyQuery, boolean smartCorrectionQuery, // String language) { // // logger.info("Dialect used for smart correction: " + language); // // // data input // List inputParameters = new ArrayList(); // // data output // List output = new ArrayList(); // // // get list of algorithms // List algorithms = new ArrayList(); // algorithms = getDatabaseManagerAlgorithms(); // // // get algorithmId // String algorithmId = null; // // for (int i = 0; i < algorithms.size(); i++) { // if (algorithms.get(i).equals("SUBMITQUERY")) { // algorithmId = algorithms.get(i); // // // print check // // logger.info("algorithmId: " + algorithmId); // } // } // // // get input parameters of the algorithm // inputParameters = getParameters(algorithmId); // // if (inputParameters.size() != 0) { // // print check // logger.info("algorithm input parameters retrieved"); // } // // // print check // // for (int i = 0; i < inputParameters.size(); i++) { // // logger.info(inputParameters.get(i).getName()); // // } // // // print check // logger.info("ResourceName: " + dataDB.get("ResourceName")); // logger.info("DatabaseName: " + dataDB.get("DatabaseName")); // logger.info("Query: " + query); // logger.info("SmartCorrections check: " + smartCorrectionQuery); // // inputParameters.get(0).setValue(dataDB.get("ResourceName")); // inputParameters.get(1).setValue(dataDB.get("DatabaseName")); // inputParameters.get(2).setValue(String.valueOf(valueReadOnlyQuery)); // inputParameters.get(3).setValue(String.valueOf(smartCorrectionQuery)); // inputParameters.get(4).setValue(language); // inputParameters.get(5).setValue(query); // // // // create data structure // // ComputationOutput outputData = new ComputationOutput(); // // // get computation id // // SMComputationConfig config = new SMComputationConfig(); // SMInputEntry[] list = new SMInputEntry[inputParameters.size()]; // int i = 0; // // for (Parameter p : inputParameters) // list[i++] = new SMInputEntry(p.getName(), p.getValue()); // config.parameters(new SMEntries(list)); // config.algorithm(algorithmId); // // // create a computation request // SMComputationRequest request = new SMComputationRequest(); // request.user(getUsername()); // request.config(config); // // // execute computation // StatisticalManagerFactory factory = getFactory(); // String computationId = factory.executeComputation(request); // // return computationId; // } // public float startCheckSubmitQueryComputation(String computationId) { // // String scope = getScope(); // String user = getUsername(); // // ScopeProvider.instance.set(scope); // // StatisticalManagerFactory factory = StatisticalManagerDSL // .createStateful().build(); // // SMComputation computation = factory.getComputation(computationId); // // SMOperationStatus status = SMOperationStatus.values()[computation // .operationStatus()]; // // float percentage = 0; // // if (status == SMOperationStatus.RUNNING) { // // // logger.info("RUNNING"); // SMOperationInfo infos = factory.getComputationInfo(computationId, // user); // // percentage = Float.parseFloat(infos.percentage()); // // logger.info("Percentage:" + // // percentage); // // computation = factory.getComputation(computationId); // // status = SMOperationStatus.values()[computation.operationStatus()]; // } else if ((status == SMOperationStatus.COMPLETED) // || (status == SMOperationStatus.FAILED)) { // // // logger.info("COMPLETED OR FAILED"); // // SMAbstractResource abstractResource = computation // .abstractResource(); // SMResource smResource = abstractResource.resource(); // int resourceTypeIndex = smResource.resourceType(); // SMResourceType smResType = SMResourceType.values()[resourceTypeIndex]; // // // displayOutput(smResource, smResType, outputData); // // // print check // // logger.info("SM resource Name: " + smResource.name()); // // logger.info("SM resource Name: " + smResource.name()); // // logger.info("SM resource ID: " + smResource.resourceId()); // // logger.info("SM resource ID: " + smResource.resourceId()); // // logger.info("SM resource Description: " + // // smResource.description()); // // percentage = 100; // } // return percentage; // // } // public List getSubmitQueryOutput(String computationId) // throws Exception { // // String scope = getScope(); // // ScopeProvider.instance.set(scope); // // StatisticalManagerFactory factory = StatisticalManagerDSL // .createStateful().build(); // // SMComputation computation = factory.getComputation(computationId); // // SMAbstractResource abstractResource = computation.abstractResource(); // SMResource smResource = abstractResource.resource(); // int resourceTypeIndex = smResource.resourceType(); // SMResourceType smResType = SMResourceType.values()[resourceTypeIndex]; // // // create data structure // ComputationOutput outputData = new ComputationOutput(); // // displayOutput(smResource, smResType, outputData); // // // print check on retrieving data // logger.info("output data retrieved"); // // // data output // List output = new ArrayList(); // // // 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(mapKeys.get(String.valueOf(i)), // mapValues.get(String.valueOf(i))); // output.add(row); // } // // return output; // } @Override public List sample(LinkedHashMap dataInput) throws Exception { // data input List inputParameters = new ArrayList(); // output sample result List output = new ArrayList(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("SAMPLEONTABLE")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm specified by id inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } // print check // for (int i = 0; i < inputParameters.size(); i++) { // logger.info(inputParameters.get(i).getName()); // } // print check logger.info("ResourceName: " + dataInput.get("ResourceName")); logger.info("DatabaseName: " + dataInput.get("DatabaseName")); logger.info("SchemaName: " + dataInput.get("SchemaName")); logger.info("TableName: " + dataInput.get("TableName")); inputParameters.get(0).setValue(dataInput.get("ResourceName")); inputParameters.get(1).setValue(dataInput.get("DatabaseName")); inputParameters.get(2).setValue(dataInput.get("SchemaName")); inputParameters.get(3).setValue(dataInput.get("TableName")); // 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(mapKeys.get(String.valueOf(i)), mapValues.get(String.valueOf(i))); output.add(row); } return output; } @Override public List smartSample(LinkedHashMap dataInput) throws Exception { // data input List inputParameters = new ArrayList(); // output sample result List output = new ArrayList(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("SMARTSAMPLEONTABLE")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } // print check // for (int i = 0; i < inputParameters.size(); i++) { // logger.info(inputParameters.get(i).getName()); // } // print check logger.info("ResourceName: " + dataInput.get("ResourceName")); logger.info("DatabaseName: " + dataInput.get("DatabaseName")); logger.info("SchemaName: " + dataInput.get("SchemaName")); logger.info("TableName: " + dataInput.get("TableName")); inputParameters.get(0).setValue(dataInput.get("ResourceName")); inputParameters.get(1).setValue(dataInput.get("DatabaseName")); inputParameters.get(2).setValue(dataInput.get("SchemaName")); inputParameters.get(3).setValue(dataInput.get("TableName")); // 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(mapKeys.get(String.valueOf(i)), mapValues.get(String.valueOf(i))); output.add(row); } return output; } @Override public List randomSample(LinkedHashMap dataInput) throws Exception { // data input List inputParameters = new ArrayList(); // output sample result List output = new ArrayList(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("RANDOMSAMPLEONTABLE")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm specified by id inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } // print check algorithm input parameters // for (int i = 0; i < inputParameters.size(); i++) { // logger.info(inputParameters.get(i).getName()); // } // print check logger.info("ResourceName: " + dataInput.get("ResourceName")); logger.info("DatabaseName: " + dataInput.get("DatabaseName")); logger.info("SchemaName: " + dataInput.get("SchemaName")); logger.info("TableName: " + dataInput.get("TableName")); inputParameters.get(0).setValue(dataInput.get("ResourceName")); inputParameters.get(1).setValue(dataInput.get("DatabaseName")); inputParameters.get(2).setValue(dataInput.get("SchemaName")); inputParameters.get(3).setValue(dataInput.get("TableName")); // 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(mapKeys.get(String.valueOf(i)), mapValues.get(String.valueOf(i))); output.add(row); } return output; } @Override public LinkedHashMap getTableDetails( LinkedHashMap dataInput) throws Exception { // data input List inputParameters = new ArrayList(); // data ouptut LinkedHashMap outputParameters = new LinkedHashMap(); // get list of algorithms List algorithms = new ArrayList(); algorithms = getDatabaseManagerAlgorithms(); // get algorithmId String algorithmId = null; for (int i = 0; i < algorithms.size(); i++) { if (algorithms.get(i).equals("GETTABLEDETAILS")) { algorithmId = algorithms.get(i); // print check // logger.info("algorithmId: " + algorithmId); } } // get input parameters of the algorithm specified by id inputParameters = getParameters(algorithmId); if (inputParameters.size() != 0) { // print check logger.info("algorithm input parameters retrieved"); } // print check algorithm input parameters // for (int i = 0; i < inputParameters.size(); i++) { // logger.info(inputParameters.get(i).getName()); // } // print check logger.info("ResourceName: " + dataInput.get("ResourceName")); logger.info("DatabaseName: " + dataInput.get("DatabaseName")); logger.info("SchemaName: " + dataInput.get("SchemaName")); logger.info("TableName: " + dataInput.get("TableName")); inputParameters.get(0).setValue(dataInput.get("ResourceName")); inputParameters.get(1).setValue(dataInput.get("DatabaseName")); inputParameters.get(2).setValue(dataInput.get("SchemaName")); inputParameters.get(3).setValue(dataInput.get("TableName")); // 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"); // output data values LinkedHashMap mapValues = new LinkedHashMap(); // output data 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); outputParameters.put(mapKeys.get(String.valueOf(i)), obj); // print check // logger.info("value: " + outputMap.get(String.valueOf(i))); // logger.info("key: " + outputKey.get(String.valueOf(i))); } return outputParameters; } // parse a csv row in a list of values @Override public List parseCVSString(List result, List attrNames) throws Exception { List rows = new ArrayList(); if (result != null) { for (int i = 0; i < result.size(); i++) { List attrValues = parse(result.get(i).getValue()); Row element = new Row(attrNames, attrValues, i); rows.add(element); } } return rows; } private List parse(String row) throws Exception { String delimiter = ","; // print check // logger.info("row: " + row); List elements = new ArrayList(); String phrase = row; int idxdelim = -1; boolean quot = false; phrase = phrase.trim(); while ((idxdelim = phrase.indexOf(delimiter)) >= 0) { quot = phrase.startsWith("\""); if (quot) { phrase = phrase.substring(1); String quoted = ""; if (phrase.startsWith("\"")) phrase = phrase.substring(1); else { RE regexp = new RE("[^\\\\]\""); boolean matching = regexp.match(phrase); if (matching) { int i0 = regexp.getParenStart(0); quoted = phrase.substring(0, i0 + 1).trim(); phrase = phrase.substring(i0 + 2).trim(); } } if (phrase.startsWith(delimiter)) phrase = phrase.substring(1); elements.add(quoted); } else { elements.add(phrase.substring(0, idxdelim)); phrase = phrase.substring(idxdelim + 1).trim(); } // logger.info("server token: " + phrase); } if (phrase.startsWith("\"")) phrase = phrase.substring(1); if (phrase.endsWith("\"")) phrase = phrase.substring(0, phrase.length() - 1); elements.add(phrase); // logger.info("size: " + elements.size()); return elements; } private List getDatabaseManagerAlgorithms() { ArrayList dbAlg = new ArrayList(); StatisticalManagerFactory factory = getFactory(); // get and print algorithms SMListGroupedAlgorithms groups = factory.getAlgorithms(); if (groups == null) logger.info("GROUPS OF ALGORITHMS IS NULL!"); else logger.info("GROUPS OF ALGORITHMS IS NOT NULL!"); // get list categories for (SMGroupedAlgorithms group : groups.thelist()) { for (SMAlgorithm algorithm : group.thelist()) { dbAlg.add(algorithm.name()); } } return dbAlg; } private List getParameters(String algorithmId) { StatisticalManagerFactory factory = getFactory(); SMParameters smParams = factory.getAlgorithmParameters(algorithmId); List params = new ArrayList(); for (SMParameter smParam : smParams.list()) { SMTypeParameter smType = smParam.type(); StatisticalServiceType smTypeName = smType.name(); String paramName = smParam.name(); String paramDescription = smParam.description(); String defaultValue = smParam.defaultValue(); String objectType = smType.values().get(0); // print CHECK // logger.info("parameters: "); // logger.info(paramName); // logger.info(paramDescription); // logger.info(objectType); // logger.info(defaultValue); Parameter objectParam = new Parameter(paramName, paramDescription, objectType, defaultValue); params.add(objectParam); } return params; } private String startComputation(String algorithmName, List parameters, ComputationOutput outputData) throws Exception { SMComputationConfig config = new SMComputationConfig(); SMInputEntry[] list = new SMInputEntry[parameters.size()]; int i = 0; for (Parameter p : parameters) list[i++] = new SMInputEntry(p.getName(), p.getValue()); config.parameters(new SMEntries(list)); config.algorithm(algorithmName); // create a computation request SMComputationRequest request = new SMComputationRequest(); request.user(getUsername()); request.config(config); try { StatisticalManagerFactory factory = getFactory(); String computationId = factory.executeComputation(request); float percentage = 0; String scope = getScope(); String username = getUsername(); while (percentage < 100) { percentage = checkComputationStatus(scope, computationId, username, outputData); Thread.sleep(3000); } return computationId; } catch (Exception e) { // e.printStackTrace(); throw e; } } private float checkComputationStatus(String scope, String computationId, String user, ComputationOutput outputData) throws Exception { ScopeProvider.instance.set(scope); StatisticalManagerFactory factory = StatisticalManagerDSL .createStateful().build(); SMComputation computation = factory.getComputation(computationId); SMOperationStatus status = SMOperationStatus.values()[computation .operationStatus()]; float percentage = 0; if (status == SMOperationStatus.RUNNING) { // logger.info("RUNNING"); SMOperationInfo infos = factory.getComputationInfo(computationId, user); // percentage = Float.parseFloat(infos.percentage()); // logger.info("Percentage:" + // percentage); // computation = factory.getComputation(computationId); status = SMOperationStatus.values()[computation.operationStatus()]; } else if ((status == SMOperationStatus.COMPLETED) || (status == SMOperationStatus.FAILED)) { // logger.info("COMPLETED OR FAILED"); SMAbstractResource abstractResource = computation .abstractResource(); SMResource smResource = abstractResource.resource(); int resourceTypeIndex = smResource.resourceType(); SMResourceType smResType = SMResourceType.values()[resourceTypeIndex]; displayOutput(smResource, smResType, outputData); // print check // logger.info("SM resource Name: " + smResource.name()); // logger.info("SM resource Name: " + smResource.name()); // logger.info("SM resource ID: " + smResource.resourceId()); // logger.info("SM resource ID: " + smResource.resourceId()); // logger.info("SM resource Description: " + // smResource.description()); percentage = 100; } return percentage; } private void displayOutput(SMResource smResource, SMResourceType smResType, ComputationOutput outputData) throws Exception { if (smResType.equals(SMResourceType.OBJECT)) { // switch (smResType) { // case FILE: // SMFile fileRes = (SMFile) smResource; // System.out.println("Output is a file"); // break; // case OBJECT: SMObject objRes = (SMObject) smResource; if (objRes.name().contentEquals(PrimitiveTypes.MAP.toString())) { logger.info("Output is a map"); getMap(objRes, outputData); } else if (objRes.name().contentEquals( PrimitiveTypes.IMAGES.toString())) { // logger.info("Output are images"); } // else // System.out.println("Output is other"); // rootLogger.log(Level.SEVERE, "Output is other"); } // EXCEPTION MANAGEMENT if (smResType.equals(SMResourceType.ERROR)) { Exception e = new Exception(smResource.description()); // e.printStackTrace(); logger.error(e); throw e; } } // get output result private void getMap(SMObject objRes, ComputationOutput outputData) throws Exception { // output data values LinkedHashMap mapValues = new LinkedHashMap(); // output data keys LinkedHashMap mapKeys = new LinkedHashMap(); InputStream is = getStorageClientInputStream(objRes.url()); // object serializer XStream xstream = new XStream(); xstream.alias( "org.gcube_system.namespaces.data.analysis.statisticalmanager.types.SMObject", SMObject.class); xstream.alias( "org.gcube_system.namespaces.data.analysis.statisticalmanager.types.SMFile", SMFile.class); xstream.alias( "org.gcube_system.namespaces.data.analysis.statisticalmanager.types.SMResource", SMResource.class); xstream.alias( "org.gcube_system.namespaces.data.analysis.statisticalmanager.types.SMTable", SMTable.class); @SuppressWarnings("unchecked") Map smMap = (Map) (xstream .fromXML(is)); is.close(); int i = 0; for (String key : smMap.keySet()) { // add key value mapKeys.put(String.valueOf(i), key); SMResource smres = smMap.get(key); int resourceTypeIndex = smres.resourceType(); SMResourceType smsubResType = SMResourceType.values()[resourceTypeIndex]; // logger.info("ResourceType: " + smsubResType); if (smsubResType == SMResourceType.OBJECT) { SMObject obje = (SMObject) smres; String outstring = obje.url(); // logger.info("key: " + smsubResType); // logger.info("object: " + outstring); mapValues.put(String.valueOf(i), outstring); i++; } } outputData.setMapValues(mapValues); outputData.setmapKeys(mapKeys); } private InputStream getStorageClientInputStream(String url) throws Exception { URL u = new URL(null, url, new URLStreamHandler() { @Override protected URLConnection openConnection(URL u) throws IOException { return new SMPURLConnection(u); } }); return u.openConnection().getInputStream(); } private StatisticalManagerFactory getFactory() { HttpSession httpSession = this.getThreadLocalRequest().getSession(); return SessionUtil.getFactory(httpSession); } private String getUsername() { HttpSession httpSession = this.getThreadLocalRequest().getSession(); return SessionUtil.getUsername(httpSession); } private String getScope() { HttpSession httpSession = this.getThreadLocalRequest().getSession(); return SessionUtil.getScope(httpSession); } public void removeComputation(String computationId) throws Exception { StatisticalManagerFactory factory = getFactory(); factory.removeComputation(computationId); } }