…Martin, thanks for your reply, I read what you recommend me, but, I understand this is about the steps in a workflow, but I want to do is in one transaction add differents attachments to a task, some comments to the same task, and if all of them end successfully update the task, in other case rollback everything done before exception was triggered. All of this made programmatically.
The code here :
@SuppressWarnings("unchecked")
public static HashMap<String, Object> updateTaskById(String userId, String taskId, String processInstanceId, HashMap<String, Object> params) throws BussinesException, IOException, SQLException, NotSupportedException, SystemException, NamingException
{
HashMap<String, Object> result = new HashMap<String, Object>();
String consultedUser = (String) params.get("consultedUser");
WorkflowService.getIdentityService().setAuthenticatedUserId(consultedUser != null && consultedUser.trim().length() > 0 ? consultedUser : userId);
HashMap<String, String> documentsData = (HashMap<String, String>) params.get("documentsData");
HashMap<String, Object> variables = (HashMap<String, Object>) params.get("variables");
System.out.println("VARIABLES=>" + variables);
String dirPath = null;
String[] documents = ((String) variables.get("document.documents")).split(";");
for (String doc : documents)
{
String documentPath = documentsData.get(doc + ".path");
String documentType = documentsData.get(doc + ".contetType");
String attachmentId;
if (dirPath == null && documentPath != null && documentPath.length() > 0)
{
dirPath = documentPath.substring(0, documentPath.lastIndexOf("/") + 1);
}
if (documentPath != null && documentPath.length() > 0)
{
if (dirPath == null)
{
dirPath = documentPath.substring(0, documentPath.lastIndexOf("/") + 1);
}
File file = new File(documentPath);
String docKey;
attachmentId = (String) variables.get(docKey = "document." + doc + ".value");
if (file.exists())
{
/*If some addAttachment fail, perform a rollback */
variables.put(docKey, addAttachment(documentType, taskId, processInstanceId, file.getName(), "", new FileInputStream(file)));
if (attachmentId != null && attachmentId.length() > 0)
{
removeAttachmentById(attachmentId);
}
}
else
{
throw new BussinesException("No se encontro el Archivo");
}
}
}
ArrayList<HashMap<String, Object>> comments = (ArrayList<HashMap<String, Object>>) params.get("comments");
for (HashMap<String, Object> comment : comments)
{
String fullMessage = (String) comment.get("fullMessage");
if (comment.get("id") == null && fullMessage != null)
{
System.out.println("Insert Comment=>" + comment);
/*If some addComment fail, perform a rollback */
WorkflowService.getTaskService().addComment(taskId, processInstanceId, fullMessage);
}
}
if (dirPath != null)
{
FileUtils.cleanDirectory(new File(dirPath));
}
updateTaskParams(taskId, variables);
/*If everything goes fine perform commit */
result.put("variables", variables);
System.out.println("VARIABLES=>" + variables);
return result;
}
Thanks for your help.