Search in sources :

Example 26 with WorkflowTask

use of org.alfresco.service.cmr.workflow.WorkflowTask in project alfresco-remote-api by Alfresco.

the class TaskInstancePut method buildModel.

@Override
protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    // getting task id from request parameters
    String taskId = params.get("task_instance_id");
    JSONObject json = null;
    try {
        WorkflowTask workflowTask = workflowService.getTaskById(taskId);
        String currentUser = authenticationService.getCurrentUserName();
        // read request json
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // update task properties
        workflowTask = workflowService.updateTask(taskId, parseTaskProperties(json, workflowTask), null, null);
        // task was not found -> return 404
        if (workflowTask == null) {
            throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Failed to find workflow task with id: " + taskId);
        }
        // build the model for ftl
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("workflowTask", modelBuilder.buildDetailed(workflowTask));
        return model;
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from request.", iox);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from request.", je);
    } catch (AccessDeniedException ade) {
        throw new WebScriptException(HttpServletResponse.SC_UNAUTHORIZED, "Failed to update workflow task with id: " + taskId, ade);
    } catch (WorkflowException we) {
        throw new WebScriptException(HttpServletResponse.SC_UNAUTHORIZED, "Failed to update workflow task with id: " + taskId, we);
    }
}
Also used : AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) HashMap(java.util.HashMap) WorkflowException(org.alfresco.service.cmr.workflow.WorkflowException) JSONException(org.json.JSONException) WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject)

Example 27 with WorkflowTask

use of org.alfresco.service.cmr.workflow.WorkflowTask in project alfresco-remote-api by Alfresco.

the class WorkflowInstanceDelete method canUserEndWorkflow.

/**
 * Determines if the current user can cancel or delete the
 * workflow instance with the given id. Throws a WebscriptException
 * with status-code 404 if workflow-instance to delete wasn't found.
 *
 * @param instanceId The id of the workflow instance to check
 * @return true if the user can end the workflow, false otherwise
 */
private boolean canUserEndWorkflow(String instanceId) {
    boolean canEnd = false;
    // get the initiator
    WorkflowInstance wi = workflowService.getWorkflowById(instanceId);
    if (wi == null) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "The workflow instance to delete/cancel with id " + instanceId + " doesn't exist: ");
    }
    String currentUserName = authenticationService.getCurrentUserName();
    // ALF-17405: Admin can always delete/cancel workflows, regardless of the initiator
    if (authorityService.isAdminAuthority(currentUserName)) {
        canEnd = true;
    } else {
        String ownerName = null;
        // Determine if the current user is the initiator of the workflow.
        // Get the username of the initiator.
        NodeRef initiator = wi.getInitiator();
        if (initiator != null && nodeService.exists(initiator)) {
            ownerName = (String) nodeService.getProperty(initiator, ContentModel.PROP_USERNAME);
        } else {
            /*
                 * Fix for MNT-14411 : Re-created user can't cancel created task.
                 * If owner value can't be found on workflow properties
                 * because initiator nodeRef no longer exists get owner from
                 * initiatorhome nodeRef owner property.
                 */
            WorkflowTask startTask = workflowService.getStartTask(wi.getId());
            Map<QName, Serializable> props = startTask.getProperties();
            ownerName = (String) props.get(ContentModel.PROP_OWNER);
            if (ownerName == null) {
                NodeRef initiatorHomeNodeRef = (NodeRef) props.get(QName.createQName("", WorkflowConstants.PROP_INITIATOR_HOME));
                if (initiatorHomeNodeRef != null) {
                    ownerName = (String) nodeService.getProperty(initiatorHomeNodeRef, ContentModel.PROP_OWNER);
                }
            }
        }
        // if the current user started the workflow allow the cancel action
        if (currentUserName.equals(ownerName)) {
            canEnd = true;
        }
    }
    return canEnd;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) QName(org.alfresco.service.namespace.QName) WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance)

Example 28 with WorkflowTask

use of org.alfresco.service.cmr.workflow.WorkflowTask in project alfresco-remote-api by Alfresco.

the class InviteByTicket method getInvitationStatus.

private String getInvitationStatus(NominatedInvitation invitation) {
    String invitee = invitation.getInviteeUserName();
    String site = invitation.getResourceName();
    // check is invitee is site member
    boolean isUserMember = serviceRegistry.getSiteService().isMember(site, invitee);
    WorkflowTaskQuery query = new WorkflowTaskQuery();
    query.setTaskName(WorkflowModelNominatedInvitation.WF_TASK_ACTIVIT_INVITE_PENDING);
    query.setProcessId(invitation.getInviteId());
    // query current workflow's task activitiInvitePendingTask
    List<WorkflowTask> pendingInvitationTasks = serviceRegistry.getWorkflowService().queryTasks(query, false);
    // if it's here - pending invitation
    if (!pendingInvitationTasks.isEmpty()) {
        return InviteInfo.INVITATION_STATUS_PENDING;
    } else if (isUserMember) {
        return InviteInfo.INVITATION_STATUS_ACCEPTED;
    } else {
        return InviteInfo.INVITATION_STATUS_REJECTED;
    }
}
Also used : WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) WorkflowTaskQuery(org.alfresco.service.cmr.workflow.WorkflowTaskQuery)

Example 29 with WorkflowTask

use of org.alfresco.service.cmr.workflow.WorkflowTask in project alfresco-remote-api by Alfresco.

the class AbstractWorkflowRestApiTest method testWorkflowInstanceDeleteAsAdministrator.

public void testWorkflowInstanceDeleteAsAdministrator() throws Exception {
    // Start workflow as USER1
    personManager.setUser(USER1);
    WorkflowDefinition adhocDef = workflowService.getDefinitionByName(getAdhocWorkflowDefinitionName());
    Map<QName, Serializable> params = new HashMap<QName, Serializable>();
    params.put(WorkflowModel.ASSOC_ASSIGNEE, personManager.get(USER2));
    Date dueDate = new Date();
    params.put(WorkflowModel.PROP_DUE_DATE, dueDate);
    params.put(WorkflowModel.PROP_PRIORITY, 1);
    params.put(WorkflowModel.ASSOC_PACKAGE, packageRef);
    params.put(WorkflowModel.PROP_CONTEXT, packageRef);
    WorkflowPath adhocPath = workflowService.startWorkflow(adhocDef.getId(), params);
    WorkflowTask startTask = workflowService.getTasksForWorkflowPath(adhocPath.getId()).get(0);
    startTask = workflowService.endTask(startTask.getId(), null);
    WorkflowInstance adhocInstance = startTask.getPath().getInstance();
    // Run next request as admin
    String admin = authenticationComponent.getDefaultAdministratorUserNames().iterator().next();
    AuthenticationUtil.setFullyAuthenticatedUser(admin);
    sendRequest(new DeleteRequest(URL_WORKFLOW_INSTANCES + "/" + adhocInstance.getId()), Status.STATUS_OK);
    WorkflowInstance instance = workflowService.getWorkflowById(adhocInstance.getId());
    if (instance != null) {
        assertFalse("The deleted workflow is still active!", instance.isActive());
    }
    List<WorkflowInstance> instances = workflowService.getActiveWorkflows(adhocInstance.getDefinition().getId());
    for (WorkflowInstance activeInstance : instances) {
        assertFalse(adhocInstance.getId().equals(activeInstance.getId()));
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) WorkflowDefinition(org.alfresco.service.cmr.workflow.WorkflowDefinition) WorkflowPath(org.alfresco.service.cmr.workflow.WorkflowPath) WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance) DeleteRequest(org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest) Date(java.util.Date)

Example 30 with WorkflowTask

use of org.alfresco.service.cmr.workflow.WorkflowTask in project alfresco-remote-api by Alfresco.

the class AbstractWorkflowRestApiTest method testWorkflowPermissions.

public void testWorkflowPermissions() throws Exception {
    // Start workflow as USER1 and assign task to USER1.
    personManager.setUser(USER1);
    WorkflowDefinition adhocDef = workflowService.getDefinitionByName(getAdhocWorkflowDefinitionName());
    Map<QName, Serializable> params = new HashMap<QName, Serializable>();
    params.put(WorkflowModel.ASSOC_ASSIGNEE, personManager.get(USER1));
    Calendar dueDateCal = Calendar.getInstance();
    Date dueDate = dueDateCal.getTime();
    params.put(WorkflowModel.PROP_DUE_DATE, dueDate);
    params.put(WorkflowModel.PROP_PRIORITY, 1);
    params.put(WorkflowModel.ASSOC_PACKAGE, packageRef);
    WorkflowPath adhocPath = workflowService.startWorkflow(adhocDef.getId(), params);
    String workflowId = adhocPath.getInstance().getId();
    workflows.add(workflowId);
    WorkflowTask startTask = workflowService.getStartTask(workflowId);
    workflowService.endTask(startTask.getId(), null);
    // Check tasks of USER1 from behalf of USER2
    personManager.setUser(USER2);
    Response response = sendRequest(new GetRequest(MessageFormat.format(URL_USER_TASKS, USER1)), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    String jsonStr = response.getContentAsString();
    JSONObject json = new JSONObject(jsonStr);
    JSONArray results = json.getJSONArray("data");
    assertNotNull(results);
    assertTrue("User2 should not see any tasks if he is not initiator or assignee", results.length() == 0);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) Calendar(java.util.Calendar) JSONArray(org.json.JSONArray) WorkflowDefinition(org.alfresco.service.cmr.workflow.WorkflowDefinition) WorkflowPath(org.alfresco.service.cmr.workflow.WorkflowPath) WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) Date(java.util.Date) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)

Aggregations

WorkflowTask (org.alfresco.service.cmr.workflow.WorkflowTask)37 Serializable (java.io.Serializable)22 HashMap (java.util.HashMap)21 QName (org.alfresco.service.namespace.QName)20 Date (java.util.Date)15 WorkflowPath (org.alfresco.service.cmr.workflow.WorkflowPath)15 WorkflowDefinition (org.alfresco.service.cmr.workflow.WorkflowDefinition)14 JSONObject (org.json.JSONObject)11 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)11 FacesContext (javax.faces.context.FacesContext)10 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)10 WorkflowInstance (org.alfresco.service.cmr.workflow.WorkflowInstance)8 UserTransaction (javax.transaction.UserTransaction)7 JSONArray (org.json.JSONArray)7 Calendar (java.util.Calendar)6 NodeRef (org.alfresco.service.cmr.repository.NodeRef)6 WorkflowTaskQuery (org.alfresco.service.cmr.workflow.WorkflowTaskQuery)6 Node (org.alfresco.web.bean.repository.Node)6 User (org.alfresco.web.bean.repository.User)6 TransientNode (org.alfresco.web.bean.repository.TransientNode)5