Search in sources :

Example 11 with WorkflowInstance

use of org.alfresco.service.cmr.workflow.WorkflowInstance in project acs-community-packaging by Alfresco.

the class UIWorkflowSummary method encodeBegin.

@Override
@SuppressWarnings("unchecked")
public void encodeBegin(FacesContext context) throws IOException {
    if (!isRendered())
        return;
    WorkflowInstance wi = getValue();
    if (wi != null) {
        ResponseWriter out = context.getResponseWriter();
        ResourceBundle bundle = Application.getBundle(context);
        // output surrounding table and style if necessary
        out.write("<table");
        if (this.getAttributes().get("style") != null) {
            out.write(" style=\"");
            out.write((String) this.getAttributes().get("style"));
            out.write("\"");
        }
        if (this.getAttributes().get("styleClass") != null) {
            out.write(" class=\"");
            out.write((String) this.getAttributes().get("styleClass"));
            out.write("\"");
        }
        out.write(">");
        // output the title and description
        out.write("<tr><td>");
        out.write(bundle.getString("title"));
        out.write(":</td><td>");
        out.write(wi.definition.title);
        if (wi.definition.description != null && wi.definition.description.length() > 0) {
            out.write("&nbsp;(");
            out.write(Utils.encode(wi.definition.description));
            out.write(")");
        }
        out.write("</td></tr><tr><td>");
        out.write(bundle.getString("initiated_by"));
        out.write(":</td><td>");
        NodeService nodeService = getNodeService(context);
        if (wi.initiator != null) {
            if (nodeService.exists(wi.initiator)) {
                out.write(Utils.encode(User.getFullName(Repository.getServiceRegistry(context).getNodeService(), wi.initiator)));
            } else {
                out.write("&lt;");
                out.write(bundle.getString("unknown"));
                out.write("&gt;");
            }
        }
        out.write("</td></tr><tr><td>");
        out.write(bundle.getString("started_on"));
        out.write(":</td><td>");
        if (wi.startDate != null) {
            out.write(Utils.getDateFormat(context).format(wi.startDate));
        }
        out.write("</td></tr><tr><td>");
        out.write(bundle.getString("completed_on"));
        out.write(":</td><td>");
        if (wi.endDate != null) {
            out.write(Utils.getDateFormat(context).format(wi.endDate));
        } else {
            out.write("&lt;");
            out.write(bundle.getString("in_progress"));
            out.write("&gt;");
        }
        out.write("</td></tr></table>");
    }
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) NodeService(org.alfresco.service.cmr.repository.NodeService) ResourceBundle(java.util.ResourceBundle) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance)

Example 12 with WorkflowInstance

use of org.alfresco.service.cmr.workflow.WorkflowInstance 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 13 with WorkflowInstance

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

the class WorkflowInstanceDiagramGet method execute.

@Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    // getting workflow instance id from request parameters
    String workflowInstanceId = params.get("workflow_instance_id");
    WorkflowInstance workflowInstance = workflowService.getWorkflowById(workflowInstanceId);
    // workflow instance was not found -> return 404
    if (workflowInstance == null) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find workflow instance with id: " + workflowInstanceId);
    }
    // check whether there is a diagram available
    if (!workflowService.hasWorkflowImage(workflowInstanceId)) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find diagram for workflow instance with id: " + workflowInstanceId);
    }
    // copy image data into temporary file
    File file = TempFileProvider.createTempFile("workflow-diagram-", ".png");
    InputStream imageData = workflowService.getWorkflowImage(workflowInstanceId);
    OutputStream os = new FileOutputStream(file);
    FileCopyUtils.copy(imageData, os);
    // stream temporary file back to client
    streamContent(req, res, file);
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance) File(java.io.File)

Example 14 with WorkflowInstance

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

the class WorkflowInstancesForNodeGet method buildModel.

@Override
protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    // get nodeRef from request
    NodeRef nodeRef = new NodeRef(params.get(PARAM_STORE_TYPE), params.get(PARAM_STORE_ID), params.get(PARAM_NODE_ID));
    // list all active workflows for nodeRef
    List<WorkflowInstance> workflows = workflowService.getWorkflowsForContent(nodeRef, true);
    List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(workflows.size());
    for (WorkflowInstance workflow : workflows) {
        results.add(modelBuilder.buildSimple(workflow));
    }
    Map<String, Object> model = new HashMap<String, Object>();
    // build the model for ftl
    model.put("workflowInstances", results);
    return model;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with WorkflowInstance

use of org.alfresco.service.cmr.workflow.WorkflowInstance 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)

Aggregations

WorkflowInstance (org.alfresco.service.cmr.workflow.WorkflowInstance)18 HashMap (java.util.HashMap)11 Date (java.util.Date)10 WorkflowDefinition (org.alfresco.service.cmr.workflow.WorkflowDefinition)8 WorkflowTask (org.alfresco.service.cmr.workflow.WorkflowTask)8 QName (org.alfresco.service.namespace.QName)8 Serializable (java.io.Serializable)6 Map (java.util.Map)6 WorkflowPath (org.alfresco.service.cmr.workflow.WorkflowPath)5 ArrayList (java.util.ArrayList)4 NodeRef (org.alfresco.service.cmr.repository.NodeRef)4 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)3 ResourceBundle (java.util.ResourceBundle)2 ResponseWriter (javax.faces.context.ResponseWriter)2 WorkflowTaskDefinition (org.alfresco.service.cmr.workflow.WorkflowTaskDefinition)2 WorkflowTransition (org.alfresco.service.cmr.workflow.WorkflowTransition)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 DeleteRequest (org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)2 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)2