Consolidate JSON Data from Multiple Queries into a Model

Standard

I would like to share how to load and consolidate JSON data (with the same structure) from OData services into one JSON model.

Let’s assume you need to load the following OData services into one JSON model:

/sap/opu/odata/sap/ZGW_SRV/lpadSet?&$filter=ActivityGroup%20eq%20′” + menu[a] + “‘&$format=json”

Where menu[a] is a different source of OData service.

  1. var oBundle = jQuery.sap.resources({url : “res/menu.properties”});
  2. var menu = oBundle.getText(“LOCAL_MENU”).split(“,”);

Here is a sample of JSON data structure:

{

“d”: {

“results”: [

{

“__metadata”: {

“id”: “https://sapnetweaver.com:8200/sap/opu/odata/sap/ZGW_SRV/lpadSet(‘SAP_BW_GLB_FIN_MENU‘)”,

“uri”: “https://sapnetweaver.com:8200/sap/opu/odata/sap/ZGW_SRV/lpadSet(‘SAP_BW_GLB_FIN_MENU‘)”,

“type”: “ZGW_SRV.lpad”

},

“ActivityGroup”: “SAP_BW_GLB_FIN_MENU”,

“ParentId”: “0000000011”,

“ObjectId”: “0000000002”,

“NodeType”: “F”,

“Text”: “Asset Accounting”,

“PDFUrl”: “”,

“Menulevel”: “02”,

“Description”: “”,

“Url”: “”

},

{

“__metadata”: {

“id”: “https://sapnetweaver.com:8200/sap/opu/odata/sap/ZGW_SRV/lpadSet(‘SAP_BW_GLB_SD_MENU‘)”,

“uri”: “https://sapnetweaver.com:8200/sap/opu/odata/sap/ZGW_SRV/lpadSet(‘SAP_BW_GLB_SD_MENU‘)”,

“type”: “ZGW_SRV.lpad”

},

“ActivityGroup”: “SAP_BW_GLB_SD_MENU”,

“ParentId”: “0000000011”,

“ObjectId”: “0000000003”,

“NodeType”: “F”,

“Text”: “Accounts Payable”,

“PDFUrl”: “”,

“Menulevel”: “02”,

“Description”: “”,

“Url”: “”

}

]

}

}

Let’s defining the necessary variables. Variable ml is the counter to indicate how many Ajax query we have perform. Variable outputdata is an array to store the result of OData query.

  1. var outputdata = [];
  2.   oModel = new sap.ui.model.json.JSONModel();
  3.   oModel.setSizeLimit(500);
  4.   console.log(menu.length);
  5.   var ml = 0;
  6.   var rund = true;

Create a function timeout() to check if all Ajax queries have been completed. How do we know? by checking if the menu length is the same as the variable ml. Once it has been completed, put the outpdata into a JSON model and exit the loop.

  1. function timeout() {
  2.   function run() {
  3.          if(menu.length==ml && rund == true){
  4.           rund = false;
  5.           console.log(“Done”);
  6.           clearInterval(myVar);
  7.   oModel.setData({
  8.   modelData:outputdata
  9.          });
  10.   oController.getView().setBusy(false);
  11.   console.log(oModel);
  12.          }
  13.      }
  14.   var myVar = setInterval(run, 10);
  15.   }

Set Busy dialog and run the timeout() function:

  1. oController.getView().setBusy(true);
  2.   timeout();

Perform a loop based on the total number of query in menu[a].

  1. for (a in menu ) {
  2.   $.when(
  3.   $.get(“/sap/opu/odata/sap/ZGW_SRV/lpadSet?&$filter=ActivityGroup%20eq%20′” + menu[a] + “‘&$format=json”)
  4.       .success(function(data) {
  5.       for (z=0; z<data.d.results.length; z++){
  6.       outputdata.push({ActivityGroup: data.d.results[z].ActivityGroup, Description: data.d.results[z].Description, Menulevel: data.d.results[z].Menulevel, NodeType: data.d.results[z].NodeType, ObjectId: data.d.results[z].ObjectId, PDFUrl: data.d.results[z].PDFUrl, ParentId: data.d.results[z].ParentId, Text: data.d.results[z].Text, Url: data.d.results[z].Url });
  7.       }
  8.       ml = ml + 1;
  9.       })
  10.       .error(function(jqXHR, textStatus, errorThrown) {
  11.       })
  12.   ).fail(function() {
  13.   ml = ml + 1;
  14.   }
  15.   ).done(function() {
  16.   });
  17.   }

We perform the Ajax “Get” query. If is success, under success function we put the result in outputdata array and increase the counter of variable ml.

  1. .success(function(data) {
  2.       for (z=0; z<data.d.results.length; z++){
  3.            outputdata.push({ActivityGroup: data.d.results[z].ActivityGroup, Description: data.d.results[z].Description, Menulevel: data.d.results[z].Menulevel, NodeType: data.d.results[z].NodeType, ObjectId: data.d.results[z].ObjectId, PDFUrl: data.d.results[z].PDFUrl, ParentId: data.d.results[z].ParentId, Text: data.d.results[z].Text, Url: data.d.results[z].Url });
  4.       }
  5.       ml = ml + 1;
  6.       })

If fail to get the result, we also increase the counter of variable ml.

  1. ).fail(function() {
  2.        ml = ml + 1;
  3.   }

Let’s test it out.

In the below example, the total number of query is 7. I have disabled 6 so only 1 is working. We managed to captured all the information and stored in JSON model: modelData.

blog1.jpg

That’s it for now. Thanks for reading my blog and let me know if you have any comments.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s