1 16 package com.blandware.atleap.webapp.util.core; 17 18 import com.blandware.atleap.common.util.DateUtil; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.commons.validator.Field; 22 import org.apache.commons.validator.GenericValidator; 23 import org.apache.commons.validator.ValidatorAction; 24 import org.apache.commons.validator.util.ValidatorUtils; 25 import org.apache.struts.Globals; 26 import org.apache.struts.action.ActionMessages; 27 import org.apache.struts.validator.Resources; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import java.io.PrintWriter ; 31 import java.io.StringWriter ; 32 import java.text.SimpleDateFormat ; 33 import java.util.Date ; 34 import java.util.Locale ; 35 36 37 47 public class ValidationUtil { 48 49 protected transient final Log log = LogFactory.getLog(ValidationUtil.class); 50 51 53 63 public static boolean validateTwoFields(Object bean, ValidatorAction va, 64 Field field, ActionMessages errors, 65 HttpServletRequest request) { 66 String value = 67 ValidatorUtils.getValueAsString(bean, field.getProperty()); 68 String sProperty2 = field.getVarValue("secondProperty"); 69 String value2 = ValidatorUtils.getValueAsString(bean, sProperty2); 70 71 if ( !GenericValidator.isBlankOrNull(value) ) { 72 try { 73 if ( !value.equals(value2) ) { 74 errors.add(field.getKey(), 75 Resources.getActionMessage(request, va, field)); 76 77 return false; 78 } 79 } catch ( Exception e ) { 80 errors.add(field.getKey(), 81 Resources.getActionMessage(request, va, field)); 82 83 return false; 84 } 85 } 86 87 return true; 88 } 89 90 100 public static Date validateDate(Object bean, ValidatorAction va, 101 Field field, ActionMessages errors, 102 HttpServletRequest request) { 103 104 Log log = LogFactory.getLog(ValidationUtil.class); 105 106 if ( log.isDebugEnabled() ) { 107 log.debug("validateDate called: field=" + field.getKey()); 108 } 109 Date result = null; 110 String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 111 112 if ( log.isDebugEnabled() ) { 113 log.debug("Value: " + value); 114 } 115 116 Locale locale = (Locale ) request.getSession().getAttribute(Globals.LOCALE_KEY); 117 String datePattern = DateUtil.getDatePattern(locale); 118 119 if ( log.isDebugEnabled() ) { 120 log.debug("Date pattern: " + datePattern); 121 } 122 123 if ( !GenericValidator.isBlankOrNull(value) ) { 124 try { 125 SimpleDateFormat formatter = new SimpleDateFormat (datePattern, locale); 126 formatter.setLenient(false); 127 result = formatter.parse(value); 128 } catch ( Exception e ) { 129 if ( log.isErrorEnabled() ) { 130 StringWriter sw = new StringWriter (); 131 e.printStackTrace(new PrintWriter (sw)); 132 log.error(sw.toString()); 133 } 134 } 135 if ( result == null ) { 136 errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); 137 } 138 } 139 return result; 140 } 141 } 142 | Popular Tags |