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.9 Aggregating the Content of Data that Matches Search Conditions

Sometimes it is useful to be able to aggregate the results of a search, using the values of particular elements.

Use the setSort method to aggregate the content of the data. By specifying an aggregation function in the return expression used as a parameter of the prepareSearch method, the results of the search will be aggregated before they are returned. The aggregation process can be used to calculate totals, averages, maximums, minimums, and the number of items.

The following diagram shows the process for aggregating the content of data that matches conditions.

[Figure 9-11 Flow of the Process for Aggregating the Content of the Data that Matches Search Conditions]

mark1Sample Code

ShunConnection con = new ShunConnection();

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

con.close();

mark2(1) Create a Return Expression

Create a return expression. To aggregate the search results, specify an aggregation function in the return expression.

Refer to B.4 Return Expressions more information on how to specify return expressions and aggregation functions.

mark2(2) Create a Sort Expression

Create a sort expression.

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

mark2(3) 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(4) 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 groups that can be returned. The maximum number of groups is 1,000. 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 groups that can be returned.

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

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

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

To accomplish this process, specify an aggregation function in the return expression of the Shunsaku access class that has been generated automatically. The following examples show how to specify a Shunsaku access class and a business class that contain a return expression where an aggregation function has been specified.

Business Class

ShunConnection con = new ShunConnection();

ShunsakuAccessController controller = new ShunsakuAccessController(con);
controller.search(key);
ShunResultSet resultData = controller.getResultSet(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 = "max(/document/base/price/text())";                                      (1)(b)
    :
   getPreparedStatement(queryString, returnString);
    :
}

private void getPreparedStatement(String queryString, String returnString) {
    :
   pstmt = con.preparedSearch(queryString, returnString);                                    (3)
    :
}
(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
Specify an aggregation function.

getResultData Method of Shunsaku Access Class

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);                                            (4)
   }
    :
   return pstmt.executeSearch();                                            (5)
    :
}

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 = "";                                       (2)
   public static final String SORT_1 = "val(/document/base/prefecture/text())";     (2)
    :
}

close Method of Shunsaku Access Class

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

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006