cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jean-Christophe Kermagoret <...@bluexml.org>
Subject Re: Database CRUD with flowscript
Date Thu, 20 Oct 2005 04:52:36 GMT
Hello,

You should use the javascript debugger you enable in the first lines of 
cocoon.xconf.

Then you can set breakpoint in your main function and go step by step to 
inspect variable values.

HTH

Derek Hohls a écrit :
> I am looking for help getting a simple DB app up-and-running
> using flowscript.  I have based my approach on the PetStore example, 
> but there is a *lot* of complexity in there I am trying to avoid.  
> I have created two "stripped down" flowscript files, along with 
> the required forms.  
>  
> The sitemap calls, and so on, are as per the PetStore example.
>  
> I have also created an "indicators" database, with a simple table 
> called "foo", with "fooid" "foodesc" and "webpage" as text fields.
>  
> What I have is:
>  
> dbtest.js
>  
> //=================================
> cocoon.load("resource://org/apache/cocoon/components/flow/javascript/Database.js");
> cocoon.load("resource://org/apache/cocoon/forms/flow/javascript/Form.js");
> cocoon.load("context://projects/inddb/script/foo.js");  
>  
> //const
> var MAX_RESULTS = 5;
> var VIEW = "jexl";
> var EXT = ".jexl";
>  
> //vars
> var indDatabase = null;
> var fooForm = null;
>  
> //startup
> function main(funName) {
>   var fun = this[funName];
>   var args = new Array(arguments.length -1);
>   for (var i = 1; i < arguments.length; i++) {
>     args[i-1] = arguments[i];
>   }
>   getIndDB();
>   fun.apply(args);
>  
> }
>  
> // Index page
> function index() {
>   EXT = ".jexl"; 
>   getIndDB();
>   cocoon.sendPage("view/index" + EXT, {
>       fooForm: fooForm
>   });
> }
>  
> //IndDB
> function getIndDB() {
>   if (indDatabase == null) {
>     this.indDatabase = new IndDB("hsql");
>     this.fooForm = new FooForm();
>   }
>   return indDatabase;
> }
>  
> //why here? gives errors if in foo.js ???
> function FooForm() {
>   this.fooid = "";
>   this.foodesc = "";
>   this.webpage = "";
> }
>  
> 
> function IndDB(poolId) {
>   this.poolId = poolId;
>   this.hsql = null;
>   //this.populate(); adds data to virtual db
> }
>  
> IndDB.prototype.getConnection = function(id) {
>   if (true) {
>     // temporary hack to avoid requiring datasource config in cocoon.xconf
>     java.lang.Class.forName("com.mysql.jdbc.Driver");
>     var jdbc = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/indicators",
"me", "mypassword")
>     var conn = new Database(jdbc);
>     if (this.hsql == null) {
>       // keep hsql in-memory database alive
>       this.hsql = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/indicators",
"me", "mypassword");
>     }
>     return conn;
>   } else {
>     // lookup datasource in cocoon.xconf
>     return Database.getConnection(id);
>   }
> }
>  
> IndDB.prototype.testDuplicateFoo = function(fooid) {
>   var conn = this.getConnection(this.poolId);
>   var rs = conn.query("select count(*) as ROWCOUNT from foo where fooid = ?", [fooid]);
>   var result = rs.rows[0].ROWCOUNT;
>   conn.close();
>   return new Number(result);
> }
>  
> //=================================
>  
> and then in foo.js
>  
> //=================================
>  
> IndDB.prototype.getFoo = function(fooid) {
>   var conn = this.getConnection(this.poolId);
>   var result = conn.query("select * from foo where fooid = ?", [fooid]);  
>   var record = result.rows[0];
>   conn.close();
>   return record;
> }
>  
> IndDB.prototype.insertFoo = function(model) {
>   var conn = this.getConnection(this.poolId);
>   conn.update("INSERT INTO foo (fooid, foodesc, webpage) VALUES (?, ?, ?)", [model.fooid,
model.foodesc, model.webpage]);
>   conn.close();
> }
>  
> IndDB.prototype.updateFoo = function(model) {
>   var conn = this.getConnection(this.poolId);
>   conn.update("UPDATE foo SET fooid = ? , foodesc = ? , webpage = ? ", [model.fooid,
model.foodesc, model.webpage]);
>   conn.close();
> }
>  
> function Foo() {
>   this.fooid = "";
>   this.foodesc = "";
>   this.webpage = "";
> }
>  
> function editFoo() {
>   editFooData();
>   cocoon.sendPage("db/foos");
> }
>  
> function newFoo() {
>   newFooData();
>   cocoon.sendPage("db/foos");
> }
>  
> function editFooData() {
>   var editFooDataForm = new Form("view/forms/editFooForm_d.xml");
>   var model = editFooDataForm.getModel();
>   model.message = "";
>   model.fooid = fooForm.fooid;
>  model.foodesc = fooForm.foodesc;
>  model.webpage = fooForm.webpage;
>   editFooDataForm.showForm("view/editFooForm.cforms");
>  fooForm.foo = getindDB().getFoo(fooForm.fooid);
> }
>  
> function newFooData() {
>   var newFooDataForm = new Form("view/forms/editFooForm_d.xml");
>   var model = newFooDataForm.getModel();
>   model.message = "";
>   newFooDataForm.showForm("view/editFooForm.cforms");
>   while (getIndDB().testDuplicateFoo(model.fooid) > 0) {
>     model.message = "ID is already in use. Please choose another Foo ID.";
>     newFootDataForm.showForm("view/newFooForm.cforms");
>   } 
>   fooForm = new FooForm(model.fooid);
>   fooForm.foo = new Foo();
>   editFooData(); 
>  
> }
>  
> //=================================
>  
> When I call editFoo?fooid=xyz I get
>  
> An Error Occurred
> The undefined value has no properties.
> org.apache.avalon.framework.CascadingRuntimeException: 
> The undefined value has no properties.
> cause: ConversionError: The undefined value has no properties. 
> (file:/D:/tomcat/webapps/cocoon/projects/inddb/script/dbtest.js; line 28)
>  
> (N.B. Line 28 is the "fun.apply(args);" call in the dbtest..js)
>  
> Any help with how to go about debugging this is appreciated,
> along with some critical comment on perhaps streamlining the
> above approach - I also need to add a "deleteFoo()" function
> and some tips on how to write this would be appreciated.
>  
> Thanks
> Derek
> 
> 


-- 

BlueXML
Jean-Christophe Kermagoret
Directeur associé
jck@bluexml.org


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


Mime
View raw message