01: package dinamica.validators;
02:
03: import java.util.HashMap;
04: import javax.servlet.http.HttpServletRequest;
05: import dinamica.*;
06:
07: /**
08: * Generic Validator to test if a record key is duplicated,
09: * 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: /* (non-Javadoc)
37: * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
38: */
39: public boolean isValid(HttpServletRequest req,
40: Recordset inputParams, HashMap<String, String> attribs)
41: throws Throwable {
42:
43: boolean flag = true;
44:
45: //get db channel
46: Db db = getDb();
47:
48: //detect if sql parameter was passed to the validator
49: boolean bSql = attribs.containsKey("sql");
50:
51: String sql = "";
52:
53: if (!bSql) {
54: //validate plugin configuration
55: boolean b1 = attribs.containsKey("table");
56: if (!b1)
57: throw new Throwable(
58: "Bad configuration - 'table' attribute not found.");
59: boolean b2 = attribs.containsKey("column");
60: if (!b2)
61: throw new Throwable(
62: "Bad configuration - 'column' attribute not found.");
63: boolean b3 = attribs.containsKey("varchar");
64: if (!b3)
65: throw new Throwable(
66: "Bad configuration - 'varchar' attribute not found.");
67:
68: //define value enclosing character (quote or nothing)
69: String delim = "";
70: String isVarchar = (String) attribs.get("varchar");
71: if (isVarchar.equals("true"))
72: delim = "'";
73:
74: //get db parameters
75: String table = (String) attribs.get("table");
76: String col = (String) attribs.get("column");
77:
78: //get input value
79: String value = String.valueOf(inputParams.getValue(col));
80:
81: //build sql
82: sql = "select " + col + " from " + table + " where " + col
83: + " = " + delim + value + delim;
84: } else {
85: String query = (String) attribs.get("sql");
86: sql = getResource(query);
87: sql = getSQL(sql, inputParams);
88: }
89:
90: //execute sql
91: Recordset rs = db.get(sql);
92: if (rs.getRecordCount() > 0)
93: flag = false;
94:
95: return flag;
96:
97: }
98:
99: }
|