Step 1. Create JSON agent function that will be called by Form

INT010B - JSON Service - Create Agent-side Business Logic

In this step, you will create the application program that calls the web service using your JSON solution.

There are two parts to the Agent solution:

The code example is generated as a LANSA function. See Solutions / Employees/samples/RDMLX. You could use the code SAMPLE_RDMLX_INBOUND_HTTP.TXT to create your function. You could write your own if appropriate.

Following is a brief description of the essential steps necessary for this click event routine to issue the web service request and receive the results.

The first thing to note is that the LANSA form communicates with the LANSA Integrator JSON service using Built-In Functions specially designed for the purpose.

1.  Using the LANSA Editor, create a new RDMLX function iiiFN11 – Get Employees Agent in the process iiiPRO09. In this case, for the outbound processing, it is easier to copy code from the inbound function rather than copying the generated sample code.

2.  Change the Function Options line of code and add keywordq Rcv_List(#EMPS) so that the function can return the list of employees to the form which will call it.

3.  After the line above, define the list EMPS with the fields EMPNO, GIVENAME, SURNAME and SALARY.  It should be a working list with Entrys set to *MAX.

4.  After the line above, define a group with the fields DEPTMENT and SECTION.  If you don't do this, the fields DEPTMENT and SECTION would not be able to be sent to the JSON server because there will be no reference to these fields anywhere else in the function.

5.  After the line above, add code to clear the list EMPs.

6.  Switch to function iiiFN10 and copy the code that opens the JSM and loads the service and paste it into iiiFN11 after the line above.  Then change HTTPInboundJSONBindService to HTTPOutboundJSONBindService

7.  Switch back to function iiiFN10 and copy the CHECK subroutine and paste it into iiiFN11. You should paste all other code so that the subroutine is at the bottom of your function.

8.  Switch back to function iiiFN10 and copy the code that binds the service IIIEMPLOYEEDEPTSECT and paste after the Load service code.  Change the TYPE to *OUTBOUND.

9.  Below the code above add code to create the JSON object (WRITE BINDTRACE(*YES)) and send the request (SEND HOST…).  Notice how the URL must be split into the HOST and the URI.

     Note: The URI is executing the Direct Service IIIEMPLOYEEJSON which you defined in INT010A – JSON Service – Define Server-side logic. The HOST value must be your server and port number.

10. Copy the BIND SERVICE logic from #3 above and paste below the code above.  Then change IIIEMPLOYEEDEPTSECT to IIIEMPLOYEE and change TYPE to *INBOUND.  This code will result in the response being received and available to process.  In this case you are only processing a list.  If there had been simple fields as well as a list, the fields would also be bound to the function's fields at this point.

11. Add code to loop through all EMPLOYEE fragments until no further fragments can be found and add each fragment's data to your list.  You could copy the 3 lines of code after the Begin_Loop from function iiiFN10 then change SET to GET.

12. After the line above add the logic to close JSM and return from this function.

13. Compile function iiiFN11. Check in to the IBM i server if necessary.

Your Code might look something like this.

FUNCTION OPTIONS(*DIRECT) RCV_LIST(#emps)

DEF_LIST NAME(#EMPS) FIELDS(#EMPNO #GIVENAME #SURNAME #SALARY) TYPE(*WORKING) ENTRYS(*MAX)

 

GROUP_BY NAME(#parms) FIELDS(#deptment #section)

CLR_LIST NAMED(#emps)

* Open service

USE BUILTIN(JSMX_OPEN) TO_GET(#JSMXSTS #JSMXMSG #JSMXHDLE1)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)

 

* Load service

CHANGE FIELD(#JSMXCMD) TO('SERVICE_LOAD SERVICE(HTTPOutboundJSONBindService) SERVICE_CONTENT(*HTTP) TRACE(*YES)')

USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)

 

* Bind service to read HTTP request content

CHANGE FIELD(#JSMXCMD) TO('BIND SERVICE(IIIEMPLOYEEDEPTSECT) TYPE(*OUTBOUND) BINDTRACE(*YES) SERVICE_EXCHANGE(*FIELD)')

USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)

 

* Create the JSON object

#JSMXCMD := 'WRITE BINDTRACE(*YES)'

USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)

 

* Send Request

#JSMXCMD := 'SEND HOST(earthd14.lansa.co.uk:80) URI(/cgi-bin/jsmdirect?IIIEMPLOYEEJSON)'

USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)

 

* Bind service to process HTTP response content

CHANGE FIELD(#JSMXCMD) TO('BIND SERVICE(IIIEMPLOYEES) TYPE(*INBOUND) BINDTRACE(*YES) SERVICE_EXCHANGE(*FIELD)')

USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)

 

* Process the employees

BEGIN_LOOP

#JSMXCMD := 'GET FRAGMENT(EMPLOYEE) SERVICE_EXCHANGE(*FIELD)'

USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)

 

LEAVE IF(#JSMXSTS = NOFRAGMENT)

 

ADD_ENTRY TO_LIST(#emps)

 

END_LOOP

 

* Close service

USE BUILTIN(JSMX_CLOSE) WITH_ARGS(#JSMXHDLE1) TO_GET(#JSMXSTS #JSMXMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)

 

 

* Check routine

SUBROUTINE NAME(CHECK) PARMS((#JSMXSTS *RECEIVED) (#JSMXMSG *RECEIVED))

IF COND('#JSMXSTS *NE OK')

USE BUILTIN(JSMX_CLOSE) WITH_ARGS(#JSMXHDLE1) TO_GET(#JSMXSTS #JSMXMSG)

EXIT

ENDIF

ENDROUTINE