KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > validators > SQLPatternTransformer


1 package dinamica.validators;
2
3 import java.util.HashMap JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import dinamica.*;
6
7
8 /**
9  * This validator transforms the value (if not null) of the corresponding
10  * parameter, by appending a '%' at the end of the text or by enclosing the
11  * the value between '%' characters.<br>
12  * It is used to create search patterns for SQL queries like 'value starts with..' that can
13  * be used in sql LIKE expressions. Always returns TRUE.<br>
14  * As a preventtive measure, all occurrences of the '%' character in the parameter's value
15  * will be erased before applying the transformation.
16  * <br><br>
17  * Requires the following custom attributes:<br>
18  * <ul>
19  * <li> parameter: Name of the request parameter to transform. This parameter
20  * MUST be defined in validator.xml and must be of type VARCHAR.
21  * <li> rule: a string that denotes the possible transformation, accepted
22  * values are 'like' and 'contains'.
23  * </ul>
24  *
25  * (c) 2005 Martin Cordova<br>
26  * This code is released under the LGPL license<br>
27  * Dinamica Framework - http://www.martincordova.com<br>
28  * @author Martin Cordova (dinamica@martincordova.com)
29  * */

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

35     public boolean isValid(HttpServletRequest JavaDoc req, Recordset inputParams,
36             HashMap JavaDoc attribs) throws Throwable JavaDoc {
37
38         boolean bParam = attribs.containsKey("parameter");
39         if (!bParam)
40             throw new Throwable JavaDoc("[" + this.getClass().getName() + "] Missing attribute [parameter] in validator.xml");
41         
42         boolean bRule = attribs.containsKey("rule");
43         if (!bRule)
44             throw new Throwable JavaDoc("[" + this.getClass().getName() + "] Missing attribute [rule] in validator.xml");
45         
46         String JavaDoc rule = (String JavaDoc)attribs.get("rule");
47         
48         if (!rule.equalsIgnoreCase("like") && !rule.equalsIgnoreCase("contains"))
49             throw new Throwable JavaDoc("[" + this.getClass().getName() + "] Invalid attribute value [rule] in validator.xml: " + rule + " - Accepted values are 'like' or 'contains'");
50         
51         String JavaDoc paramName = (String JavaDoc)attribs.get("parameter");
52         if (!inputParams.isNull(paramName))
53         {
54             String JavaDoc value = inputParams.getString(paramName);
55             value = StringUtil.replace(value, "%", "");
56             if (rule.equalsIgnoreCase("like"))
57                 value = value + "%";
58             if (rule.equalsIgnoreCase("contains"))
59                 value = "%" + value + "%";
60             inputParams.setValue(paramName, value);
61         }
62         
63         return true;
64     }
65
66 }
67
Popular Tags