Example Java™ Code

This example below shows some of the fundamentals in communicating with the Business Rules Server. For clarity, appropriate error checking is not shown.
1   try
2   {
3      PayDirector       pd           = new PayDirector();
4      BrFieldList       brFieldList  = new BrFieldList();
5      Payment           payment      = new Payment();
6      RequestResponse   reqResponse  = new RequestResponse();
7      InitRequestEx     reqInit      = new InitRequestEx();
8      DecisionRequestEx reqDec       = new DecisionRequestEx();
9      TermRequest       reqTerm      = new TermRequest();
10     String            workflow     = null;
11
12     BrField fldMyInitField  = brFieldList.insertField ("MyInitField");
13     BrField fldMyInitFlag   = brFieldList.insertField ("MyInitFlag");
14     BrField fldSortType     = brFieldList.insertField ("ibmSortType");
15
16     BrField fldMyDocCnt     = brFieldList.insertField ("MyDocCnt");
17     BrField fldMyUserExitRc = brFieldList.insertField ("MyUserExitRc");
18
19     //------------------------------------------------------------ Init
20
21     reqInit.setFieldList       (brFieldList);
22     reqInit.setRequestResponse (reqResponse);
23     reqInit.setWorkflowName    ("SampleWorkflow");
24     reqInit.setClientReference ("My Test Client");
25
26     fldMyInitField.setData ("I am a Test app");
27     fldMyInitFlag.setData  (true);
28     fldSortType.setData    (105);
29
30     int rc = pd.apiInit (reqInit);
31
32     BrField fldWorkflow  = brFieldList.getField ("ibmWorkflow");
33
34     if (fldWorkflow != null)
35     {
36        workflow = fldWorkflow.getDataAsString ();
37
38        if (workflow.equals ("SampleWorkflow") == false)
39                System.out.println ("User Exit on server changed my workflow to '"
40                                    + workflow + "'");
41     }
42
43     // Process any returning fields here (see Decision for example)
44
45     if (reqResponse.getReturnCode() != 0)
46     {
47        System.out.println (reqResponse.getErrorMessage());
48        reqResponse.putReturnCode (0);
49     }
50
51     //------------------------------------------------------------- Decision
52
53     reqDec.setFieldList               (brFieldList);
54     reqDec.setPaymentValidationRecord (payment);
55     reqDec.setRequestResponse         (reqResponse);
56
57     int myCounter = 1;
58
59     // loop on multiple items
60     {
61        brFieldList.clear ();
62
63        payment.setAmount         ("0000001234");
64        payment.setProcessControl ("1234");
65        payment.setAccount        ("12345678");
66        payment.setRoutingNumber  ("1234-5678");
67        payment.setAuxOnUs        ("123456");
68
69        fldMyDocCnt.setData (myCounter++);
70
71        rc = pd.PdAPIDecisionEx (reqDec);
72
73        boolean valError = payment.isValidationError ();
74
75        int myReturnCode = fldMyUserExitRc.getDataAsInt ();
76
77        if (reqResponse.getReturnCode() != 0)
78        {
79           System.out.println (reqResponse.getErrorMessage());
80           reqResponse.putReturnCode (0);
81        }
82     }
83
84     //------------------------------------------------------------ Term
85
86     reqTerm.setRequestResponse (reqResponse);
87
88     rc = pd.PdAPITerm (reqTerm);
89  }
90  catch (BrFieldException e)
91  {
92  }
3
Establishes the Business Rules Server API.
4
Used to process the initialize message data fields (both framework and user fields) and decision message fields (user fields). This is an optional control whose requirement is determined by the workflow running on the Business Rules Server side.
5
An optional component used only on the decision message. It contains the normalized payment record.
6
The RequestResponse is shared by all messages.
7
The next three components handle the initialize, decision, and terminate messages.
12
Adds a user field named MyInitField to the BrFieldList collection. Only the place holder is added, the actual data is added in line 26. As long as the BrFieldList object remains in scope, a reference obtained with the insertField (and getField) routines will remain valid.
16
These two fields will only be used in the decision request in this example, but it is permissible to add them now.
21
Provides the BrFieldList to the initialize message. The decision message is the only other message that uses the BrFieldList and is set on line 53.
26
Next three lines put data into their respective fields.
30
This call goes to the Business Rules Server and will not return until the response is received or the message times out. Upon return, the API will add response fields to brFieldList.
32
The Business Rules Server framework returns the name of the workflow actually loaded. Until the response is received, ibmWorkflow does not exist in the collection for brFieldList and a call to getField returns null. insertField can be used as the other example fields are done.
61
This is an important step. Refer to clear.
63
Use the normalized payment record setters to set some of the fields. These values are not hard coded. When the decision message is called in line 71, the API will get the fields from the payment interface and translate them into their respective, previously defined names. For example, the value inserted in setAmount will be moved into a field names ibmNprAmount. This is how the Business Rules Server will receive it.
69
Use this means to send user data. The API will add it directly to the list of fields obtained from payment. The Business Rules Server receives it as one big list of data fields.
88
The terminate message must be the last one sent. It allows the Business Rules Server to clean up.
It is possible to construct an application that performs an initialize, decision, or terminate operation for each document processed. However, it is highly recommended not to design an application of this type. The Business Rules Server does a lot of preparatory work during the initialization message in an effort to streamline operations during the decision messages. This slows down the overall time to process a group of items considerably.