001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.validation;
020:
021: import org.apache.commons.validator.ValidatorAction;
022: import org.apache.commons.validator.Field;
023: import org.apache.commons.validator.ValidatorUtil;
024: import org.apache.commons.validator.GenericValidator;
025: import org.apache.struts.action.ActionMessages;
026: import org.apache.struts.validator.Resources;
027: import org.apache.struts.validator.FieldChecks;
028: import org.apache.beehive.netui.pageflow.internal.InternalExpressionUtils;
029: import org.apache.beehive.netui.util.logging.Logger;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.ServletContext;
033:
034: public class ValidatorRules extends FieldChecks {
035: private static final Logger _log = Logger
036: .getInstance(ValidatorRules.class);
037:
038: /**
039: * Check if a given expression evaluates to <code>true</code>.
040: *
041: * @param bean the bean that validation is being performed on.
042: * @param va the <code>ValidatorAction</code> that is currently being performed.
043: * @param field the <code>Field</code> object associated with the current field being validated.
044: * @param errors the <code>ActionMessages</code> object to add errors to if any validation errors occur.
045: * @param request the current request object.
046: * @return <code>true</code> if the given expression evaluates to <code>true</code>
047: */
048: public static boolean validateValidWhen(Object bean,
049: ValidatorAction va, Field field, ActionMessages errors,
050: HttpServletRequest request, ServletContext servletContext) {
051:
052: String value;
053:
054: if (isString(bean)) {
055: value = (String) bean;
056: } else {
057: value = ValidatorUtil.getValueAsString(bean, field
058: .getProperty());
059: }
060:
061: if (!GenericValidator.isBlankOrNull(value)) {
062: String condition = field.getVarValue("netui_validwhen");
063:
064: try {
065: if (!InternalExpressionUtils.evaluateCondition(
066: condition, bean, request, servletContext)) {
067: errors.add(field.getKey(), Resources
068: .getActionError(request, va, field));
069: return false;
070: }
071: } catch (Exception e) {
072: _log.error("Error evaluating expression "
073: + condition
074: + " for ValidWhen rule on field "
075: + field.getProperty()
076: + " on bean of type "
077: + (bean != null ? bean.getClass().getName()
078: : null));
079:
080: errors.add(field.getKey(), Resources.getActionError(
081: request, va, field));
082: return false;
083: }
084: }
085:
086: return true;
087: }
088:
089: /**
090: * Check if a field's value is within a range ("min" and "max" Long variables on the passed-in Field).
091: *
092: * @param bean the bean that validation is being performed on.
093: * @param va the <code>ValidatorAction</code> that is currently being performed.
094: * @param field the <code>Field</code> object associated with the current field being validated.
095: * @param errors the <code>ActionMessages</code> object to add errors to if any validation errors occur.
096: * @param request the current request object.
097: * @return <code>true</code> if in range, false otherwise.
098: */
099: public static boolean validateLongRange(Object bean,
100: ValidatorAction va, Field field, ActionMessages errors,
101: HttpServletRequest request) {
102:
103: String value;
104:
105: if (isString(bean)) {
106: value = (String) bean;
107: } else {
108: value = ValidatorUtil.getValueAsString(bean, field
109: .getProperty());
110: }
111:
112: if (!GenericValidator.isBlankOrNull(value)) {
113: try {
114: long longValue = Long.parseLong(value);
115: long min = Long.parseLong(field.getVarValue("min"));
116: long max = Long.parseLong(field.getVarValue("max"));
117:
118: if (longValue < min || longValue > max) {
119: errors.add(field.getKey(), Resources
120: .getActionError(request, va, field));
121: return false;
122: }
123: } catch (Exception e) {
124: errors.add(field.getKey(), Resources.getActionError(
125: request, va, field));
126: return false;
127: }
128: }
129:
130: return true;
131: }
132: }
|