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 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.
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.
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(); |
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 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.
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. |
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.
Use the close methods of the ShunResultSet object and the ShunPreparedStatement object to release these objects when they are no longer required.
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.
ShunConnection con = new ShunConnection(); ShunsakuAccessController controller = new ShunsakuAccessController(con); controller.search(key); Object[] resultData = controller.getResultData(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 = "/"; (b) : getPreparedStatement(queryString, returnString); : } private void getPreparedStatement(String queryString, String returnString) { : pstmt = con.preparedSearch(queryString, returnString); (2) : } |
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) : } |
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) : } |
public void close() throws ShunException { : pstmt.close(); (6) : } |
Contents
Index
![]() ![]() |