Step 12. Create the JSON Server Business Logic

INT010A - JSON Service - Define Server-side logic

Before you can test or use your JSON Server solution, you need to create the business logic that supports it. That is, a function to receive a request with department code and section code parameters and to build the Employee return parameter which contains the requested employee details.

You do not need to concern yourself with the communication details as LANSA Integrator and the JSON Wizard has generated the files which implement that.

Following is an outline of the code's functionality using the LANSA RDMLX code generated by Integrator Studio as SAMPLE_RDMLX_INBOUND_HTTP.txt. Note that this program could alternatively have been coded as an RDML function or as an ILE RPG program. Integrator Studio also generates an RPG and an RDML example.

1. Using the LANSA Editor, create a new process iiiPRO09JSON and an RDMLX function iiiFN10 – Get Employees Server, belonging to it, without using a template. With your iii JSON Tutorial project open in Integrator Studio, expand Solutions / EmployeeDeptSect /samples / RDMLX and open the generated RDMLX file SAMPLE_RDMLX_INBOUND_HTTP.TXT and copy it into your function (replacing existing code).d

2.  The commands in the code below are to create a connection to the JSM server and to load the JSON Server service. This is accomplished using the JSMX_OPEN Built-In Function and the SERVICE_LOAD service command.

3.  The function binds to the service iiiEMPLOYEEDEPTSECT which results in the data being returned into fields in the function.  Change this line of code by replacing the III with your initials.

4.  The function must tell the function it wants to return a response prior to reading any data that will be part of the response.  Add the following code after the code above.  Notice how the service that is being bound is now the IIIEMPLOYEES service, which represents the data to be returned.  The previously bound service in #2 above was the data you wanted to receive.

5.  Add the following code right after the code above.  Logic needs to be added to send data for each employee in the employee master.  Notice how you need to put the JSM logic to set the EMPLOYEE fragment inside the SELECT/ENDSELECT loop.  Remember that the Employee Id, First Name, Surname and Salary fields are already mapped to the elements of the Employee return parameter.  Notice how you should use the Nbr_Keys(*COMPUTE) so that you can return all employees, just employees for a department or employees for a department and section by only writing a single Select line of code.

6.  Add more code after the code above to send the response and close the JSM to force the response to be sent.  

7.  Compile your function. If you are using an IBM i server, check in to the server and compile.

     Your logic should look like the following:

Function Options(*DIRECT)

 

* Open service

Use Builtin(JSMX_OPEN) To_Get(#JSMXSTS #JSMMSG #JSMXHDLE1)

Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMMSG)

 

* Load service

Change Field(#JSMXCMD) To('SERVICE_LOAD SERVICE(HTTPInboundJSONBindService) SERVICE_CONTENT(*HTTP) TRACE(*YES)')

Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMMSG)

Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMMSG)

 

* Bind service to read HTTP request content

Change Field(#JSMXCMD) To('BIND SERVICE(IIIEMPLOYEEDEPTSECT) TYPE(*INBOUND) BINDTRACE(*YES) SERVICE_EXCHANGE(*FIELD)')

Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMMSG)

Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMMSG)

 

* Bind service to send response

Change Field(#JSMXCMD) To('BIND SERVICE(IIIEMPLOYEES) TYPE(*OUTBOUND) BINDTRACE(*YES) SERVICE_EXCHANGE(*FIELD)')

Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMMSG)

Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMMSG)

 

* Get the Data from the files

Select Fields(#EMPNO #GIVENAME #SURNAME #SALARY) From_File(PSLMST1) With_Key(#DEPTMENT #SECTION) Nbr_Keys(*COMPUTE)

Change Field(#JSMXCMD) To('SET FRAGMENT(EMPLOYEE) SERVICE_EXCHANGE(*FIELD)')

Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMMSG)

Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMMSG)

Endselect

 

* Send Response

Change Field(#JSMXCMD) To('SEND BINDTRACE(*YES)')

Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMMSG)

Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMMSG)

 

* Close JSM to force response to be sent

Use Builtin(JSMX_CLOSE) With_Args(#JSMXHDLE1) To_Get(#JSMXSTS #JSMMSG)

 

* Check routine

Subroutine Name(CHECK) Parms((#JSMXSTS *RECEIVED) (#JSMMSG *RECEIVED))

If Cond('#JSMXSTS *NE OK')

Use Builtin(JSMX_CLOSE) With_Args(#JSMXHDLE1) To_Get(#JSMXSTS #JSMMSG)

Exit

Endif

Endroutine