Code review

This section lists the java code snippets needed in Process_A and Process_B.

  1. AssignInputValue

  2. CopyDataObjects

  3. ReadInputvariable

  4. CreateOutputVariable

  5. AssignValues

  6. ReadOnlyAccess

Java libraries

The following Java classes and interfaces are commonly used in processes that contain Java snippet activities:

import commonj.sdo.DataObject;
import commonj.sdo.Type;
import com.ibm.websphere.sca.ServiceManager;
import com.ibm.websphere.bo.BOFactory;
import com.ibm.websphere.bo.BOCopy;

Process Variables

The variables that have been defined for the business process can also be used in snippet activities. In the Java code you can refer to these variables with the same name as defined at the process level. The Java types of these variables are derived from the types of the process variables. The next section explains how these Java types are derived.

Data types

There are two ways to specify the type of a process variable. As defined in the BPEL standard, you can assign to a process variable the type of a WSDL message expressed as XML Schema types. The other way is to assign to a variable an XML Schema type directly. If the type is a primitive schema type, the Java representation is the corresponding Java class, e.g. an xsd:int is represented as java.lang.Integer. If the type is a complex type, the Java representation is a commonj.sdo.DataObject.

If the type is derived from a WSDL interface, you have to inspect the type of the message. There are mainly two styles how WSDL messages can be laid out: RPC style or document/literal wrapped style. In the case of document/literal wrapped style, a single-part message of a primitive type is encapsulated, so the resulting Java representation is commonj.sdo.DataObject. In the case of the RPC style, the representation is the corresponding Java class.

Read-write vs. read-only mode

As default in snippet activities, write access to process variables is not restricted. If you do not have to change the values of process variables, you can restrict the access to read-only. This helps to improve the performance. The following code snippet shows how you can change the access to variables to read-only:

// @bpe.readOnlyVariables names="MyBO Output"

Notes:

  1. In the names="..." construct you can list a single variable or a list of variables separated by blanks.
  2. You can use every Java comment construct to declare the read-only behavior (/*..*/, /**...*/, //).
  3. The comment can be placed everywhere in your snippet.
  4. Java snippets in conditions, loops, etc. have a default access of read-only.
  5. You can change the behavior of Java snippets in conditions, loops, etc. in the same way to read-write using//@bpe.readWriteVariables names="...".

Creating data objects

To create data objects you need the BOFactory service. The following code snippet illustrates how you can obtain the BOFactory service:

BOFactory boFactory = (BOFactory)ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOFactory");

The following code snippet shows how you can determine the type of a process variable and create a new data object of that type:

Type type = getVariableType("Output");
Output = boFactory.createByType(type);

To create a nested data object, you can use the createDataObject method of the DataObject:

DataObject nested = Output.createDataObject("processBusinessObject");

You can also create a data object using the create method of the BOFactory service. You have to specify name and namespace:

MyBO = factory.create("http://JavaSnippets/bpc/samples","BO");

Copying data objects

To copy data objects you need the BOCopy service. The following code snippet illustrates how you can obtain the BOCopy service:

BOCopy copyService = (BOCopy)ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOCopy");

The BOCopy service offers to methods to create a deep copy of data objects: copy and copyInto. The copy method returns a copy of the original DataObject and the copyInto method allows to copy the original DataObject into the structure of an existing DataObject.

Copy:

DataObject dataObject = copyService.copy(ProcessBusinessObject);

Copy into:

DataObject copyBO = copyService.copy(sourceBO);
targetBO.set(path, copyBO);

(The Method copyInto is deprecated.)