Sunday, March 8, 2015

Oracle BPM: Assign User to Application Role Programmatically

In order to assign users to application role programmatically in Oracle BPM, user the following code:

       


import java.util.HashMap;
import java.util.Map;

import oracle.bpel.services.bpm.common.IBPMContext;
import oracle.bpel.services.workflow.client.IWorkflowServiceClientConstants;
import oracle.bpel.services.workflow.client.WorkflowServiceClientFactory;
import oracle.bpel.services.workflow.verification.IWorkflowContext;

import oracle.bpm.client.BPMServiceClientFactory;
import oracle.bpm.services.organization.BPMOrganizationException;
import oracle.bpm.services.organization.model.ApplicationContext;
import oracle.bpm.services.organization.model.ApplicationContextTypeEnum;
import oracle.bpm.services.organization.model.ParticipantTypeEnum;
import oracle.bpm.services.organization.model.PrincipleRefType;
import oracle.bpm.workspace.util.WorkspaceUtil;

public class CustomWorklistUtils {
    public CustomWorklistUtils() {
        super();
    }
    private IBPMContext bpmContext = null;

    private static String adminUsername;
    private static String adminPassword;
    private static String bpmURL;


    public boolean createNewMember(String appRoleName, String member,
                                   String type) {
        System.out.println("Assigning User to ApplicationRole(Lane)...");
        System.out.println("...appRoleName: " + appRoleName);
        System.out.println("...member: " + member);
        System.out.println("...type: " + type);
        if (appRoleName == null) {
            return false;
        }

        PrincipleRefType principleRefType =
            WorkspaceUtil.getBPMObjectFactory().createPrincipleRefType();
        principleRefType.setName(member);
        principleRefType.setRealm(getBPMContext().getIdentityContext());

        if (type.equalsIgnoreCase(("LABEL_USER"))) {
            principleRefType.setType(ParticipantTypeEnum.USER);
        } else if (type.equalsIgnoreCase(("LABEL_GROUP"))) {
            principleRefType.setType(ParticipantTypeEnum.GROUP);
        } else if (type.equalsIgnoreCase(("LABEL_APPROLE"))) {
            principleRefType.setType(ParticipantTypeEnum.APPROLE);
        }

        ApplicationContext appContext =
            WorkspaceUtil.getBPMObjectFactory().createApplicationContext();
        appContext.setApplicationType(ApplicationContextTypeEnum.ORACLE_BPM_PROCESS_ROLES_APP);
        oracle.bpm.services.organization.model.Participant participant =
            WorkspaceUtil.getBPMObjectFactory().createParticipant(principleRefType);
        try {
            WorkspaceUtil.getBPMOrgUnitService().grantAppRoleToPrincipal(getBPMContext(),
                                                                         appContext,
                                                                         appRoleName,
                                                                         participant);

            return true;
        } catch (BPMOrganizationException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean removeMemberFromAppRole(String appRoleName, String member,
                                           String type) {
        if (appRoleName == null) {
            return false;
        }

        PrincipleRefType principleRefType =
            WorkspaceUtil.getBPMObjectFactory().createPrincipleRefType();
        principleRefType.setName(member);
        principleRefType.setRealm(getBPMContext().getIdentityContext());

        if (type.equalsIgnoreCase(("LABEL_USER"))) {
            principleRefType.setType(ParticipantTypeEnum.USER);
        } else if (type.equalsIgnoreCase(("LABEL_GROUP"))) {
            principleRefType.setType(ParticipantTypeEnum.GROUP);
        } else if (type.equalsIgnoreCase(("LABEL_APPROLE"))) {
            principleRefType.setType(ParticipantTypeEnum.APPROLE);
        }

        ApplicationContext appContext =
            WorkspaceUtil.getBPMObjectFactory().createApplicationContext();
        appContext.setApplicationType(ApplicationContextTypeEnum.ORACLE_BPM_PROCESS_ROLES_APP);
        oracle.bpm.services.organization.model.Participant participant;
        participant =
                WorkspaceUtil.getBPMObjectFactory().createParticipant(principleRefType);
        try {
            WorkspaceUtil.getBPMOrgUnitService().revokeAppRoleFromPrincipal(getBPMContext(),
                                                                            appContext,
                                                                            appRoleName,
                                                                            participant);

            return true;
        } catch (BPMOrganizationException ex) {
            ex.printStackTrace();
        }

        return false;
    }

    public IBPMContext getBPMContext() {

        try {
            if (bpmContext == null) {

                bpmContext = getIBPMContext(adminUsername, adminPassword);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bpmContext;
    }

    public static IBPMContext getIBPMContext(String username,
                                             String password) throws Exception {
        return getBPMServiceClientFactory().getBPMUserAuthenticationService().authenticate(username,
                                                                                           password.toCharArray(),
                                                                                           null);
    }

    public static BPMServiceClientFactory getBPMServiceClientFactory() {

        bpmURL = "t3://localhost:8001";
        adminUsername = "weblogic";
        adminPassword = "welcome1";

        Map properties = new HashMap();

        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.CLIENT_TYPE,
                       WorkflowServiceClientFactory.REMOTE_CLIENT);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL,
                       bpmURL);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_INITIAL_CONTEXT_FACTORY,
                       "weblogic.jndi.WLInitialContextFactory");
        return BPMServiceClientFactory.getInstance(properties, null, null);
    }

}

       
 

Note that you have to add the Organization Service into the foreign JNDI on your domain:
Name: ejb/bpm/services/BPMOrganizationServiceBean
Local JNDI Name: ejb/bpm/services/BPMOrganizationServiceBean
Remote JNDI Name: ejb/bpm/services/BPMOrganizationServiceBean

 CustomWorklistUtils.java

1 comment:

  1. Hi,

    I am trying to follow this implementation in BPM 12.2.1.2.

    Could you please give me the steps to "add the Organization Service into the foreign JNDI on your domain" ?

    When I follow the Oracle documentation to do so I am encountering an error on save because Remote and Local JNDI names are the same ("ejb/bpm/services/BPMOrganizationServiceBean").

    Also could you please help me to find "WorkspaceUtil" class.
    I don't seem to be able to access it.

    Thanks

    Leslie

    ReplyDelete