diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/DMConverter.java b/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/DMConverter.java new file mode 100644 index 0000000..90e899e --- /dev/null +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/DMConverter.java @@ -0,0 +1,100 @@ +/** + * + */ +package org.gcube.common.workspacetaskexecutor.dataminer; + +import org.gcube.common.workspacetaskexecutor.shared.TaskParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ColumnListParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ColumnParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.DateParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.EnumParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.FileParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ListParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ObjectParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.Parameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ParameterType; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.TabularListParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.TabularParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.TimeParameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.WKTParameter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class DMConverter. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 7, 2018 + */ +public class DMConverter { + + + /** The logger. */ + private static Logger logger = LoggerFactory.getLogger(DMConverter.class); + + + /** + * To dm parameter. + * + * @param taskParameter the task parameter + * @return the parameter + */ + public static Parameter toDMParameter(TaskParameter taskParameter){ + + ParameterType toParameterType = null; + try{ + + if(taskParameter==null || taskParameter.getType()==null) + return null; + + toParameterType = ParameterType.valueOf(taskParameter.getType().getType()); + Parameter p = null; + switch (toParameterType) { + case FILE: + p =new FileParameter(); + break; + case OBJECT: + p = new ObjectParameter(); + break; + case TABULAR: + p = new TabularParameter(); + break; + case ENUM: + p = new EnumParameter(); + break; + case LIST: + p = new ListParameter(); + break; + case COLUMN: + p = new ColumnParameter(); + break; + case COLUMN_LIST: + p = new ColumnListParameter(); + break; + case DATE: + p = new DateParameter(); + break; + case TABULAR_LIST: + p = new TabularListParameter(); + break; + case TIME: + p = new TimeParameter(); + break; + case WKT: + p = new WKTParameter(); + break; + default: + break; + } + + p.setName(taskParameter.getKey()); + p.setValue(taskParameter.getValue()); + return p; + + }catch(Exception e){ + logger.warn("Impossible to convert the value: "+taskParameter+" at one of values "+ParameterType.values()); + return null; + } + } +} diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/DataMinerAccessPoint.java b/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/DataMinerAccessPoint.java index a0a9f24..dfdf97c 100644 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/DataMinerAccessPoint.java +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/DataMinerAccessPoint.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import org.gcube.common.workspacetaskexecutor.shared.BaseTaskConfiguration; +import org.gcube.common.workspacetaskexecutor.shared.TaskParameter; import org.gcube.common.workspacetaskexecutor.shared.TaskStatus; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskComputation; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration; @@ -24,8 +25,8 @@ import org.gcube.data.analysis.dataminermanagercl.shared.data.OutputData; import org.gcube.data.analysis.dataminermanagercl.shared.data.computations.ComputationId; import org.gcube.data.analysis.dataminermanagercl.shared.data.output.MapResource; import org.gcube.data.analysis.dataminermanagercl.shared.data.output.Resource; -import org.gcube.data.analysis.dataminermanagercl.shared.parameters.FileParameter; import org.gcube.data.analysis.dataminermanagercl.shared.parameters.Parameter; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ParameterType; import org.gcube.data.analysis.dataminermanagercl.shared.process.ComputationStatus; import org.gcube.data.analysis.dataminermanagercl.shared.process.ComputationStatus.Status; import org.gcube.data.analysis.dataminermanagercl.shared.process.Operator; @@ -384,21 +385,23 @@ public class DataMinerAccessPoint { * @param parameters the parameters * @return the operator */ - private Operator addParametersToOperator(Operator operator, Map parameters) { + private Operator addParametersToOperator(Operator operator, Map parameters) { logger.debug("Adding parameters to operator"); List listParameters = new ArrayList(); for (String key : parameters.keySet()) { -// ObjectParameter op = new ObjectParameter(); -// op.setName(key); -// op.setValue(parameters.get(key)); + TaskParameter taskParameter = parameters.get(key); + ParameterType toParameterType = null; + + if(taskParameter.getType()==null) + continue; + + Parameter dmParameter = DMConverter.toDMParameter(taskParameter); + if(dmParameter!=null) + listParameters.add(dmParameter); - FileParameter fp=new FileParameter(); - fp.setName(key); - fp.setValue(parameters.get(key)); - listParameters.add(fp); } logger.debug("Parameters list is: " + listParameters); operator.setOperatorParameters(listParameters); 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 47b8a49..ecee082 100644 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/WorkspaceDataMinerTaskExecutor.java +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/dataminer/WorkspaceDataMinerTaskExecutor.java @@ -7,6 +7,8 @@ import org.apache.commons.lang.Validate; import org.gcube.common.homelibrary.home.workspace.WorkspaceItem; import org.gcube.common.workspacetaskexecutor.shared.ExecutableItem; import org.gcube.common.workspacetaskexecutor.shared.ExecutableTask; +import org.gcube.common.workspacetaskexecutor.shared.TaskParameterAvailableTypes; +import org.gcube.common.workspacetaskexecutor.shared.TaskParameterType; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskComputation; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskExecutionStatus; @@ -16,8 +18,10 @@ import org.gcube.common.workspacetaskexecutor.shared.exception.TaskConfiguration import org.gcube.common.workspacetaskexecutor.shared.exception.TaskErrorException; import org.gcube.common.workspacetaskexecutor.shared.exception.TaskNotExecutableException; import org.gcube.common.workspacetaskexecutor.shared.exception.WorkspaceFolderLocked; +import org.gcube.common.workspacetaskexecutor.util.Converter; import org.gcube.common.workspacetaskexecutor.util.JsonUtil; import org.gcube.common.workspacetaskexecutor.util.WsUtil; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ParameterType; import org.json.JSONArray; import org.json.JSONException; import org.slf4j.Logger; @@ -183,6 +187,7 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask checkItemConfigurations(String workspaceItemId) throws ItemNotConfiguredException, WorkspaceFolderLocked, Exception{ @@ -207,24 +212,14 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask confs = getListOfTaskConfigurations(workspaceItemId); - if(propConfigValue==null || propConfigValue.isEmpty()){ - logger.debug("The item: "+workspaceItemId+" has not a configuration "+WS_DM_TASK_TASK_CONF); - return false; - } - - try{ - jsonUtil.readObject(propConfigValue, TaskConfiguration.class); - logger.debug("The item: "+workspaceItemId+" has a valid "+WS_DM_TASK_TASK_CONF+" configuration"); - return true; - }catch(Exception e){ - logger.error("Error on serializing configuration: "+propConfigValue, e); + if(confs==null || confs.isEmpty()){ + logger.debug("The item: "+workspaceItemId+" has not a (valid) configuration "+WS_DM_TASK_TASK_CONF); return false; } + logger.debug("The item: "+workspaceItemId+" has a valid "+WS_DM_TASK_TASK_CONF+" with "+confs.size()+" configuration/s"); + return true; } @@ -325,6 +320,22 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask types = new ArrayList(); + for (String string : typeNames) { + types.add(new TaskParameterType(string)); + } + return new TaskParameterAvailableTypes(types); + + } + /* (non-Javadoc) * @see org.gcube.common.workspacetaskexecutor.shared.ExecutableItem#eraseAllExecutableConfigurations() */ diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/BaseTaskConfiguration.java b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/BaseTaskConfiguration.java index 2ccacfd..9fe9f1c 100644 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/BaseTaskConfiguration.java +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/BaseTaskConfiguration.java @@ -42,8 +42,7 @@ public interface BaseTaskConfiguration { * * @return the map parameters */ - Map getMapParameters(); - + Map getMapParameters(); /** diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameter.java b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameter.java new file mode 100644 index 0000000..8c68a74 --- /dev/null +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameter.java @@ -0,0 +1,146 @@ +/** + * + */ +package org.gcube.common.workspacetaskexecutor.shared; + +import java.io.Serializable; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + + +/** + * The Class TaskParameter. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 7, 2018 + */ +public class TaskParameter implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 3607328256110736864L; + private String key; + private String value; + @JsonIgnoreProperties + private String defaultValue; //optional + private TaskParameterType type; + + /** + * Instantiates a new task parameter. + */ + public TaskParameter() { + + } + + /** + * Instantiates a new task parameter. + * + * @param key the key + * @param value the value + * @param type the type + */ + public TaskParameter(String key, String value, String defaultValue, TaskParameterType type) { + + super(); + this.key = key; + this.defaultValue = defaultValue; + this.value = value; + this.type = type; + } + + + /** + * @return the key + */ + public String getKey() { + + return key; + } + + + /** + * @return the value + */ + public String getValue() { + + return value; + } + + + /** + * @return the defaultValue + */ + public String getDefaultValue() { + + return defaultValue; + } + + + /** + * @return the type + */ + public TaskParameterType getType() { + + return type; + } + + + /** + * @param key the key to set + */ + public void setKey(String key) { + + this.key = key; + } + + + /** + * @param value the value to set + */ + public void setValue(String value) { + + this.value = value; + } + + + /** + * @param defaultValue the defaultValue to set + */ + public void setDefaultValue(String defaultValue) { + + this.defaultValue = defaultValue; + } + + + /** + * @param type the type to set + */ + public void setType(TaskParameterType type) { + + this.type = type; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("TaskParameter [key="); + builder.append(key); + builder.append(", value="); + builder.append(value); + builder.append(", defaultValue="); + builder.append(defaultValue); + builder.append(", type="); + builder.append(type); + builder.append("]"); + return builder.toString(); + } + + + + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameterAvailableTypes.java b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameterAvailableTypes.java new file mode 100644 index 0000000..161d152 --- /dev/null +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameterAvailableTypes.java @@ -0,0 +1,67 @@ +/** + * + */ + +package org.gcube.common.workspacetaskexecutor.shared; + +import java.io.Serializable; +import java.util.List; + + +/** + * The Class TaskParameterAvailableTypes. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 7, 2018 + */ +public class TaskParameterAvailableTypes implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 5005902773845652944L; + public List parametersTypes; + + /** + * Instantiates a new base task parameter types. + */ + public TaskParameterAvailableTypes() { + + } + + /** + * Instantiates a new task parameter available types. + * + * @param availableParameterTypes the available parameter types + */ + public TaskParameterAvailableTypes(List availableParameterTypes) { + + this.parametersTypes = availableParameterTypes; + } + + + /** + * Gets the parameters types. + * + * @return the parameters types + */ + public List getParametersTypes() { + + return parametersTypes; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("TaskParameterTypes [parametersTypes="); + builder.append(parametersTypes); + builder.append("]"); + return builder.toString(); + } + + +} diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameterType.java b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameterType.java new file mode 100644 index 0000000..6a18af9 --- /dev/null +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/shared/TaskParameterType.java @@ -0,0 +1,65 @@ +/** + * + */ + +package org.gcube.common.workspacetaskexecutor.shared; + +import java.io.Serializable; + + +/** + * The Class TaskParameterType. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 7, 2018 + */ +public final class TaskParameterType implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 5084714477390724544L; + private String type; + + /** + * Instantiates a new task parameter type. + */ + public TaskParameterType() { + + } + + /** + * Instantiates a new task parameter type. + * + * @param type the type + */ + public TaskParameterType(String type) { + + this.type = type; + } + + + /** + * Gets the type. + * + * @return the type + */ + public String getType() { + + return type; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("TaskParameterType [type="); + builder.append(type); + builder.append("]"); + return builder.toString(); + } +} 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 ec7cce8..2f0daf9 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 @@ -9,6 +9,7 @@ import java.util.Map; import java.util.Random; import org.gcube.common.workspacetaskexecutor.shared.BaseTaskConfiguration; +import org.gcube.common.workspacetaskexecutor.shared.TaskParameter; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -39,7 +40,7 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable { private String maskedToken; private String workspaceItemId; @JsonIgnoreProperties - private Map mapParameters; // optional + private Map mapParameters; // optional private String configurationKey; /** @@ -63,7 +64,7 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable { public TaskConfiguration( String configurationKey, String taskId, String taskDescription, String scope, String maskedToken, String workspaceItemId, - Map mapParameters) { + Map mapParameters) { setConfigurationKey(configurationKey); this.taskId = taskId; @@ -146,16 +147,24 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable { return workspaceItemId; } + /** - * Gets the map parameters. - * * @return the mapParameters */ - public Map getMapParameters() { + public Map getMapParameters() { return mapParameters; } + + /** + * @param mapParameters the mapParameters to set + */ + public void setMapParameters(Map mapParameters) { + + this.mapParameters = mapParameters; + } + /** * Sets the task description. * @@ -189,16 +198,7 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable { this.workspaceItemId = workspaceItemId; } - /** - * Sets the map parameters. - * - * @param mapParameters - * the mapParameters to set - */ - public void setMapParameters(Map mapParameters) { - this.mapParameters = mapParameters; - } /* * (non-Javadoc) @@ -234,6 +234,8 @@ public class TaskConfiguration implements BaseTaskConfiguration, Serializable { this.maskedToken = maskedToken; } + + /* (non-Javadoc) * @see java.lang.Object#toString() */ diff --git a/src/main/java/org/gcube/common/workspacetaskexecutor/util/Converter.java b/src/main/java/org/gcube/common/workspacetaskexecutor/util/Converter.java index 4369829..7a7fb65 100644 --- a/src/main/java/org/gcube/common/workspacetaskexecutor/util/Converter.java +++ b/src/main/java/org/gcube/common/workspacetaskexecutor/util/Converter.java @@ -3,11 +3,14 @@ */ package org.gcube.common.workspacetaskexecutor.util; +import java.util.Arrays; + import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskComputation; import org.gcube.data.analysis.dataminermanagercl.shared.data.computations.ComputationId; /** + * The Class Converter. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * May 3, 2018 @@ -44,4 +47,15 @@ public class Converter { return new ComputationId(computationId.getId(), computationId.getUrlId(), computationId.getOperatorId(), computationId.getOperatorName(), computationId.getEquivalentRequest()); } + + + /** + * Convert enum names to arra string. + * + * @param e the e + * @return the string[] + */ + public static String[] convertEnumNamesToArraString(Class> e){ + return Arrays.stream(e.getEnumConstants()).map(Enum::name).toArray(String[]::new); + } } diff --git a/src/test/java/org/gcube/common/workspacetaskexecutor/TestDataMinerTaskExecutor.java b/src/test/java/org/gcube/common/workspacetaskexecutor/TestDataMinerTaskExecutor.java index 1f07242..616836d 100644 --- a/src/test/java/org/gcube/common/workspacetaskexecutor/TestDataMinerTaskExecutor.java +++ b/src/test/java/org/gcube/common/workspacetaskexecutor/TestDataMinerTaskExecutor.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.UUID; import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException; @@ -17,9 +18,14 @@ import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundExcept import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.workspacetaskexecutor.dataminer.WorkspaceDataMinerTaskExecutor; +import org.gcube.common.workspacetaskexecutor.shared.TaskParameter; +import org.gcube.common.workspacetaskexecutor.shared.TaskParameterAvailableTypes; +import org.gcube.common.workspacetaskexecutor.shared.TaskParameterType; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration; +import org.gcube.common.workspacetaskexecutor.util.Converter; import org.gcube.common.workspacetaskexecutor.util.JsonUtil; import org.gcube.common.workspacetaskexecutor.util.WsUtil; +import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ParameterType; import org.json.JSONArray; import org.json.JSONException; @@ -95,35 +101,38 @@ public class TestDataMinerTaskExecutor { exec.withOwner(USERNAME); //jsonCheck(); - //GET LIST CONFIGURATIONS - getConfigurations(exec); - //ERASE ALL CONFIGURATIONS eraseAllTaskConfigurations(exec); + //GET LIST CONFIGURATIONS + //getConfigurations(exec); + + //ERASE ALL CONFIGURATIONS + //eraseAllTaskConfigurations(exec); + //SET TASK CONFIGURATION setDummyTaskConfigurations(exec); - - deleteConfiguration(exec, listDummyConf.get(1)); - +// +//// 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); +// 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 @@ -212,14 +221,37 @@ public class TestDataMinerTaskExecutor { // TODO Auto-generated catch block e.printStackTrace(); } - - - } public static TaskConfiguration createDummyConfiguration(int index){ - Map mapParameters = new HashMap(); - mapParameters.put("publiclink", "this is the public link "+index); + String[] availableTypes = Converter.convertEnumNamesToArraString(ParameterType.class); + List types = new ArrayList(); + for (String string : availableTypes) { + types.add(new TaskParameterType(string)); + } + + TaskParameterAvailableTypes tpt = new TaskParameterAvailableTypes(types); + System.out.println(tpt.toString()); + +// for (String availableType : tpt.getParametersTypes()) { +// System.out.println(availableType); +// } + + Map mapParameters = new HashMap(); + + TaskParameter tp = new TaskParameter(); + tp.setKey("publiclink"); + tp.setValue("this is the public link "+index); + tp.setType(new TaskParameterType(ParameterType.FILE.toString())); + + mapParameters.put("publiclink", tp); + + TaskParameter tp2 = new TaskParameter(); + tp2.setKey("key"+index); + tp2.setValue("value "+index); + tp2.setType(tpt.getParametersTypes().get(new Random().nextInt(tpt.getParametersTypes().size()))); + mapParameters.put("publiclink", tp2); + return new TaskConfiguration(index+"", UUID.randomUUID().toString(), null, SCOPE, "my token", WORKSPACE_FOLDER_ID, mapParameters); }