Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX - |
Contents
Index
![]() ![]() |
Part 2 Developing Applications by APIs | > Chapter 9 Developing Java Applications | > 9.3 How to Use Java APIs | > 9.3.2 Data Searches |
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.
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(); |
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.
Create a sort expression.
Refer to B.5 Sort Expressions for more information on sort expressions.
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.
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.
Execute the search using the executeSearch method. A ShunResultSet object will be created to hold 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.
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. |
Use the close methods of the ShunResultSet object and the ShunPreparedStatement object to release these objects when they are no longer required.
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.
ShunConnection con = new ShunConnection(); ShunsakuAccessController controller = new ShunsakuAccessController(con); controller.search(key); ShunResultSet resultData = controller.getResultSet(ShunsakuAccessController.SORT_1); controller.close(); con.close(); |
public class ShunsakuAccessController { : private ShunPreparedStatement pstmt; : } |
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) : } |
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) : } |
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) : } |
public void close() throws ShunException { : pstmt.close(); (7) : } |
Contents
Index
![]() ![]() |