ALF-19858: Fixed instance cleanup and added more unit tests

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@54602 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tijs Rademakers
2013-08-29 08:19:08 +00:00
parent 104dc484d5
commit 61e3565081
9 changed files with 1376 additions and 629 deletions

View File

@@ -61,7 +61,6 @@ import org.alfresco.repo.workflow.WorkflowPropertyHandlerRegistry;
import org.alfresco.repo.workflow.WorkflowQNameConverter;
import org.alfresco.repo.workflow.activiti.ActivitiConstants;
import org.alfresco.repo.workflow.activiti.ActivitiNodeConverter;
import org.alfresco.repo.workflow.activiti.ActivitiScriptNode;
import org.alfresco.repo.workflow.activiti.ActivitiTypeConverter;
import org.alfresco.repo.workflow.activiti.ActivitiUtil;
import org.alfresco.repo.workflow.activiti.properties.ActivitiPropertyConverter;
@@ -85,11 +84,7 @@ import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.InvalidQNameException;
@@ -101,8 +96,6 @@ import org.apache.commons.io.IOUtils;
public class ProcessesImpl extends WorkflowRestImpl implements Processes
{
protected static final String BPM_PACKAGE = "bpm_package";
protected static String PROCESS_STATUS_ANY = "any";
protected static String PROCESS_STATUS_ACTIVE = "active";
protected static String PROCESS_STATUS_COMPLETED = "completed";
@@ -130,7 +123,6 @@ public class ProcessesImpl extends WorkflowRestImpl implements Processes
protected WorkflowPackageImpl workflowPackageComponent;
protected ServiceRegistry serviceRegistry;
protected AuthorityDAO authorityDAO;
protected NodeService nodeService;
protected PersonService personService;
protected MessageService messageService;
protected String engineId;
@@ -163,11 +155,6 @@ public class ProcessesImpl extends WorkflowRestImpl implements Processes
this.workflowPackageComponent = workflowPackageComponent;
}
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public void setPersonService(PersonService personService)
{
this.personService = personService;
@@ -754,42 +741,7 @@ public class ProcessesImpl extends WorkflowRestImpl implements Processes
}
validateIfUserAllowedToWorkWithProcess(processId);
ActivitiScriptNode packageScriptNode = null;
try
{
HistoricVariableInstance variableInstance = activitiProcessEngine.getHistoryService()
.createHistoricVariableInstanceQuery()
.processInstanceId(processId)
.variableName(BPM_PACKAGE)
.singleResult();
if (variableInstance != null)
{
packageScriptNode = (ActivitiScriptNode) variableInstance.getValue();
}
else
{
throw new EntityNotFoundException(processId);
}
}
catch (ActivitiObjectNotFoundException e)
{
throw new EntityNotFoundException(processId);
}
List<Item> page = new ArrayList<Item>();
if (packageScriptNode != null)
{
List<ChildAssociationRef> documentList = nodeService.getChildAssocs(packageScriptNode.getNodeRef());
for (ChildAssociationRef childAssociationRef : documentList)
{
Item item = createItemForNodeRef(childAssociationRef.getChildRef());
page.add(item);
}
}
return CollectionWithPagingInfo.asPaged(paging, page, false, page.size());
return getItemsFromProcess(processId, paging);
}
@Override
@@ -805,52 +757,8 @@ public class ProcessesImpl extends WorkflowRestImpl implements Processes
throw new InvalidArgumentException("itemId is required to get an attached item");
}
NodeRef nodeRef = getNodeRef(itemId);
validateIfUserAllowedToWorkWithProcess(processId);
ActivitiScriptNode packageScriptNode = null;
try
{
HistoricVariableInstance variableInstance = activitiProcessEngine.getHistoryService()
.createHistoricVariableInstanceQuery()
.processInstanceId(processId)
.variableName(BPM_PACKAGE)
.singleResult();
if (variableInstance != null)
{
packageScriptNode = (ActivitiScriptNode) variableInstance.getValue();
}
else
{
throw new EntityNotFoundException(processId);
}
}
catch (ActivitiObjectNotFoundException e)
{
throw new EntityNotFoundException(processId);
}
Item item = null;
if (packageScriptNode != null)
{
List<ChildAssociationRef> documentList = nodeService.getChildAssocs(packageScriptNode.getNodeRef());
for (ChildAssociationRef childAssociationRef : documentList)
{
if (childAssociationRef.getChildRef().equals(nodeRef))
{
item = createItemForNodeRef(childAssociationRef.getChildRef());
break;
}
}
}
if (item == null) {
throw new EntityNotFoundException(itemId);
}
return item;
return getItemFromProcess(itemId, processId);
}
@Override
@@ -867,46 +775,7 @@ public class ProcessesImpl extends WorkflowRestImpl implements Processes
}
validateIfUserAllowedToWorkWithProcess(processId);
NodeRef nodeRef = getNodeRef(item.getId());
ActivitiScriptNode packageScriptNode = null;
try
{
packageScriptNode = (ActivitiScriptNode) activitiProcessEngine.getRuntimeService().getVariable(processId, BPM_PACKAGE);
}
catch (ActivitiObjectNotFoundException e)
{
throw new EntityNotFoundException(processId);
}
if (packageScriptNode == null)
{
throw new InvalidArgumentException("process doesn't contain a workflow package variable");
}
// check if noderef exists
try
{
nodeService.getProperties(nodeRef);
}
catch (Exception e)
{
throw new EntityNotFoundException("item with id " + nodeRef.toString() + " not found");
}
try
{
QName workflowPackageItemId = QName.createQName("wpi", nodeRef.toString());
nodeService.addChild(packageScriptNode.getNodeRef(), nodeRef,
WorkflowModel.ASSOC_PACKAGE_CONTAINS, workflowPackageItemId);
}
catch (Exception e)
{
throw new ApiException("could not add item to process " + e.getMessage(), e);
}
return item;
return createItemInProcess(item.getId(), processId);
}
@Override
@@ -922,49 +791,8 @@ public class ProcessesImpl extends WorkflowRestImpl implements Processes
throw new InvalidArgumentException("itemId is required to delete an attached item");
}
NodeRef nodeRef = getNodeRef(itemId);
validateIfUserAllowedToWorkWithProcess(processId);
ActivitiScriptNode packageScriptNode = null;
try
{
packageScriptNode = (ActivitiScriptNode) activitiProcessEngine.getRuntimeService().getVariable(processId, BPM_PACKAGE);
}
catch (ActivitiObjectNotFoundException e)
{
throw new EntityNotFoundException(processId);
}
if (packageScriptNode == null)
{
throw new InvalidArgumentException("process doesn't contain a workflow package variable");
}
boolean itemIdFoundInPackage = false;
List<ChildAssociationRef> documentList = nodeService.getChildAssocs(packageScriptNode.getNodeRef());
for (ChildAssociationRef childAssociationRef : documentList)
{
if (childAssociationRef.getChildRef().equals(nodeRef))
{
itemIdFoundInPackage = true;
break;
}
}
if (itemIdFoundInPackage == false)
{
throw new EntityNotFoundException("Item " + itemId + " not found in the process package variable");
}
try
{
nodeService.removeChild(packageScriptNode.getNodeRef(), nodeRef);
}
catch (InvalidNodeRefException e)
{
throw new EntityNotFoundException("Item " + itemId + " not found");
}
deleteItemFromProcess(itemId, processId);
}
@Override
@@ -1233,33 +1061,4 @@ public class ProcessesImpl extends WorkflowRestImpl implements Processes
processInfo.setProcessDefinitionKey(getLocalProcessDefinitionKey(definitionEntity.getKey()));
return processInfo;
}
protected Item createItemForNodeRef(NodeRef nodeRef) {
Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
Item item = new Item();
String name = (String) properties.get(ContentModel.PROP_NAME);
String title = (String) properties.get(ContentModel.PROP_TITLE);
String description = (String) properties.get(ContentModel.PROP_DESCRIPTION);
Date createdAt = (Date) properties.get(ContentModel.PROP_CREATED);
String createdBy = (String) properties.get(ContentModel.PROP_CREATOR);
Date modifiedAt = (Date) properties.get(ContentModel.PROP_MODIFIED);
String modifiedBy = (String) properties.get(ContentModel.PROP_MODIFIER);
ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
item.setId(nodeRef.getId());
item.setName(name);
item.setTitle(title);
item.setDescription(description);
item.setCreatedAt(createdAt);
item.setCreatedBy(createdBy);
item.setModifiedAt(modifiedAt);
item.setModifiedBy(modifiedBy);
if (contentData != null)
{
item.setMimeType(contentData.getMimetype());
item.setSize(contentData.getSize());
}
return item;
}
}

View File

@@ -54,7 +54,6 @@ import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.resource.parameters.where.QueryHelper;
import org.alfresco.rest.workflow.api.Processes;
import org.alfresco.rest.workflow.api.Tasks;
import org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker.QueryVariableHolder;
import org.alfresco.rest.workflow.api.model.FormModelElement;
@@ -123,7 +122,6 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
private WorkflowObjectFactory workflowFactory;
private WorkflowQNameConverter qNameConverter;
private MessageService messageService;
private Processes processes;
public void setRestVariableHelper(RestVariableHelper restVariableHelper)
{
@@ -135,11 +133,6 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
this.messageService = messageService;
}
public void setProcesses(Processes processes)
{
this.processes = processes;
}
@Override
public CollectionWithPagingInfo<Task> getTasks(Parameters parameters)
{
@@ -634,7 +627,7 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
throw new InvalidArgumentException("Task id is required");
}
HistoricTaskInstance taskInstance = getValidHistoricTask(taskId, true);
HistoricTaskInstance taskInstance = getValidHistoricTask(taskId);
return new Task(taskInstance);
}
@@ -818,7 +811,7 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
public CollectionWithPagingInfo<FormModelElement> getTaskFormModel(String taskId, Paging paging)
{
// Check if task can be accessed by the current user
HistoricTaskInstance task = getValidHistoricTask(taskId, true);
HistoricTaskInstance task = getValidHistoricTask(taskId);
String formKey = task.getFormKey();
// Lookup type definition for the task
@@ -830,7 +823,7 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
public CollectionWithPagingInfo<TaskVariable> getTaskVariables(String taskId, Paging paging, VariableScope scope)
{
// Ensure the user is allowed to get variables for the task involved.
HistoricTaskInstance taskInstance = getValidHistoricTask(taskId, true);
HistoricTaskInstance taskInstance = getValidHistoricTask(taskId);
String formKey = taskInstance.getFormKey();
// Based on the scope, right variables are queried
@@ -878,13 +871,13 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
@Override
public TaskVariable updateTaskVariable(String taskId, TaskVariable taskVariable)
{
org.activiti.engine.task.Task taskInstance = getValidTask(taskId, true);
org.activiti.engine.task.Task taskInstance = getValidTask(taskId);
return updateVariableInTask(taskId, taskInstance, taskVariable);
}
public List<TaskVariable> updateTaskVariables(String taskId, List<TaskVariable> variables)
{
org.activiti.engine.task.Task taskInstance = getValidTask(taskId, true);
org.activiti.engine.task.Task taskInstance = getValidTask(taskId);
List<TaskVariable> updatedVariables = new ArrayList<TaskVariable>();
if (variables != null)
{
@@ -1003,7 +996,7 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
}
// Fetch task to check if user is authorized to perform the delete
getValidTask(taskId, true);
getValidTask(taskId);
// Check if variable is present on the scope
if (activitiProcessEngine.getTaskService().hasVariableLocal(taskId, variableName) == false)
@@ -1017,7 +1010,7 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
public CollectionWithPagingInfo<TaskCandidate> getTaskCandidates(String taskId, Paging paging)
{
// Fetch task to check if user is authorized to perform the delete
getValidTask(taskId, true);
getValidTask(taskId);
List<IdentityLink> links = activitiProcessEngine.getTaskService().getIdentityLinksForTask(taskId);
List<TaskCandidate> page = new ArrayList<TaskCandidate>();
@@ -1038,49 +1031,49 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
@Override
public Item createItem(String taskId, Item item)
{
org.activiti.engine.task.Task task = getValidTask(taskId, false);
org.activiti.engine.task.Task task = getValidTask(taskId);
if (task.getProcessInstanceId() == null)
{
throw new UnsupportedResourceOperationException("Task is not part of process, no items available.");
}
return processes.createItem(task.getProcessInstanceId(), item);
return createItemInProcess(item.getId(), task.getProcessInstanceId());
}
@Override
public void deleteItem(String taskId, String itemId)
{
org.activiti.engine.task.Task task = getValidTask(taskId, false);
if(task.getProcessInstanceId() == null)
{
throw new UnsupportedResourceOperationException("Task is not part of process, no items available.");
}
processes.deleteItem(task.getProcessInstanceId(), itemId);
}
@Override
public Item getItem(String taskId, String itemId)
{
HistoricTaskInstance task = getValidHistoricTask(taskId, true);
org.activiti.engine.task.Task task = getValidTask(taskId);
if (task.getProcessInstanceId() == null)
{
throw new UnsupportedResourceOperationException("Task is not part of process, no items available.");
}
return processes.getItem(task.getProcessInstanceId(), itemId);
deleteItemFromProcess(itemId, task.getProcessInstanceId());
}
@Override
public Item getItem(String taskId, String itemId)
{
HistoricTaskInstance task = getValidHistoricTask(taskId);
if (task.getProcessInstanceId() == null)
{
throw new UnsupportedResourceOperationException("Task is not part of process, no items available.");
}
return getItemFromProcess(itemId, task.getProcessInstanceId());
}
@Override
public CollectionWithPagingInfo<Item> getItems(String taskId, Paging paging)
{
HistoricTaskInstance task = getValidHistoricTask(taskId, true);
HistoricTaskInstance task = getValidHistoricTask(taskId);
if(task.getProcessInstanceId() == null)
if (task.getProcessInstanceId() == null)
{
throw new UnsupportedResourceOperationException("Task is not part of process, no items available.");
}
return processes.getItems(task.getProcessInstanceId(), paging);
return getItemsFromProcess(task.getProcessInstanceId(), paging);
}
protected String getFormResourceKey(final org.activiti.engine.task.Task task)
@@ -1175,7 +1168,7 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
* @throws EntityNotFoundException when the task was not found
* @throws PermissionDeniedException when the current logged in user isn't allowed to access task.
*/
protected HistoricTaskInstance getValidHistoricTask(String taskId, boolean validIfClaimable)
protected HistoricTaskInstance getValidHistoricTask(String taskId)
{
HistoricTaskInstanceQuery query = activitiProcessEngine.getHistoryService()
.createHistoricTaskInstanceQuery()
@@ -1214,19 +1207,15 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
else
{
boolean isTaskClaimable = false;
if (validIfClaimable)
if (taskInstance.getEndTime() == null)
{
if (taskInstance.getEndTime() == null)
{
// Task is not yet finished, so potentially claimable. If user is part of a "candidateGroup", the task is accessible to the
// user regardless of not being involved/owner/assignee
isTaskClaimable = activitiProcessEngine.getTaskService()
.createTaskQuery()
.taskCandidateGroupIn(new ArrayList<String>(authorityService.getAuthoritiesForUser(AuthenticationUtil.getRunAsUser())))
.taskId(taskId)
.count() == 1;
}
// Task is not yet finished, so potentially claimable. If user is part of a "candidateGroup", the task is accessible to the
// user regardless of not being involved/owner/assignee
isTaskClaimable = activitiProcessEngine.getTaskService()
.createTaskQuery()
.taskCandidateGroupIn(new ArrayList<String>(authorityService.getAuthoritiesForUser(AuthenticationUtil.getRunAsUser())))
.taskId(taskId)
.count() == 1;
}
if (isTaskClaimable == false)
@@ -1246,7 +1235,7 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
* @throws EntityNotFoundException when the task was not found
* @throws PermissionDeniedException when the current logged in user isn't allowed to access task.
*/
protected org.activiti.engine.task.Task getValidTask(String taskId, boolean validIfClaimable)
protected org.activiti.engine.task.Task getValidTask(String taskId)
{
if (taskId == null)
{
@@ -1289,18 +1278,13 @@ public class TasksImpl extends WorkflowRestImpl implements Tasks
}
else
{
boolean isTaskClaimable = false;
if (validIfClaimable)
{
// Task is not yet finished, so potentially claimable. If user is part of a "candidateGroup", the task is accessible to the
// user regardless of not being involved/owner/assignee
isTaskClaimable = activitiProcessEngine.getTaskService()
.createTaskQuery()
.taskCandidateGroupIn(new ArrayList<String>(authorityService.getAuthoritiesForUser(AuthenticationUtil.getRunAsUser())))
.taskId(taskId)
.count() == 1;
}
// Task is not yet finished, so potentially claimable. If user is part of a "candidateGroup", the task is accessible to the
// user regardless of not being involved/owner/assignee
boolean isTaskClaimable = activitiProcessEngine.getTaskService()
.createTaskQuery()
.taskCandidateGroupIn(new ArrayList<String>(authorityService.getAuthoritiesForUser(AuthenticationUtil.getRunAsUser())))
.taskId(taskId)
.count() == 1;
if (isTaskClaimable == false)
{

View File

@@ -1,5 +1,6 @@
package org.alfresco.rest.workflow.api.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@@ -11,6 +12,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
@@ -28,8 +30,10 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.workflow.WorkflowConstants;
import org.alfresco.repo.workflow.WorkflowModel;
import org.alfresco.repo.workflow.activiti.ActivitiConstants;
import org.alfresco.repo.workflow.activiti.ActivitiScriptNode;
import org.alfresco.rest.framework.core.exceptions.ApiException;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException;
@@ -37,6 +41,7 @@ import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.workflow.api.model.FormModelElement;
import org.alfresco.rest.workflow.api.model.Item;
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ClassDefinition;
import org.alfresco.service.cmr.dictionary.Constraint;
@@ -44,7 +49,11 @@ import org.alfresco.service.cmr.dictionary.ConstraintDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.namespace.NamespaceService;
@@ -60,10 +69,13 @@ import org.apache.commons.beanutils.ConvertUtils;
*/
public class WorkflowRestImpl
{
protected static final String BPM_PACKAGE = "bpm_package";
protected TenantService tenantService;
protected AuthorityService authorityService;
protected NamespaceService namespaceService;
protected DictionaryService dictionaryService;
protected NodeService nodeService;
protected ProcessEngine activitiProcessEngine;
protected boolean deployWorkflowsInTenant;
protected List<String> excludeModelTypes = new ArrayList<String>(Arrays.asList("bpm_priority", "bpm_description", "bpm_dueDate"));
@@ -96,6 +108,11 @@ public class WorkflowRestImpl
this.dictionaryService = dictionaryService;
}
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public void setActivitiProcessEngine(ProcessEngine activitiProcessEngine)
{
this.activitiProcessEngine = activitiProcessEngine;
@@ -123,6 +140,192 @@ public class WorkflowRestImpl
return nodeRef;
}
/**
* Get all items from the process package variable
*/
public CollectionWithPagingInfo<Item> getItemsFromProcess(String processId, Paging paging)
{
ActivitiScriptNode packageScriptNode = null;
try
{
HistoricVariableInstance variableInstance = activitiProcessEngine.getHistoryService()
.createHistoricVariableInstanceQuery()
.processInstanceId(processId)
.variableName(BPM_PACKAGE)
.singleResult();
if (variableInstance != null)
{
packageScriptNode = (ActivitiScriptNode) variableInstance.getValue();
}
else
{
throw new EntityNotFoundException(processId);
}
}
catch (ActivitiObjectNotFoundException e)
{
throw new EntityNotFoundException(processId);
}
List<Item> page = new ArrayList<Item>();
if (packageScriptNode != null)
{
List<ChildAssociationRef> documentList = nodeService.getChildAssocs(packageScriptNode.getNodeRef());
for (ChildAssociationRef childAssociationRef : documentList)
{
Item item = createItemForNodeRef(childAssociationRef.getChildRef());
page.add(item);
}
}
return CollectionWithPagingInfo.asPaged(paging, page, false, page.size());
}
/**
* Get an item from the process package variable
*/
public Item getItemFromProcess(String itemId, String processId)
{
NodeRef nodeRef = getNodeRef(itemId);
ActivitiScriptNode packageScriptNode = null;
try
{
HistoricVariableInstance variableInstance = activitiProcessEngine.getHistoryService()
.createHistoricVariableInstanceQuery()
.processInstanceId(processId)
.variableName(BPM_PACKAGE)
.singleResult();
if (variableInstance != null)
{
packageScriptNode = (ActivitiScriptNode) variableInstance.getValue();
}
else
{
throw new EntityNotFoundException(processId);
}
}
catch (ActivitiObjectNotFoundException e)
{
throw new EntityNotFoundException(processId);
}
Item item = null;
if (packageScriptNode != null)
{
List<ChildAssociationRef> documentList = nodeService.getChildAssocs(packageScriptNode.getNodeRef());
for (ChildAssociationRef childAssociationRef : documentList)
{
if (childAssociationRef.getChildRef().equals(nodeRef))
{
item = createItemForNodeRef(childAssociationRef.getChildRef());
break;
}
}
}
if (item == null) {
throw new EntityNotFoundException(itemId);
}
return item;
}
/**
* Create a new item in the process package variable
*/
public Item createItemInProcess(String itemId, String processId)
{
NodeRef nodeRef = getNodeRef(itemId);
ActivitiScriptNode packageScriptNode = null;
try
{
packageScriptNode = (ActivitiScriptNode) activitiProcessEngine.getRuntimeService().getVariable(processId, BPM_PACKAGE);
}
catch (ActivitiObjectNotFoundException e)
{
throw new EntityNotFoundException(processId);
}
if (packageScriptNode == null)
{
throw new InvalidArgumentException("process doesn't contain a workflow package variable");
}
// check if noderef exists
try
{
nodeService.getProperties(nodeRef);
}
catch (Exception e)
{
throw new EntityNotFoundException("item with id " + nodeRef.toString() + " not found");
}
try
{
QName workflowPackageItemId = QName.createQName("wpi", nodeRef.toString());
nodeService.addChild(packageScriptNode.getNodeRef(), nodeRef,
WorkflowModel.ASSOC_PACKAGE_CONTAINS, workflowPackageItemId);
}
catch (Exception e)
{
throw new ApiException("could not add item to process " + e.getMessage(), e);
}
Item responseItem = createItemForNodeRef(nodeRef);
return responseItem;
}
/**
* Delete an item from the process package variable
*/
public void deleteItemFromProcess(String itemId, String processId)
{
NodeRef nodeRef = getNodeRef(itemId);
ActivitiScriptNode packageScriptNode = null;
try
{
packageScriptNode = (ActivitiScriptNode) activitiProcessEngine.getRuntimeService().getVariable(processId, BPM_PACKAGE);
}
catch (ActivitiObjectNotFoundException e)
{
throw new EntityNotFoundException(processId);
}
if (packageScriptNode == null)
{
throw new InvalidArgumentException("process doesn't contain a workflow package variable");
}
boolean itemIdFoundInPackage = false;
List<ChildAssociationRef> documentList = nodeService.getChildAssocs(packageScriptNode.getNodeRef());
for (ChildAssociationRef childAssociationRef : documentList)
{
if (childAssociationRef.getChildRef().equals(nodeRef))
{
itemIdFoundInPackage = true;
break;
}
}
if (itemIdFoundInPackage == false)
{
throw new EntityNotFoundException("Item " + itemId + " not found in the process package variable");
}
try
{
nodeService.removeChild(packageScriptNode.getNodeRef(), nodeRef);
}
catch (InvalidNodeRefException e)
{
throw new EntityNotFoundException("Item " + itemId + " not found");
}
}
/**
* Get the process definition from the cache if available
*
@@ -342,4 +545,33 @@ public class WorkflowRestImpl
return variableInstances;
}
protected Item createItemForNodeRef(NodeRef nodeRef) {
Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
Item item = new Item();
String name = (String) properties.get(ContentModel.PROP_NAME);
String title = (String) properties.get(ContentModel.PROP_TITLE);
String description = (String) properties.get(ContentModel.PROP_DESCRIPTION);
Date createdAt = (Date) properties.get(ContentModel.PROP_CREATED);
String createdBy = (String) properties.get(ContentModel.PROP_CREATOR);
Date modifiedAt = (Date) properties.get(ContentModel.PROP_MODIFIED);
String modifiedBy = (String) properties.get(ContentModel.PROP_MODIFIER);
ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
item.setId(nodeRef.getId());
item.setName(name);
item.setTitle(title);
item.setDescription(description);
item.setCreatedAt(createdAt);
item.setCreatedBy(createdBy);
item.setModifiedAt(modifiedAt);
item.setModifiedBy(modifiedBy);
if (contentData != null)
{
item.setMimeType(contentData.getMimetype());
item.setSize(contentData.getSize());
}
return item;
}
}