KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > webui > validator > JManageFieldChecks


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.webui.validator;
17
18 import org.apache.commons.validator.ValidatorAction;
19 import org.apache.commons.validator.Field;
20 import org.apache.commons.validator.ValidatorUtil;
21 import org.apache.commons.validator.GenericValidator;
22 import org.apache.struts.action.ActionErrors;
23 import org.apache.struts.validator.Resources;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 /**
29  * Date: Nov 4, 2004 12:33:58 PM
30  * @author Shashank Bellary
31  */

32 public class JManageFieldChecks implements Serializable JavaDoc{
33
34     private static final String JavaDoc FIELD_TEST_NULL = "NULL";
35     private static final String JavaDoc FIELD_TEST_NOTNULL = "NOTNULL";
36     private static final String JavaDoc FIELD_TEST_EQUAL = "EQUAL";
37     private static final String JavaDoc FIELD_TEST_NOT_EQUAL = "NOTEQUAL";
38
39     /**
40      * This method compares two specified fields for equality.
41      * @param bean
42      * @param validatorAction
43      * @param field
44      * @param errors
45      * @param request
46      * @return
47      */

48     public static boolean validateMatch(Object JavaDoc bean,
49                                         ValidatorAction validatorAction,
50                                         Field field, ActionErrors errors,
51                                         HttpServletRequest JavaDoc request) {
52
53         String JavaDoc value = ValidatorUtil.getValueAsString(bean, field.getProperty());
54         String JavaDoc sProperty2 = field.getVarValue("secondProperty");
55         String JavaDoc value2 = ValidatorUtil.getValueAsString(bean, sProperty2);
56
57         if (!GenericValidator.isBlankOrNull(value) &&
58                 !GenericValidator.isBlankOrNull(value2) &&
59                 !value.equals(value2)) {
60             errors.add(field.getKey(),
61                     Resources.getActionError(request, validatorAction,
62                             field));
63             return false;
64         }
65         return true;
66     }
67
68
69     /**
70      * Checks if the field isn't null based on the values of other fields.
71      *
72      * @param bean The bean validation is being performed on.
73      * @param va The <code>ValidatorAction</code> that is currently being
74      * performed.
75      * @param field The <code>Field</code> object associated with the current
76      * field being validated.
77      * @param errors The <code>ActionErrors</code> object to add errors to if
78      * any validation errors occur.
79      * @param validator The <code>Validator</code> instance, used to access
80      * other field values.
81      * @param request Current request object.
82      * @return true if meets stated requirements, false otherwise.
83      */

84     public static boolean validateRequiredIf(Object JavaDoc bean, ValidatorAction va,
85                                              Field field, ActionErrors errors,
86                                              org.apache.commons.validator.Validator validator,
87                                              HttpServletRequest JavaDoc request) {
88         Object JavaDoc form = validator.getResource(
89                 org.apache.commons.validator.Validator.BEAN_KEY);
90         String JavaDoc value = null;
91         boolean required = false;
92         if(isString(bean)){
93             value = (String JavaDoc)bean;
94         }else{
95             value = ValidatorUtil.getValueAsString(bean, field.getProperty());
96         }
97         int i = 0;
98         String JavaDoc fieldJoin = "AND";
99         if(!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))){
100             fieldJoin = field.getVarValue("fieldJoin");
101         }
102         if(fieldJoin.equalsIgnoreCase("AND")){
103             required = true;
104         }
105         while(!GenericValidator.isBlankOrNull(field.getVarValue("field"+i))){
106             String JavaDoc dependProp = field.getVarValue("field"+i);
107             String JavaDoc dependTest = field.getVarValue("fieldTest"+i);
108             String JavaDoc dependTestValue = field.getVarValue("fieldValue"+i);
109             String JavaDoc dependVal = null;
110             boolean thisRequired = false;
111
112             dependVal = ValidatorUtil.getValueAsString(form, dependProp);
113             if(dependTest.equals(FIELD_TEST_NULL)){
114                 if((dependVal != null) && (dependVal.length() > 0)){
115                     thisRequired = false;
116                 }else{
117                     thisRequired = true;
118                 }
119             }else if(dependTest.equals(FIELD_TEST_NOTNULL)){
120                 if((dependVal != null) && (dependVal.length() > 0)){
121                     thisRequired = true;
122                 }else{
123                     thisRequired = false;
124                 }
125             }else if(dependTest.equals(FIELD_TEST_EQUAL)){
126                 thisRequired = dependTestValue.equalsIgnoreCase(dependVal);
127             }else if(dependTest.equals(FIELD_TEST_NOT_EQUAL)){
128                 thisRequired = !dependTestValue.equalsIgnoreCase(dependVal);
129             }
130
131             if(fieldJoin.equalsIgnoreCase("AND")){
132                 required = required && thisRequired;
133             }else{
134                 required = required || thisRequired;
135             }
136             i++;
137         }
138         if(required){
139             if(GenericValidator.isBlankOrNull(value)){
140                 errors.add(field.getKey(),
141                         Resources.getActionError(request, va, field));
142                 return false;
143             }else{
144                 return true;
145             }
146         }
147         return true;
148     }
149
150     /**
151      * Return <code>true</code> if the specified object is a String or a <code>null</code>
152      * value.
153      *
154      * @param o Object to be tested
155      * @return The string value
156      */

157     protected static boolean isString(Object JavaDoc o) {
158         return (o == null) ? true : String JavaDoc.class.isInstance(o);
159     }
160 }
Popular Tags