193 lines
5.8 KiB
Java
193 lines
5.8 KiB
Java
package org.gcube.common.workspacetaskexecutor.dataminer;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
|
import org.gcube.common.workspacetaskexecutor.CheckableTask;
|
|
import org.gcube.common.workspacetaskexecutor.ConfigurableTask;
|
|
import org.gcube.common.workspacetaskexecutor.ExecutableTask;
|
|
import org.gcube.common.workspacetaskexecutor.JsonUtil;
|
|
import org.gcube.common.workspacetaskexecutor.TaskConfiguration;
|
|
import org.gcube.common.workspacetaskexecutor.WsUtil;
|
|
import org.gcube.common.workspacetaskexecutor.shared.ItemNotExecutableException;
|
|
import org.gcube.common.workspacetaskexecutor.shared.dataminer.AlgorithmConfiguration;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
/**
|
|
* The Class WorkspaceDataMinerTaskExecute.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Apr 26, 2018
|
|
*/
|
|
public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<AlgorithmConfiguration>, ConfigurableTask<AlgorithmConfiguration>, CheckableTask<AlgorithmConfiguration>{
|
|
|
|
/** The logger. */
|
|
private static Logger logger = LoggerFactory.getLogger(WorkspaceDataMinerTaskExecutor.class);
|
|
|
|
private static WorkspaceDataMinerTaskExecutor INSTANCE = null;
|
|
|
|
public static final String WS_TASK_TASK_CONF = "WS-TASK.TASK-CONF";
|
|
|
|
private String usernameOwner;
|
|
|
|
private JsonUtil<AlgorithmConfiguration> jsonUtil = new JsonUtil<AlgorithmConfiguration>();
|
|
|
|
|
|
/**
|
|
* Instantiates a new workspace data miner task executor.
|
|
*/
|
|
private WorkspaceDataMinerTaskExecutor() {
|
|
}
|
|
|
|
|
|
/**
|
|
* With owner.
|
|
*
|
|
* @param usernameOwner the username owner
|
|
*/
|
|
public void withOwner(String usernameOwner){
|
|
this.usernameOwner = usernameOwner;
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the single instance of WorkspaceDataMinerTaskExecutor.
|
|
*
|
|
* @return single instance of WorkspaceDataMinerTaskExecutor
|
|
*/
|
|
public static WorkspaceDataMinerTaskExecutor getInstance() {
|
|
if (INSTANCE == null) {
|
|
INSTANCE = new WorkspaceDataMinerTaskExecutor();
|
|
}
|
|
return INSTANCE;
|
|
}
|
|
|
|
|
|
/**
|
|
* Check owner.
|
|
*
|
|
* @throws Exception the exception
|
|
*/
|
|
private void checkOwner() throws Exception {
|
|
|
|
if(usernameOwner==null || usernameOwner.isEmpty())
|
|
throw new Exception("You must set a valid 'usernameOwner'. Using the method #withOwner");
|
|
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.CheckableTask#checkItemExecutable(java.lang.String)
|
|
*/
|
|
@Override
|
|
public AlgorithmConfiguration checkItemExecutable(String workspaceItemId) throws ItemNotExecutableException, Exception {
|
|
|
|
AlgorithmConfiguration conf = null;
|
|
checkOwner();
|
|
WorkspaceItem item = WsUtil.getItem(usernameOwner, workspaceItemId);
|
|
String propConfigValue = WsUtil.getPropertyValue(item, WS_TASK_TASK_CONF);
|
|
|
|
logger.info("Read "+WS_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, AlgorithmConfiguration.class);
|
|
}catch(Exception e){
|
|
logger.error("Error on serializing configuration: "+propConfigValue, e);
|
|
throw new ItemNotExecutableException("The item id "+workspaceItemId+" has a wrong "+AlgorithmConfiguration.class.getSimpleName()+". Do you want create a new one?");
|
|
}
|
|
|
|
logger.info("Found configuration: "+conf);
|
|
|
|
return conf;
|
|
}
|
|
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ConfigurableTask#removeTaskConfig(java.lang.Object)
|
|
*/
|
|
@Override
|
|
public Boolean removeTaskConfig(String workspaceItemId) throws ItemNotExecutableException, Exception {
|
|
|
|
WorkspaceItem item = WsUtil.getItem(usernameOwner, workspaceItemId);
|
|
String propConfig = WsUtil.getPropertyValue(item, WS_TASK_TASK_CONF);
|
|
|
|
if(propConfig==null || propConfig.isEmpty())
|
|
throw new ItemNotExecutableException("The item id "+workspaceItemId+" has not a "+AlgorithmConfiguration.class.getSimpleName());
|
|
|
|
WsUtil.setPropertyValue(item, WS_TASK_TASK_CONF, null);
|
|
logger.info("Set property value "+WS_TASK_TASK_CONF+" as null for the workspace item id: "+workspaceItemId);
|
|
return true;
|
|
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ConfigurableTask#addTaskConfig(java.lang.Object)
|
|
*/
|
|
@Override
|
|
public Boolean setTaskConfig(AlgorithmConfiguration config) throws Exception {
|
|
|
|
WorkspaceItem item = WsUtil.getItem(usernameOwner, config.getWorkspaceItemId());
|
|
String jsonConfValue = null;
|
|
|
|
try{
|
|
jsonConfValue = jsonUtil.toJSON(config);
|
|
logger.debug("The json configuration is: "+jsonConfValue);
|
|
}catch(Exception e){
|
|
throw new Exception("This "+config+" is wrong!. Please create a new one");
|
|
}
|
|
|
|
if(jsonConfValue!=null){
|
|
WsUtil.setPropertyValue(item, WS_TASK_TASK_CONF, jsonConfValue);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ExecutableTask#doRun(java.lang.String)
|
|
*/
|
|
@Override
|
|
public AlgorithmConfiguration doRun(String itemId) throws ItemNotExecutableException, Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
return null;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ExecutableTask#stopRun(java.lang.String)
|
|
*/
|
|
@Override
|
|
public Boolean stopRun(String itemId)
|
|
throws ItemNotExecutableException, Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
return null;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ExecutableTask#monitorRunStatus(java.lang.String)
|
|
*/
|
|
@Override
|
|
public AlgorithmConfiguration monitorRunStatus(String itemId)
|
|
throws ItemNotExecutableException, Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
return null;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.common.workspacetaskexecutor.ExecutableTask#registerCallbackForId(java.lang.String)
|
|
*/
|
|
@Override
|
|
public void registerCallbackForId(String itemId)
|
|
throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
|
|
|
}
|