cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leszek Gawron <o...@wlkp.org>
Subject Re: storing in database
Date Wed, 10 Sep 2003 18:56:23 GMT
On Wed, Sep 10, 2003 at 07:17:53PM +0200, Garvin Riensche wrote:
> Hi there,
> 
> I started looking at cocoons database support and figured out how to connect
> cocoon 2.1-m1 to a postgresql 7.3 database and how data can be read out of
> the database. But i didn't understand yet how i can store data. Let's
> consider a simple example where i have a textfield and want to store its
> input in my database. How can that be done?
the most simple case is to use a xsp action which makes use of esql
logicsheet. I have created sucessfully some small web applications using this
approach. When you have a lot of controller logic and a great number of
actions you can switch to a little bit more advanced approach. Flow I mean.

Not using any form framework and persistence framework that would be:

function validate( model ) {
  // do some validation here
  if ( model.property1 == null || model.property2 == null ) 
    return false;

  if ( model.property2 == "" )
    return false;
  return true;
}


function insertData( model ) {
  var conn = Database.getConnection( "poolname" );
  try {
    var result = conn.query( "insert into table values ( ?, ? ); select @@identity as id",

                             [ model.property1,
                             model.property2 ] );
    conn.commit();
    return result.rows.row[0].id;
  } catch ( ex ) {
    conn.rollback();
  } finally {
    conn.close();
  }
}

function addRecord() {
  // model
  var model = new Object();

  // default values
  model.property1 = "foo";
  model.property2 = "bar";
  var errorMessage = "";

  while( true ) {
    if ( cocoon.request.getParameter( "submit" ) != null ) {
      if ( validate( model ) ) {
        var recordId = insertData( model ) ;

        // your record browsing view
        cocoon.redirectTo( "view/record/" + recordId );
    }
    // your record edit view (with form to edit data)
    cocoon.sendPageAndWait( "view/addrecord.jx", 
                            { "dataModel" : model,
                            "errorMessage": errorMessage } );

  }
}


all you have to do now is to register your .js script at sitemap
and add these matchers: 


<map:match pattern="add">
  <map:call function="addRecord"/>
</map:match>

<map:match patter="*.cont">
  <map:call continuation="{1}"/>
</map:match>

This is very simple yet fully functional example. The only thing you have to
do is to define your view for which you have:
* xsp
* jexl 
* jxpath
* velocity

When you master this level you will be able to use:

* form framework
  allows th define the form internals and provide automatical validation

* persistence framework
  you wont need to do sql queries - you will just operate on plain java beans


For a little more advanced example browse petstore block - it still does not
use any form framework or O/R mapping tool so it's quite easy to understand.

	LG
-- 
            __
         | /  \ |        Leszek Gawron            //  \\
        \_\\  //_/       ouzo@wlkp.org           _\\()//_
         .'/()\'.     Phone: +48(501)720812     / //  \\ \
          \\  //  recursive: adj; see recursive  | \__/ |


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message