01: package dinamica.validators;
02:
03: import java.util.HashMap;
04: import javax.servlet.http.HttpServletRequest;
05: import dinamica.*;
06:
07: /**
08: * This validator can be used to detect if there are
09: * 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: /* (non-Javadoc)
30: * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
31: */
32: public boolean isValid(HttpServletRequest req,
33: Recordset inputParams, HashMap<String, String> attribs)
34: throws Throwable {
35:
36: boolean flag = true;
37:
38: //get db channel
39: Db db = getDb();
40:
41: //detect if sql parameter was passed to the validator
42: boolean bSql = attribs.containsKey("sql");
43:
44: if (!bSql) {
45:
46: throw new Throwable("[" + this .getClass().getName()
47: + "] Missing attribute [sql] in validator.xml");
48:
49: } else {
50:
51: //read config
52: String query = (String) attribs.get("sql");
53:
54: //load template and replace parameter values
55: String sql = getResource(query);
56: sql = getSQL(sql, inputParams);
57:
58: //execute sql
59: Recordset rs = db.get(sql);
60: if (rs.getRecordCount() > 0)
61: flag = false;
62:
63: }
64:
65: return flag;
66:
67: }
68:
69: }
|