databases-manager-portlet/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java

1511 lines
44 KiB
Java

package org.gcube.portlets.user.databasesmanager.server;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
//import java.util.logging.Level;
//import java.util.logging.Logger;
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.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 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 {
// private LinkedHashMap<String, String> outputMap;
// private LinkedHashMap<String, String> outputKey;
// the result generated in the LoadTables method
private List<Result> result = null;
// information about a database
private String currentDB = "";
private String previousDB = "";
// information about a schema
private String currentSchema = "";
private String previousSchema = "";
// GWT logger
// private static Logger rootLogger = Logger
// .getLogger("GWTdbManagerServiceImpl");
// logger
private static Logger logger = Logger
.getLogger(GWTdbManagerServiceImpl.class);
public GWTdbManagerServiceImpl() {
}
public List<FileModel> getResource() throws Exception {
List<Parameter> inputParameters = new ArrayList<Parameter>();
List<FileModel> outputParameters = new ArrayList<FileModel>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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
// System.out.println("algorithmId: " + algorithmId);
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
inputParameters = getParameters(algorithmId);
// print check
// System.out.println("getting input parameters");
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
// print check
// for (int i = 0; i < inputParameters.size(); i++) {
//
// // System.out.println(inputParameters.get(i).getName());
// rootLogger.log(Level.INFO, inputParameters.get(i).getName());
//
// }
// create data structure
ComputationOutput outputData = new ComputationOutput();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// print check
// retrieve data
// System.out.println("output data retrieved");
// rootLogger.log(Level.SEVERE, "output data retrieved");
// logger.info("output data retrieved");
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
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;
}
@Override
public LinkedHashMap<String, FileModel> getDBInfo(String resourceName)
throws Exception {
List<Parameter> inputParameters = new ArrayList<Parameter>();
LinkedHashMap<String, FileModel> outputParameters = new LinkedHashMap<String, FileModel>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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
// System.out.println("algorithmId: " + algorithmId);
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
inputParameters = getParameters(algorithmId);
// print check
for (int i = 0; i < inputParameters.size(); i++) {
// System.out.println(inputParameters.get(i).getName());
inputParameters.get(i).setValue(resourceName);
// rootLogger.log(Level.INFO, "ResourceName: " + resourceName);
// print check
// rootLogger.log(Level.INFO, inputParameters.get(i).getName());
}
// create data structure
ComputationOutput outputData = new ComputationOutput();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// //print check
// retrieve data
// System.out.println("output data retrieved");
// rootLogger.log(Level.SEVERE, "output data retrieved");
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
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);
// System.out.println("value: " + mapValues.get(String.valueOf(i)));
// System.out.println("key: " + mapKeys.get(String.valueOf(i)));
outputParameters.put(mapKeys.get(String.valueOf(i)), obj);
}
return outputParameters;
}
@Override
public List<FileModel> getDBSchema(LinkedHashMap<String, String> dataInput)
throws Exception {
List<Parameter> inputParameters = new ArrayList<Parameter>();
List<FileModel> outputParameters = new ArrayList<FileModel>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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);
logger.info("id: " + algorithmId);
// print check
// System.out.println("algorithmId: " + algorithmId);
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
inputParameters = getParameters(algorithmId);
// print check
// for (int i = 0; i < inputParameters.size(); i++) {
//
// // System.out.println(inputParameters.get(i).getName());
// rootLogger.log(Level.INFO, inputParameters.get(i).getName());
// }
inputParameters.get(0).setValue(dataInput.get("ResourceName"));
inputParameters.get(1).setValue(dataInput.get("DatabaseName"));
// print check
// rootLogger.log(Level.INFO,
// "ResourceName: " + dataInput.get("ResourceName"));
logger.info("ResourceName: " + dataInput.get("ResourceName"));
// rootLogger.log(Level.INFO,
// "DatabaseName: " + dataInput.get("DatabaseName"));
logger.info("DatabaseName: " + dataInput.get("DatabaseName"));
// create data structure
ComputationOutput outputData = new ComputationOutput();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// print check
// retrieve data
// System.out.println("output data retrieved");
// rootLogger.log(Level.SEVERE, "output data retrieved");
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
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;
}
private List<Result> getTables(LinkedHashMap<String, String> dataInput)
throws Exception {
List<Parameter> inputParameters = new ArrayList<Parameter>();
List<Result> outputParameters = new ArrayList<Result>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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
// System.out.println("algorithmId: " + algorithmId);
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
inputParameters = getParameters(algorithmId);
// print check
// for (int i = 0; i < inputParameters.size(); i++) {
//
// // System.out.println(inputParameters.get(i).getName());
// rootLogger.log(Level.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"));
// rootLogger.log(Level.SEVERE,
// "ResourceName: " + dataInput.get("ResourceName"));
// rootLogger.log(Level.SEVERE,
// "DatabaseName: " + dataInput.get("DatabaseName"));
// rootLogger.log(Level.SEVERE,
// "SchemaName: " + dataInput.get("SchemaName"));
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();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// print check
// retrieve data
// System.out.println("output data retrieved");
// rootLogger.log(Level.SEVERE, "output data retrieved");
// rootLogger.log(Level.SEVERE, "output data size: " +
// outputMap.size());
// System.out.println("size outputMap: " + outputMap.size());
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
mapValues = outputData.getMapValues();
mapKeys = outputData.getmapKeys();
for (int i = 0; i < mapValues.size(); i++) {
// outputParameters.add(outputKey.get(String.valueOf(i)) + " "
// + outputMap.get(String.valueOf(i)));
// FileModel obj = new FileModel(outputMap.get(String.valueOf(i)));
// obj.setIsLoaded(true);
// obj.setIsTable(true);
Result row = new Result(String.valueOf(i), mapValues.get(String
.valueOf(i)));
outputParameters.add(row);
}
return outputParameters;
}
@Override
public PagingLoadResult<Result> LoadTables(PagingLoadConfig config,
LinkedHashMap<String, String> dataInput, boolean SearchTable,
String keyword) throws Exception {
// check on a database
currentDB = dataInput.get("DatabaseName");
if (!currentDB.equals(previousDB)) {
result = null;
System.gc();
}
previousDB = currentDB;
// check on a schema
currentSchema = dataInput.get("SchemaName");
if (!currentSchema.equals(previousSchema)) {
result = null;
System.gc();
}
previousSchema = currentSchema;
// get tables
if (result == null)
result = getTables(dataInput);
// Create a sublist and add data to list according
// to the limit and offset value of the config
List<Result> sublist = new ArrayList<Result>();
BasePagingLoadResult loadResult = null;
// rootLogger.log(Level.INFO, "Searching in the table: " + SearchTable);
// rootLogger.log(Level.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>(result.subList(start, limit));
} else {
// print check
// rootLogger.log(Level.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<Result>(sublist.subList(start, limit));
}
}
// // print check
// rootLogger.log(Level.INFO, "result size: " + totalNumber);
// rootLogger.log(Level.INFO, "limit: " + limit);
// rootLogger.log(Level.INFO, "offset: " + config.getOffset());
// rootLogger.log(Level.INFO, "start: " + start);
loadResult = new BasePagingLoadResult<Result>(sublist,
config.getOffset(), totalNumber);
return loadResult;
}
@Override
public List<Result> submitQuery(LinkedHashMap<String, String> dataDB,
String query, boolean valueReadOnlyQuery,
boolean smartCorrectionQuery, String language) throws Exception {
// System.out.println("language detected: " + language);
// rootLogger.log(Level.SEVERE, "Dialect used for smart correction:: "
// + language);
logger.info("Dialect used for smart correction: " + language);
List<Parameter> inputParameters = new ArrayList<Parameter>();
List<Result> output = new ArrayList<Result>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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
// System.out.println("algorithmId: " + algorithmId);
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
inputParameters = getParameters(algorithmId);
// print check
// for (int i = 0; i < inputParameters.size(); i++) {
// // System.out.println(inputParameters.get(i).getName());
// // System.out.println(inputParameters.get(i).getDefaultValue());
// rootLogger.log(Level.INFO, inputParameters.get(i).getName());
// }
inputParameters.get(0).setValue(dataDB.get("ResourceName"));
inputParameters.get(1).setValue(dataDB.get("DatabaseName"));
// print check
// rootLogger
// .log(Level.INFO, "ResourceName " + dataDB.get("ResourceName"));
// rootLogger.log(Level.INFO, "DatabaseName" +
// dataDB.get("DatabaseName"));
// rootLogger.log(Level.INFO, "Query" + query);
// rootLogger.log(Level.INFO, "SmartCorrections check:" +
// smartCorrectionQuery);
logger.info("ResourceName: " + dataDB.get("ResourceName"));
logger.info("DatabaseName: " + dataDB.get("DatabaseName"));
logger.info("Query: " + query);
logger.info("SmartCorrections check: " + smartCorrectionQuery);
inputParameters.get(2).setValue(String.valueOf(valueReadOnlyQuery));
inputParameters.get(3).setValue(String.valueOf(smartCorrectionQuery));
inputParameters.get(4).setValue(language);
inputParameters.get(5).setValue(query);
// System.out.println("size outputMap pre computation: "
// + outputMap.size());
// create data structure
ComputationOutput outputData = new ComputationOutput();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// print check
// retrieve data
// System.out.println("output data retrieved");
// System.out.println("size outputMap: " + outputMap.size());
// rootLogger.log(Level.SEVERE, "output data retrieved");
// rootLogger.log(Level.SEVERE, "output data size: " +
// outputMap.size());
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
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);
// outputParameters.add(outputKey.get(String.valueOf(i)) + " "
// + outputMap.get(String.valueOf(i)));
// FileModel obj = new FileModel(outputMap.get(String.valueOf(i)));
// outputParameters.add(obj);
}
return output;
}
@Override
public List<Result> sample(LinkedHashMap<String, String> dataInput)
throws Exception {
List<Parameter> inputParameters = new ArrayList<Parameter>();
// sample result
List<Result> output = new ArrayList<Result>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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);
// System.out.println("algorithmId: " + algorithmId);
// print check
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
inputParameters = getParameters(algorithmId);
// print check
// for (int i = 0; i < inputParameters.size(); i++) {
//
// // System.out.println(inputParameters.get(i).getName());
//
// rootLogger.log(Level.INFO, inputParameters.get(i).getName());
//
// }
// print check
// rootLogger.log(Level.INFO,
// "ResourceName " + dataInput.get("ResourceName"));
// rootLogger.log(Level.INFO,
// "DatabaseName " + dataInput.get("DatabaseName"));
// rootLogger.log(Level.INFO, "SchemaName " +
// dataInput.get("SchemaName"));
// rootLogger.log(Level.INFO, "TableName " +
// dataInput.get("TableName"));
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"));
// System.out.println("size outputMap pre computation: "
// + outputMap.size());
// create data structure
ComputationOutput outputData = new ComputationOutput();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// retrieve data
// System.out.println("output data retrieved");
// System.out.println("size outputMap: " + outputMap.size());
// print check
// rootLogger.log(Level.SEVERE, "output data retrieved");
// rootLogger.log(Level.SEVERE, "output data size: " +
// outputMap.size());
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
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<Result> smartSample(LinkedHashMap<String, String> dataInput)
throws Exception {
List<Parameter> inputParameters = new ArrayList<Parameter>();
// sample result
List<Result> output = new ArrayList<Result>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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);
// System.out.println("algorithmId: " + algorithmId);
// print check
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
inputParameters = getParameters(algorithmId);
// print check
// for (int i = 0; i < inputParameters.size(); i++) {
//
// // System.out.println(inputParameters.get(i).getName());
//
// rootLogger.log(Level.INFO, inputParameters.get(i).getName());
//
// }
// print check
// rootLogger.log(Level.INFO,
// "ResourceName " + dataInput.get("ResourceName"));
// rootLogger.log(Level.INFO,
// "DatabaseName " + dataInput.get("DatabaseName"));
// rootLogger.log(Level.INFO, "SchemaName " +
// dataInput.get("SchemaName"));
// rootLogger.log(Level.INFO, "TableName " +
// dataInput.get("TableName"));
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"));
// System.out.println("size outputMap pre computation: "
// + outputMap.size());
// create data structure
ComputationOutput outputData = new ComputationOutput();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// retrieve data
// System.out.println("output data retrieved");
// System.out.println("size outputMap: " + outputMap.size());
// print check
// rootLogger.log(Level.SEVERE, "output data retrieved");
// rootLogger.log(Level.SEVERE, "output data size: " +
// outputMap.size());
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
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<Result> randomSample(LinkedHashMap<String, String> dataInput)
throws Exception {
// TODO Auto-generated method stub
List<Parameter> inputParameters = new ArrayList<Parameter>();
// sample result
List<Result> output = new ArrayList<Result>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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);
// System.out.println("algorithmId: " + algorithmId);
// print check
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
inputParameters = getParameters(algorithmId);
// print check
// for (int i = 0; i < inputParameters.size(); i++) {
//
// // System.out.println(inputParameters.get(i).getName());
//
// rootLogger.log(Level.INFO, inputParameters.get(i).getName());
//
// }
// print check
// rootLogger.log(Level.INFO,
// "ResourceName " + dataInput.get("ResourceName"));
// rootLogger.log(Level.INFO,
// "DatabaseName " + dataInput.get("DatabaseName"));
// rootLogger.log(Level.INFO, "SchemaName " +
// dataInput.get("SchemaName"));
// rootLogger.log(Level.INFO, "TableName " +
// dataInput.get("TableName"));
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"));
// System.out.println("size outputMap pre computation: "
// + outputMap.size());
// create data structure
ComputationOutput outputData = new ComputationOutput();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// retrieve data
// System.out.println("output data retrieved");
// System.out.println("size outputMap: " + outputMap.size());
// print check
// rootLogger.log(Level.SEVERE, "output data retrieved");
// rootLogger.log(Level.SEVERE, "output data size: " +
// outputMap.size());
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
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<String, FileModel> getTableDetails(
LinkedHashMap<String, String> dataInput) throws Exception {
// TODO Auto-generated method stub
List<Parameter> inputParameters = new ArrayList<Parameter>();
LinkedHashMap<String, FileModel> outputParameters = new LinkedHashMap<String, FileModel>();
// get list of algorithms
List<String> algorithms = new ArrayList<String>();
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);
// System.out.println("algorithmId: " + algorithmId);
// print check
// rootLogger.log(Level.SEVERE, "algorithmId: " + algorithmId);
}
}
// get input parameters of the algorithm
// rootLogger.log(Level.SEVERE, "getting input parameters");
logger.info("getting input parameters");
inputParameters = getParameters(algorithmId);
// print check
// for (int i = 0; i < inputParameters.size(); i++) {
//
// // System.out.println(inputParameters.get(i).getName());
//
// rootLogger.log(Level.INFO, inputParameters.get(i).getName());
//
// }
// print check
// rootLogger.log(Level.INFO,
// "ResourceName " + dataInput.get("ResourceName"));
// rootLogger.log(Level.INFO,
// "DatabaseName " + dataInput.get("DatabaseName"));
// rootLogger.log(Level.INFO, "SchemaName " +
// dataInput.get("SchemaName"));
// rootLogger.log(Level.INFO, "TableName " +
// dataInput.get("TableName"));
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"));
// System.out.println("size outputMap pre computation: "
// + outputMap.size());
// create data structure
ComputationOutput outputData = new ComputationOutput();
String computationId = startComputation(algorithmId, inputParameters,
outputData);
// retrieve data
// System.out.println("output data retrieved");
// System.out.println("size outputMap: " + outputMap.size());
// print check
// rootLogger.log(Level.SEVERE, "output data retrieved");
// rootLogger.log(Level.SEVERE, "output data size: " +
// outputMap.size());
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
mapValues = outputData.getMapValues();
mapKeys = outputData.getmapKeys();
for (int i = 0; i < mapValues.size(); i++) {
// outputParameters.add(outputKey.get(String.valueOf(i)) + " "
// + outputMap.get(String.valueOf(i)));
FileModel obj = new FileModel(mapValues.get(String.valueOf(i)));
// obj.setIsLoaded(true);
// outputParameters.add(obj);
// System.out.println("value: " + outputMap.get(String.valueOf(i)));
// System.out.println("key: " + outputKey.get(String.valueOf(i)));
outputParameters.put(mapKeys.get(String.valueOf(i)), obj);
}
return outputParameters;
}
// parse a csv row
public List<Row> parseCVSString(List<Result> result, List<String> attrNames)
throws Exception {
List<Row> rows = new ArrayList<Row>();
if (result != null) {
for (int i = 0; i < result.size(); i++) {
List<String> attrValues = parse(result.get(i).getValue());
Row element = new Row(attrNames, attrValues, i);
rows.add(element);
}
}
return rows;
// code worked
// List<String> values = parse(row);
//
// for (int i = 0; i < values.size(); i++) {
//
// System.out.println("vaalue: " + values.get(i));
//
// }
//
// Row element = new Row(attributes, values);
//
// return element;
// List<Row> rowElements = new ArrayList<Row>();
//
// for (int i=0;i<result.size();i++){
//
// List<String> attributes = parse(result.get(i).getValue());
//
// Row element=new Row(attributes);
//
// rowElements.add(element);
// }
//
// System.out.println("server rowElement size: " + rowElements.size());
//
// return rowElements;
}
private List<String> parse(String row) throws Exception {
String delimiter = ",";
// System.out.println("row: " + row);
List<String> elements = new ArrayList<String>();
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();
}
// System.out.println("server token: " + phrase);
}
if (phrase.startsWith("\""))
phrase = phrase.substring(1);
if (phrase.endsWith("\""))
phrase = phrase.substring(0, phrase.length() - 1);
elements.add(phrase);
// System.out.println("size: " + elements.size());
return elements;
}
private List<String> getDatabaseManagerAlgorithms() {
ArrayList<String> dbAlg = new ArrayList<String>();
StatisticalManagerFactory factory = getFactory();
// get and print algorithms
SMListGroupedAlgorithms groups = factory.getAlgorithms();
if (groups == null)
// System.out.println("GROUPS OF ALGORITHMS IS NULL!");
// rootLogger.log(Level.SEVERE, "GROUPS OF ALGORITHMS IS NULL!");
logger.info("GROUPS OF ALGORITHMS IS NULL!");
else
// System.out.println("GROUPS OF ALGORITHMS IS NOT NULL!");
// rootLogger.log(Level.SEVERE,
// "GROUPS OF ALGORITHMS IS NOT NULL!");
logger.info("GROUPS OF ALGORITHMS IS NOT NULL!");
// get list categories
for (SMGroupedAlgorithms group : groups.thelist()) {
// if (group.category().equals("DATABASES")) {
for (SMAlgorithm algorithm : group.thelist()) {
// if (algorithm.name().equals("LISTDBINFO")) {
// System.out.println(algorithm.name());
dbAlg.add(algorithm.name());
// }
}
}
return dbAlg;
}
private List<Parameter> getParameters(String algorithmId) {
// System.out.println("algorithmId: " + algorithmId);
StatisticalManagerFactory factory = getFactory();
SMParameters smParams = factory.getAlgorithmParameters(algorithmId);
List<Parameter> params = new ArrayList<Parameter>();
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);
// System.out.println("GWTdbManagerServiceImpl->parameters");
// CHECK
// rootLogger.log(Level.INFO, "parameters: ");
// rootLogger.log(Level.INFO, paramName);
// rootLogger.log(Level.INFO, paramDescription);
// rootLogger.log(Level.INFO, objectType);
// rootLogger.log(Level.INFO, defaultValue);
// System.out.println(paramName);
// System.out.println(paramDescription);
// System.out.println(objectType);
// System.out.println(defaultValue);
Parameter objectParam = new Parameter(paramName, paramDescription,
objectType, defaultValue);
params.add(objectParam);
}
return params;
}
private String startComputation(String algorithmName,
List<Parameter> 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(1000);
}
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()];
// this.outputMap=null;
float percentage = 0;
if (status == SMOperationStatus.RUNNING) {
// logger.info("RUNNING");
SMOperationInfo infos = factory.getComputationInfo(computationId,
user);
// percentage = Float.parseFloat(infos.percentage());
// System.out.println("GWTdbManagerServiceImpl->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];
// TODO: ECXEPTION MANAGEMENT
// if (SMResourceType.ERROR){
//
//
// }
// throws(smResource.description());
displayOutput(smResource, smResType, outputData);
// System.out.println("GWTdbManagerServiceImpl-> SM resource Name: "
// + smResource.name());
// rootLogger.log(Level.SEVERE,
// "SM resource Name: " + smResource.name());
// System.out.println("GWTdbManagerServiceImpl-> SM resource ID: "
// + smResource.resourceId());
// print check
// rootLogger.log(Level.SEVERE,
// "SM resource ID: " + smResource.resourceId());
// System.out
// .println("GWTdbManagerServiceImpl-> SM resource Description: "
// + smResource.description());
// print check
// rootLogger.log(Level.SEVERE, "SM resource Description: "
// + smResource.description());
percentage = 100;
}
return percentage;
}
private void displayOutput(SMResource smResource, SMResourceType smResType,
ComputationOutput outputData) throws Exception {
// System.out.println("displayOutput");
// print check
// rootLogger.log(Level.SEVERE, "displayOutput");
// LinkedHashMap<String, String> mapValues = new LinkedHashMap<String,
// String>();
// mapValues = outputData.getMapValues();
// outputMap = new LinkedHashMap<String, String>();
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())) {
// Map<String, String> outputMap = getMap(objRes);
// this.outputMap = (LinkedHashMap<String, String>)
// getMap(objRes);
// mapValues = (LinkedHashMap<String, String>) getMap(objRes,
// outputData);
logger.info("Output is a map");
getMap(objRes, outputData);
} else if (objRes.name().contentEquals(
PrimitiveTypes.IMAGES.toString())) {
// System.out.println("Output are images");
// rootLogger.log(Level.SEVERE, "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;
// throw new Exception(smResource.description());
}
// break;
// case TABULAR:
// SMTable tableRes = (SMTable) smResource;
// System.out.println("Output is a table");
// break;
// }
}
// private Map<String, String> getMap(SMObject objRes, ComputationOutput
// outputData) throws Exception {
private void getMap(SMObject objRes, ComputationOutput outputData)
throws Exception {
// System.out.println("getMap");
// rootLogger.log(Level.SEVERE, "getMap");
// output data
LinkedHashMap<String, String> mapValues = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> mapKeys = new LinkedHashMap<String, String>();
// outputKey = new LinkedHashMap<String, String>();
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<String, SMResource> smMap = (Map<String, SMResource>) (xstream
.fromXML(is));
is.close();
// LinkedHashMap<String, String> outputmap = new LinkedHashMap<String,
// String>();
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];
// System.out.println("GWTdbManagerServiceImpl-> ResourceType:"
// + smsubResType);
// rootLogger.log(Level.INFO, "ResourceType: " + smsubResType);
if (smsubResType == SMResourceType.OBJECT) {
SMObject obje = (SMObject) smres;
String outstring = obje.url();
// logger.info("key: " + smsubResType);
// logger.info("object: " + outstring);
// outputmap.put(key, outstring);
mapValues.put(String.valueOf(i), outstring);
i++;
}
}
outputData.setMapValues(mapValues);
outputData.setmapKeys(mapKeys);
// System.out.println("Map retrieved with size: " + smMap.size());
// rootLogger
// .log(Level.SEVERE, "Map retrieved with size: " + smMap.size());
// return outputmap;
}
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);
}
}