Enhancement on Project Activity #11690
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/workspace-task-executor-library@167299 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
4978626ce7
commit
0e383c40be
|
@ -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<TaskConfiguration, TaskExecutionStatus>, ConfigurableTask<TaskConfiguration>, IsItemExecutable<TaskConfiguration>{
|
||||
public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfiguration, TaskExecutionStatus>, ExecutableItem<TaskConfiguration>{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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<TaskConfig
|
|||
Validate.notNull(taskConfiguration.getTaskId(), "The Task Id in the configuration is null");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the configuration from saved.
|
||||
*
|
||||
* @param workspaceItemId the workspace item id
|
||||
* @param configurationKey the configuration key
|
||||
* @return the configuration from saved
|
||||
* @throws TaskConfigurationNotFoundException the task configuration not found exception
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
private TaskConfiguration getConfigurationFromSaved(String workspaceItemId, String configurationKey) throws TaskConfigurationNotFoundException, Exception{
|
||||
|
||||
List<TaskConfiguration> 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<TaskConfiguration> getListOfTaskConfigurations(String workspaceItemId) throws Exception {
|
||||
logger.debug("Get list of Task Configurations for "+workspaceItemId+" starts...");
|
||||
List<TaskConfiguration> 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<List<TaskConfiguration>> mapType = new TypeReference<List<TaskConfiguration>>() {};
|
||||
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<TaskConfig
|
|||
*/
|
||||
@Override
|
||||
public Boolean isItemExecutable(String workspaceItemId) throws ItemNotExecutableException, Exception {
|
||||
|
||||
logger.debug("Is Item "+workspaceItemId+" Executable starts...");
|
||||
checkOwner();
|
||||
WorkspaceItem item = WsUtil.getItem(usernameOwner, workspaceItemId);
|
||||
String propConfigValue = WsUtil.getPropertyValue(item, WS_DM_TASK_TASK_CONF);
|
||||
|
@ -165,20 +209,51 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
|
|||
*/
|
||||
@Override
|
||||
public Boolean removeTaskConfiguration(TaskConfiguration taskConfiguration) throws ItemNotExecutableException, Exception {
|
||||
logger.debug("Remove task configuration "+taskConfiguration+ "starts...");
|
||||
|
||||
ValidateTaskConfiguration(taskConfiguration);
|
||||
checkOwner();
|
||||
boolean found = false;
|
||||
|
||||
WorkspaceItem item = WsUtil.getItem(usernameOwner, taskConfiguration.getWorkspaceItemId());
|
||||
String propConfig = WsUtil.getPropertyValue(item, WS_DM_TASK_TASK_CONF);
|
||||
List<TaskConfiguration> 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<TaskConfiguration> newConfigurations = new ArrayList<TaskConfiguration>(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<TaskConfig
|
|||
*/
|
||||
@Override
|
||||
public Boolean setTaskConfiguration(TaskConfiguration taskConfiguration) throws Exception {
|
||||
|
||||
logger.debug("Set task configuration "+taskConfiguration+" starts...");
|
||||
ValidateTaskConfiguration(taskConfiguration);
|
||||
checkOwner();
|
||||
|
||||
boolean found = false;
|
||||
WorkspaceItem item = WsUtil.getItem(usernameOwner, taskConfiguration.getWorkspaceItemId());
|
||||
String jsonConfValue = null;
|
||||
List<TaskConfiguration> configurations = getListOfTaskConfigurations(taskConfiguration.getWorkspaceItemId());
|
||||
|
||||
if(configurations==null)
|
||||
configurations = new ArrayList<TaskConfiguration>(1); //It is the first configuration servert-side
|
||||
|
||||
List<TaskConfiguration> newConfigurations = new ArrayList<TaskConfiguration>(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<TaskConfig
|
|||
ValidateTaskConfiguration(taskConfiguration);
|
||||
checkOwner();
|
||||
|
||||
TaskConfiguration conf = checkItemExecutable(taskConfiguration.getWorkspaceItemId());
|
||||
|
||||
TaskConfiguration taskConf = getConfigurationFromSaved(taskConfiguration.getWorkspaceItemId(), taskConfiguration.getConfigurationKey());
|
||||
DataMinerAccessPoint dap = getDataMinerAccessPoint();
|
||||
return dap.doRunTask(conf);
|
||||
return dap.doRunTask(taskConf);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
@ -247,4 +357,7 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
|
|||
return dap.getTaskStatus(taskConfiguration);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.common.workspacetaskexecutor.shared;
|
||||
|
||||
import org.gcube.common.workspacetaskexecutor.shared.exception.ItemNotExecutableException;
|
||||
|
||||
|
||||
/**
|
||||
* The Interface ConfigurableTask.
|
||||
*
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* May 2, 2018
|
||||
* @param <I> the generic type
|
||||
*/
|
||||
public interface ConfigurableTask<I extends BaseTaskConfiguration> {
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
|
@ -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 <T> the generic type must extends {@link BaseTaskConfiguration}
|
||||
*/
|
||||
public interface ExecutableItem<T extends BaseTaskConfiguration> {
|
||||
|
||||
/**
|
||||
* 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<T> 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;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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 <T> the generic type must exteds {@link BaseTaskConfiguration}
|
||||
*/
|
||||
public interface IsItemExecutable<T extends BaseTaskConfiguration> {
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
}
|
|
@ -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<String, String> 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<String, String> 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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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 <T> 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 <T> String toJSON(T obj) throws JsonProcessingException{
|
||||
public <T> 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 <T> 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 <T> String toJSONArray(List<T> obj) throws JsonProcessingException{
|
||||
public <T> JSONArray toJSONArray(List<T> obj) throws JsonProcessingException, JSONException{
|
||||
|
||||
// Convert List<T> 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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<TaskConfiguration> 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<String, String> map = new HashMap<String, String>();
|
||||
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<String, String> map = new HashMap<String, String>();
|
||||
// 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<TaskConfiguration> 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<TaskConfiguration> 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<List<TaskConfiguration>> mapType = new TypeReference<List<TaskConfiguration>>() {};
|
||||
List<TaskConfiguration> listUnM = jUtil.readList(jsonArray, mapType);
|
||||
List<TaskConfiguration> 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<String, String> mapParameters = new HashMap<String, String>();
|
||||
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<TaskConfiguration> gelDummyListOfConfigurations(){
|
||||
TaskConfiguration c1 = createDummyConfiguration();
|
||||
TaskConfiguration c2 = createDummyConfiguration();
|
||||
List<TaskConfiguration> listConfigurations = new ArrayList<>();
|
||||
listConfigurations.add(c1);
|
||||
listConfigurations.add(c2);
|
||||
public static List<TaskConfiguration> gelDummyListOfConfigurations(int total){
|
||||
List<TaskConfiguration> listConfigurations = new ArrayList<>(total);
|
||||
for (int i=0; i<total; i++) {
|
||||
TaskConfiguration c = createDummyConfiguration(i);
|
||||
listConfigurations.add(c);
|
||||
}
|
||||
return listConfigurations;
|
||||
}
|
||||
|
||||
public static void jsonCheck(){
|
||||
|
||||
|
||||
List<TaskConfiguration> listConfigurations = gelDummyListOfConfigurations();
|
||||
List<TaskConfiguration> listConfigurations = gelDummyListOfConfigurations(3);
|
||||
try {
|
||||
String jsonArray = jUtil.toJSONArray(listConfigurations);
|
||||
JSONArray jsonArray = jUtil.toJSONArray(listConfigurations);
|
||||
System.out.println("Json array: "+jsonArray);
|
||||
TypeReference<List<TaskConfiguration>> mapType = new TypeReference<List<TaskConfiguration>>() {};
|
||||
List<TaskConfiguration> listUnM = jUtil.readList(jsonArray, mapType);
|
||||
List<TaskConfiguration> 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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue