RE: Validation Questions

From: Laszlo Spoor (lspoor_cayenn..otmail.com)
Date: Tue Feb 22 2005 - 16:55:08 EST

  • Next message: Fredrik Liden: "How do do a group by"

    Hi Fredrik,

    Normally you don't have to worry about a ValidationException when you use
    something like Commons Validator, since this framework should take care of
    your form validation and if you treat your objects right nothing will fail.

    But still.. When your form has passed Validation succesfully and it is
    submitted. Commons Validator will pass control back to your code. Something
    like a 'ProcessServlet, ActionBean, ProcessJSP, etc..' is called. In this
    stage you could try something like:

    DataContext context= DataContext.getThreadDataContext() ;
    try
    {
      Artist picasso = (Artist)
    context.createAndRegisterNewObject(Artist.class);
      picasso.setName("Pablo Picasso");

      Painting selfPortrait = (Painting)
    context.createAndRegisterNewObject(Painting.class);
      selfPortrait.setName("Self-portrait");
      selfPortrait.setYear(new Integer(1907));

      Painting theDream = (Painting)
    context.createAndRegisterNewObject(Painting.class);
      theDream.setName("The Dream");
      theDream.setYear(new Integer(1932));

      // Set artist on both paintings; as a side effect this will automatically
      // add these paintings to the Artist's paintings collection.
      selfPortrait.setToArtist(picasso);
      theDream.setToArtist(picasso);

      // Final step - commit changes.
      // All three objects will be stored in the DB in one method call
      context.commitChanges();
    }
    catch (ValidationException ve)
    {
      context.rollbackChanges()
      // log it or something
      throw new CustomAppExeption();
    }

    As for you other questions. As far as I understand things.. Normally: as
    longs as you don't commit, there are no changes in the database. However, it
    could be possible that some parameters for DataContext can force this.

    Hope this helps, laszlo

    >From: "Fredrik Liden" <flide..ranslate.com>
    >Reply-To: cayenne-use..bjectstyle.org
    >To: <cayenne-use..bjectstyle.org>
    >Subject: RE: Validation Questions
    >Date: Tue, 22 Feb 2005 13:11:20 -0800
    >
    >Thanks for replying Mike,
    >
    >Lets say I use commons validator and then when I press commitChanges the
    >Cayenne Validation Exception is thrown and an jsp error page apprears. On
    >this page would I need to rollback the transaction here? and if I don't
    >roll back anything, will there be any unreleased locks on some records?
    >
    >-----Original Message-----
    >From: Mike Kienenberger [mailto:mkienen..laska.net]
    >Sent: Tuesday, February 22, 2005 12:58 PM
    >To: cayenne-use..bjectstyle.org
    >Cc: cayenne-use..bjectstyle.org
    >Subject: Re: Validation Questions
    >
    >
    >Fredrik Liden <flide..ranslate.com> wrote:
    > > Could someone give me some pointers on how to use Validation in a
    > > project? If it's recommended at all.
    > >
    > > I have one jsp with a form and a servlet that processes an add action.
    > > How would I use validation in this case?
    > >
    > > Wrap the add part with a try and catch? and requestdispatcher.
    > > How would I track down failurs to individual objects to create a more
    > > descriptive error message? Using if then statements? I'm sure there's a
    > > better way right?
    >
    >Normally you perform validation and type conversion in your form processing
    >logic. How that's done depends on what web app framework you're using.
    >Commons Validator is one way to do this:
    >http://jakarta.apache.org/commons/validator/
    >
    >
    > > Also, if the validationException is thrown, do I need to call rollback
    > > manually or is this done automaticallly?
    >
    >It's not automatic.
    >If you want to throw everything out at that point, you'd call rollback
    >manually.
    >If you want to try to correct the error and recommit, you could do that as
    >well.
    >
    >-Mike

    _________________________________________________________________
    MSN Search, for accurate results! http://search.msn.nl



    This archive was generated by hypermail 2.0.0 : Tue Feb 22 2005 - 16:56:03 EST