Quantcast
Channel: Web Dynpro ABAP
Viewing all articles
Browse latest Browse all 141

Real-time notifications and workflow using ABAP Push Channels (websockets) Part 4: Creating a Web Dynpro ABAP Application for receiving notifications

$
0
0

This is Part 4 of the below series of blogs:

 

 

 

Now we are going to create a similar application to what we did in Part 3, however this time we are going to use APC (websockets) in a Web Dynpro ABAP application. We do this using a fairly new feature called HTML Islands.

 

 

Create a new Web Dynpro ABAP component called ZWDC_WF_INBOX_MINI

024.PNG

 

 

In the Component Controller create a new Node

025.PNG

 

 

Enter the following:
Node Name: WORKITEMS
Dictionary structure: SWR_WIHDR
Cardinality: 0..n
Init. Lead Selection: No

 

 

Then press the Add Attributes from Structure button

026.PNG

 

Select the WI_TEXT, WI_CD and WI_CT attributes and press the green tick

027.PNG

 

 

 

Create a new method GET_USER_WORKITEMS is the Component Controller and put the following code in it:

028.PNG

 

 

METHOD get_user_workitems .
     
DATA: lo_nd_workitems TYPE REF TO if_wd_context_node.

     
DATA: lt_workitems TYPE wd_this->elements_workitems.

      lo_nd_workitems
= wd_context->get_child_node( name = wd_this->wdctx_workitems ).

     
CALL FUNCTION 'SAP_WAPI_CREATE_WORKLIST'
          
TABLES
                worklist
= lt_workitems.

     
SORT lt_workitems BY wi_id DESCENDING.

      lo_nd_workitems
->bind_table( new_items = lt_workitems set_initial_elements = abap_true ).

ENDMETHOD.

 

 

Then add the following code to the WDDOINIT Method:

METHOD wddoinit .
      wd_this
->get_user_workitems( ).
ENDMETHOD.

 

 

In the V_MAIN View, go to Context and map WORKITEMS to CONTEXT by dragging and dropping it from the right side to the left side.

030.PNG

 

 

Go into Layout tab and start by changing the ROOTUIELEMENTCONTAINER Layout to a RowLayout:

031.PNG

 

 

Right click on ROOTUIELEMENTCONTAINER and click Insert Element.

032.PNG

 

 

 

Enter the following:
ID: WS_HTML_ISLAND
Typ: HtmlIsland

033.PNG

 

 

 

Select WS_HTML_ISLAND and change the height and width to 0px as we do not want this HTML Island to be visible.

034b.PNG

 

 

Now right click on WS_HTML_ISLAND and click on Insert Event.

035.PNG

 

 

Give the Event a name ON_MESSAGE and the press the Create button next to onAction to Create an event handler.

036.PNG

 

 

Enter the Action name as ON_MESSAGE and make sure to tick Transfer UI Event Parameters, then press the green tick.

037.PNG

 

 

Double click on the ON_MESSAGE action created to forward navigate into the method. Then paste in the code below

038.PNG

 

METHOD onactionon_mesage .
     
DATA: lo_api_controller     TYPE REF TO if_wd_controller.

      lo_api_controller ?= wd_this
->wd_get_api( ).

*   report message
      lo_api_controller
->get_message_manager( )->report_message( message_text = data
                                                                message_type
= if_wd_message_manager=>co_type_info
                                                                is_permanent
= abap_false  ).
* refresh work items
      wd_comp_controller
->get_user_workitems( ).
ENDMETHOD.

 

 

 

Go back into the Layout tab and right click on WS_HTML_ISLAND again and add a script this time.

039.PNG

 

 

Enter in websocket_wf_notify.js in the source field. We wil create and upload this javascript file afterwards.

040.PNG

 

 

Save the following source to a file name websocket_wf_notify.js and upload it as a MIME object for the webdynpro.

 

Source code:

 

var MySocket = { 

 

      init: function(callback){

            //Save the call back object to this object

            this.callback = callback;

            var that = this;

            var hostLocation = window.location, socket, socketHostURI, webSocketURI;

 

            if (hostLocation.protocol === "https:") {

                  socketHostURI = "wss:";

            } else {

                  socketHostURI = "ws:";

            }

            socketHostURI += "//" + hostLocation.host;

            webSocketURI = socketHostURI + '/sap/bc/apc/sap/zapc_wf_notify';

        

            try {

                  socket = new WebSocket(webSocketURI);

                  socket.onopen = function() { };

                  var that = this;

   

                  //Create function for handling websocket messages

                  socket.onmessage = function(wfNotify) {

                        // When a message is received fire an event to call the web dynpro code to post the message and

                        // refresh the worklist

                        that.callback.fireEvent('ON_MESSAGE', wfNotify.data);

                  };

   

                  socket.onclose = function() {

                  };

            } catch (exception) {

   

            }

      }

};

 

 

 

Right click on the web dynpro component and go to Create->Mime Object->Import.

041.PNG

 

 

Select the file you created in the previous step websocket_wf_notify.js and click open.

042.PNG

 

 

Then press save.

043.PNG

 

 

Right click on ROOTUIELEMENTCONTAINER again and Insert Element.

044.PNG

 

 

And enter MESSAGE_AREA and select MessageArea as the Type.

045.PNG

 

 

Make sure ROOTUIELEMENTCONTAINER is selected and then press the Wizard button

046.PNG

 

 

Select Table and press Continue.

047.PNG

 

 

Press Context to select the WORKITEMS context:

048.PNG

 

 

Select WORKITEMS and press the green tick.

049.PNG

 

 

No Need to change anything on the next screen, just press the green tick.

 

 

 

In the table properties change design to Alternating and set visibleRowCount to 25.

051.PNG

 

 

Now go into the Methods tab and select the WDDOMODIFYVIEW method.

 

 

And enter the following code to initialise the web socket when the view first loads:

 

METHOD wddomodifyview .
     
DATA: lo_html_island TYPE REF TO cl_wd_html_island.

     
IF first_time EQ abap_true.

           lo_html_island ?= view
->get_element( `WS_HTML_ISLAND` ).

           lo_html_island
->add_script_call( cl_wd_html_script_call=>new_call( )->variable( `MySocket` )->function( `init` )->add_callback_api( )  ).
     
ENDIF.
ENDMETHOD.

 

 

 

Right click on ZWDC_WF_INBOX_MINI and go to Create->Web Dynpro Application.

 

Enter an application name and Description and press the green tick.

 

054.PNG

 

Save and activate everything then right click the Web Dynpro application to run.

 

 

 

Now open the window next to the SAP GUI and create a new parked journal, which will go to my inbox for approval. On saving the Parked Journal document, the notification is immediately displayed in my browser window and the list is automatically refreshed.

 

Before saving:

056.PNG

 

After Saving - Inbox updated with new document automatically and notification displayed:

057.PNG


Viewing all articles
Browse latest Browse all 141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>