Friday 15 January 2016

How to set current user in BPM tasks

To obtain current user in BPM tasks (especially when page fragments are being used) you can follow below approach.

1. First create the following java class, the current user's login is stored in HttpSession (this survives AM activation/passivation events).

package home.common.ext;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import javax.servlet.http.HttpSession;

import oracle.bpel.services.workflow.WorkflowException;
import oracle.bpel.services.workflow.client.IWorkflowServiceClient;
import oracle.bpel.services.workflow.query.ITaskQueryService;
import oracle.bpel.services.workflow.verification.IWorkflowContext;
import oracle.bpel.services.workflow.worklist.adf.ADFWorklistBeanUtil;

import oracle.jbo.client.Configuration;


public class ContextUtils {
    public ContextUtils() {
        super();
    }

    public void initializeUserContextData(String applicationId) {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext ectx = context.getExternalContext();
        HttpSession userSession = (HttpSession)ectx.getSession(true);
        try {
            String ctx =
                (String)context.getApplication().evaluateExpressionGet(context,
                                                                       "#{pageFlowScope.bpmWorklistContext}",
                                                                       String.class);
            IWorkflowServiceClient workflowSvcClient =
                ADFWorklistBeanUtil.getWorkflowServiceClient();
            ITaskQueryService wfQueryService =
                workflowSvcClient.getTaskQueryService();
            IWorkflowContext wfContext;
            String userName = "";
            try {
                wfContext = wfQueryService.getWorkflowContext(ctx);
                userName = wfContext.getUser();
            } catch (WorkflowException e) {
                e.printStackTrace();
            }
            if (userName != null) {
                userSession.setAttribute("currentUserLogin", userName);
                System.out.println("Current user's login from workspace is: " +
                                   userName);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. Create data control from this class (Just right click on this class and choose "Create Data Control" from the context menu);

3. Add this method as a default activity into the Task UI project.

4. The value is also available in Model project, so you can populate your WHO columns with current user login.



No comments:

Post a Comment