From e5274527bf66f8809908fc0b45a632b3dc22310b Mon Sep 17 00:00:00 2001 From: Loredana Liccardo Date: Fri, 1 Aug 2014 16:11:36 +0000 Subject: [PATCH] -cancel of a submit query managed removing the computation from the statistical and closing the db connection. - class UIDGenerator added. git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/databases-manager-portlet@99195 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/GWTdbManagerService.java | 4 +- .../client/GWTdbManagerServiceAsync.java | 6 +- .../client/panels/GxtBorderLayoutPanel.java | 28 +- .../toolbar/GxtToolbarFunctionality.java | 73 +- .../client/utils/UIDGenerator.java | 67 ++ .../server/GWTdbManagerServiceImpl.java | 680 ++++++++++-------- .../databasesmanager/DatabasesManager.gwt.xml | 8 +- 7 files changed, 540 insertions(+), 326 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/databasesmanager/client/utils/UIDGenerator.java diff --git a/src/main/java/org/gcube/portlets/user/databasesmanager/client/GWTdbManagerService.java b/src/main/java/org/gcube/portlets/user/databasesmanager/client/GWTdbManagerService.java index 80226ba..23e3572 100644 --- a/src/main/java/org/gcube/portlets/user/databasesmanager/client/GWTdbManagerService.java +++ b/src/main/java/org/gcube/portlets/user/databasesmanager/client/GWTdbManagerService.java @@ -26,7 +26,7 @@ public interface GWTdbManagerService extends RemoteService { List submitQuery(LinkedHashMap dataDB, String query, boolean valueReadOnlyQuery, - boolean smartCorrectionQuery, String language) throws Exception; + boolean smartCorrectionQuery, String language, String UID) throws Exception; List sample(LinkedHashMap dataInput) throws Exception; @@ -47,6 +47,8 @@ public interface GWTdbManagerService extends RemoteService { LinkedHashMap dataInput, boolean SearchTable, String keyword) throws Exception; + Boolean removeComputation(String uidSubmitQuery) throws Exception; + } diff --git a/src/main/java/org/gcube/portlets/user/databasesmanager/client/GWTdbManagerServiceAsync.java b/src/main/java/org/gcube/portlets/user/databasesmanager/client/GWTdbManagerServiceAsync.java index 247a8f3..2d2678f 100644 --- a/src/main/java/org/gcube/portlets/user/databasesmanager/client/GWTdbManagerServiceAsync.java +++ b/src/main/java/org/gcube/portlets/user/databasesmanager/client/GWTdbManagerServiceAsync.java @@ -23,7 +23,7 @@ public interface GWTdbManagerServiceAsync { void submitQuery(LinkedHashMap dataDB, String query, boolean valueReadOnlyQuery, boolean smartCorrectionQuery, - String language, AsyncCallback> callback); + String language, String UID, AsyncCallback> callback); void parseCVSString(List result, List attrNames, AsyncCallback> callback); @@ -43,5 +43,7 @@ public interface GWTdbManagerServiceAsync { void LoadTables(PagingLoadConfig config, LinkedHashMap dataInput, boolean SearchTable, String keyword, AsyncCallback> callback); - + + void removeComputation(String uidSubmitQuery, + AsyncCallback callback); } diff --git a/src/main/java/org/gcube/portlets/user/databasesmanager/client/panels/GxtBorderLayoutPanel.java b/src/main/java/org/gcube/portlets/user/databasesmanager/client/panels/GxtBorderLayoutPanel.java index 05e4af7..e1beae4 100644 --- a/src/main/java/org/gcube/portlets/user/databasesmanager/client/panels/GxtBorderLayoutPanel.java +++ b/src/main/java/org/gcube/portlets/user/databasesmanager/client/panels/GxtBorderLayoutPanel.java @@ -792,6 +792,12 @@ public class GxtBorderLayoutPanel extends ContentPanel { final Dialog form = dialogList.get(dialogID); // get the data form related to the ID final SubmitQueryData dataQuery = submitQueryDataList.get(dialogID); + + + //get the UID related to submitQuery operation + LinkedHashMap uidSubmitQueryList = toolbar.getUIDSubmitQueryList(); + String UID = uidSubmitQueryList.get(dialogID); + // rootLogger.log(Level.INFO, "query: "+ dataQuery.getQuery() + // "submitquery->dialogID: " @@ -858,7 +864,7 @@ public class GxtBorderLayoutPanel extends ContentPanel { // remote rpc RPCservice.submitQuery(dataForSubmitQuery, dataQuery.getQuery(), true, - dataQuery.getSmartCorrection(), language, + dataQuery.getSmartCorrection(), language, UID, new AsyncCallback>() { // TO REMOVE data "true" as input if you manage the @@ -889,6 +895,16 @@ public class GxtBorderLayoutPanel extends ContentPanel { @Override public void onSuccess(List result) { + + if (result == null){ + + if (form.getBody().isMasked()) + form.getBody().unmask(); + + rootLogger.log(Level.SEVERE, "No results have been returned"); + return; + } + rootLogger.log(Level.SEVERE, "SUCCESS RPC submitQuery"); rootLogger.log(Level.SEVERE, "output size: " + result.size()); @@ -903,11 +919,11 @@ public class GxtBorderLayoutPanel extends ContentPanel { // convertedQuery = result.get(1); } - // get the attributes list for the result table - getListAttributes(result.get(0).getValue()); - // remove the header in order to parse only the result - result.remove(0); - + // get the attributes list for the result table + getListAttributes(result.get(0).getValue()); + // remove the header in order to parse only the result + result.remove(0); + // parse the result in order to obtain a table boolean submitQueryEventManaged = true; parseResult(result, form, dialogID, diff --git a/src/main/java/org/gcube/portlets/user/databasesmanager/client/toolbar/GxtToolbarFunctionality.java b/src/main/java/org/gcube/portlets/user/databasesmanager/client/toolbar/GxtToolbarFunctionality.java index be0cd1d..2fdfc48 100644 --- a/src/main/java/org/gcube/portlets/user/databasesmanager/client/toolbar/GxtToolbarFunctionality.java +++ b/src/main/java/org/gcube/portlets/user/databasesmanager/client/toolbar/GxtToolbarFunctionality.java @@ -1,11 +1,13 @@ package org.gcube.portlets.user.databasesmanager.client.toolbar; import java.util.LinkedHashMap; +import java.util.List; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import org.gcube.portlets.user.databasesmanager.client.GWTdbManagerServiceAsync; +import org.gcube.portlets.user.databasesmanager.client.datamodel.Result; import org.gcube.portlets.user.databasesmanager.client.datamodel.SubmitQueryData; import org.gcube.portlets.user.databasesmanager.client.events.LoadTablesEvent; import org.gcube.portlets.user.databasesmanager.client.events.RandomSamplingEvent; @@ -17,6 +19,8 @@ import org.gcube.portlets.user.databasesmanager.client.events.SubmitQueryEvent; import org.gcube.portlets.user.databasesmanager.client.events.interfaces.SelectedTableEventHandler; import org.gcube.portlets.user.databasesmanager.client.form.GxtFormSubmitQuery; import org.gcube.portlets.user.databasesmanager.client.utils.ConstantsPortlet; +import org.gcube.portlets.user.databasesmanager.client.utils.UIDGenerator; + import com.extjs.gxt.ui.client.Style.ButtonArrowAlign; import com.extjs.gxt.ui.client.Style.ButtonScale; import com.extjs.gxt.ui.client.event.ButtonEvent; @@ -31,6 +35,7 @@ import com.extjs.gxt.ui.client.widget.layout.FitLayout; import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem; import com.extjs.gxt.ui.client.widget.toolbar.ToolBar; import com.google.gwt.event.shared.HandlerManager; +import com.google.gwt.user.client.rpc.AsyncCallback; //toolbar to contain buttons public class GxtToolbarFunctionality { @@ -50,7 +55,9 @@ public class GxtToolbarFunctionality { // dialog list. Each dialog contains a form private LinkedHashMap dialogList = new LinkedHashMap(); private LinkedHashMap submitQueryDataList = new LinkedHashMap(); - private static int ID = 0; // ID asscociated to a dialog form + private static int ID = 0; // ID associated to a dialog form + //uid list related to submit query operations + private LinkedHashMap uidSubmitQueryList = new LinkedHashMap(); // GWT logger private static Logger rootLogger = Logger .getLogger("GxtToolbarFunctionality"); @@ -208,6 +215,8 @@ public class GxtToolbarFunctionality { // form to submit a query final GxtFormSubmitQuery form = new GxtFormSubmitQuery(); + + dialog.add(form); dialog.show(); @@ -241,11 +250,7 @@ public class GxtToolbarFunctionality { @Override public void componentSelected(ButtonEvent ce) { - //start computation and get id - - //generate a UUID for this request -// UUID id = UUID.randomUUID(); - + // recover info from dialog setInfoOnSubmitQuery(form, dialog, dialogID); @@ -265,8 +270,40 @@ public class GxtToolbarFunctionality { rootLogger.log(Level.INFO, "button Cancel event"); - //remove computation - + + // remove computation + RPCservice.removeComputation( + uidSubmitQueryList + .get(dialogID), + new AsyncCallback() { + + @Override + public void onSuccess( + Boolean result) { + rootLogger + .log(Level.SEVERE, + "SUCCESS RPC removeComputation"); + if (result + .booleanValue() == true) { + + rootLogger + .log(Level.INFO, + "computation removed with uid: " + + uidSubmitQueryList + .get(dialogID)); + } + + } + + @Override + public void onFailure( + Throwable caught) { + rootLogger + .log(Level.SEVERE, + "FAILURE RPC removeComputation"); + } + }); + dialog.hide(); } }); @@ -435,16 +472,26 @@ public class GxtToolbarFunctionality { SubmitQueryData data = form.getSubmitQueryData(); // data = form.getSubmitQueryData(); - + String query = data.getQuery(); - + if ((query == null) || (query.equals(""))) { MessageBox.alert("Warning", "Query field null", null); } else { dialogList.put(new Integer(dialogID), SubmtQueryDialog); submitQueryDataList.put(new Integer(dialogID), data); + + + //generate a UID for this request + UIDGenerator generator = new UIDGenerator(); + String uidSubmitQuery = generator.get(); + //add uid for the submit query operation + uidSubmitQueryList.put(new Integer(dialogID), uidSubmitQuery); + +// System.out.println("UID: " + uidSubmitQuery); + SubmtQueryDialog.getBody().mask("Loading", "x-mask-loading"); - + // fire event eventBus.fireEvent(new SubmitQueryEvent(dialogID)); } @@ -466,6 +513,10 @@ public class GxtToolbarFunctionality { public LinkedHashMap getSubmitQueryDataList() { return submitQueryDataList; } + + public LinkedHashMap getUIDSubmitQueryList() { + return uidSubmitQueryList; + } // public Integer getDialogID() { // return new Integer(ID); diff --git a/src/main/java/org/gcube/portlets/user/databasesmanager/client/utils/UIDGenerator.java b/src/main/java/org/gcube/portlets/user/databasesmanager/client/utils/UIDGenerator.java new file mode 100644 index 0000000..238cc12 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/databasesmanager/client/utils/UIDGenerator.java @@ -0,0 +1,67 @@ +package org.gcube.portlets.user.databasesmanager.client.utils; + +public class UIDGenerator { + private static final char[] CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + .toCharArray(); + + /** + * Generate a random uuid of the specified length. Example: uuid(15) returns + * "VcydxgltxrVZSTV" + * + * @param len + * the desired number of characters + */ + public static String get(int len) { + return get(len, CHARS.length); + } + + /** + * Generate a random uuid of the specified length, and radix. Examples: + *
    + *
  • uuid(8, 2) returns "01001010" (8 character ID, base=2) + *
  • uuid(8, 10) returns "47473046" (8 character ID, base=10) + *
  • uuid(8, 16) returns "098F4D35" (8 character ID, base=16) + *
+ * + * @param len + * the desired number of characters + * @param radix + * the number of allowable values for each character (must be <= + * 62) + */ + public static String get(int len, int radix) { + if (radix > CHARS.length) { + throw new IllegalArgumentException(); + } + char[] uuid = new char[len]; + // Compact form + for (int i = 0; i < len; i++) { + uuid[i] = CHARS[(int) (Math.random() * radix)]; + } + return new String(uuid); + } + + /** + * Generate a RFC4122, version 4 ID. Example: + * "92329D39-6F5C-4520-ABFC-AAB64544E172" + */ + public static String get() { + char[] uuid = new char[36]; + int r; + + // rfc4122 requires these characters + uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; + uuid[14] = '4'; + + // Fill in random data. At i==19 set the high bits of clock sequence as + // per rfc4122, sec. 4.1.5 + for (int i = 0; i < 36; i++) { + if (uuid[i] == 0) { + r = (int) (Math.random() * 16); + uuid[i] = CHARS[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf]; + } + } + return new String(uuid); + } +} + 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 4269616..621ee30 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 @@ -59,18 +59,20 @@ import org.apache.log4j.Logger; public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements GWTdbManagerService { -// // the result generated in the LoadTables method -// private List result = null; + // // the result generated in the LoadTables method + // private List result = null; // information about a database -// private String currentDB = ""; -// private String previousDB = ""; + // private String currentDB = ""; + // private String previousDB = ""; // information about a schema -// private String currentSchema = ""; -// private String previousSchema = ""; - - //Hashmap that contains computationId -// HashMap computationIDMap = new HashMap(); - + // private String currentSchema = ""; + // private String previousSchema = ""; + + // //Hashmap that contains computationId with a uid key + // HashMap computationIDMap = new HashMap(); + + String operation = ""; + // logger private static Logger logger = Logger .getLogger(GWTdbManagerServiceImpl.class); @@ -78,37 +80,41 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements public GWTdbManagerServiceImpl() { } - - private void initVariables(){ - ASLSession session = WsUtil.getAslSession(this.getThreadLocalRequest().getSession()); - + + 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); - - + + // Hashmap that contains computationId with a uid key + HashMap computationIDMap = new HashMap(); + session.setAttribute("ComputationIDList", computationIDMap); + } // to get resources from IS @Override public List getResource() throws Exception { - - //initialize variables with application startup + + // initialize variables with application startup initVariables(); - - // data input + + // data input List inputParameters = new ArrayList(); // data output List outputParameters = new ArrayList(); @@ -237,8 +243,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements @Override public List getDBSchema(LinkedHashMap dataInput) throws Exception { - - + // data input List inputParameters = new ArrayList(); // data output @@ -382,17 +387,19 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements 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"); + + 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 = null; result = new ArrayList(); System.gc(); } @@ -405,7 +412,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements currentSchema = dataInput.get("SchemaName"); String previousSchema = (String) session.getAttribute("previousSchema"); if (!currentSchema.equals(previousSchema)) { -// result = null; + // result = null; result = new ArrayList(); System.gc(); } @@ -414,9 +421,9 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements session.setAttribute("previousSchema", previousSchema); // get tables -// if (result == null) -// result = getTables(dataInput); - if (result.size()==0) + // if (result == null) + // result = getTables(dataInput); + if (result.size() == 0) result = getTables(dataInput); // Create a sublist and add data to list according @@ -470,278 +477,291 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements 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 + @Override + public List submitQuery(LinkedHashMap dataDB, + String query, boolean valueReadOnlyQuery, + boolean smartCorrectionQuery, String language, String UID) + throws Exception { -// // 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; -// } + logger.info("Dialect used for smart correction: " + language); -// 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; -// -// } + // data input + List inputParameters = new ArrayList(); + // data output + // List output = new ArrayList(); + List output = null; -// 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; -// } + // 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(); + + try { + + // computation id + String computationId = startComputation(algorithmId, + inputParameters, outputData, UID); + + // 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(); + + if (mapValues.size() != 0) { + output = new ArrayList(); + + 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); + } + } + + } catch (Exception e) { + // TODO: handle exception + // e.printStackTrace(); + } + + 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) @@ -1161,9 +1181,44 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements return params; } + private synchronized void updateJob(String jobID, String computationId) { + if (jobID != null) { + // add the computation in the map + ASLSession session = WsUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + HashMap computationIDMap = (HashMap) session + .getAttribute("ComputationIDList"); + computationIDMap.put(jobID, computationId); + session.setAttribute("ComputationIDList", computationIDMap); + } + } + + private synchronized String removeJob(String jobID) { + if (jobID != null) { + // add the computation in the map + ASLSession session = WsUtil.getAslSession(this + .getThreadLocalRequest().getSession()); + HashMap computationIDMap = (HashMap) session + .getAttribute("ComputationIDList"); + String computationId = computationIDMap.get(jobID); + if (computationId != null) { + computationIDMap.remove(jobID); + session.setAttribute("ComputationIDList", computationIDMap); + return computationId; + } + } + return null; + } + private String startComputation(String algorithmName, List parameters, ComputationOutput outputData) throws Exception { + return startComputation(algorithmName, parameters, outputData, null); + } + + private String startComputation(String algorithmName, + List parameters, ComputationOutput outputData, + String jobID) throws Exception { SMComputationConfig config = new SMComputationConfig(); SMInputEntry[] list = new SMInputEntry[parameters.size()]; @@ -1187,13 +1242,19 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements String scope = getScope(); String username = getUsername(); + updateJob(jobID, computationId); + logger.info("startComputation->The computation has started!"); while (percentage < 100) { percentage = checkComputationStatus(scope, computationId, username, outputData); Thread.sleep(3000); } + logger.info("startComputation->The computation has finished!"); + removeJob(jobID); + return computationId; } catch (Exception e) { + logger.info("startComputation->The job submit has failed!"); // e.printStackTrace(); throw e; } @@ -1370,11 +1431,26 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } - public void removeComputation(String computationId) throws Exception { - StatisticalManagerFactory factory = getFactory(); - factory.removeComputation(computationId); + public Boolean removeComputation(String uidSubmitQuery) throws Exception { + + // System.out.println("server UID: " + uidSubmitQuery); + + String computationId = removeJob(uidSubmitQuery); + if (computationId != null) { + StatisticalManagerFactory factory = getFactory(); + try { + factory.removeComputation(computationId); + } catch (Exception e) { + + e.printStackTrace(); + logger.info("Could not remove the computation ID " + + computationId + " corresponding to jobID " + + uidSubmitQuery); + logger.error(e); + } + } + + return (new Boolean(true)); } - - } diff --git a/src/main/resources/org/gcube/portlets/user/databasesmanager/DatabasesManager.gwt.xml b/src/main/resources/org/gcube/portlets/user/databasesmanager/DatabasesManager.gwt.xml index 51b719a..0eb7860 100644 --- a/src/main/resources/org/gcube/portlets/user/databasesmanager/DatabasesManager.gwt.xml +++ b/src/main/resources/org/gcube/portlets/user/databasesmanager/DatabasesManager.gwt.xml @@ -5,7 +5,7 @@ - + @@ -27,8 +27,8 @@ - - - + + +