1 18 package org.apache.beehive.netui.pageflow.validation; 19 20 import org.apache.commons.validator.ValidatorAction; 21 import org.apache.commons.validator.Field; 22 import org.apache.commons.validator.ValidatorUtil; 23 import org.apache.commons.validator.GenericValidator; 24 import org.apache.struts.action.ActionMessages; 25 import org.apache.struts.validator.Resources; 26 import org.apache.struts.validator.FieldChecks; 27 import org.apache.beehive.netui.pageflow.internal.InternalExpressionUtils; 28 import org.apache.beehive.netui.util.logging.Logger; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.ServletContext ; 32 33 public class ValidatorRules 34 extends FieldChecks 35 { 36 private static final Logger _log = Logger.getInstance( ValidatorRules.class ); 37 38 48 public static boolean validateValidWhen( Object bean, ValidatorAction va, Field field, ActionMessages errors, 49 HttpServletRequest request, ServletContext servletContext ) 50 { 51 52 String value; 53 54 if ( isString( bean ) ) 55 { 56 value = ( String ) bean; 57 } 58 else 59 { 60 value = ValidatorUtil.getValueAsString( bean, field.getProperty() ); 61 } 62 63 if ( ! GenericValidator.isBlankOrNull( value ) ) 64 { 65 String condition = field.getVarValue( "netui_validwhen" ); 66 67 try 68 { 69 if ( ! InternalExpressionUtils.evaluateCondition( condition, bean, request, servletContext ) ) 70 { 71 errors.add( field.getKey(), Resources.getActionError( request, va, field ) ); 72 return false; 73 } 74 } 75 catch ( Exception e ) 76 { 77 _log.error( "Error evaluating expression " + condition + " for ValidWhen rule on field " 78 + field.getProperty() + " on bean of type " + 79 ( bean != null ? bean.getClass().getName() : null ) ); 80 81 errors.add( field.getKey(), Resources.getActionError( request, va, field ) ); 82 return false; 83 } 84 } 85 86 return true; 87 } 88 89 99 public static boolean validateLongRange( Object bean, ValidatorAction va, Field field, ActionMessages errors, 100 HttpServletRequest request ) 101 { 102 103 String value; 104 105 if ( isString( bean ) ) 106 { 107 value = ( String ) bean; 108 } 109 else 110 { 111 value = ValidatorUtil.getValueAsString( bean, field.getProperty() ); 112 } 113 114 if ( ! GenericValidator.isBlankOrNull( value ) ) 115 { 116 try 117 { 118 long longValue = Long.parseLong( value ); 119 long min = Long.parseLong( field.getVarValue( "min" ) ); 120 long max = Long.parseLong( field.getVarValue( "max" ) ); 121 122 if ( longValue < min || longValue > max ) 123 { 124 errors.add( field.getKey(), Resources.getActionError( request, va, field ) ); 125 return false; 126 } 127 } 128 catch ( Exception e ) 129 { 130 errors.add( field.getKey(), Resources.getActionError( request, va, field ) ); 131 return false; 132 } 133 } 134 135 return true; 136 } 137 } 138 | Popular Tags |