KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transactions > orders > SearchOrders


1 package transactions.orders;
2
3 import dinamica.*;
4
5 /**
6  * Build a complex select query and retrieve the recordset.<br>
7  * This class also takes care of retrieving the last filter recordset
8  * from the session and refresh its values using the current input parameters,
9  * so that when the filter is displayed again it will show the last
10  * search parameters used.
11  * <br><br>
12  * Creation date: 25/02/2004<br>
13  * Last Update: 25/02/2004<br>
14  * (c) 2004 Martin Cordova<br>
15  * This code is released under the LGPL license<br>
16  * @author Martin Cordova (dinamica@martincordova.com)
17  * */

18 public class SearchOrders extends GenericTransaction
19 {
20
21     /**
22      * Returns 1 if no records are found to satisfy the search criteria,
23      * otherwise returns 0.
24      */

25     public int service(Recordset inputs) throws Throwable JavaDoc
26     {
27
28         //create any auto-recordsets defined in config.xml
29
int rc = super.service(inputs);
30         
31         //retrieve filter recordset from session
32
//and copy input values to restore filter state later
33
Recordset filter = (Recordset)getSession().getAttribute("orders.filter");
34         inputs.copyValues(filter);
35         
36         /* start query assembling logic */
37         
38         //load base query
39
String JavaDoc qBase = getResource("query-base.sql");
40
41         //the "orderby" and "sortmode" fields must be replaced by hand
42
//because they are not regular field markers - the getSQL()
43
//method won't handle this case
44
String JavaDoc orderby = inputs.getString("orderby");
45         String JavaDoc sortmode = inputs.getString("sortmode");
46         qBase = StringUtil.replace(qBase,"${orderby}", orderby);
47         qBase = StringUtil.replace(qBase,"${sortmode}", sortmode);
48         
49         //variable where clause
50
StringBuffer JavaDoc qFilter = new StringBuffer JavaDoc();
51         
52         //we will use generic logic to read
53
//all the applicable clauses
54
String JavaDoc[] params = {
55                             "customerid",
56                             "productid",
57                             "dfrom",
58                             "dto",
59                             "total"
60                           };
61         
62         for (int i=0;i<params.length;i++)
63         {
64             if (inputs.getValue(params[i])!=null)
65                 qFilter.append(getResource("clause-" + params[i]+ ".sql"));
66         }
67
68         //retrieve the assembled filter
69
String JavaDoc where = qFilter.toString();
70         
71         //the "operator" field must be replaced by hand
72
//because it's not a normal field value - the getSQL()
73
//method won't handle properly this case
74
String JavaDoc operator = inputs.getString("operator");
75         where = StringUtil.replace(where,"${operator}", operator);
76
77         //place the where clause into the base query
78
qBase = StringUtil.replace(qBase,"${filter}", where);
79
80         /* end of query assembling logic */
81         
82         //generate SQL with the filter values
83
String JavaDoc sql = getSQL(qBase, inputs);
84         
85         //execute query
86
Db db = getDb();
87         Recordset rs = db.get(sql);
88         
89         //set return value for page flow
90
if (rs.getRecordCount()>0)
91         {
92             //publish recordset or store in session
93
//if you are going to paginate results using another Action
94
getSession().setAttribute("orders.results", rs);
95             rc = 0;
96         }
97         else
98         {
99             rc = 1;
100         }
101         
102         return rc;
103         
104     }
105
106 }
107
Popular Tags