Search in sources :

Example 1 with ActivitiScriptNode

use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project alfresco-remote-api by Alfresco.

the class TaskWorkflowApiTest method testGetTaskVariablesRawVariableTypes.

@Test
public void testGetTaskVariablesRawVariableTypes() throws Exception {
    RequestContext requestContext = initApiClientWithTestUser();
    ProcessInstance processInstance = startAdhocProcess(requestContext.getRunAsUser(), requestContext.getNetworkId(), null);
    try {
        Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        assertNotNull(task);
        Calendar dateCal = Calendar.getInstance();
        // Set all supported variables on the task to check the resulting types
        Map<String, Object> variablesToSet = new HashMap<String, Object>();
        variablesToSet.put("testVarString", "string");
        variablesToSet.put("testVarInteger", 1234);
        variablesToSet.put("testVarLong", 123456789L);
        variablesToSet.put("testVarDouble", 1234.5678D);
        variablesToSet.put("testVarFloat", 1234.0F);
        variablesToSet.put("testVarBoolean", Boolean.TRUE);
        variablesToSet.put("testVarDate", dateCal.getTime());
        variablesToSet.put("testVarQName", ContentModel.TYPE_AUTHORITY);
        variablesToSet.put("testVarNodeRef", new ActivitiScriptNode(new NodeRef("workspace:///testNode"), serviceRegistry));
        variablesToSet.put("testVarRawNodeRef", new NodeRef("workspace:///testNode"));
        activitiProcessEngine.getTaskService().setVariablesLocal(task.getId(), variablesToSet);
        Map<String, Object> actualLocalVariables = activitiProcessEngine.getTaskService().getVariablesLocal(task.getId());
        TasksClient tasksClient = publicApiClient.tasksClient();
        // Get all local variables
        JSONObject variables = tasksClient.findTaskVariables(task.getId(), Collections.singletonMap("where", "(scope = local)"));
        assertNotNull(variables);
        JSONObject list = (JSONObject) variables.get("list");
        assertNotNull(list);
        JSONArray entries = (JSONArray) list.get("entries");
        assertNotNull(entries);
        // Check pagination object for size
        JSONObject pagination = (JSONObject) list.get("pagination");
        assertNotNull(pagination);
        assertEquals(actualLocalVariables.size(), ((Long) pagination.get("count")).intValue());
        assertEquals(actualLocalVariables.size(), ((Long) pagination.get("totalItems")).intValue());
        assertEquals(0L, pagination.get("skipCount"));
        assertFalse((Boolean) pagination.get("hasMoreItems"));
        assertEquals(actualLocalVariables.size(), entries.size());
        // Add JSON entries to map for easy access when asserting values
        Map<String, JSONObject> entriesByName = new HashMap<String, JSONObject>();
        for (int i = 0; i < entries.size(); i++) {
            JSONObject var = (JSONObject) entries.get(i);
            entriesByName.put((String) ((JSONObject) var.get("entry")).get("name"), (JSONObject) var.get("entry"));
        }
        // Check all values and types
        JSONObject var = entriesByName.get("testVarString");
        assertNotNull(var);
        assertEquals("d:text", var.get("type"));
        assertEquals("string", var.get("value"));
        var = entriesByName.get("testVarInteger");
        assertNotNull(var);
        assertEquals("d:int", var.get("type"));
        assertEquals(1234L, var.get("value"));
        var = entriesByName.get("testVarLong");
        assertNotNull(var);
        assertEquals("d:long", var.get("type"));
        assertEquals(123456789L, var.get("value"));
        var = entriesByName.get("testVarDouble");
        assertNotNull(var);
        assertEquals("d:double", var.get("type"));
        assertEquals(1234.5678D, var.get("value"));
        var = entriesByName.get("testVarFloat");
        assertNotNull(var);
        assertEquals("d:float", var.get("type"));
        assertEquals(1234.0D, var.get("value"));
        var = entriesByName.get("testVarBoolean");
        assertNotNull(var);
        assertEquals("d:boolean", var.get("type"));
        assertEquals(Boolean.TRUE, var.get("value"));
        var = entriesByName.get("testVarDate");
        assertNotNull(var);
        assertEquals("d:datetime", var.get("type"));
        assertEquals(dateCal.getTime(), parseDate(var, "value"));
        var = entriesByName.get("testVarQName");
        assertNotNull(var);
        assertEquals("d:qname", var.get("type"));
        assertEquals("cm:authority", var.get("value"));
        var = entriesByName.get("testVarRawNodeRef");
        assertNotNull(var);
        assertEquals("d:noderef", var.get("type"));
        assertEquals("workspace:///testNode", var.get("value"));
        var = entriesByName.get("testVarNodeRef");
        assertNotNull(var);
        assertEquals("d:noderef", var.get("type"));
        assertEquals("workspace:///testNode", var.get("value"));
    } finally {
        cleanupProcessInstance(processInstance);
    }
}
Also used : Task(org.activiti.engine.task.Task) HashMap(java.util.HashMap) Calendar(java.util.Calendar) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) JSONArray(org.json.simple.JSONArray) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) JSONObject(org.json.simple.JSONObject) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) JSONObject(org.json.simple.JSONObject) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 2 with ActivitiScriptNode

use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project alfresco-remote-api by Alfresco.

the class WorkflowRestImpl method getItemFromProcess.

/**
 * 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;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Item(org.alfresco.rest.workflow.api.model.Item) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 3 with ActivitiScriptNode

use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project alfresco-remote-api by Alfresco.

the class WorkflowRestImpl method getItemsFromProcess.

/**
 * 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());
}
Also used : Item(org.alfresco.rest.workflow.api.model.Item) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) ArrayList(java.util.ArrayList) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 4 with ActivitiScriptNode

use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project alfresco-remote-api by Alfresco.

the class WorkflowRestImpl method validateIfUserAllowedToWorkWithProcess.

/**
 * Validates if the logged in user is allowed to get information about a specific process instance.
 * If the user is not allowed an exception is thrown.
 *
 * @param processId identifier of the process instance
 */
protected List<HistoricVariableInstance> validateIfUserAllowedToWorkWithProcess(String processId) {
    List<HistoricVariableInstance> variableInstances = activitiProcessEngine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(processId).list();
    Map<String, Object> variableMap = new HashMap<String, Object>();
    if (variableInstances != null && variableInstances.size() > 0) {
        for (HistoricVariableInstance variableInstance : variableInstances) {
            variableMap.put(variableInstance.getVariableName(), variableInstance.getValue());
        }
    } else {
        throw new EntityNotFoundException(processId);
    }
    if (tenantService.isEnabled()) {
        String tenantDomain = (String) variableMap.get(ActivitiConstants.VAR_TENANT_DOMAIN);
        if (TenantUtil.getCurrentDomain().equals(tenantDomain) == false) {
            throw new PermissionDeniedException("Process is running in another tenant");
        }
    }
    // MNT-17918 - required for initiator variable already updated as NodeRef type
    Object initiator = variableMap.get(WorkflowConstants.PROP_INITIATOR);
    String nodeId = ((initiator instanceof ActivitiScriptNode) ? ((ActivitiScriptNode) initiator).getNodeRef().getId() : ((NodeRef) initiator).getId());
    if (initiator != null && AuthenticationUtil.getRunAsUser().equals(nodeId)) {
        // user is allowed
        return variableInstances;
    }
    String username = AuthenticationUtil.getRunAsUser();
    if (authorityService.isAdminAuthority(username)) {
        // Admin is allowed to read all processes in the current tenant
        return variableInstances;
    } else {
        // MNT-12382 check for membership in the assigned group
        ActivitiScriptNode group = (ActivitiScriptNode) variableMap.get("bpm_groupAssignee");
        if (group != null) {
            // check that the process is unclaimed
            Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processId).singleResult();
            if ((task != null) && (task.getAssignee() == null) && isUserInGroup(username, group.getNodeRef())) {
                return variableInstances;
            }
        }
        // If non-admin user, involvement in the task is required (either owner, assignee or externally involved).
        HistoricTaskInstanceQuery query = activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery().processInstanceId(processId).taskInvolvedUser(AuthenticationUtil.getRunAsUser());
        List<HistoricTaskInstance> taskList = query.list();
        if (org.apache.commons.collections.CollectionUtils.isEmpty(taskList)) {
            throw new PermissionDeniedException("user is not allowed to access information about process " + processId);
        }
    }
    return variableInstances;
}
Also used : Task(org.activiti.engine.task.Task) HistoricTaskInstance(org.activiti.engine.history.HistoricTaskInstance) HashMap(java.util.HashMap) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) HistoricTaskInstanceQuery(org.activiti.engine.history.HistoricTaskInstanceQuery) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)

Example 5 with ActivitiScriptNode

use of org.alfresco.repo.workflow.activiti.ActivitiScriptNode in project records-management by Alfresco.

the class RequestInfoUtils method getRecordName.

/**
 * Helper method to extract the record name from the task
 *
 * @param delegateTask  The delegate task
 * @return Returns the name of the record or an empty string if the record name could not be found
 *               (may be because the record has been deleted in the mean time)
 */
public static String getRecordName(DelegateTask delegateTask) {
    ParameterCheck.mandatory("delegateTask", delegateTask);
    String recordName = StringUtils.EMPTY;
    NodeService nodeService = getServiceRegistry().getNodeService();
    ActivitiScriptNode scriptNode = (ActivitiScriptNode) delegateTask.getVariable("bpm_package");
    List<ChildAssociationRef> childAssocs = nodeService.getChildAssocs(scriptNode.getNodeRef());
    if (childAssocs.size() > 0) {
        NodeRef docRef = childAssocs.get(0).getChildRef();
        recordName = (String) nodeService.getProperty(docRef, ContentModel.PROP_NAME);
    }
    return recordName;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) NodeService(org.alfresco.service.cmr.repository.NodeService) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Aggregations

ActivitiScriptNode (org.alfresco.repo.workflow.activiti.ActivitiScriptNode)12 NodeRef (org.alfresco.service.cmr.repository.NodeRef)8 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)5 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)5 HashMap (java.util.HashMap)4 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)4 Task (org.activiti.engine.task.Task)4 ArrayList (java.util.ArrayList)3 HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)3 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)3 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)3 Item (org.alfresco.rest.workflow.api.model.Item)3 JSONObject (org.json.simple.JSONObject)3 Test (org.junit.Test)3 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)2 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)2 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)2 TasksClient (org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient)2 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)2 QName (org.alfresco.service.namespace.QName)2