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.7 Obtaining Sorted Data

Sometimes it is useful to be able to sort the results of a search, using a particular element as a sort key.

To obtain sorted partial information about the data, use the setSort method.

The following diagram shows the process for obtaining data after it has been sorted.

[Figure 9-9 Flow of the Process for Obtaining Data after It Has Been Sorted]

When search results are sorted, the sort process looks up all of the XML documents that match the search conditions. To improve response performance, it is important to narrow down the data to be sorted by specifying a search expression that produces an appropriate number of hits. To find out how many XML documents match the search conditions before starting a sort process, specify 0 in the requestCount parameter (the number of items to return per request) of the setRequest method.

mark1Sample Code

ShunConnection con = new ShunConnection();

String queryExpression = "/document/base/prefecture == 'Sydney'";
String returnExpression = "/document/base/name, /document/base/price";
String sortExpression = "val(/document/base/price/text()) DESC";                      (1)
ShunPreparedStatement pstmt = con.prepareSearch(queryExpression, returnExpression);   (2)
pstmt.setSort(sortExpression);                                                        (3)
ShunResultSet rs = pstmt.executeSearch();                                             (4)
 while(rs.next()) {                                                                   (5)
  System.out.println("[Search results] = " + rs.getString());                         (5)
}
rs.close();                                                                           (6)
pstmt.close();                                                                        (6)

con.close();

mark2(1) Create a Sort Expression

Create a sort expression.

Refer to B.5 Sort Expressions for more information on sort expressions.

mark2(2) Create a ShunPreparedStatement Object

Specify a search expression and a return expression with the prepareSearch method () to create a ShunPreparedStatement object.

Refer to Appendix B Format of Search, Return and Sort Expressions for more information on search expressions and return expressions.

mark2(3) Set the Sort Expression

Use the setSort method to set the sort expression

The total length of the sort keys specified in the sort expression determines the number of data items that can be returned. A maximum of 1,000 items can be returned. No more data can be returned even if a higher value is specified for the reply start number or the number of items to return per request. Use the getReturnableCount method to obtain the maximum number of data items that can be returned.
Appendix C Allowable Values provides a guide to the key lengths and the number of data items that can be returned.

Consecutive data can be extracted by specifying an acquisition start position. This can be used to extract upwards of 1,000 items of data. Refer to 9.3.2.8 Obtaining Consecutive Groups of XML Documents that Match Conditions for details.

mark2(4) Execute the Search (Create a ShunResultSet Object)

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

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.

Different methods can be used to extract XML documents for different objectives. The following table shows the methods that can be used.

[Table 9-5 Methods that Can be Used to Extract XML Documents]

Method

Function

getString

Extracts XML documents as String objects.

getStringArray

Extracts XML documents as a two-dimensional array of String objects.

getStream

Extracts XML documents as InputStream objects.

Note. The getStringArray method is an effective way to extract search results in text format.

The getRecordID method can be used to obtain record IDs (which uniquely identify each data item) in addition to the results of the search. Record IDs are used to extract or delete the corresponding entire XML documents.

mark2(6) 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

The following is sample code for a business class that uses a Shunsaku access class created with the Studio interactive wizard, and a prototype Shunsaku access class that is used by the business class.

Business Class

ShunConnection con = new ShunConnection();

ShunsakuAccessController controller = new ShunsakuAccessController(con);
controller.search(key);
Object[] resultData = 
            controller.getResultData(ShunsakuAccessController.SORT_1);
controller.close();

con.close();

ShunPreparedStatement Object

public class ShunsakuAccessController {
    :
   private ShunPreparedStatement pstmt;
    :
}

search Method of Shunsaku Access Class

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

private void getPreparedStatement(String queryString, String returnString) {
    :
   pstmt = con.preparedSearch(queryString, returnString);                                   (2)
    :
}
(a) Search Expression
This expression is defined using the interactive wizard. The interactive wizard can also be used to create complex search expressions.
(b) Return Expression
"/" is specified here to extract the entire XML document.

getResultData Method of Shunsaku Access Class

public Object[] getResultData(String sortString) throws ShunException {
    :
   resultSet = getResultSet(sortString);
    :
   resultData = parseResultSet(resultSet);                                 (5)
    :
   resultSet.close();                                                      (6)
}

public ShunResultSet getResultSet(String sortString) throws ShunException {
   return getResultSet(sortString, 1, Integer.MAX_VALUE);
}

public ShunResultSet getResultSet(String sortString,
                                  int position,
                                  int requestCount)
                                  throws ShunException {
    :
  if(!sortString.equals(SORT_NONE)) {
      pstmt.setSort(sortString);                                           (3)
   }
    :
   return pstmt.executeSearch();                                           (4)
    :
}

Sort Expression

The sort expression is created using the interactive wizard, and the sort identifier is defined as a constant of the Shunsaku access class.

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

close Method of Shunsaku Access Class

public void close() throws ShunException {
    :
   pstmt.close();                                                (6)
    :
}

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006