KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > validators > DuplicatedKeyValidator


1 package dinamica.validators;
2
3 import java.util.HashMap JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import dinamica.*;
6
7 /**
8  * Generic Validator to test if a record key is duplicated,
9  * requires the following custom attributes:<br>
10  * <ul>
11  * <li> sql: query to find any related record. You may use field makers
12  * that will be replaced by the corresponding request parameters passed
13  * as a recordset to the isValid method.
14  * </ul>
15  * <br>If you don't use the SQL query you may use the
16  * attributes shown below (not recommended).
17  * <ul>
18  * <li> table: name of the search table.
19  * <li> column: name of the search column, also the
20  * name of the parameter whose value will be used.
21  * <li> varchar: true|false indicates if the column is of type varchar or alike,
22  * this is used to demarcate the column search value with the proper delimiter, if any.
23  * </ul>
24  * <br>
25  * Works only for text or numeric values, dates not allowed.
26  * <br><br>
27  * Creation date: 10/feb/2004<br>
28  * Last Update: 10/feb/2004<br>
29  * (c) 2004 Martin Cordova<br>
30  * This code is released under the LGPL license<br>
31  * @author Martin Cordova (dinamica@martincordova.com)
32  * */

33
34 public class DuplicatedKeyValidator extends AbstractValidator
35 {
36
37     /* (non-Javadoc)
38      * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
39      */

40     public boolean isValid(
41         HttpServletRequest JavaDoc req,
42         Recordset inputParams,
43         HashMap JavaDoc attribs)
44         throws Throwable JavaDoc
45     {
46         
47         boolean flag = true;
48         
49         //get db channel
50
Db db = getDb();
51
52         //detect if sql parameter was passed to the validator
53
boolean bSql = attribs.containsKey("sql");
54
55         String JavaDoc sql = "";
56
57         if (!bSql)
58         {
59             //validate plugin configuration
60
boolean b1 = attribs.containsKey("table");
61             if (!b1)
62                 throw new Throwable JavaDoc("Bad configuration - 'table' attribute not found.");
63             boolean b2 = attribs.containsKey("column");
64             if (!b2)
65                 throw new Throwable JavaDoc("Bad configuration - 'column' attribute not found.");
66             boolean b3 = attribs.containsKey("varchar");
67             if (!b3)
68                 throw new Throwable JavaDoc("Bad configuration - 'varchar' attribute not found.");
69     
70             //define value enclosing character (quote or nothing)
71
String JavaDoc delim = "";
72             String JavaDoc isVarchar = (String JavaDoc)attribs.get("varchar");
73             if (isVarchar.equals("true"))
74                 delim = "'";
75     
76             //get db parameters
77
String JavaDoc table = (String JavaDoc)attribs.get("table");
78             String JavaDoc col = (String JavaDoc)attribs.get("column");
79     
80             //get input value
81
String JavaDoc value = String.valueOf(inputParams.getValue(col));
82     
83             //build sql
84
sql = "select " + col + " from " + table + " where " + col + " = " + delim + value + delim;
85         }
86         else
87         {
88             String JavaDoc query = (String JavaDoc)attribs.get("sql");
89             sql = getResource(query);
90             sql = getSQL(sql, inputParams);
91         }
92         
93         //execute sql
94
Recordset rs = db.get(sql);
95         if (rs.getRecordCount()>0)
96             flag = false;
97
98         return flag;
99         
100     }
101
102 }
103
Popular Tags