Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX -
Contents Index PreviousNext

Part 2 Developing Applications by APIs> Chapter 9 Developing Java Applications> 9.3 How to Use Java APIs> 9.3.2 Data Searches

9.3.2.8 Obtaining Consecutive Groups of XML Documents that Match Conditions

It is often desirable for a Web application to be able to divide up search results into a fixed number of items and to obtain these as consecutive groups.

XML documents can be obtained consecutively by specifying the last position information or the first position information obtained by the preceding search process as the acquisition start position or acquisition end position parameter of the setRequest method.

The following diagram shows the process for obtaining consecutive groups XML documents that match conditions.

[Figure 9-10 Flow of the Process for Obtaining XML Documents that Match Conditions Consecutively]

mark1Sample Code

String queryExpression = "/document/base/prefecture == 'Sydney'";
String returnExpression = "/document/base/name, /document/base/price";
String sortExpression = "val(/document/base/price/text()) DESC";

ShunConnection con = new ShunConnection();
ShunPreparedStatement pstmt = con.prepareSearch(queryExpression, returnExpression);
pstmt.setSort(sortExpression);

// Extraction direction
int direction = 0;
// First position information
String sFirstPos = null;
// Last position information
String sLastPos = null;

// Change the setRequest settings according to the page to be displayed
// To display the next page, set the direction to "NEXT". To display the previous page, set the direction to "PREV"
if(direction == NEXT) {
  // Display the next page                                               (2)
  pstmt.setRequest(sLastPos, 30, pstmt.SHUN_DIRECTION_FORWARD_EXCLUSIVE);
}else if(direction == PREV) {
  // Display the previous page                                           (3)
  pstmt.setRequest(sFirstPos, 30, pstmt.SHUN_DIRECTION_BACKWARD_EXCLUSIVE);
}else{
  // Display the first page                                              (1)
  pstmt.setRequest(null, 30, 0);
}

// Execute the search                                                    (4)
ShunResultSet rs = pstmt.executeSearch();

// Extract the results of the search                                     (5)
System.out.println("[Number of hits] = " + rs.getHitCount());
while(rs.next()) {
  System.out.println("[Search results] = " + rs.getString());
}

// Store the first position information and last position information    (6)
sFirstPos = rs.getFirstPosition();
sLastPos = rs.getLastPosition();

// Release ShunResultSet object                                          (7)
// Release ShunPreparedStatement object
rs.close();
pstmt.close();

con.close();

mark2(1) Display the First Page

Specify the data acquisition position and the number of items to return per request with the setRequest method. Search processing that displays the first page will start extracting data from the first item, so specify null for the data acquisition position (the position of the first or last data item to be acquired). If null is specified for the data acquisition position, data will be extracted from the first item.

mark2(2) Display the Next Page

Specify the data acquisition position, the number of items to return per request and the extraction direction with the setRequest method. For search processing that displays the next page, use the getLastPosition method to obtain the last position information from the previous search, and then specify this position information as the data acquisition position (the acquisition start position). This specification makes it possible to obtain the data that follows the previously acquired data.

mark2(3) Display the Previous Page

Specify the data acquisition position, the number of items to return per request and the extraction direction with the setRequest method. For search processing that displays the previous page, use the getFirstPosition method to obtain the first position information from the previous search, and then specify this position information as the data acquisition position (the acquisition end position). This specification makes it possible to obtain the data that precedes the previously acquired data.

mark2(4) Execute the Search

Execute the search using the executeSearch method. A ShunResultSet object will be created to hold the results of the search.

mark2(5) Extract the Results of the Search

  1. Always invoke the next method before extracting the results of the search. The next method returns "true" if there is still more data that can be extracted and "false" otherwise.
  2. Use the getString method to extract XML documents.

mark2(6) Store the first position information and last position information

  1. Store the first position information using the getFirstPosition method.
  2. Store the last position information using the getLastPosition method.

mark2(7) Release the ShunResultSet Object and the ShunPreparedStatement Object

Use the close methods of the ShunResultSet object and the ShunPreparedStatement object to release these objects when they are no longer required.

mark1Sample Codes for Studio

Business Class

// Search data on next page
public void next(DispatchContext context, shunProduct.ShunProductBean 
dataBean) {
  execute(context, dataBean, ShunAccessController.FORWARD);
 }
// Search data on previous page
 public void back(DispatchContext context, shunProduct.ShunProductBean dataBean) {
  execute(context, dataBean, ShunAccessController.BACKWARD);
 }
// Execute search 
private void execute(DispatchContext context, shunProduct.ShunProductBean dataBean, int direction)
 {
  String key = dataBean.getKey();
  ShunConnection connection = null;
  ShunsakuAccessController accessController = null;
       :
  // Connect to Shunsaku server
   connection = new ShunConnection("localhost", 33101);
   accessController = new ShunsakuAccessController(connection);


   // Prepare for search
   accessController.search(key);
   // Obtain data acquisition position
   String dataPosition = null;
   if(direction == ShunsakuAccessController.FORWARD)
   {
    dataPosition = dataBean.getLastPosition();
   }
   else if(direction == ShunsakuAccessController.BACKWARD)
   {
    dataPosition = dataBean.getFirstPosition();
   }
   // Execute search
   Object[] resultData = accessController.getResultData(ShunAccessController.SORT_1, dataPosition, 30, direction);
   int hit = resultData.length;
   // Set number of hits, acquisition start position and acquisition end position in data Bean
   dataBean.setHitCount(hit);
   dataBean.setFirstPosition(accessController.getFirstPosition());
dataBean.setLastPosition(accessController.getLastPosition());
   accessController.close();
   connection.close(); 
    :
   // Create and set result data and transfer to next page
   ShunProductListModel listData = new ShunProductListModel(resultData);
   DataBean.setResultList(listData);
   DataBean.setVerb("output");
   Context.setResponseBean("body", dataBean);
   :
}

Sort Expression

public class ShunsakuAccessController {
    :
   public static final String SORT_NONE = ""l;
   public static final String SORT_1 = "val(/document/base/price/text()) DESC";
    :
}

Shunsaku Access Class

public void search(String keys) throws ShunException {
    :
   queryString = "/document/base/city == '" + keys + "'";
   returnString = "/";
    :
   getPreparedStatement(queryString, returnString);
    :
}

private void getPreparedStatement(String queryString, String returnString)
{
    :
   pstmt = con.preparedSearch(queryString, returnString);
    :
}

 public Object[] getResultData(String sortString, String dataPosition, int requestCount, int direction)throws ShunException 

 {
  ShunResultSet resultSet;
  Object[] resultData = null;
  // Execute search
  resultSet = getResultSet(sortString, dataPosition,requestCount,direction);
  this.lastPosition = resultSet.getLastPosition();
  this.firstPosition = resultSet.getFirstPosition();
    :
   resultData = parseResultSet(resultSet);
    :
  return resultData;
 }

 public ShunResultSet getResultSet(String sortString, String dataPosition, int requestCount, int direction)throws ShunException
 {
  ShunResultSet resultSet;
  Object[] resultData = null;

  // Specify sort expression
  if (!sortString.equals(SORT_NON)) {
   this.fPreparedStatement.setSort(sortString);
  }

  // Set number of items to return per request
  this.fPreparedStatement.setRequest(dataPosition, requestCount, direction);

  // Execute search and create ShunResultSet object
  return this.fPreparedStatement.executeSearch();
 }
 public String getFirstPosition()
 {
  return firstPosition;
 }
 public String getLastPosition()
 {
  return lastPosition;
 }

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006