diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/WorkspaceDataMinerTaskExecutor.java b/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/WorkspaceDataMinerTaskExecutor.java index fa04c22..81f9b60 100644 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/WorkspaceDataMinerTaskExecutor.java +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/WorkspaceDataMinerTaskExecutor.java @@ -1,28 +1,40 @@ package org.gcube.common.workspacetaskexecutor.dataminer; +import java.util.ArrayList; +import java.util.List; + import org.apache.commons.lang.Validate; import org.gcube.common.homelibrary.home.workspace.WorkspaceItem; -import org.gcube.common.workspacetaskexecutor.shared.IsItemExecutable; -import org.gcube.common.workspacetaskexecutor.shared.ConfigurableTask; +import org.gcube.common.workspacetaskexecutor.shared.ExecutableItem; import org.gcube.common.workspacetaskexecutor.shared.ExecutableTask; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskExecutionStatus; import org.gcube.common.workspacetaskexecutor.shared.exception.ItemNotExecutableException; +import org.gcube.common.workspacetaskexecutor.shared.exception.TaskConfigurationNotFoundException; import org.gcube.common.workspacetaskexecutor.shared.exception.TaskErrorException; import org.gcube.common.workspacetaskexecutor.shared.exception.TaskNotExecutableException; import org.gcube.common.workspacetaskexecutor.util.JsonUtil; import org.gcube.common.workspacetaskexecutor.util.WsUtil; +import org.json.JSONArray; +import org.json.JSONException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.core.type.TypeReference; + /** * The Class WorkspaceDataMinerTaskExecutor. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * May 2, 2018 + * May 3, 2018 */ -public class WorkspaceDataMinerTaskExecutor implements ExecutableTask, ConfigurableTask, IsItemExecutable{ +public class WorkspaceDataMinerTaskExecutor implements ExecutableTask, ExecutableItem{ + + /** + * + */ + public static final String FIELD_CONFIGURATION_KEY = "configurationKey"; /** The logger. */ private static Logger logger = LoggerFactory.getLogger(WorkspaceDataMinerTaskExecutor.class); @@ -105,30 +117,62 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask listConfigs = getListOfTaskConfigurations(workspaceItemId); + + if(listConfigs!=null){ + //validating the configuration server-side + for (TaskConfiguration taskConf : listConfigs) { + //if the configurationKey are equals + if(taskConf.getConfigurationKey().compareTo(configurationKey)==0){ + return taskConf; + } + } + } + + throw new TaskConfigurationNotFoundException("The configuration with "+FIELD_CONFIGURATION_KEY+" "+configurationKey+" does not exist"); + } + /* (non-Javadoc) * @see org.gcube.common.workspacetaskexecutor.CheckableTask#checkItemExecutable(java.lang.String) */ @Override - public TaskConfiguration checkItemExecutable(String workspaceItemId) throws ItemNotExecutableException, Exception { - - TaskConfiguration conf = null; + public List getListOfTaskConfigurations(String workspaceItemId) throws Exception { + logger.debug("Get list of Task Configurations for "+workspaceItemId+" starts..."); + List conf = null; checkOwner(); WorkspaceItem item = WsUtil.getItem(usernameOwner, workspaceItemId); - String propConfigValue = WsUtil.getPropertyValue(item, WS_DM_TASK_TASK_CONF); - - logger.info("Read "+WS_DM_TASK_TASK_CONF+" value: "+propConfigValue); - - if(propConfigValue==null || propConfigValue.isEmpty()) - throw new ItemNotExecutableException("The item id "+workspaceItemId+" has not a "+TaskConfiguration.class.getSimpleName()); - - try{ - conf = jsonUtil.readObject(propConfigValue, TaskConfiguration.class); - }catch(Exception e){ - logger.error("Error on serializing configuration: "+propConfigValue, e); - throw new ItemNotExecutableException("The item id "+workspaceItemId+" has a wrong "+TaskConfiguration.class.getSimpleName()+". Do you want create a new one?"); + String arrayConf = WsUtil.getPropertyValue(item, WS_DM_TASK_TASK_CONF); + logger.info("Read "+WS_DM_TASK_TASK_CONF+" value: "+arrayConf); + if(arrayConf==null || arrayConf.isEmpty()){ + logger.warn("The item id "+workspaceItemId+" has not "+TaskConfiguration.class.getSimpleName() +" saved"); + return null; } - logger.info("Found configuration: "+conf); + try{ + TypeReference> mapType = new TypeReference>() {}; + conf = jsonUtil.readList(arrayConf, mapType); + }catch(Exception e){ + logger.error("Error on serializing configuration: "+arrayConf, e); + logger.info("The item id "+workspaceItemId+" has a wrong "+TaskConfiguration.class.getSimpleName()+" saved. Deleting them.."); + eraseAllTaskConfigurations(workspaceItemId); + } + + logger.debug("Found configuration/s: "+conf); + + if(conf!=null) + logger.info("Returning "+conf.size()+" configuration/s"); return conf; } @@ -138,7 +182,7 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask configurations = getListOfTaskConfigurations(taskConfiguration.getWorkspaceItemId()); - if(propConfig==null || propConfig.isEmpty()) - throw new ItemNotExecutableException("The item id "+taskConfiguration.getWorkspaceItemId()+" has not a "+TaskConfiguration.class.getSimpleName()); + if(configurations==null) + throw new ItemNotExecutableException("The item "+taskConfiguration.getWorkspaceItemId()+" has not configurations saved"); - WsUtil.setPropertyValue(item, WS_DM_TASK_TASK_CONF, null); - logger.info("Set property value "+WS_DM_TASK_TASK_CONF+" as null for the workspace item id: "+taskConfiguration.getWorkspaceItemId()); - return true; + List newConfigurations = new ArrayList(configurations.size()); + + for (TaskConfiguration tc : configurations) { + if(tc.getConfigurationKey().compareTo(taskConfiguration.getConfigurationKey())!=0){ + newConfigurations.add(tc); + }else{ + //Configuration found + found = true; + } + } + + if(found){ + JSONArray newConfgs = jsonUtil.toJSONArray(newConfigurations); + WsUtil.setPropertyValue(item, WS_DM_TASK_TASK_CONF, newConfgs.toString()); + logger.info("Removed task configuration "+taskConfiguration+ " from saved configurations"); + return true; + } + + logger.info("Task configuration "+taskConfiguration+ " not found, removed configuration is false"); + return false; + + } + + + /* (non-Javadoc) + * @see org.gcube.common.workspacetaskexecutor.shared.ExecutableItem#getTaskConfiguration(java.lang.String) + */ + @Override + public TaskConfiguration getTaskConfiguration(String itemId, String configurationKey) + throws TaskConfigurationNotFoundException, Exception { + + return getConfigurationFromSaved(itemId, configurationKey); } /* (non-Javadoc) @@ -186,27 +261,63 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask configurations = getListOfTaskConfigurations(taskConfiguration.getWorkspaceItemId()); + if(configurations==null) + configurations = new ArrayList(1); //It is the first configuration servert-side + + List newConfigurations = new ArrayList(configurations.size()); + + for (TaskConfiguration tc : configurations) { + if(tc.getConfigurationKey().compareTo(taskConfiguration.getConfigurationKey())!=0){ + newConfigurations.add(tc); + }else{ + //Configuration found + found = true; + logger.info("The configuration with "+FIELD_CONFIGURATION_KEY +" found, updating it"); + newConfigurations.add(taskConfiguration); + } + } + + if(!found){ + logger.info("The configuration with "+FIELD_CONFIGURATION_KEY +" not found, adding it as new"); + newConfigurations.add(taskConfiguration); + } try{ - jsonConfValue = jsonUtil.toJSON(taskConfiguration); - logger.debug("The json configuration is: "+jsonConfValue); - }catch(Exception e){ - throw new Exception("This "+taskConfiguration+" is wrong!. Please create a new one"); + JSONArray jsonConfigs = jsonUtil.toJSONArray(newConfigurations); + WsUtil.setPropertyValue(item, WS_DM_TASK_TASK_CONF, jsonConfigs.toString()); + logger.debug("Updated json configuration/s is/are: "+jsonConfigs.toString()); + logger.info(taskConfiguration +" added/updated"); + }catch(JSONException e){ + logger.error("Error on saving Task Configuration: "+taskConfiguration, e); + throw new Exception("Error on saving Task Configuration: "+taskConfiguration+", Please retry"); } - if(jsonConfValue!=null){ - WsUtil.setPropertyValue(item, WS_DM_TASK_TASK_CONF, jsonConfValue); - return true; - } - - return false; + return true; } + /* (non-Javadoc) + * @see org.gcube.common.workspacetaskexecutor.shared.ExecutableItem#eraseAllExecutableConfigurations() + */ + @Override + public Boolean eraseAllTaskConfigurations(String itemId) throws ItemNotExecutableException, Exception { + logger.info("Erase all configurations starts..."); + Validate.notNull(itemId, "The itemId is null"); + checkOwner(); + //Check if the item is executable + isItemExecutable(itemId); + WorkspaceItem item = WsUtil.getItem(usernameOwner, itemId); + WsUtil.setPropertyValue(item, WS_DM_TASK_TASK_CONF, null); + return true; + } + + /* (non-Javadoc) * @see org.gcube.common.workspacetaskexecutor.ExecutableTask#doRun(org.gcube.common.workspacetaskexecutor.BaseTaskConfiguration) */ @@ -217,10 +328,9 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask the generic type - */ -public interface ConfigurableTask { - - - /** - * Removes the task configuration. - * - * @param config the config - * @return the boolean - * @throws ItemNotExecutableException the item not executable exception - * @throws Exception the exception - */ - Boolean removeTaskConfiguration(I config) throws ItemNotExecutableException, Exception; - - - /** - * Sets the task configuration. - * - * @param config the config - * @return the boolean - * @throws ItemNotExecutableException the item not executable exception - * @throws Exception the exception - */ - Boolean setTaskConfiguration(I config) throws ItemNotExecutableException, Exception; -} diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/ExecutableItem.java b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/ExecutableItem.java new file mode 100644 index 0000000..8519345 --- /dev/null +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/ExecutableItem.java @@ -0,0 +1,90 @@ +/* + * + */ +package org.gcube.common.workspacetaskexecutor.shared; + +import java.util.List; + +import org.gcube.common.workspacetaskexecutor.shared.exception.ItemNotExecutableException; +import org.gcube.common.workspacetaskexecutor.shared.exception.TaskConfigurationNotFoundException; + + + +/** + * The Interface ExecutableItem. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 3, 2018 + * @param the generic type must extends {@link BaseTaskConfiguration} + */ +public interface ExecutableItem { + + /** + * Checks if is item executable. + * + * @param itemId the item id + * @return the boolean + * @throws ItemNotExecutableException the item not executable exception + * @throws Exception the exception + */ + Boolean isItemExecutable(String itemId) throws ItemNotExecutableException, Exception; + + + /** + * Sets the task configuration. + * + * @param config the config + * @return the boolean + * @throws ItemNotExecutableException the item not executable exception + * @throws Exception the exception + */ + Boolean setTaskConfiguration(T config) throws ItemNotExecutableException, Exception; + + + /** + * Removes the task configuration. + * + * @param config the config + * @return the boolean + * @throws ItemNotExecutableException the item not executable exception + * @throws Exception the exception + */ + Boolean removeTaskConfiguration(T config) throws ItemNotExecutableException, Exception; + + + /** + * Gets the task configuration. + * + * @param itemId the item id + * @param configurationKey the configuration key + * @return the task configuration + * @throws TaskConfigurationNotFoundException the task configuration not found exception + * @throws Exception + */ + T getTaskConfiguration(String itemId, String configurationKey) throws TaskConfigurationNotFoundException, Exception; + + + /** + * Gets the list of task configurations. + * + * @param itemId the item id + * @return the list of executable configurations. It is the list of its {@link BaseTaskConfiguration} + * @throws ItemNotExecutableException the item not executable exception + * @throws Exception the exception + */ + List getListOfTaskConfigurations(String itemId) throws ItemNotExecutableException, Exception; + + + /** + * Erase all task configurations. + * + * @param itemId the item id + * @return the boolean + * @throws ItemNotExecutableException the item not executable exception + * @throws Exception the exception + */ + Boolean eraseAllTaskConfigurations(String itemId) throws ItemNotExecutableException, Exception; + + + +} diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/IsItemExecutable.java b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/IsItemExecutable.java deleted file mode 100644 index 1b30ba6..0000000 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/IsItemExecutable.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * - */ -package org.gcube.common.workspacetaskexecutor.shared; - -import org.gcube.common.workspacetaskexecutor.shared.exception.ItemNotExecutableException; - - -/** - * The Interface IsItemExecutable. - * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * May 3, 2018 - * @param the generic type must exteds {@link BaseTaskConfiguration} - */ -public interface IsItemExecutable { - - - /** - * Check item executable. - * - * @param itemId the item id - * @return the t - * @throws ItemNotExecutableException the item not executable exception - * @throws Exception the exception - */ - T checkItemExecutable(String itemId) throws ItemNotExecutableException, Exception; - - - /** - * Checks if is item executable. - * - * @param itemId the item id - * @return the boolean - * @throws ItemNotExecutableException the item not executable exception - * @throws Exception the exception - */ - Boolean isItemExecutable(String itemId) throws ItemNotExecutableException, Exception; - -} diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/dataminer/TaskConfiguration.java b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/dataminer/TaskConfiguration.java index d88cb85..b5d1f26 100644 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/dataminer/TaskConfiguration.java +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/dataminer/TaskConfiguration.java @@ -29,11 +29,12 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable { private String taskId; @JsonIgnoreProperties private String taskDescription; //optional - private String token; + @JsonIgnoreProperties + private String token; //optional private String workspaceItemId; @JsonIgnoreProperties private Map mapParameters; //optional - @JsonIgnoreProperties + private String configurationKey; @@ -44,17 +45,22 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable { } + /** - * @param taskId - * @param taskDescription - * @param token - * @param workspaceItemId - * @param mapParameters + * Instantiates a new task configuration. + * + * @param configurationKey the configuration key + * @param taskId the task id + * @param taskDescription the task description + * @param token the token + * @param workspaceItemId the workspace item id + * @param mapParameters the map parameters */ - public TaskConfiguration( + public TaskConfiguration(String configurationKey, String taskId, String taskDescription, String token, String workspaceItemId, Map mapParameters) { + setConfigurationKey(configurationKey); this.taskId = taskId; this.taskDescription = taskDescription; this.token = token; @@ -156,6 +162,8 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable { /** + * Sets the task id. + * * @param taskId the taskId to set */ public void setTaskId(String taskId) { diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/exception/TaskConfigurationNotFoundException.java b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/exception/TaskConfigurationNotFoundException.java new file mode 100644 index 0000000..2f6e1be --- /dev/null +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/exception/TaskConfigurationNotFoundException.java @@ -0,0 +1,33 @@ +package org.gcube.common.workspacetaskexecutor.shared.exception; + + +/** + * The Class NoValidTaskConfigurationException. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 3, 2018 + */ +public class TaskConfigurationNotFoundException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 5569218683782613183L; + + /** + * Instantiates a new item not synched. + */ + public TaskConfigurationNotFoundException() { + super(); + } + + /** + * Instantiates a new item not synched. + * + * @param arg0 the arg 0 + */ + public TaskConfigurationNotFoundException(String arg0) { + super(arg0); + } + +} diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/util/JsonUtil.java b/src/main/java/org/gcube/common/workspacetaskexecutor/util/JsonUtil.java index 4bde7b1..ad10356 100644 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/util/JsonUtil.java +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/util/JsonUtil.java @@ -6,9 +6,12 @@ package org.gcube.common.workspacetaskexecutor.util; import java.io.IOException; import java.util.List; +import org.gcube.common.workspacetaskexecutor.util.JsonUtil.ActionResponse.Action; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; @@ -27,6 +30,8 @@ import com.fasterxml.jackson.databind.SerializationFeature; public class JsonUtil { private ObjectMapper objectMapper = new ObjectMapper(); + private static Logger logger = LoggerFactory.getLogger(JsonUtil.class); + /** @@ -42,13 +47,15 @@ public class JsonUtil { * * @param the generic type * @param obj the obj - * @return the string + * @return the JSON object * @throws JsonProcessingException the json processing exception + * @throws JSONException the JSON exception */ - public String toJSON(T obj) throws JsonProcessingException{ + public JSONObject toJSON(T obj) throws JsonProcessingException, JSONException{ // Convert object to JSON string - return objectMapper.writeValueAsString(obj); + String json = objectMapper.writeValueAsString(obj); + return new JSONObject(json); } @@ -86,18 +93,22 @@ public class JsonUtil { } + /** * To json array. * * @param the generic type * @param obj the obj - * @return the string + * @return the JSON array * @throws JsonProcessingException the json processing exception + * @throws JSONException the JSON exception */ - public String toJSONArray(List obj) throws JsonProcessingException{ + public JSONArray toJSONArray(List obj) throws JsonProcessingException, JSONException{ // Convert List to JSON string - return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); + String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); + + return new JSONArray(json); } @@ -107,9 +118,14 @@ public class JsonUtil { * @param jsonArray the json array * @param key the key * @param value the value - * @return the JSON array + * @return the action response + * @throws JSONException the JSON exception */ - public JSONArray removeJsonObject(JSONArray jsonArray, String key, String value){ + public ActionResponse removeJsonObject(JSONArray jsonArray, String key, String value) throws JSONException{ + + ActionResponse resp = new ActionResponse(); + logger.debug("Removing json object with ("+key+","+value+")"); + if (jsonArray != null) { JSONArray newArray = new JSONArray(); //THE NEW JSON ARRAY for (int i=0; i < jsonArray.length(); i++){ @@ -121,14 +137,17 @@ public class JsonUtil { //RETURNING IT IN THE NEW JSON ARRAY if(!itemArr.getString(key).equals(value)){ newArray.put(itemArr); + }else{ + resp.setActionPerformed(Action.REMOVE); } } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error("JSONException: ", e.getMessage()); + throw new JSONException(e.getMessage()); } } - return newArray; + resp.setNewArray(newArray); + return resp; } return null; @@ -144,8 +163,12 @@ public class JsonUtil { * @param key the key * @param value the value * @return the JSON array + * @throws JSONException */ - public JSONArray updateJsonObject(JSONArray jsonArray, JSONObject jsonObject, String key, String value){ + public ActionResponse updateJsonObject(JSONArray jsonArray, JSONObject jsonObject, String key, String value) throws JSONException{ + + ActionResponse resp = new ActionResponse(); + if (jsonArray != null) { boolean found = false; JSONArray newArray = new JSONArray(); //THE NEW JSON ARRAY @@ -160,25 +183,104 @@ public class JsonUtil { if(itemArr.getString(key).equals(value)){ newArray.put(jsonObject); found = true; + resp.setActionPerformed(Action.UPDATE); + logger.debug("Updated json with key: "+key+" and value: "+value); }else newArray.put(itemArr); } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error("JSONException: ", e); + throw new JSONException(e.getMessage()); } } if(!found){ newArray.put(jsonObject); + logger.debug("Added json with key: "+key+" and value: "+value); + resp.setActionPerformed(Action.ADD); } - return newArray; + resp.setNewArray(newArray); + return resp; } return null; } + public static class ActionResponse{ + public static enum Action {ADD, REMOVE, UPDATE} + JSONArray newArray = null; + Action actionPerformed; + + /** + * + */ + public ActionResponse() { + + // TODO Auto-generated constructor stub + } + /** + * @param newArray + * @param actionPerformed + */ + public ActionResponse(JSONArray newArray, Action actionPerformed) { + + super(); + this.newArray = newArray; + this.actionPerformed = actionPerformed; + } + + /** + * @return the newArray + */ + public JSONArray getNewArray() { + + return newArray; + } + + /** + * @return the actionPerformed + */ + public Action getActionPerformed() { + + return actionPerformed; + } + + /** + * @param newArray the newArray to set + */ + public void setNewArray(JSONArray newArray) { + + this.newArray = newArray; + } + + /** + * @param actionPerformed the actionPerformed to set + */ + public void setActionPerformed(Action actionPerformed) { + + this.actionPerformed = actionPerformed; + } + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("ActionResponse [newArray="); + builder.append(newArray); + builder.append(", actionPerformed="); + builder.append(actionPerformed); + builder.append("]"); + return builder.toString(); + } + + + + + } + } diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/util/WsUtil.java b/src/main/java/org/gcube/common/workspacetaskexecutor/util/WsUtil.java index f7e3bdf..212936a 100644 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/util/WsUtil.java +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/util/WsUtil.java @@ -126,7 +126,7 @@ public class WsUtil { Properties propertiesOBJ = item.getProperties(); properties.put(propertyName, propertyValue); propertiesOBJ.addProperties(properties); - logger.debug("Added properties "+properties+" to item: "+item); + logger.debug("Added properties "+properties+" to item: "+item.getId()); return true; } catch (InternalErrorException e) { diff --git a/src/test/java/org/gcube/common/workspacetaskexecutor/TestDataMinerTaskExecutor.java b/src/test/java/org/gcube/common/workspacetaskexecutor/TestDataMinerTaskExecutor.java index 47f279f..2b927f3 100644 --- a/src/test/java/org/gcube/common/workspacetaskexecutor/TestDataMinerTaskExecutor.java +++ b/src/test/java/org/gcube/common/workspacetaskexecutor/TestDataMinerTaskExecutor.java @@ -20,6 +20,8 @@ import org.gcube.common.workspacetaskexecutor.dataminer.WorkspaceDataMinerTaskEx import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration; import org.gcube.common.workspacetaskexecutor.util.JsonUtil; import org.gcube.common.workspacetaskexecutor.util.WsUtil; +import org.json.JSONArray; +import org.json.JSONException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; @@ -40,6 +42,51 @@ public class TestDataMinerTaskExecutor { private static JsonUtil jUtil = new JsonUtil(); + //CREATE DUMMY CONFIGURATIONS + private static List listDummyConf = gelDummyListOfConfigurations(3); + + /** + * + */ + private static void eraseAllTaskConfigurations(WorkspaceDataMinerTaskExecutor exec) { + + try { + Boolean done = exec.eraseAllTaskConfigurations(WORKSPACE_FOLDER_ID); + System.out.println("\n\nErase configurations done: "+done); + } + catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + + private static void setDummyTaskConfigurations(WorkspaceDataMinerTaskExecutor exec){ + + + //SET TASK CONFIGURATION + try { + for (TaskConfiguration taskConfiguration : listDummyConf) { + exec.setTaskConfiguration(taskConfiguration); + } + System.out.println("\n\nSet Task configurations done!"); + } + catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private static void deleteConfiguration(WorkspaceDataMinerTaskExecutor exec, TaskConfiguration conf){ + try { + Boolean done = exec.removeTaskConfiguration(conf); + System.out.println("\n\nErase configuration done: "+done); + } + catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public static void main(String[] args) { @@ -48,12 +95,56 @@ public class TestDataMinerTaskExecutor { exec.withOwner(USERNAME); //jsonCheck(); - checkGubeProperties(gelDummyListOfConfigurations()); + //GET LIST CONFIGURATIONS + getConfigurations(exec); + + //ERASE ALL CONFIGURATIONS + //eraseAllTaskConfigurations(exec); + + //SET TASK CONFIGURATION + //setDummyTaskConfigurations(exec); + + //deleteConfiguration(exec, listDummyConf.get(1)); + + //getConfigurations(exec); + + + listDummyConf.get(2).setTaskId("Updated task id"); + HashMap map = new HashMap(); + map.put("Pippo", "Value Pippo"); + map.put("Paperino", "Value Paperino"); + listDummyConf.get(2).setMapParameters(map); + + try { + exec.setTaskConfiguration(listDummyConf.get(2)); + } + catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }; + + getConfigurations(exec); + +// +// //UPDATE TASK CONFIGURATION +// HashMap map = new HashMap(); +// map.put("Pippo", "Value Pippo"); +// map.put("Paperino", "Value Paperino"); +// c2.setMapParameters(map); +// +// try { +// exec.setTaskConfiguration(c2); +// } +// catch (Exception e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } + + //checkGubeProperties(gelDummyListOfConfigurations()); // try { -// new TaskConfiguration(algorithmId, taskDescription, token, workspaceItemId, mapParameters) -// exec.removeTaskConfiguration(WORKSPACE_FOLDER_ID); +// //exec.removeTaskConfiguration(taskConfiguration) // //exec.checkItemExecutable(WORKSPACE_FOLDER_ID); // } // catch (ItemNotExecutableException e) { @@ -67,31 +158,26 @@ public class TestDataMinerTaskExecutor { // e.printStackTrace(); // } + } + public static List getConfigurations(WorkspaceDataMinerTaskExecutor exec){ -// try { -// AlgorithmConfiguration config = createDummyConfiguration(); -// exec.setTaskConfiguration(config); -// } -// catch (Exception e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// -// -// try { -// AlgorithmConfiguration conf = exec.checkItemExecutable(WORKSPACE_FOLDER_ID); -// -// System.out.println("The conf is: "+conf); -// } -// catch (ItemNotExecutableException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// catch (Exception e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } + try { + List conf = exec.getListOfTaskConfigurations(WORKSPACE_FOLDER_ID); + if(conf!=null){ + System.out.println("\n\nSaved configuration/s is/are: "); + for (TaskConfiguration taskConfiguration : conf) { + System.out.println(taskConfiguration); + } + } + + return conf; + } + catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; } @@ -101,24 +187,24 @@ public class TestDataMinerTaskExecutor { try { WorkspaceItem workspaceItem = WsUtil.getItem(USERNAME, WORKSPACE_FOLDER_ID); - String jsonArray = jUtil.toJSONArray(listConfigurations); + JSONArray jsonArray = jUtil.toJSONArray(listConfigurations); System.out.println("Json array to save: "+jsonArray); - WsUtil.setPropertyValue(workspaceItem, WorkspaceDataMinerTaskExecutor.WS_DM_TASK_TASK_CONF, jsonArray); + WsUtil.setPropertyValue(workspaceItem, WorkspaceDataMinerTaskExecutor.WS_DM_TASK_TASK_CONF, jsonArray.toString()); //GET - jsonArray = WsUtil.getPropertyValue(workspaceItem, WorkspaceDataMinerTaskExecutor.WS_DM_TASK_TASK_CONF); + String jsonArrayConf = WsUtil.getPropertyValue(workspaceItem, WorkspaceDataMinerTaskExecutor.WS_DM_TASK_TASK_CONF); System.out.println("Json array read from "+WorkspaceDataMinerTaskExecutor.WS_DM_TASK_TASK_CONF+": "+jsonArray); TypeReference> mapType = new TypeReference>() {}; - List listUnM = jUtil.readList(jsonArray, mapType); + List listUnM = jUtil.readList(jsonArrayConf, mapType); System.out.println("Json array to listUnM..."); for (TaskConfiguration taskConfiguration : listUnM) { System.out.println(taskConfiguration); } } catch (WorkspaceFolderNotFoundException | ItemNotFoundException - | InternalErrorException | HomeNotFoundException | JsonProcessingException e) { + | InternalErrorException | HomeNotFoundException | JsonProcessingException | JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } @@ -131,38 +217,38 @@ public class TestDataMinerTaskExecutor { } - public static TaskConfiguration createDummyConfiguration(){ + public static TaskConfiguration createDummyConfiguration(int index){ Map mapParameters = new HashMap(); - mapParameters.put("publiclink", "this is the public link"); - return new TaskConfiguration(UUID.randomUUID().toString(), null, "my token", WORKSPACE_FOLDER_ID, mapParameters); + mapParameters.put("publiclink", "this is the public link "+index); + return new TaskConfiguration(index+"", UUID.randomUUID().toString(), null, "my token", WORKSPACE_FOLDER_ID, mapParameters); } - public static List gelDummyListOfConfigurations(){ - TaskConfiguration c1 = createDummyConfiguration(); - TaskConfiguration c2 = createDummyConfiguration(); - List listConfigurations = new ArrayList<>(); - listConfigurations.add(c1); - listConfigurations.add(c2); + public static List gelDummyListOfConfigurations(int total){ + List listConfigurations = new ArrayList<>(total); + for (int i=0; i listConfigurations = gelDummyListOfConfigurations(); + List listConfigurations = gelDummyListOfConfigurations(3); try { - String jsonArray = jUtil.toJSONArray(listConfigurations); + JSONArray jsonArray = jUtil.toJSONArray(listConfigurations); System.out.println("Json array: "+jsonArray); TypeReference> mapType = new TypeReference>() {}; - List listUnM = jUtil.readList(jsonArray, mapType); + List listUnM = jUtil.readList(jsonArray.toString(), mapType); System.out.println("Json array to listUnM..."); for (TaskConfiguration taskConfiguration : listUnM) { System.out.println(taskConfiguration); } } - catch (JsonProcessingException e) { + catch (JsonProcessingException | JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }