use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project records-management by Alfresco.
the class RequestInfoUtils method getInitiator.
/**
* Helper method to extract the initiator from the task
*
* @param delegateTask The delegate task
* @return Returns the initiator of the workflow. First it will be checked if
* a rule creator exists, which means the the workflow was started via rule.
* In this case the creator of the rule will receive the review task.
* If a rule creator cannot be found the code will try to find the initiator
* of the workflow. If also this is not the case the admin user will be returned.
*/
public static String getInitiator(DelegateTask delegateTask) {
ParameterCheck.mandatory("delegateTask", delegateTask);
String userName = null;
String ruleCreator = (String) delegateTask.getVariable("rmwf_ruleCreator");
if (StringUtils.isBlank(ruleCreator)) {
ActivitiScriptNode initiator = (ActivitiScriptNode) delegateTask.getVariable("initiator");
if (initiator.exists()) {
userName = (String) initiator.getProperties().get(ContentModel.PROP_USERNAME.toString());
} else {
userName = AuthenticationUtil.getAdminUserName();
}
} else {
userName = ruleCreator;
}
return userName;
}
use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project alfresco-remote-api by Alfresco.
the class ProcessesImpl method updateVariableInProcess.
protected Variable updateVariableInProcess(String processId, String processDefinitionId, Variable variable) {
if (variable.getName() == null) {
throw new InvalidArgumentException("Variable name is required.");
}
// Get start-task definition for explicit typing of variables submitted at the start
String formKey = null;
StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(processDefinitionId);
if (startFormData != null) {
formKey = startFormData.getFormKey();
}
DataTypeDefinition dataTypeDefinition = null;
TypeDefinition startTaskTypeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(formKey, true);
TypeDefinitionContext context = new TypeDefinitionContext(startTaskTypeDefinition, getQNameConverter());
if (context.getPropertyDefinition(variable.getName()) != null) {
dataTypeDefinition = context.getPropertyDefinition(variable.getName()).getDataType();
if (variable.getType() != null && dataTypeDefinition.getName().toPrefixString(namespaceService).equals(variable.getType()) == false) {
throw new InvalidArgumentException("type of variable " + variable.getName() + " should be " + dataTypeDefinition.getName().toPrefixString(namespaceService));
}
} else if (context.getAssociationDefinition(variable.getName()) != null) {
dataTypeDefinition = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
}
if (dataTypeDefinition == null && variable.getType() != null) {
try {
QName dataType = QName.createQName(variable.getType(), namespaceService);
dataTypeDefinition = dictionaryService.getDataType(dataType);
} catch (InvalidQNameException iqne) {
throw new InvalidArgumentException("Unsupported type of variable: '" + variable.getType() + "'.");
}
} else if (dataTypeDefinition == null) {
// Fallback to raw value when no type has been passed and not present in model
dataTypeDefinition = dictionaryService.getDataType(restVariableHelper.extractTypeFromValue(variable.getValue()));
}
if (dataTypeDefinition == null) {
throw new InvalidArgumentException("Unsupported type of variable: '" + variable.getType() + "'.");
}
Object actualValue = null;
if ("java.util.Date".equalsIgnoreCase(dataTypeDefinition.getJavaClassName())) {
// fix for different ISO 8601 Date format classes in Alfresco (org.alfresco.util and Spring Surf)
actualValue = ISO8601DateFormat.parse((String) variable.getValue());
} else if (variable.getName().equals(WorkflowConstants.PROP_INITIATOR)) {
// update the initiator if exists
NodeRef initiator = getNodeRef((String) variable.getValue());
if (nodeService.exists(initiator)) {
actualValue = getNodeConverter().convertNode(initiator);
// Also update the initiator home reference, if one exists
NodeRef initiatorHome = (NodeRef) nodeService.getProperty(initiator, ContentModel.PROP_HOMEFOLDER);
if (initiatorHome != null) {
Variable initiatorHomeVar = new Variable();
initiatorHomeVar.setName(WorkflowConstants.PROP_INITIATOR_HOME);
initiatorHomeVar.setValue(initiatorHome);
updateVariableInProcess(processId, processDefinitionId, initiatorHomeVar);
}
} else {
throw new InvalidArgumentException("Variable value should be a valid person NodeRef.");
}
} else {
if (context.getAssociationDefinition(variable.getName()) != null) {
actualValue = convertAssociationDefinitionValue(context.getAssociationDefinition(variable.getName()), variable.getName(), variable.getValue());
} else {
actualValue = DefaultTypeConverter.INSTANCE.convert(dataTypeDefinition, variable.getValue());
}
}
activitiProcessEngine.getRuntimeService().setVariable(processId, variable.getName(), actualValue);
// Variable value needs to be of type NodeRef
if (actualValue instanceof ActivitiScriptNode) {
variable.setValue(((ActivitiScriptNode) actualValue).getNodeRef());
} else {
variable.setValue(actualValue);
}
// Set actual used type before returning
variable.setType(dataTypeDefinition.getName().toPrefixString(namespaceService));
return variable;
}
use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project alfresco-remote-api by Alfresco.
the class WorkflowRestImpl method deleteItemFromProcess.
/**
* 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);
activitiWorkflowEngine.dispatchPackageUpdatedEvent(packageScriptNode, null, null, processId, null);
} catch (InvalidNodeRefException e) {
throw new EntityNotFoundException("Item " + itemId + " not found");
}
}
use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project alfresco-remote-api by Alfresco.
the class WorkflowRestImpl method createItemInProcess.
/**
* 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);
activitiWorkflowEngine.dispatchPackageUpdatedEvent(packageScriptNode, null, null, processId, null);
return responseItem;
}
use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testAssociationDefinitionExtraction.
/*
* Test association definition extraction from the dictionary as per MNT-11472.
*
* We are going to test association definition extraction through dictionary, when one is not retrieved in context.
* Context doesn't contains all type definitions, because it requires entire history extraction of a process that causes performance implications.
* Type definition extraction from the dictionary is quite fast and it doesn't affect performance.
*
*/
@Test
@SuppressWarnings("unchecked")
public void testAssociationDefinitionExtraction() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
// The "wbpm:delegatee" custom association is defined in bpmDelegateeModel.xml. This association has "cm:person" target type.
final String delegatee = "wbpm_delegatee";
final String user = requestContext.getRunAsUser();
final String networkId = requestContext.getNetworkId();
// Get person
final ActivitiScriptNode person = TenantUtil.runAsUserTenant(new TenantRunAsWork<ActivitiScriptNode>() {
@Override
public ActivitiScriptNode doWork() throws Exception {
return getPersonNodeRef(user);
}
}, user, networkId);
// Start custom process instance
ProcessInstance processInstance = TenantUtil.runAsUserTenant(new TenantRunAsWork<ProcessInstance>() {
@Override
public ProcessInstance doWork() throws Exception {
deployProcessDefinition("workflow/testCustomDelegatee.bpmn20.xml");
String processDefinitionKey = "@" + networkId + "@myProcess";
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("bpm_assignee", person);
variables.put("wf_notifyMe", Boolean.FALSE);
variables.put(WorkflowConstants.PROP_INITIATOR, person);
return activitiProcessEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, "myProcessKey", variables);
}
}, user, networkId);
// Get task
Task activeTask = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(activeTask);
try {
TasksClient tasksClient = publicApiClient.tasksClient();
// Define the "wbpm_delegatee" variable for the taskDefine delegatee user name. POST request will be executed.
JSONArray variablesArray = new JSONArray();
JSONObject variableBody = new JSONObject();
variableBody.put("name", delegatee);
variableBody.put("value", user);
variableBody.put("scope", "local");
variablesArray.add(variableBody);
try {
// Set variable for the task.
JSONObject response = tasksClient.createTaskVariables(activeTask.getId(), variablesArray);
assertNotNull(response);
JSONObject variable = (JSONObject) response.get("entry");
assertEquals(delegatee, variable.get("name"));
// Check that d:noderef type was returned with appropriate nodeRef Id value.
assertEquals("d:noderef", variable.get("type"));
assertEquals(person.getNodeRef().getId(), variable.get("value"));
// Get process variables. GET request will be executed.
response = publicApiClient.processesClient().getProcessvariables(processInstance.getId());
assertNotNull(response);
assertTrue(delegatee + " variable was not set", response.toJSONString().contains(delegatee));
JSONArray entries = (JSONArray) response.get("entries");
// Find "wbpm_delegatee" variable.
for (Object entry : entries) {
variable = (JSONObject) ((JSONObject) entry).get("entry");
if (variable.get("name").equals(delegatee)) {
// Check that type corresponds to the target class.
// It means that assoc type def was retrieved successfully from the dictionary.
assertEquals("cm:person", variable.get("type"));
// Value should be an actual user name.
assertEquals(user, variable.get("value"));
}
}
} catch (PublicApiException ex) {
fail("Unexpected result " + ex);
}
} finally {
cleanupProcessInstance(processInstance);
}
}
Aggregations