FeedbackMessages related request

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

FeedbackMessages related request

Ernesto Reinaldo Barreiro-2
Dear all,

First of all I want to thank wicket's developers for the nice product! In
second place I want to make clear that I am a wicket newbie, so maybe my
request is a stupid one... In that case my apologies to the developers... and
please help me find the right way to do it in wicket:-)

We have a problem regarding the way Wicket handles feedback messages. I'll try
to describe it as accurately as possible.

We are extending wicket form components to create our own RAD components:
our form components are panels which have embedded Wicket standard form
components. We would like to replace the FeedbackMessagesPanel that comes
with Wicket with our own FeedbackMessagesPanel which will display the
feedback messages in a table. Additionally, for messages comming from certain
types of Exceptions, e.g. for those comming from persistance exceptions, we
want to display more information regarding the exception (maybe in some
"graphical" way like a tree with nested exceptions, etc). There we encounter
the following problem:

The method

        public void error(IValidationError error)
        {
                if (error == null)
                {
                        throw new IllegalArgumentException("Argument [[error]] cannot be null");
                }

                String message = error.getErrorMessage(new MessageSource());

                if (message == null)
                {
                        // XXX maybe make message source remember tried resource keys so a
                        // more specific error message can be created
                        error("Could not locate error message for error: " + error.toString());
                }
                else
                {
                        error(message);
                }

        }

of FormComponent ends up creating an string message which is used to generate
a FeedBackMessage on the method

public final void error(final String message)
{
        getPage().getFeedbackMessages().error(this, message);
}

of Component. Thus all the information containned in (any implementation) of
IValidationError error is finally "squeezed" into an String object which is
used to build a FeedbackMessage object. Thus, even if all our pages inherit  
from our own extension of WebPage, for which the getFeedbackMessages() method
is overriden to create our own type of FeebackMessages, all I can pass to the
newly created FeedBackMessages is an String!

One solution for us would be to override, for each of the FormComponents we
create, the method

public void error(IValidationError error)
{
}

and do our logic there. But this is tiresome and error-prone! Other solution,
the one we would prefer, is to modify the code on the method

public void error(IValidationError error)
{
}

of FormComponent to use some kind of (plugable)
"FeedbackMessageRegisteringStrategy". Then you would have a
DefaultFeedbackMessageRegisteringStrategy class which would be the one that
actually does the logic that is now inside the error(IValidationError error)
method.

More in detail I suggest having an interface

package wicket;

import java.io.Serializable;

import wicket.validation.IMessageSource;
import wicket.validation.IValidationError;

public interface IFeedbackMessageRegisteringStrategy extends Serializable
{
        void error(Component component, IValidationError error, IMessageSource
messageSource);
}

and an implementation of IFeedbackMessageRegisteringStrategy

package wicket.markup.html.form;

import wicket.Component;
import wicket.IFeedbackMessageRegisteringStrategy;
import wicket.validation.IMessageSource;
import wicket.validation.IValidationError;

public class DefaultFeedbackMessageRegisteringStrategy implements
IFeedbackMessageRegisteringStrategy
{
        private static final long serialVersionUID = 1L;

        public void error(Component component, IValidationError error, IMessageSource
messageSource)
        {
                if (error == null)
                {
                        throw new IllegalArgumentException("Argument [[error]] cannot be null");
                }

                String message = error.getErrorMessage(messageSource);

                if (message == null)
                {
                        // XXX maybe make message source remember tried resource keys so a
                        // more specific error message can be created
                        component.error("Could not locate error message for error: " +
error.toString());
                }
                else
                {
                        component.error(message);
                }
        }
}

then on FormComponent we declare a field

private IFeedbackMessageRegisteringStrategy messageRegisteringStrategy = new
DefaultFeedbackMessageRegisteringStrategy();

and setters/getters for the field and the method error(IValidationError error)
can be rewritten as

public void error(IValidationError error)
{
        if(getMessageRegisteringStrategy() != null)
         getMessageRegisteringStrategy().error(this, error, new MessageSource());
}

That way the generation of feedback messages is more configurable and all we
would have to do is to plug in our own strategy! For instance, if you would
like to do some loggin of errors then the only thing you will have to do is
extend DefaultFeedbackMessageRegisteringStrategy to do that loggin.

I'll have tested the modifications I suggest on our own toy application and
they seem to work fine. I'll attach the files I had to modify just I case
some of the developers/commiters want to test them (in the hope my request is
not an stupid one and gets some attention).

Best regards,

Ernesto Reinaldo Barreiro

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Wicket-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-develop

IFeedbackMessageRegisteringStrategy.java (1K) Download Attachment
DefaultFeedbackMessageRegisteringStrategy.java (1K) Download Attachment
FormComponent.java (32K) Download Attachment