Step 2. Code iiiFN06 Server SET functionality
INT006 - Department Inquiry Functions
In this step, you will write the RDMLX to access the database file DEPTAB to FETCH the department description. You will write the RDMLX for the server side SEND to respond to the client request.
1. Working with function iiiFN06, after the GET from the DEPARTMENTREQUEST fragment, remove the block highlighted below:
then write the RDMLX code to clear the DEPTDESC field, FETCH the field DEPTDESC (department description) from file DEPTAB with key DEPTMENT (department code).
Check the I/O status of the FETCH operation. If it is not *OKAY, change the department description to the literal 'Department not found'.
The RDMLX code might appear as follows:
#DEPTDESC := *NULL
FETCH FIELDS(#DEPTDESC) FROM_FILE(DEPTAB) WITH_KEY(#DEPTMENT) IO_ERROR(*NEXT) VAL_ERROR(*NEXT)
IF_STATUS IS_NOT(*OKAY)
#DEPTDESC := 'Department Not Found'
ENDIF
2. You now need to add code to create the outbound HTTP content. You can take this code fragment from the RDMLX generated by Studio for your SAMPLE_RDMLX_OUTBOUND_HTTP.txt in the text editor. Copy the highlighted code into function iiiFN06 immediately following the comment:
* <<< Outbound binding logic goes here >>>.
3. In the CHECK subroutine, add an ABORT command to the IF..ENDIF condition so that the program ends if an error has occurred.
4. Compile the function.
5. If you are using an IBM i JSM server, check the function into the IBM i and compile it.
Your finished RDMLX code might appear as follows:
FUNCTION OPTIONS(*DIRECT)
* The following fields are used by the xml binding map
* #DEPTMENT
* The following fragments are used by the xml binding map
GROUP_BY NAME(#DEPTREQ) FIELDS(#DEPTMENT)
* 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(HTTPInboundXMLBindService) 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(IIIPRO04_REQUEST) TYPE(*INBOUND) BINDTRACE(*YES)')
USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)
* Get fragment - DEPTREQ
CHANGE FIELD(#JSMXCMD) TO('GET FRAGMENT(DepartmentRequest) SERVICE_EXCHANGE(*FIELD)')
USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)
* handle request for department description
#deptdesc := *blanks
FETCH FIELDS(#deptdesc) FROM_FILE(deptab) WITH_KEY(#deptment) IO_ERROR(*NEXT) VAL_ERROR(*NEXT)
IF_STATUS IS_NOT(*OKAY)
#deptdesc := ('Department not found')
ENDIF
* <<< Outbound binding logic goes here >>>
* Bind service to create HTTP request content
CHANGE FIELD(#JSMXCMD) TO('BIND SERVICE(IIIPRO04_RESPONSE) TYPE(*OUTBOUND)')
USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)
* Set fragment - DEPARTMENTRESPONSE
CHANGE FIELD(#JSMXCMD) TO('SET FRAGMENT(DEPARTMENTRESPONSE) SERVICE_EXCHANGE(*FIELD)')
USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)
* Write content
CHANGE FIELD(#JSMXCMD) TO('WRITE INDENT(*YES) BINDTRACE(*YES)')
USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)
* Send HTTP response content
CHANGE FIELD(#JSMXCMD) TO('SEND')
USE BUILTIN(JSMX_COMMAND) WITH_ARGS(#JSMXHDLE1 #JSMXCMD) TO_GET(#JSMXSTS #JSMXMSG)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMXSTS #JSMXMSG)
* 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)
ABORT
ENDIF
ENDROUTINE