Page 1 of 1

#XPRIM_JsonObject: Updating a record using PUT

Posted: Fri Apr 22, 2022 1:12 am
by Dino
to follow up with examples for XPRIM_JsonObject....
If you publish a restful service, for example for DEPTAB using the assistant:
test420_01.jpg
test420_01.jpg (82.1 KiB) Viewed 41416 times
so this is now available:
http://localhost:8080/rho/TEST420PR/deptments/ADM

(Notice that in my installation the partition is part of the name. In some others, the partition is not needed)

you can create a server module (example TEST420SM) to request the update of a record using a PUT like this:

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_SRVM)
Group_By Name(#Fields) Fields(#DEPTMENT #DEPTDESC)
Srvroutine Name(SendPUT)
Group_Map For(*Input) Group(#Fields)
Field_Map For(*Output) Field(#STD_QSEL)

Define_Com Class(#XPRIM_HttpRequest) Name(#Req)
Define_Com Class(#XPRIM_UriBuilder) Name(#Url)
Define_Com Class(#XPRIM_JsonObject) Name(#JsonObject)

#Url.SetScheme( 'http' )
#Url.SetHost( 'localhost:8080' )
#Url.SetPath( ("/" + #partition + "/TEST420PR/deptments/" + #DEPTMENT) )

#JsonObject.InsertString Key("DEPTMENT") String(#DEPTMENT)
#JsonObject.InsertString Key("DEPTDESC") String(#DEPTDESC)

#Req.Content.AddJsonObject( #JsonObject )

* Execute the request (POST verb)
#Req.DoPut Url(#Url)

If (#Req.Response.IsSuccessfulRequest)
#STD_QSEL := #Req.Response.IsSuccessfulRequest + ' ' + #Req.Response.AsString.AsNativeString
Else
#STD_QSEL := #Req.Response.ErrorCode.AsNativeString + ' ' + #Req.Response.ErrorMessage.AsNativeString
Endif
Endroutine
End_Com
and call this server module for example from this webpage:

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_WEB) Theme(#SYS_THEME<MaterialDesignBlue>) LayoutManager(#LayoutMain) Height(721)

Define_Com Class(#PRIM_TBLO) Name(#LayoutMain)
Define_Com Class(#PRIM_TBLO.Column) Name(#LayoutMainColumn1) DisplayPosition(1) Parent(#LayoutMain) Width(2)
Define_Com Class(#PRIM_TBLO.Column) Name(#LayoutMainColumn2) DisplayPosition(2) Parent(#LayoutMain) Width(20)
Define_Com Class(#PRIM_TBLO.Column) Name(#LayoutMainColumn3) DisplayPosition(3) Parent(#LayoutMain) Width(2)
Define_Com Class(#PRIM_TBLO.Row) Name(#LayoutMainRow1) DisplayPosition(1) Parent(#LayoutMain) Height(2)
Define_Com Class(#PRIM_TBLO.Row) Name(#LayoutMainRow2) DisplayPosition(2) Parent(#LayoutMain) Height(6.76)
Define_Com Class(#PRIM_TBLO.Row) Name(#LayoutMainRow3) DisplayPosition(3) Parent(#LayoutMain) Height(20.24)
Define_Com Class(#PRIM_TBLO.Item) Name(#LayoutMainItem1) Manage(#laTitle) Parent(#LayoutMain) Row(#LayoutMainRow1) Sizing(None) Column(#LayoutMainColumn2)
Define_Com Class(#PRIM_TBLO.Item) Name(#LayoutMainItem2) Manage(#Messages) Parent(#LayoutMain) Row(#LayoutMainRow2) Column(#LayoutMainColumn2) MarginBottom(10) MarginLeft(10) MarginRight(10) MarginTop(10)
Define_Com Class(#PRIM_TBLO.Item) Name(#LayoutMainItem3) Manage(#deptmentlist) Parent(#LayoutMain) Row(#LayoutMainRow3) Column(#LayoutMainColumn2) MarginBottom(10) MarginLeft(10) MarginRight(10) MarginTop(10)

Define_Com Class(#PRIM_LABL) Name(#laTitle) Caption('deptment API') DisplayPosition(1) Ellipses(Word) Height(33) Left(476) Parent(#COM_OWNER) TabPosition(1) TabStop(False) Top(9) VerticalAlignment(Center) Width(249) ThemeDrawStyle('Title')
Define_Com Class(#PRIM_LIST) Name(#Messages) DisplayPosition(2) Left(110) Parent(#COM_OWNER) TabPosition(2) Top(60) Width(980) Height(148)
Define_Com Class(#PRIM_LIST.String) Name(#MessagesColumn1) ColumnWidth(1) DisplayPosition(1) Parent(#Messages) SortOnClick(True) Source(#STD_QSEL) ColumnCaption('Messages') ColumnCaptionAlign(Left) ColumnCaptionType(Caption) ColumnUnits(Proportion)
Define_Com Class(#PRIM_LIST) Name(#deptmentlist) ColumnLines(False) DisplayPosition(3) Height(483) Left(110) Parent(#COM_OWNER) TabPosition(3) Top(228) Width(980) ColumnHeaderHeight(48) RowHeight(48)

* The first column will not have the columnreadonly false
Define_Com Class(#PRIM_LIST.String) Name(#deptmentColumn01) ColumnWidth(1) DisplayPosition(1) Parent(#deptmentlist) SortOnClick(True) Source(#DEPTMENT) DisplayAlignment(Center) ColumnUnits(Proportion)
Define_Com Class(#PRIM_LIST.String) Name(#deptmentColumn02) ColumnWidth(1) DisplayPosition(2) Parent(#deptmentlist) SortOnClick(True) Source(#DEPTDESC) DisplayAlignment(Center) ColumnUnits(Proportion) ColumnReadOnly(False)

Define_Com Class(#STD_INT) Name(#HttpStatus)

Evtroutine Handling(#Com_owner.Initialize)
Clr_List Named(#Messages)
#COM_OWNER.Getdeptment
Endroutine

Mthroutine Name(Getdeptment) Access(*Private)
Define_Com Class(#Prim_Web.HttpRequest) Name(#Getdeptment)
Define_Com Class(#prim_web.JsonElement) Name(#JRoot_Element) Reference(*Dynamic)
Define_Com Class(#PRIM_WEB.json) Name(#Json)

#Getdeptment.Url := ("/" + #partition + "/TEST420PR/deptments")

#STD_QSEL := #TIMEX.AsDisplayString( HHsMMsSS ) + " - Contacting Server"
Add_Entry To_List(#Messages)

#Getdeptment.ExecuteAsync

Evtroutine Handling(#Getdeptment.Completed)
Clr_List Named(#deptmentlist)
#HttpStatus := #Getdeptment.Response.StatusCode

#Json := #Getdeptment.Response.Content
#JRoot_Element <= #Json.RootItem

If (#HttpStatus <> 200)
#STD_QSEL := #TIMEX.AsDisplayString( HHsMMsSS ) + " - Error Status Code: " + #HttpStatus.AsString + " Message: " + #Getdeptment.Response.StatusText + " received from server."
Add_Entry To_List(#Messages)
Else
For Each(#ArrayElem) In(#JRoot_Element)
For Each(#Entry) In(#ArrayElem)
Case (#Entry.Key)
When Value_Is(= "DEPTMENT")
#DEPTMENT := #Entry.AsString.Trim
When Value_Is(= "DEPTDESC")
#DEPTDESC := #Entry.AsString.Trim
Endcase
Endfor
Add_Entry To_List(#deptmentlist)
Endfor

#STD_QSEL := #TIMEX.AsDisplayString( HHsMMsSS ) + " - Get deptment completed successfully."
Add_Entry To_List(#Messages)
Endif
Endroutine

Evtroutine Handling(#Getdeptment.Failed)
#Sys_web.Alert Caption("Get deptment Failed")
Endroutine
Endroutine

* Just keep the routines you want for the fields you want to allow update
Evtroutine Handling(#deptmentColumn02.Enter)
#COM_OWNER.Updatedeptment
Endroutine

Mthroutine Name(Updatedeptment) Access(*Private)
Define_Com Class(#TEST420SM.SendPUT) Name(#SendPUT)
Group_By Name(#Fields) Fields(#DEPTMENT #DEPTDESC)

#SendPUT.Execute Fields(#Fields) STD_QSEL(#STD_QSEL)
Add_Entry To_List(#Messages)
Endroutine
End_Com
to get this webpage:
test420_02.jpg
test420_02.jpg (140.36 KiB) Viewed 41416 times