Skip to Content
Author's profile photo Former Member

Pagination of MDM search result in Java

Problem:

SAP MDM is a repository which can hold a huge and complex data. So, searching the repository and retrieving data from the table is a heavy task. We have the SAP provided MDM APIs in place to carry out the search and return the result. But, when the result is huge, it is a risk to increase the page size according to the response result.

For example, if the search result is expected to return 10K values and for which we have increased the page size to 10k, then there is chance that the system goes down or the system will encounter an delayin response time. Thus to overcome this situation we will have the paging concept implemented here, while retrieving we just need to mention the below code snippet.

Solution:

Before retrieving the data set the Page size.

Note: Choose wisely according to the requirement (Maximum Page Size = 1000).

//Retrieve data

RetrieveLimitedRecordsExCommand getDataCommad = new RetrieveLimitedRecordsExCommand(<UserSessionContext_instance>);

getDataCommad.setResultDefinition(<ResultDefinition_instance>);

getDataCommad.setPageSize(<PageSize_int>)

getDataCommad.execute();

//Number of records the Search returned

int resultCount = getDataCommad.getSearchTableMatchCount();

//Size of the page

int pageSize = getDataCommad.getPageSize();

//Total pages

int totalPages = resultCount/pageSize;

// if there are 1030 records then totalPages = 1, while there are 2 pages 

// when Page Size is 1000.

// Hence a Modulo would help us to know if there is an extra page

int hadExtraValues  = resultCount%pageSize;

         

if(hadExtraValues != 0)

{

totalPages++; // if there is an extra page then the var:totalPages is increased by 1

}

Record[] tempRecord = null;

int arraySize = 0;

//int page Will start from 1 as the getDataCommad.execute(); will return the //records in page  = 0.

for(int page=1; page<= totalPages; page++)

{

//TODO – Code to manipulate with the records in Page = page.

      getDataCommad.setPageIndex(page)//Set the next page Index

      getDataCommad.execute();

}

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jun Wu
      Jun Wu

      do you have real example?  how you use it in your application?

      this is too general... not much useful

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      There is an example stated in the post... was that not useful?

      The code written there needs to be executed to get each pages result. .

      Or is that you want a real time scenario?

      Author's profile photo Jun Wu
      Jun Wu

      tell me how you use it to paginate

      let's say you displayed first 1000 in the screen, how you get the next 1000?

      if you really did it, tell me exactly what you have done. showing us an api call is nothing.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      I will try to explain little more simply,


      Here the whole idea of the pagination is on the MDM search result.


      For every search to the MDM, you can set the size of the PAGE using the (command: .setPageSize(<PageSize_int>)}.

      Page Size is the number of records that the search result should return in each page.

      So, taking your case into consideration, if we have 10000 records as the result of our search and we have adjusted the Page size to 1000(default).

      In this case, after the first execution {command : getDataCommad.execute()} you can get the values{command : objExCommand_allData.getRecords()[returns the list of records which satisfies the search criteria]} which are stored in the PAGE = 0.

      //Number of records the Search returned

      int resultCount = getDataCommad.getSearchTableMatchCount();

      //Size of the page

      int pageSize = getDataCommad.getPageSize();

      //Total pages

      int totalPages = resultCount/pageSize;

      // if there are 1030 records then totalPages = 1, while there are 2 pages

      // when Page Size is 1000.

      // Hence a Modulo would help us to know if there is an extra page

      int hadExtraValues = resultCount%pageSize;

      These variables helps us in iterating through the pages(Explanation provided in green)

      Now that we have retrieved the data from Page = 0, to retrieve the data from the next page,

      Set the PageIndex of the next page and {command: getDataCommad.setPageIndex(page)} and then execute { command: getDataCommad.execute();}  the search ones again.

      This way, you can iterate through the different pages and retrieve the entire data.

      for(int page=1; page<= totalPages; page++)

      {

      //TODO - Code to manipulate with the records in Page = page.

      getDataCommad.setPageIndex(page)//Set the next page Index

      getDataCommad.execute();

      }

      Hope this answers your question.

      Thanks and Regards

      Author's profile photo Manfred Klein
      Manfred Klein

       

      Hello Guys,

       

      don't know if any of you still can benefit from that, but here is how I adressed the paging issue:

       

      UserSessionContext                           sessionCtx   = null;
      ResultDefinitionEx                               resDef    = null;
      ResultDefinitionEx                               supportedResDef[] = null;
      RetrieveLimitedRecordsExCommand cmd     = null;
      RecordEx                                             products[]   = null;
      Search                                                 search    = null;
      int                                                        pageSize   = 500; //adjust as needed
      int                                                        counter    = 0;

      //retrieve User Session Context for instance with UserSessionContext(str1, str2, str3, str4)
      //build Result Definition resDef according to your table
      //retrieve search and supportedResDef as needed

      cmd = new RetrieveLimitedRecordsExCommand(sessionCtx);

      cmd.setSearch(search);                 //optional
      cmd.setResultDefinition(resDef);   //mandatory
      cmd.setSupportingResultDefinitions(supportedResDef);   //optional
      cmd.setRegionalLayer(RegionalLayer.ALL);                     //optional
      cmd.setPageSize(pageSize);                                            //optional
      cmd.execute();
      System.out.println("First batch of products loaded. Amount: "+cmd.getRecords().getCount());

      products = new RecordEx[cmd.getRecords().getCount()];
      for(int i=0; i<products.length; i++)
      {
      products[i] = (RecordEx)cmd.getRecords().getRecord(i);
      }

      //prepare anything you need for data records processing here

      counter = 0;
      while(products.length>0)  //infinity loop...
      {
      counter++;
      if(counter>100)    //...with emergency break
      {
      System.out.println("ALARM: Emergency break");
      break;
      }

      //commence processing here

      //prepare next batch of records
      products = null;
      cmd.setPageIndex(cmd.getPageIndex()+1);  //increment page index
      cmd.execute();
      products = new RecordEx[cmd.getRecords().getCount()];
      for(int i=0; i<products.length; i++)
      {
      products[i] = (RecordEx)cmd.getRecords().getRecord(i);
      }

      }

       

      with this approach you don't need to determine the count of pages up front.

       

      Have fun and regards