Wednesday, May 28, 2014

Adhoc Service Call in IBM BPM 7.5

Problem Statement 

You want to call a service residing outside of your toolkit/process-app. You know the name of the service, the process-app/toolkit acronym and possibly the snapshot name as well. You want to discover the service and execute it without having to go through the process of creating dependencies within your toolkit/process-app. Traditionally this is not possible, IBM BPM 7.5 provides API to call any service from JavaScript (Using tw.system.executeServiceByName()), but this can only execute the services that directly belong to the process-app/toolkit or any dependent toolkit(s). We want to break free of this limitation and execute any service on the environment.

Solution
Create a new Server-JS file and copy the following functions into it.
function findSnapshotForProcessApp(processApp, snapshotName) {
    var processAppObj = null;
    if(typeof processApp == 'string')
        processAppObj = tw.system.model.findProcessAppByAcronym(processApp);

    try {
        if(snapshotName) {
            return processAppObj.findSnapshotByName(snapshotName);
        }
        else {
            var snaps = processAppObj.snapshots;
            if(snaps && snaps.length > 0)
                return snaps[snaps.length -1];
            else
                return null;
        }
    }
    catch (e) {
      throw 'Error in findSnapshotForProcessApp : Required parameter "processApp" not provided or an invalid process-app acronym was provided.';
    }
}
function executeServiceByNameAdHoc(serviceName, inputParams, processApp, snapshotName) {
    var snapshot = findSnapshotForProcessApp(processApp, snapshotName);
   
    if(snapshot) {
        var serv = snapshot.findServiceByName(serviceName);
        if(serv) {
            return serv.execute(inputParams);
        }
        else // Service not found
           throw 'Error in executeServiceByNameAdHoc : No service found. Parameters provided : processApp=' + processApp + ", snapshotName=" + snapshotName + ", serviceName=" + serviceName;
       
    }
    else // Snapshot not found
        throw 'Error in executeServiceByNameAdHoc : No snapshot found. Parameters provided : processApp=' + processApp + ", snapshotName=" + snapshotName;
}

Call the function executeServiceByNameAdHoc with the following parameters to execute any adhoc service.

* @param {String} serviceName The name of the service to execute
* @param {Map} inputValues the input values for the service
* @param {String} processApp : The Acronym for the process app in which the service resides.
* @param {String} (OPTIONAL) snapshotName : Name of the specific snapshot version to execute the service from.
*
* @return A map of output values
* @type Map

Issues

If you get a "ConcurrentModificationException" while trying to execute a service with multiple parameters, the go ahead and deploy the IBM APAR JR49178. This fixes a bug related to HashMap in the method com.lombardisoftware.core.script.js. TWServiceScriptable.jsFunction_execute(Object)

No comments:

Post a Comment