KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > validators > NoRecordsFound


1 package dinamica.validators;
2
3 import java.util.HashMap JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import dinamica.*;
6
7 /**
8  * This validator returns FALSE if no rows are returned
9  * by a given SQL query. If at least one row is returned
10  * then the validator returns TRUE.
11  * <br><br>
12  * Rrequires the following custom attributes:<br>
13  * <ul>
14  * <li> sql: query to find any related record. You may use field makers
15  * that will be replaced by the corresponding request parameters passed
16  * as a recordset to the isValid method.
17  * </ul>
18  *
19  * (c) 2004 Martin Cordova<br>
20  * This code is released under the LGPL license<br>
21  * Dinamica Framework - http://www.martincordova.com<br>
22  * @author Martin Cordova (dinamica@martincordova.com)
23  * */

24 public class NoRecordsFound extends AbstractValidator
25 {
26
27     /* (non-Javadoc)
28      * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
29      */

30     public boolean isValid(
31         HttpServletRequest JavaDoc req,
32         Recordset inputParams,
33         HashMap JavaDoc attribs)
34         throws Throwable JavaDoc
35     {
36
37         boolean flag = true;
38         
39         //get db channel
40
Db db = getDb();
41
42         //detect if sql parameter was passed to the validator
43
boolean bSql = attribs.containsKey("sql");
44
45         if (!bSql)
46         {
47
48             throw new Throwable JavaDoc("[" + this.getClass().getName() + "] Missing attribute [sql] in validator.xml");
49
50         }
51         else
52         {
53
54             //read config
55
String JavaDoc query = (String JavaDoc)attribs.get("sql");
56             
57             //load template and replace parameter values
58
String JavaDoc sql = getResource(query);
59             sql = getSQL(sql, inputParams);
60
61             //execute sql
62
Recordset rs = db.get(sql);
63             if (rs.getRecordCount()==0)
64                 flag = false;
65
66         }
67         
68         return flag;
69
70     }
71
72 }
73
Popular Tags