| 1 6 7 package org.contineo.actions.validator; 8 9 import javax.servlet.http.HttpServletRequest ; 10 11 import org.apache.commons.validator.Field; 12 import org.apache.commons.validator.GenericValidator; 13 import org.apache.commons.validator.ValidatorAction; 14 import org.apache.commons.validator.ValidatorUtil; 15 import org.apache.log4j.Level; 16 import org.apache.log4j.Logger; 17 import org.apache.struts.action.ActionErrors; 18 import org.apache.struts.validator.Resources; 19 import org.contineo.core.LoggingManager; 20 24 public class ValidatorBean { 25 26 27 public ValidatorBean() { 28 } 29 30 public static boolean validateIdentical(Object bean,ValidatorAction va,Field field,ActionErrors errors,HttpServletRequest request) { 31 String value = ValidatorUtil.getValueAsString(bean,field.getProperty()); 32 String sProperty2 = field.getVarValue("secondProperty"); 33 String value2 = ValidatorUtil.getValueAsString(bean, sProperty2); 34 if (!GenericValidator.isBlankOrNull(value)) { 35 try { 36 if (!value.equals(value2)) { 37 errors.add(field.getKey(),Resources.getActionError(request, va, field)); 38 return false; 39 } 40 } catch (Exception e) { 41 logError(e.getMessage()); 42 errors.add(field.getKey(), Resources.getActionError(request, va, field)); 43 return false; 44 } 45 } 46 return true; 47 } 48 49 public static boolean validateList(Object bean,ValidatorAction va,Field field,ActionErrors errors,HttpServletRequest request) { 50 String value = ValidatorUtil.getValueAsString(bean,field.getProperty()); 51 String size = field.getVarValue("count"); 52 boolean valid = true; 54 try { 55 int count = Integer.parseInt(size); 56 for (int i=0; i<count; i++) { 57 String prop = field.getVarValue("field" + String.valueOf(i)); 58 String elem = ValidatorUtil.getValueAsString(bean, prop); 59 if (GenericValidator.isBlankOrNull(elem)) 60 valid = false; 61 } 62 if (GenericValidator.isBlankOrNull(value)) 63 valid = false; 64 } catch (Exception e) { 65 logError(e.getMessage()); 66 valid = false; 67 } 68 if (!valid) 69 errors.add(field.getKey(),Resources.getActionError(request, va, field)); 70 return true; 71 } 72 73 private static void logError(String message) { 74 Logger logger = LoggingManager.getLogger(org.contineo.actions.validator.ValidatorBean.class); 75 if (logger.isEnabledFor(Level.ERROR)) 76 logger.error(message); 77 } 78 } 79 | Popular Tags |