Commits

jcarreira  committed 882bd92

XW-156: Adding ActionMessages
also some cleanup

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@210e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits cc0357b

Comments (0)

Files changed (6)

File src/java/com/opensymphony/xwork/ActionSupport.java

         return validationAware.getActionErrors();
     }
 
+    public void setActionMessages(Collection messages) {
+        validationAware.setActionMessages(messages);
+    }
+
+    public Collection getActionMessages() {
+        return validationAware.getActionMessages();
+    }
+
     /**
      * @deprecated Use {@link #getActionErrors()}.
      */
         validationAware.addActionError(anErrorMessage);
     }
 
+    public void addActionMessage(String aMessage) {
+        validationAware.addActionMessage(aMessage);
+    }
+
     public void addFieldError(String fieldName, String errorMessage) {
         validationAware.addFieldError(fieldName, errorMessage);
     }
 
+    public String doDefault() throws Exception {
+        return SUCCESS;
+    }
+
     /**
      * A default implementation that does nothing an returns "success".
      *
         return SUCCESS;
     }
 
-    public String doDefault() throws Exception {
-        return SUCCESS;
-    }
-
     public boolean hasActionErrors() {
         return validationAware.hasActionErrors();
     }

File src/java/com/opensymphony/xwork/ValidationAware.java

     Collection getActionErrors();
 
     /**
+     * Set the Collection of Action level String messages (not errors)
+     */
+    void setActionMessages(Collection messages);
+
+    /**
+     * Get the Collection of Action level messages for this action. Messages should not be added directly
+     * here, as implementations are free to return a new Collection or an Unmodifiable Collection.
+     *
+     * @return Collection of String messages
+     */
+    Collection getActionMessages();
+
+    /**
      * Set the field error map of fieldname (String) to Collection of String error messages
      *
      * @param errorMap
     void addActionError(String anErrorMessage);
 
     /**
+     * Add an Action level message to this Action
+     */
+    void addActionMessage(String aMessage);
+
+    /**
      * Add an error message for a given field
      *
      * @param fieldName    name of field

File src/java/com/opensymphony/xwork/ValidationAwareSupport.java

  */
 package com.opensymphony.xwork;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 
 /**
     //~ Instance fields ////////////////////////////////////////////////////////
 
     private Collection actionErrors;
+    private Collection actionMessages;
     private Map fieldErrors;
 
     //~ Methods ////////////////////////////////////////////////////////////////
         return new ArrayList(internalGetActionErrors());
     }
 
+    public synchronized void setActionMessages(Collection messages) {
+        this.actionMessages = messages;
+    }
+
+    public synchronized Collection getActionMessages() {
+        return new ArrayList(internalGetActionMessages());
+    }
+
     public synchronized void setFieldErrors(Map errorMap) {
         this.fieldErrors = errorMap;
     }
 
     /**
-    * Get the field specific errors.
-    *
-    * @return an unmodifiable Map with errors mapped from fieldname (String) to Collection of String error messages
-    */
+* Get the field specific errors.
+*
+* @return an unmodifiable Map with errors mapped from fieldname (String) to Collection of String error messages
+*/
     public synchronized Map getFieldErrors() {
         return new HashMap(internalGetFieldErrors());
     }
         internalGetActionErrors().add(anErrorMessage);
     }
 
+    /**
+ * Add an Action level message to this Action
+ */
+    public void addActionMessage(String aMessage) {
+        internalGetActionMessages().add(aMessage);
+    }
+
     public synchronized void addFieldError(String fieldName, String errorMessage) {
         final Map errors = internalGetFieldErrors();
         List thisFieldErrors = (List) errors.get(fieldName);
     }
 
     /**
-    * Note that this does not have the same meaning as in WW 1.x
-    * @return (hasActionErrors() || hasFieldErrors())
-    */
+* Note that this does not have the same meaning as in WW 1.x
+* @return (hasActionErrors() || hasFieldErrors())
+*/
     public synchronized boolean hasErrors() {
         return (hasActionErrors() || hasFieldErrors());
     }
         return actionErrors;
     }
 
+    private Collection internalGetActionMessages() {
+        if (actionMessages == null) {
+            actionMessages = new ArrayList();
+        }
+
+        return actionMessages;
+    }
+
     private Map internalGetFieldErrors() {
         if (fieldErrors == null) {
             fieldErrors = new HashMap();

File src/java/com/opensymphony/xwork/validator/DelegatingValidatorContext.java

         return validationAware.getActionErrors();
     }
 
+    /**
+ * Set the Collection of Action level String messages (not errors)
+ */
+    public void setActionMessages(Collection messages) {
+        validationAware.setActionMessages(messages);
+    }
+
+    /**
+ * Get the Collection of Action level messages for this action. Messages should not be added directly
+ * here, as implementations are free to return a new Collection or an Unmodifiable Collection.
+ *
+ * @return Collection of String messages
+ */
+    public Collection getActionMessages() {
+        return validationAware.getActionMessages();
+    }
+
     public void setFieldErrors(Map errorMap) {
         validationAware.setFieldErrors(errorMap);
     }
     }
 
     /**
- * Translates a simple field name into a full field name in Ognl syntax
- * @param fieldName
- * @return
- */
+* Translates a simple field name into a full field name in Ognl syntax
+* @param fieldName
+* @return
+*/
     public String getFullFieldName(String fieldName) {
         return fieldName;
     }
         validationAware.addActionError(anErrorMessage);
     }
 
+    /**
+ * Add an Action level message to this Action
+ */
+    public void addActionMessage(String aMessage) {
+        validationAware.addActionMessage(aMessage);
+    }
+
     public void addFieldError(String fieldName, String errorMessage) {
         validationAware.addFieldError(fieldName, errorMessage);
     }
         return validationAware.hasFieldErrors();
     }
 
-    protected static LocaleProvider makeLocaleProvider(Object object) {
-        if (object instanceof LocaleProvider) {
-            return (LocaleProvider) object;
-        } else {
-            return new ActionContextLocaleProvider();
-        }
-    }
-
     public static TextProvider makeTextProvider(Object object, LocaleProvider localeProvider) {
         if (object instanceof TextProvider) {
             return (TextProvider) object;
         }
     }
 
+    protected static LocaleProvider makeLocaleProvider(Object object) {
+        if (object instanceof LocaleProvider) {
+            return (LocaleProvider) object;
+        } else {
+            return new ActionContextLocaleProvider();
+        }
+    }
+
     protected static ValidationAware makeValidationAware(Object object) {
         if (object instanceof ValidationAware) {
             return (ValidationAware) object;
             return null;
         }
 
+        /**
+ * Set the Collection of Action level String messages (not errors)
+ */
+        public void setActionMessages(Collection messages) {
+            for (Iterator iterator = messages.iterator(); iterator.hasNext();) {
+                String s = (String) iterator.next();
+                addActionMessage(s);
+            }
+        }
+
+        /**
+ * Get the Collection of Action level messages for this action. Messages should not be added directly
+ * here, as implementations are free to return a new Collection or an Unmodifiable Collection.
+ *
+ * @return Collection of String messages
+ */
+        public Collection getActionMessages() {
+            return null;
+        }
+
         public void setFieldErrors(Map errorMap) {
             for (Iterator iterator = errorMap.entrySet().iterator();
                     iterator.hasNext();) {
             log.error("Validation error: " + anErrorMessage);
         }
 
+        /**
+ * Add an Action level message to this Action
+ */
+        public void addActionMessage(String aMessage) {
+            log.info("Validation Message: " + aMessage);
+        }
+
         public void addFieldError(String fieldName, String errorMessage) {
             log.error("Validation error for " + fieldName + ":" + errorMessage);
         }

File src/java/com/opensymphony/xwork/validator/validators/VisitorFieldValidator.java

 import com.opensymphony.xwork.validator.ValidatorContext;
 
 import java.util.Collection;
-import java.util.Iterator;
 
 
 /**
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    public void setContext(String context) {
-        this.context = context;
-    }
-
-    public String getContext() {
-        return context;
+    public void setAppendPrefix(boolean appendPrefix) {
+        this.appendPrefix = appendPrefix;
     }
 
     public boolean isAppendPrefix() {
         return appendPrefix;
     }
 
-    public void setAppendPrefix(boolean appendPrefix) {
-        this.appendPrefix = appendPrefix;
+    public void setContext(String context) {
+        this.context = context;
+    }
+
+    public String getContext() {
+        return context;
     }
 
     public void validate(Object object) throws ValidationException {
     private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
         stack.push(o);
+
         ValidatorContext validatorContext;
+
         if (appendPrefix) {
-            validatorContext = new AppendingValidatorContext(getValidatorContext(), o, fieldName, getMessage(o), appendPrefix);
+            validatorContext = new AppendingValidatorContext(getValidatorContext(), o, fieldName, getMessage(o));
         } else {
             ValidatorContext parent = getValidatorContext();
             validatorContext = new DelegatingValidatorContext(parent, DelegatingValidatorContext.makeTextProvider(o, parent), parent);
         }
+
         ActionValidatorManager.validate(o, visitorContext, validatorContext);
         stack.pop();
     }
         String field;
         String message;
 
-        public AppendingValidatorContext(ValidatorContext parent, Object object, String field, String message, boolean appendPrefix) {
+        public AppendingValidatorContext(ValidatorContext parent, Object object, String field, String message) {
             super(parent, makeTextProvider(object, parent), parent);
 
             //            super(parent);
         }
 
         /**
-         * Translates a simple field name into a full field name in Ognl syntax
-         * @param fieldName
-         * @return
-         */
+ * Translates a simple field name into a full field name in Ognl syntax
+ * @param fieldName
+ * @return
+ */
         public String getFullFieldName(String fieldName) {
             return field + "." + fieldName;
         }

File src/test/com/opensymphony/xwork/interceptor/ConversionErrorInterceptorTest.java

  */
 package com.opensymphony.xwork.interceptor;
 
+import com.mockobjects.dynamic.C;
 import com.mockobjects.dynamic.Mock;
 
 import com.opensymphony.xwork.Action;
         conversionErrors = new HashMap();
         context.setConversionErrors(conversionErrors);
         mockInvocation.matchAndReturn("getInvocationContext", context);
+        mockInvocation.expect("addPreResultListener", C.isA(PreResultListener.class));
         mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
     }
 }