taylor 2004/11/10 17:46:39 Modified: portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/model PortletApplicationModel.java portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks GenericFrameworkPortlet.java portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/spring PortletApplicationModelImpl.java portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/spring/validation ValidationSupport.java Log: some basic validation added Revision Changes Path 1.2 +3 -2 jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/model/PortletApplicationModel.java Index: PortletApplicationModel.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/model/PortletApplicationModel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- PortletApplicationModel.java 4 Nov 2004 18:09:33 -0000 1.1 +++ PortletApplicationModel.java 11 Nov 2004 01:46:39 -0000 1.2 @@ -16,6 +16,7 @@ package org.apache.portals.bridges.frameworks.model; import java.util.Map; +import java.util.ResourceBundle; import javax.portlet.PortletConfig; import javax.portlet.PortletException; @@ -40,7 +41,7 @@ Map createPrefsBean(ModelBean mb, Map prefs); - boolean validate(Object bean, String view) + Map validate(Object bean, String view, ResourceBundle bundle) throws PortletException; String getForward(String view, String status); 1.6 +25 -13 jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/GenericFrameworkPortlet.java Index: GenericFrameworkPortlet.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/GenericFrameworkPortlet.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- GenericFrameworkPortlet.java 5 Nov 2004 22:34:23 -0000 1.5 +++ GenericFrameworkPortlet.java 11 Nov 2004 01:46:39 -0000 1.6 @@ -19,6 +19,7 @@ import java.lang.reflect.Method; import java.util.Iterator; import java.util.Map; +import java.util.ResourceBundle; import java.util.StringTokenizer; import javax.portlet.ActionRequest; @@ -31,6 +32,7 @@ import javax.portlet.PortletPreferences; import javax.portlet.PortletRequest; import javax.portlet.PortletRequestDispatcher; +import javax.portlet.PortletSession; import javax.portlet.ReadOnlyException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; @@ -66,6 +68,7 @@ private static final String PREFS_SUFFIX = ".prefs"; + private static final String SESSION_ERROR_MESSAGES = "portals.bridges.framework.errors"; /** * Action signature for calling velocity portlet actions */ @@ -129,9 +132,12 @@ String forward = null; // (3) validate the bean - if (model.validate(bean, view)) + ResourceBundle bundle = this.getPortletConfig().getResourceBundle(request.getLocale()); + Map errors = model.validate(bean, view, bundle); + if (errors.isEmpty()) { - + request.getPortletSession().removeAttribute(SESSION_ERROR_MESSAGES, PortletSession.PORTLET_SCOPE); + // (4) execute the velocity action String action = request.getParameter(FrameworkConstants.BRIDGES_ACTION); if (null == action) @@ -154,6 +160,7 @@ else { // failed validation + request.getPortletSession().setAttribute(SESSION_ERROR_MESSAGES, errors, PortletSession.PORTLET_SCOPE); forward = model.getForward(view, ForwardConstants.FAILURE); } @@ -317,7 +324,7 @@ if (view == null || view.equals(this.getDefaultViewPage())) { // clear it - response.setRenderParameter(FrameworkConstants.VIEW_VIEW_MODE, (String)null); + response.setRenderParameter(FrameworkConstants.VIEW_VIEW_MODE, view); //(String) null); } else { @@ -329,10 +336,10 @@ if (view == null || view.equals(this.getDefaultEditPage())) { // clear it - response.setRenderParameter(FrameworkConstants.VIEW_EDIT_MODE, (String)null); + response.setRenderParameter(FrameworkConstants.VIEW_EDIT_MODE, view); //(String) null); } else - { + { response.setRenderParameter(FrameworkConstants.VIEW_EDIT_MODE, view); } } @@ -340,7 +347,7 @@ { if (view == null || view.equals(this.getDefaultHelpPage())) { - response.setRenderParameter(FrameworkConstants.VIEW_HELP_MODE, (String)null); + response.setRenderParameter(FrameworkConstants.VIEW_HELP_MODE, view); //(String) null); } else { @@ -355,10 +362,10 @@ if (view == null || view.equals(this.getDefaultViewPage())) { // clear it - response.setRenderParameter(FrameworkConstants.VIEW_VIEW_MODE, (String)null); + response.setRenderParameter(FrameworkConstants.VIEW_VIEW_MODE, view); //(String) null); } else - { + { response.setRenderParameter(FrameworkConstants.VIEW_VIEW_MODE, view); } } @@ -367,10 +374,10 @@ if (view == null || view.equals(this.getDefaultEditPage())) { // clear it - response.setRenderParameter(FrameworkConstants.VIEW_EDIT_MODE, (String)null); + response.setRenderParameter(FrameworkConstants.VIEW_EDIT_MODE, view); //(String) null); } else - { + { response.setRenderParameter(FrameworkConstants.VIEW_EDIT_MODE, view); } } @@ -378,10 +385,10 @@ { if (view == null || view.equals(this.getDefaultHelpPage())) { - response.setRenderParameter(FrameworkConstants.VIEW_HELP_MODE, (String)null); + response.setRenderParameter(FrameworkConstants.VIEW_HELP_MODE, view); //(String) null); } else - { + { response.setRenderParameter(FrameworkConstants.VIEW_HELP_MODE, view); } } @@ -513,8 +520,13 @@ } putRequestVariable(request, FrameworkConstants.FORWARD_TOOL, new Forwarder(model, request, response)); + Map errors = (Map)request.getPortletSession().getAttribute(SESSION_ERROR_MESSAGES, PortletSession.PORTLET_SCOPE); + if (errors != null) + { + putRequestVariable(request, "ERRORS", errors); + } request.setAttribute(FrameworkConstants.MODEL_TOOL, model); - + PortletContext context = getPortletContext(); PortletRequestDispatcher rd = context.getRequestDispatcher(template); rd.include(request, response); 1.2 +12 -9 jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/spring/PortletApplicationModelImpl.java Index: PortletApplicationModelImpl.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/spring/PortletApplicationModelImpl.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- PortletApplicationModelImpl.java 4 Nov 2004 18:09:33 -0000 1.1 +++ PortletApplicationModelImpl.java 11 Nov 2004 01:46:39 -0000 1.2 @@ -19,6 +19,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.ResourceBundle; import javax.portlet.PortletConfig; import javax.portlet.PortletException; @@ -32,7 +33,6 @@ import org.apache.portals.bridges.frameworks.model.ModelBean; import org.apache.portals.bridges.frameworks.model.PortletApplicationModel; import org.apache.portals.bridges.frameworks.spring.ModelBeanImpl; -import org.apache.portals.bridges.frameworks.spring.validation.ValidationSupport; /** @@ -242,26 +242,30 @@ return prefs; } - public boolean validate(Object bean, String view) + public Map validate(Object bean, String view, ResourceBundle bundle) throws PortletException { + Map result = new HashMap(); if (validations == null) { - return true; // no validation configured + return result; // no validation configured } // Get the bean name from the bean-view map String validatorFormName = (String)viewValidatorMap.get(view); if (validatorFormName == null) { - return true; // no validation for this bean + return result; // no validation for this bean } - // TODO: do we need to create a validator per request? Validator validator = new Validator(validations, validatorFormName); // Tell the validator which bean to validate against. validator.setParameter(Validator.BEAN_PARAM, bean); + // Parameters used by our validators + validator.setParameter("java.util.Map", result); + validator.setParameter("java.util.ResourceBundle", bundle); + ValidatorResults results = null; try @@ -270,16 +274,15 @@ results = validator.validate(); if (results.isEmpty()) { - return true; + return result; } - ValidationSupport.printResults(bean, results, validations, validatorFormName); } catch (ValidatorException e) { throw new PortletException("Error in processing validation: ", e); } - return false; + return result; } public String getForward(String view, String status) 1.2 +300 -163 jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/spring/validation/ValidationSupport.java Index: ValidationSupport.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/spring/validation/ValidationSupport.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ValidationSupport.java 4 Nov 2004 18:09:33 -0000 1.1 +++ ValidationSupport.java 11 Nov 2004 01:46:39 -0000 1.2 @@ -21,6 +21,7 @@ import java.util.Iterator; import java.util.Locale; import java.util.Map; +import java.util.ResourceBundle; import org.apache.commons.beanutils.DynaBean; import org.apache.commons.beanutils.PropertyUtils; @@ -33,234 +34,370 @@ import org.apache.commons.validator.ValidatorResources; import org.apache.commons.validator.ValidatorResult; import org.apache.commons.validator.ValidatorResults; -import org.apache.commons.validator.util.ValidatorUtils; - /** * ValidationSupport * - * @author <a href="mailto:[EMAIL PROTECTED]">David Sean Taylor</a> + * @author <a href="mailto:[EMAIL PROTECTED]">David Sean Taylor </a> * @version $Id$ */ public class ValidationSupport { + public static String getValueAsString(Object bean, String property) { Object value = null; - + if (bean instanceof Map) { - value = ((Map)bean).get(property); + value = ((Map) bean).get(property); } else if (bean instanceof DynaBean) { - value = ((DynaBean)bean).get(property); + value = ((DynaBean) bean).get(property); } else { - try { + try + { value = PropertyUtils.getProperty(bean, property); - - } catch(IllegalAccessException e) { + + } + catch (IllegalAccessException e) + { //log.error(e.getMessage(), e); - } catch(InvocationTargetException e) { + } + catch (InvocationTargetException e) + { //log.error(e.getMessage(), e); - } catch(NoSuchMethodException e) { + } + catch (NoSuchMethodException e) + { //log.error(e.getMessage(), e); } } - - if (value == null) - { - return null; - } - if (value instanceof String[]) + if (value == null) { return null; } + + if (value instanceof String[]) { return ((String[]) value).length > 0 ? value.toString() : ""; - } else if (value instanceof Collection) + } + else if (value instanceof Collection) { return ((Collection) value).isEmpty() ? "" : value.toString(); - } else { + } + else + { return value.toString(); - } + } } - + /** * Checks if the field is required. - * - * @return boolean If the field isn't <code>null</code> and - * has a length greater than zero, <code>true</code> is returned. - * Otherwise <code>false</code>. + * + * @return boolean If the field isn't <code>null</code> and has a length + * greater than zero, <code>true</code> is returned. Otherwise + * <code>false</code>. */ - public static boolean validateRequired(Object bean, Field field) + public static boolean validateRequired(Object bean, ValidatorAction va, Field field, Map errors, + ResourceBundle bundle) { - String value = getValueAsString(bean, field.getProperty()); - boolean valid = !GenericValidator.isBlankOrNull(value); - return valid; + String value = getValueAsString(bean, field.getProperty()); + boolean valid = !GenericValidator.isBlankOrNull(value); + if (!valid) + { + if (bundle == null) + { + errors.put(field.getKey(), "Field " + field.getKey() + " is a required field."); + } + else + { + String displayName = bundle.getString(field.getArg(0).getKey()); + if (displayName == null) + { + displayName = field.getKey(); + } + Object[] args = + { displayName}; + + String message = bundle.getString(va.getMsg()); + if (message == null) + { + message = "Field {0} is a required field."; + } + errors.put(field.getKey(), MessageFormat.format(message, args)); + } + } + return valid; } - public static boolean validateRange(Object bean, Field field) + public static boolean validateRange(Object bean, ValidatorAction va, Field field, Map errors, ResourceBundle bundle) { - String result = ValidatorUtils.getValueAsString(bean, field.getProperty()); - if (result == null) - return false; - - Integer intValue = GenericTypeValidator.formatInt(result); - if (intValue == null) - return false; - - int value = intValue.intValue(); - + int value = 0; + String result = getValueAsString(bean, field.getProperty()); + if (result != null) + { + Integer intValue = GenericTypeValidator.formatInt(result); + if (intValue != null) + { + value = intValue.intValue(); + } + } + String minResult = field.getVarValue("min"); if (minResult == null) - return false; - + { + minResult = "0"; + } String maxResult = field.getVarValue("max"); if (maxResult == null) - return false; - + { + maxResult = "0"; + } int min = GenericTypeValidator.formatInt(minResult).intValue(); int max = GenericTypeValidator.formatInt(maxResult).intValue(); - return GenericValidator.isInRange(value, min, max); - } + boolean valid = GenericValidator.isInRange(value, min, max); + if (!valid) + { + if (bundle == null) + { + errors.put(field.getKey(), "Field " + field.getKey() + " is out of range: [" + min + "- " + max + "]"); + } + else + { + String displayName = bundle.getString(field.getArg(0).getKey()); + if (displayName == null) + { + displayName = field.getKey(); + } + Object[] args = + { displayName, minResult, maxResult}; + + String message = bundle.getString(va.getMsg()); + if (message == null) + { + message = "Field {0} is out of range: [{1} - {2}]"; + } + errors.put(field.getKey(), MessageFormat.format(message, args)); + } + } + return valid; + } - public static boolean validateDoubleRange(Object bean, Field field) + public static boolean validateDoubleRange(Object bean, ValidatorAction va, Field field, Map errors, ResourceBundle bundle) { - String result = ValidatorUtils.getValueAsString(bean, field.getProperty()); - if (result == null) - return false; - - Double doubleValue = GenericTypeValidator.formatDouble(result); - if (doubleValue == null) - return false; - - double value = doubleValue.intValue(); + double value = 0; + String result = getValueAsString(bean, field.getProperty()); + if (result != null) + { + Double doubleValue = GenericTypeValidator.formatDouble(result); + if (doubleValue != null) + { + value = doubleValue.doubleValue(); + } + } + String minResult = field.getVarValue("min"); + if (minResult == null) + { + minResult = "0"; + } + String maxResult = field.getVarValue("max"); + if (maxResult == null) + { + maxResult = "0"; + } - String minResult = field.getVarValue("min"); - if (minResult == null) - return false; + double min = GenericTypeValidator.formatDouble(minResult).doubleValue(); + double max = GenericTypeValidator.formatDouble(maxResult).doubleValue(); + boolean valid = GenericValidator.isInRange(value, min, max); + if (!valid) + { + if (bundle == null) + { + errors.put(field.getKey(), "Field " + field.getKey() + " is out of range: [" + min + "- " + max + "]"); + } + else + { + String displayName = bundle.getString(field.getArg(0).getKey()); + if (displayName == null) + { + displayName = field.getKey(); + } + Object[] args = + { displayName, minResult, maxResult}; - String maxResult = field.getVarValue("max"); - if (maxResult == null) - return false; - - double min = GenericTypeValidator.formatDouble(minResult).doubleValue(); - double max = GenericTypeValidator.formatDouble(maxResult).doubleValue(); - - return GenericValidator.isInRange(value, min, max); + String message = bundle.getString(va.getMsg()); + if (message == null) + { + message = "Field {0} is out of range: [{1} - {2}]"; + } + errors.put(field.getKey(), MessageFormat.format(message, args)); + } + } + return valid; } - - public static boolean validateMask(Object bean, Field field) + + public static boolean validateMask(Object bean, ValidatorAction va, Field field, Map errors, ResourceBundle bundle) { - String mask = field.getVarValue("mask"); - String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); + String value = getValueAsString(bean, field.getProperty()); - try + String mask = field.getVarValue("mask"); + if (mask == null) { - if (!GenericValidator.isBlankOrNull(value) - && !GenericValidator.matchRegexp(value, mask)) - { - // errors.add( - // field.getKey(), - // Resources.getActionMessage(request, va, field)); + return true; // no mask provide, let it pass + } + + if (GenericValidator.isBlankOrNull(value)) + { + return true; // this is how struts handles it + } - return false; - } - else + boolean valid = GenericValidator.matchRegexp(value, mask); + if (!valid) + { + if (bundle == null) { - return true; + errors.put(field.getKey(), "Field " + field.getKey() + " failed to match validation pattern: " + mask); } - } - catch (Exception e) + else + { + String displayName = bundle.getString(field.getArg(0).getKey()); + if (displayName == null) + { + displayName = field.getKey(); + } + Object[] args = + { displayName, mask}; + + String message = bundle.getString(va.getMsg()); + if (message == null) + { + message = "Field {0} failed to match validation pattern: {2}"; + } + errors.put(field.getKey(), MessageFormat.format(message, args)); + } + } + return valid; + } + + public static boolean validateMaxLength(Object bean, ValidatorAction va, Field field, Map errors, ResourceBundle bundle) + { + String value = getValueAsString(bean, field.getProperty()); + + int max = Integer.parseInt(field.getVarValue("maxlength")); + + if (GenericValidator.isBlankOrNull(value)) { - //log.error(e.getMessage(), e); + return true; } - return true; + + boolean valid = GenericValidator.maxLength(value, max); + if (!valid) + { + if (bundle == null) + { + errors.put(field.getKey(), "Field " + field.getKey() + " surpasses maximum length: " + max); + } + else + { + String displayName = bundle.getString(field.getArg(0).getKey()); + if (displayName == null) + { + displayName = field.getKey(); + } + Object[] args = + { displayName, new Integer(max)}; + + String message = bundle.getString(va.getMsg()); + if (message == null) + { + message = "Field {0} surpasses maximum length {1}"; + } + errors.put(field.getKey(), MessageFormat.format(message, args)); + } + } + return valid; } + public static void printResults(Object bean, ValidatorResults results, ValidatorResources resources, String formName) + { + + boolean success = true; - public static void printResults( - Object bean, - ValidatorResults results, - ValidatorResources resources, - String formName) - { - - boolean success = true; - - // Start by getting the form for the current locale and Bean. - Form form = resources.getForm(Locale.getDefault(), formName); - - System.out.println("\n\nValidating:"); - System.out.println(bean); - - // Iterate over each of the properties of the Bean which had messages. - Iterator propertyNames = results.getPropertyNames().iterator(); - while (propertyNames.hasNext()) { - String propertyName = (String) propertyNames.next(); - - // Get the Field associated with that property in the Form - Field field = form.getField(propertyName); - - // Look up the formatted name of the field from the Field arg0 - String prettyFieldName = propertyName; //apps.getString(field.getArg(0).getKey()); - - // Get the result of validating the property. - ValidatorResult result = results.getValidatorResult(propertyName); - - // Get all the actions run against the property, and iterate over their names. - Map actionMap = result.getActionMap(); - Iterator keys = actionMap.keySet().iterator(); - while (keys.hasNext()) { - String actName = (String) keys.next(); - - // Get the Action for that name. - ValidatorAction action = resources.getValidatorAction(actName); - - // If the result is valid, print PASSED, otherwise print FAILED - System.out.println( - propertyName - + "[" - + actName - + "] (" - + (result.isValid(actName) ? "PASSED" : "FAILED") - + ")"); - - //If the result failed, format the Action's message against the formatted field name - if (!result.isValid(actName)) { - success = false; - String message = "invalid field"; // apps.getString(action.getMsg()); - if (actName.equals("doubleRange")) - { - Arg f1 = field.getArg(1); - Arg f2 = field.getArg(2); - Arg f0 = field.getArg(0); - Object[] args = { prettyFieldName, field.getVar("min").getValue(), field.getVar("max").getValue() }; - System.out.println( - " Error message will be: " - + MessageFormat.format(message, args)); - } - else - { - Object[] args = { prettyFieldName }; - System.out.println( - " Error message will be: " - + MessageFormat.format(message, args)); - } + // Start by getting the form for the current locale and Bean. + Form form = resources.getForm(Locale.getDefault(), formName); + + System.out.println("\n\nValidating:"); + System.out.println(bean); + + // Iterate over each of the properties of the Bean which had messages. + Iterator propertyNames = results.getPropertyNames().iterator(); + while (propertyNames.hasNext()) + { + String propertyName = (String) propertyNames.next(); + + // Get the Field associated with that property in the Form + Field field = form.getField(propertyName); + + // Look up the formatted name of the field from the Field arg0 + String prettyFieldName = propertyName; //apps.getString(field.getArg(0).getKey()); + + // Get the result of validating the property. + ValidatorResult result = results.getValidatorResult(propertyName); + + // Get all the actions run against the property, and iterate over + // their names. + Map actionMap = result.getActionMap(); + Iterator keys = actionMap.keySet().iterator(); + while (keys.hasNext()) + { + String actName = (String) keys.next(); + // Get the Action for that name. + ValidatorAction action = resources.getValidatorAction(actName); + + // If the result is valid, print PASSED, otherwise print FAILED + System.out.println(propertyName + "[" + actName + "] (" + + (result.isValid(actName) ? "PASSED" : "FAILED") + ")"); + + //If the result failed, format the Action's message against the + // formatted field name + if (!result.isValid(actName)) + { + success = false; + String message = "invalid field"; // apps.getString(action.getMsg()); + if (actName.equals("doubleRange")) + { + Arg f1 = field.getArg(1); + Arg f2 = field.getArg(2); + Arg f0 = field.getArg(0); + Object[] args = + { prettyFieldName, field.getVar("min").getValue(), field.getVar("max").getValue()}; + System.out.println(" Error message will be: " + MessageFormat.format(message, args)); + } + else + { + Object[] args = + { prettyFieldName}; + System.out.println(" Error message will be: " + MessageFormat.format(message, args)); } + } } - if (success) { - System.out.println("FORM VALIDATION PASSED"); - } else { - System.out.println("FORM VALIDATION FAILED"); - } - } - -} + if (success) + { + System.out.println("FORM VALIDATION PASSED"); + } + else + { + System.out.println("FORM VALIDATION FAILED"); + } + + } + +} \ No newline at end of file
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]