cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Νίκος Παράσχου <nipar...@gmail.com>
Subject Re: How can I handle validation exception?
Date Wed, 11 Mar 2009 09:01:52 GMT
The following code works fine:

        context.setValidatingObjectsOnCommit(false);

        ValidationResult validationResult = new ValidationResult();
        company.validateForInsert(validationResult);

        if(validationResult.hasFailures()) {
            List failures = validationResult.getFailures();

            for(int i = 0; i < failures.size(); i++) {
                ValidationFailure tmp = (ValidationFailure)
failures.get(i);
                System.out.println("\n" + tmp.getDescription());
            }
        }
        else {
            context.commitChanges();
        }

If I don't lower the flag ValidatingObjectsOnCommit (which means "dirty"
objects will be validated automatically), how can I handle the
validation failures?

Is there any way to do something like the following:

try {
	context.commitChanges();
}
catch(Some ValidationException thrown earlier) {
	// Get failures list and print error messages.
}

Nikos

Στις 11-03-2009, ημέρα Τετ, και ώρα 10:30 +0200, ο/η Andrus Adamchik
έγραψε:
> > Is the following correct?
> 
> 
> Looks about right. Also there's a few pre-canned classes implementing  
> ValidationFailure in the org.apache.cayenne.validation package that  
> can be used to build failures.
> 
> > Or how can I retrieve the error message
> > from the validationResult when a ValidationException is thrown
> 
> http://cayenne.apache.org/doc/api/org/apache/cayenne/validation/ValidationResult.html
> 
> Use 'getFailures()' to get a list of failures, and analyze them one by  
> one using ValidationFailure methods.
> 
> Andrus
> 
> On Mar 11, 2009, at 9:36 AM, Νίκος Παράσχου wrote:
> 
> > Thank you Andrus,
> >
> > Yes, I do not throw the exception myself. But, how am I going to  
> > handle
> > the exception thrown by Cayenne? Or how can I retrieve the error  
> > message
> > from the validationResult when a ValidationException is thrown, so  
> > that
> > I can print it in the GUI?
> >
> > Is the following correct?
> >
> >    @Override
> >    protected void validateForSave(ValidationResult validationResult) {
> >      super.validateForSave(validationResult);
> >
> >      if(getCompanyName().equalsIgnoreCase("")) {
> >
> >          validationResult.addFailure(new ValidationFailure() {
> >
> >                @Override
> >                public Object getSource() {
> >                    return this;
> >                }
> >
> >                @Override
> >                public Object getError() {
> >                    return Company.CNAME_PROPERTY;
> >                }
> >
> >                @Override
> >                public String getDescription() {
> >                    return "My error message";
> >                }
> >            });
> >      }
> >    }
> >
> > Nikos
> >
> > Στις 10-03-2009, ημέρα Τρι, και ώρα 18:16 +0200, ο/ 
> > η Andrus Adamchik
> > έγραψε:
> >> The code example looks about right. However this is probably what's
> >> wrong: "When a specific condition is not met, a ValidationException  
> >> is
> >> thrown.". You should not throw the exception yourself. Just add an
> >> error to the validationResult, and let Cayenne throw it later.
> >>
> >> Andrus
> >>
> >>
> >> On Mar 10, 2009, at 5:03 PM, Νίκος Παράσχου wrote:
> >>
> >>> Hello,
> >>>
> >>> I implemented validateForSave(ValidationResult validationResult) in
> >>> one
> >>> of my ObjEntities. When a specific condition is not met, a
> >>> ValidationException is thrown.
> >>>
> >>> How can I handle this ValidationException?
> >>>
> >>> The following doesn't work (I get: ValidationException is not thrown
> >>> anywhere inside block try{}):
> >>>
> >>> try {
> >>> 	mycontext.commit();
> >>> }
> >>> catch(ValidationException vex) {
> >>> 	...
> >>> }
> >>>
> >>> Thank you,
> >>> Nikos
> >>>
> >>>
> >>>
> >>
> >
> >
> 


Mime
View raw message