KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > validators > CanDeleteRecord


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 can be used to detect if there are
9  * any related records in another table before trying to
10  * delete a certain record. Returns TRUE if there are no
11  * related records meaning that the record can be deleted.
12  * You will need to call this validator for every referential
13  * integrity check before deleting a record.
14  * <br><br>
15  * Rrequires the following custom attributes:<br>
16  * <ul>
17  * <li> sql: query to find any related record. You may use field makers
18  * that will be replaced by the corresponding request parameters passed
19  * as a recordset to the isValid method.
20  * </ul>
21  *
22  * (c) 2004 Martin Cordova<br>
23  * This code is released under the LGPL license<br>
24  * Dinamica Framework - http://www.martincordova.com
25  * @author Martin Cordova (dinamica@martincordova.com)
26  * */

27 public class CanDeleteRecord extends AbstractValidator
28 {
29
30     /* (non-Javadoc)
31      * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
32      */

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