You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Jonas Ekstedt <ek...@ibg.uu.se> on 2004/10/27 16:21:15 UTC

A widget framework (long)

Hello

I have created a widget framework that I would like to donate to Cocoon
(if you want it). I think it has several advantages to the current
Cocoon Forms framework in that it is more straightforward to use and
that it is easier to extend. I've included some documentation below or
you can read the same text better formatted at:

http://home.student.uu.se/j/joek8725/widgets.html

It is by no means finished yet but I would be very happy if you could
find some use for it.

// Jonas

------------------------------------------------------------------------

WIDGETS

"Widgets" is a widget framework for Cocoon based on the taglib block. It
grew out of an exam project that I did a few months ago.


THE PROBLEM WITH COCOON FORMS

The problem (as I see it) with Cocoon Forms is its concept of a
ViewModel (the widget tree) that sits in between the model and the view:
    Model (eg. bean) <-> ViewModel <-> View (eg. JXTemplate) 

Having a ViewModel as a buffert between the Model and the View
introduces a number of problems. First of all any changes made to the
model has to be synhronized with changes to the ViewModel (eg if a list
containg beans is expanded with one more bean then the ViewModel has to
create widgets for the added bean). Another problem is if I have several
widgets mapped to the same bean value. Then a change of value in one
widget won't update the values of the other widgets. 

Another set of problems has to do with the View. I can't use viewlogic
(eg. JXTemplate) to alter which widgets are rendered. It would be a
piece of cake to make a wizard using JXTemplate but because the
ViewModel has to know which widgets are rendered any changes to the
visibility of the widgets has to go through the ViewModel. This means
that to make a wizard (or tab-layout etc) I have to create them as
Cocoon Form widgets using Java which is cumbersome. 

Anyways, here is my take on how to make a widget framework. See the
installation notes below if you want to try it out for yourself. 


THE FRAMEWORK

I'll explain the widget framework using a calculator example. A
three-paged wizard will ask for two values (page 1 and 2) and present
the sum on the third page. First we'll code the machinery for the wizard
and afterwards add the calculator logic. 


THE PAGE OBJECT

First I'll show you the flow code and the associated JXTemplate page. 

---------------------------------------------------------------
cocoon.load("resource://widgets/flow/Page.js");

function public_calculator() {

    var page = new Page();
    var calculatorHandler = new CalculatorHandler();
    page.setEventHandler(calculatorHandler);
    page.show("calculator.jx", {}, {wizard: "term1"});
    cocoon.sendPage("main.html");
}


function CalculatorHandler() {
}

CalculatorHandler.prototype.action_previous = function(requestProcessor, models, bizData) {
    if (bizData["wizard"] == "term2") {
	bizData["wizard"] = "term1";
    } else if (bizData["wizard"] == "sum") {
	bizData["wizard"] = "term2";
    }
    return true;
}

CalculatorHandler.prototype.action_next = function(requestProcessor, models, bizData) {
    if (bizData["wizard"] == "term1") {
      bizData["wizard"] = "term2";
    } else if (bizData["wizard"] == "term2") {
      bizData["wizard"] = "sum";
    }
    return true;
}

CalculatorHandler.prototype.action_exit = function(requestProcessor, models, bizData) {
    return false;
}
---------------------------------------------------

---------------------------------------------------
<?xml version="1.0"?>

<html xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
  xmlns:wt="urn:widgets"
  xmlns:wi="http://widget/instance"
  >

  <body>
    <h1>Calculator wizard</h1>
    <jx:if test="${wizard == 'term1'}">
      Page term1
      <a href="${continuation.id}.kont?cocoon-event=action:next">Next</a>
    </jx:if>

    <jx:if test="${wizard == 'term2'}">
      Page term2
      <a href="${continuation.id}.kont?cocoon-event=action:previous">Previous</a>
      <a href="${continuation.id}.kont?cocoon-event=action:next">Next</a>
    </jx:if>

    <jx:if test="${wizard == 'sum'}">
      Page sum
      <a href="${continuation.id}.kont?cocoon-event=action:previous">Previous</a>
    </jx:if>
    <a href="${continuation.id}.kont?cocoon-event=action:exit">Exit</a>
  </body>
</html>
---------------------------------------------------

The JXTemplate is nothing out of the ordinary. It will simply hide the
pages of the wizard that are not active (as set in ${wizard}). The flow
however needs a bit of explaining. First, we create a Page object. To
this Page object we add an event handler. The functions of the event
handler are called depending on what events are triggered by the user.
Clicking a "Next" link will trigger the next continuation with the
request parameter "cocoon-event" set to "action:next". The Page object
will respond by invoking the action_next function of the event handler.
The Page object will continue showing the page as long as the event
handler function returns true. 

As you can see in the flow above a "next" event will check which wizard
is currently active and set bizData["wizard"] accordingly. 

Here is the code for the Page object. I'll discuss it further later on. 

---------------------------------------------------
function Page() {
}

Page.prototype.setEventHandler = function(eh) {
    this.eventHandler = eh;
    this.eventHandler.page = this;
}

Page.prototype.show = function(uri, models, bizData) {

    if (! bizData) {
	var bizData = {};
    }

    while (1) {
	for (key in models) {
	    cocoon.request.setAttribute(key, models[key]);
	}

	var requestProcessor = Packages.widgets.flow.RequestProcessor.getInstance(cocoon.request);
	cocoon.sendPageAndWait(uri, bizData);
	requestProcessor.process(cocoon.request);

	if (this.eventHandler == undefined) {
	    return;
	}

	var proceed = this.eventHandler["action_" + requestProcessor.event.action](requestProcessor, models, bizData);

	if (! proceed) {
	    return;
	}
    }
}
---------------------------------------------------
		  
ADDING THE MODEL

Lets turn to how we should represent the state of our calculator. We
need to store two terms and supply logic to present the sum. We will use
a bean for this. 

---------------------------------------------------
package widgets.samples;

public class Calculator {

    String term1;
    String term2;

    public String getTerm1() {
	return term1;
    }

    public void setTerm1(String term1) {
	this.term1 = term1;
    }

    public String getTerm2() {
	return term2;
    }

    public void setTerm2(String term2) {
	this.term2 = term2;
    }

    public String getSum() {
	return String.valueOf(Integer.parseInt(term1) + Integer.parseInt(term2));
    }

}
---------------------------------------------------
		  
Note that to simplify matters we represent the terms as StringS and then
only convert them when we present the sum. 

The widget framework introduces the concept of Models. A Model is a
wrapper around your data so that the View can access it in a uniform
way. Currently there are four different type of Models: 

* BeanModel: wraps a POJO
* DOMModel: wraps a DOM
* MapModel: wraps a java.util.Map
* ResultSetModel: wraps a database query

As we decided to represent the calculator as a bean we will choose to
wrap it in a BeanModel. 

---------------------------------------------------
importClass(Packages.widgets.model.BeanModel);
importClass(Packages.widgets.samples.Calculator);
cocoon.load("resource://widgets/flow/Page.js");

function public_calculator() {

    var calculator = new Calculator();
    var calculatorModel = new BeanModel(calculator);
    var page = new Page();
    var calculatorHandler = new CalculatorHandler();
    page.setEventHandler(calculatorHandler);
    page.show("calculator.jx", {calculator: calculatorModel}, {wizard: "term1"});
    cocoon.sendPage("main.html");
}
---------------------------------------------------

Note that we send in the model to the page.show() function. The key
("calculator") of the model in the map will be used in the view to
uniquely identify which model is referenced. It will also be used when
we populate the model with values from the request. 

In order to show the model in the view we change the JXTemplate: 

---------------------------------------------------
<?xml version="1.0"?>

<html xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
  xmlns:wt="urn:widgets"
  xmlns:wi="http://widget/instance"
  >

  <body>
    <h1>Calculator wizard</h1>
    <form action="${continuation.id}.kont" method="post">
      <wt:model id="calculator">
	<jx:if test="${wizard == 'term1'}">
	  <p>
	    <b>Term 1:</b>
	    <wt:field type="text" attribute="term1"/>
	  </p>
	  <p>
	    <wt:form-button action="next">Next</wt:form-button>
	  </p>
	</jx:if>

	<jx:if test="${wizard == 'term2'}">
	  <p>
	    <b>Term 2:</b>
	    <wt:field type="text" attribute="term2"/>
	  </p>
	  <p>
	    <wt:form-button action="previous">Previous</wt:form-button>
	    <wt:form-button action="next">Next</wt:form-button>
	  </p>
	</jx:if>

	<jx:if test="${wizard == 'sum'}">
	  <p>
	    <b>Sum:</b>
	    <wt:out attribute="sum"/>
	  </p>
	  <p>
	    <wt:form-button action="previous">Previous</wt:form-button>
	  </p>
	</jx:if>
      </wt:model>
      <a href="${continuation.id}.kont?cocoon-event=action:exit">Exit</a>
    </form>
  </body>
</html>
---------------------------------------------------

The wt:model tag is used to setup the context that subsequent widget
tags will operate on. In this case we identify the model as "calculator"
(which is the key we used in the page.show() method call. This means
that any descendant tags will look up their values in our calculator
bean. It is possible to supply the view with several models. 

When the wt:field tag is executed (during taglib transformation) it will
fetch the value of attribute "term1" from the containing model (in this
case the calculator bean). Subsequent xslt transformation will turn it
into a html input text tag. 

The wt:form-button will translate into a html button element. The name
of the buttons will be "cocoon-event" and its value will be set to eg.
"action:next" so that a button press will trigger the event handling in
the Page object. 

Contrary to Cocoon Forms we can choose at render time the type of widget
used to show model values. In Cocoon Forms it is very difficult to
change how we represent data, eg. once you have decided that term1
should be rendered as a text field you cannot change that very easy.
Here we can use a text field, a select box or whatever to render term1,
we can even render the value of term1 several times using different
types of widgets. 

The flow now needs a few changes to cope with population of the bean
from the request. But first I'll talk a bit about the RequestProcessor. 


THE REQUESTPROCESSOR

Everytime the Page object show a page it will create a RequestProcessor.
All widgets that represents some sort of user input (eg. fields, select
boxes etc.) will register the expected request parameter name with the
RequestProcessor. So that when the form is submitted the
RequestProcessor knows which widgets has been rendered and can build a
population map based on that. That means that when we populate the beans
only the data that we have specifically rendered as form widgets will be
eligble for population. Because of this we can use JXTemplate (as in the
example above) to selectively hide widgets without messing up the
population procedure. 

Below is the changed flow that now populates the widget model everytime
an event is triggered. 

---------------------------------------------------
importClass(Packages.widgets.model.BeanModel);
importClass(Packages.widgets.samples.Calculator);

cocoon.load("resource://widgets/flow/Page.js");

function public_calculator() {

    var calculator = new Calculator();
    var calculatorModel = new BeanModel(calculator);
    var calculatorHandler = new CalculatorHandler();

    var page = new Page();
    page.setEventHandler(calculatorHandler);

    page.show("calculator.jx", {calculator: calculatorModel}, {wizard: "term1"});

    cocoon.sendPage("main.html");
}


function CalculatorHandler() {
}

CalculatorHandler.prototype.action_previous = function(requestProcessor, models, bizData) {
    models["calculator"].populate(requestProcessor.getParameters("calculator:"));
    if (bizData["wizard"] == "term2") {
	bizData["wizard"] = "term1";
    } else if (bizData["wizard"] == "sum") {
	bizData["wizard"] = "term2";
    }
    return true;
}

CalculatorHandler.prototype.action_next = function(requestProcessor, models, bizData) {
    models["calculator"].populate(requestProcessor.getParameters("calculator:"));

    if (bizData["wizard"] == "term1") {
      bizData["wizard"] = "term2";
    } else if (bizData["wizard"] == "term2") {
      bizData["wizard"] = "sum";
    }
    return true;
}

CalculatorHandler.prototype.action_exit = function(requestProcessor, models, bizData) {
    return false;
}
---------------------------------------------------

The population is handled by the populate() function. Note how we call
the RequestProcessor to get the parameters. In this specific case we use
the getParameters(String prefix) function that will filter out any
parameters that does not start with prefix. The prefix "calculator:" is
added by the field tag when it renders the field widget so that we can
differentiate between parameters that belong to different models. 

In our case we would see request parameters named "calculator:term1" and
"calculator:term2" depending on which wizard page was submitted. 

Now we have a functioning calculator. The only thing missing is
validation. We need to be certain that the user enters numbers and not
characters. 


VALIDATION

A very simple calculator validator can look like this: 

---------------------------------------------------
package widgets.samples;

import org.apache.commons.validator.GenericValidator;
import widgets.model.BeanModel;

public class CalculatorValidator {

    public boolean validatePhase1(BeanModel model) {

	Calculator calculator = (Calculator) model.getBean();

	if (! GenericValidator.isInt(calculator.getTerm1())) {
	    model.setError("term1", "Value must be an integer");
	    return false;
	}
	return true;
    }

    public boolean validatePhase2(BeanModel model) {

	Calculator calculator = (Calculator) model.getBean();

	if (! GenericValidator.isInt(calculator.getTerm2())) {
	    model.setError("term2", "Value must be an integer");
	    return false;
	}
	return true;
    }
}
---------------------------------------------------

It will check if the values are indeed integer and if not add errors to
the model. Every type of model supports setting userdata on the specific
nodes of the model (cf Node.setUserdata() in DOM). A special case of
this is error messages. 

In the view an error message widget will check if errors exist for the
specified attribute and if so print it out. Below is the view again with
the added error widgets. 

---------------------------------------------------
<?xml version="1.0"?>

<html xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
  xmlns:wt="urn:widgets"
  xmlns:wi="http://widget/instance"
  >

  <body>
    <h1>Calculator wizard</h1>
    <form action="${continuation.id}.kont" method="post">
      <wt:model id="calculator">
	<jx:if test="${wizard == 'term1'}">
	  <p><wt:error attribute="term1"/></p>
	  <p>
	    <b>Term 1:</b>
	    <wt:field type="text" attribute="term1"/>
	  </p>
	  <p>
	    <wt:form-button action="next">Next</wt:form-button>
	  </p>
	</jx:if>

	<jx:if test="${wizard == 'term2'}">
	  <p><wt:error attribute="term2"/></p>
	  <p>
	    <b>Term 2:</b>
	    <wt:field type="text" attribute="term2"/>
	  </p>
	  <p>
	    <wt:form-button action="previous">Previous</wt:form-button>
	    <wt:form-button action="next">Next</wt:form-button>
	  </p>
	</jx:if>

	<jx:if test="${wizard == 'sum'}">
	  <p>
	    <b>Sum:</b>
	    <wt:out attribute="sum"/>
	  </p>
	  <p>
	    <wt:form-button action="previous">Previous</wt:form-button>
	  </p>
	</jx:if>
      </wt:model>
      <p>
	<wt:button uri="${continuation.id}.kont" action="exit">Exit</wt:button>
      </p>
    </form>
  </body>
</html>
---------------------------------------------------

The flow is changed so that an if an erroneous value was entered it will
refuse to proceed to the next wizard page. We do this by creating the
validator, adding it to our eventhandler and then invoke it everytime a
"next" event is received. 

---------------------------------------------------
importClass(Packages.widgets.model.BeanModel);
importClass(Packages.widgets.samples.Calculator);
importClass(Packages.widgets.samples.CalculatorValidator);

cocoon.load("resource://widgets/flow/Page.js");

function public_calculator() {

    var calculator = new Calculator();
    var calculatorModel = new BeanModel(calculator);
    var calculatorHandler = new CalculatorHandler();
    calculatorHandler.validator = new CalculatorValidator();

    var page = new Page();
    page.setEventHandler(calculatorHandler);

    page.show("calculator.jx", {calculator: calculatorModel}, {wizard: "term1"});

    cocoon.sendPage("main.html");
}


function CalculatorHandler() {
}

CalculatorHandler.prototype.action_previous = function(requestProcessor, models, bizData) {
    models["calculator"].populate(requestProcessor.getParameters("calculator:"));
    if (bizData["wizard"] == "term2") {
	bizData["wizard"] = "term1";
    } else if (bizData["wizard"] == "sum") {
	bizData["wizard"] = "term2";
    }
    return true;
}

CalculatorHandler.prototype.action_next = function(requestProcessor, models, bizData) {
    models["calculator"].clearErrors();
    models["calculator"].populate(requestProcessor.getParameters("calculator:"));

    if (bizData["wizard"] == "term1") {
	var valid = this.validator.validatePhase1(models["calculator"]);
	if (valid) {
	    bizData["wizard"] = "term2";
	}
    } else if (bizData["wizard"] == "term2") {
	var valid = this.validator.validatePhase2(models["calculator"]);
	if (valid) {
	    bizData["wizard"] = "sum";
	}
    }
    return true;
}

CalculatorHandler.prototype.action_exit = function(requestProcessor, models, bizData) {
    return false;
}
---------------------------------------------------
		  
SAMPLES

I have provided a few samples of how to use the widget framework. 

Login sample
Uses the MapModel to implement a login page. A validator makes sure that
the user exist and has provided the correct password 

Calculator
Described in detail above 

Personnel Database
Use the ResultSetModel to edit information in a database. It also shows
some advanced uses of JXTemplate. 

Personnel DOM
Similar to above but instead uses a DOM as backend. 




-- 
---------------------------------------------------------------
Address  :   Rackarbergsg 74, 752 32 UPPSALA
Phone    :   018-50 69 28, 0768-767 747



Re: A widget framework (long)

Posted by Sylvain Wallez <sy...@apache.org>.
Reinhard Poetz wrote:

> Interesting paper that needs some time to read AND understand.


Indeed. I started, and I see good, bad and ugly :-)

More on this later...

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: A widget framework (long)

Posted by Jonas Ekstedt <ek...@ibg.uu.se>.
Hello

I really appreciate you taking the time to look through the code. I've
inlined some comments below.

On Thu, 2004-10-28 at 15:06, Sylvain Wallez wrote:
> Jonas Ekstedt wrote:
> 
> >On Thu, 2004-10-28 at 12:50, Reinhard Poetz wrote:
> >  
> >
> >>After reading it once, one question: Do you see any way to integrate your ideas 
> >>into Cocoon Forms?
> >>    
> >>
> >
> >One part that I believe could be incorporated into CForms would be how
> >the view model is populated.
> >  
> >
> 
> That's the "ugly" I was mentioning in my previous post : this is a very 
> dangerous approach, similar to the now defunct XMLForm (see [1], 
> "mapping to the application data model"). Having a direct and live 
> mapping from the form to the data model is an open door to inconsistent 
> business data in case validation fails.
> 
> Furthermore, just like XMLForm, your approach doesn't allow smart 
> convertors to be defined, taking into account formats, locale, etc. And 
> validation occurs on strings, which is just why we rejected the use of 
> Commons Validator, as CForms widgets have strongly typed values.

The models I provided do indeed map directly to the application object,
but this was on purpose. There are use cases when that is needed (eg.
when a bean provides derived values).

The next step is to create datamodels addressing the needs you
described. Here are some ideas that I've come up with:

* Cloning models
Operates on a clone of the application object. I'm working on a
ClonedBeanModel, and sketching a TransactionResultSetModel. The former 
clones the application bean, and only when the user so request, updates
the application bean. The TransactionResultSetModel would copy the
resultset to a separate datastructure and similarly only update the
database when finished.

* Typesafe models with value conversions
This is an extension of the the cloned models concept with the addition
of type checking and value conversion in the getValue/setValue
functions.

* Secure models
The user can condition getting/setting of values by defining a security
policy. Eg. A user with role="administrator" can set the property A to
any of {1, 2, 3} but a user with role="user" can only set it to any of
{1, 2}.

As an example of a typesafe model with value conversion I updated the
framework to include a CFormModel that wraps a Cocoon Forms form. You
can download it at http://home.student.uu.se/j/joek8725/widgets.html.
The CFormModel takes advantage of the type checking and value conversion
in CForms as well as enable you to protect your business object from
being inconsistent using cforms binding. It should be said however that
the CFormModel is a terrible hack and should only be considered as an
example.

I added some samples as well. A CFormModel based Calculation wizard that
shows you type checking as well as a Personnel editor that uses CForms
binding to protect the application object.

Cheers

-- 
Jonas


Re: A widget framework (long)

Posted by Sylvain Wallez <sy...@apache.org>.
Jonas Ekstedt wrote:

>On Thu, 2004-10-28 at 12:50, Reinhard Poetz wrote:
>  
>
>>After reading it once, one question: Do you see any way to integrate your ideas 
>>into Cocoon Forms?
>>    
>>
>
>One part that I believe could be incorporated into CForms would be how
>the view model is populated.
>  
>

That's the "ugly" I was mentioning in my previous post : this is a very 
dangerous approach, similar to the now defunct XMLForm (see [1], 
"mapping to the application data model"). Having a direct and live 
mapping from the form to the data model is an open door to inconsistent 
business data in case validation fails.

Furthermore, just like XMLForm, your approach doesn't allow smart 
convertors to be defined, taking into account formats, locale, etc. And 
validation occurs on strings, which is just why we rejected the use of 
Commons Validator, as CForms widgets have strongly typed values.

However, there are some good points in your approach, namely the 
reduction of the number of files: separation of form model and business 
data is a must have for robustness and security, but needs some 
additional work.

Reducing this work could be achieved by specialized widget types, e.g. a 
"BeanWidget" that would be a container that automatically creates child 
fields for all or part of a JavaBean's properties, and similarily 
implements the binding.

For the wizard thing, the upcoming widget states [3] (I know, I'm late 
on this...) will give an immediate solution, as building a wizard will 
simply consist in changing the state of container widgets and choosing 
template parts depending on that state.

Sylvain

[1] http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=105881304808076&w=2
[2] http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=109765913421574&w=2
[3] http://issues.apache.org/bugzilla/show_bug.cgi?id=31647

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: A widget framework (long)

Posted by Jonas Ekstedt <ek...@ibg.uu.se>.
On Thu, 2004-10-28 at 12:50, Reinhard Poetz wrote:
> After reading it once, one question: Do you see any way to integrate your ideas 
> into Cocoon Forms?

One part that I believe could be incorporated into CForms would be how
the view model is populated. 

Today each widget populates itself from the Request. This means that the
widget has to be aware of whether it should populate itself or not.

If instead we build a map at render time of the parameter name of each
widget field that has been rendered and expects to be populated from the
request (ie. form fields), then this map can be used to collect values
from the request and afterwards used to populate the widgets. This means
that if a widget wasn't rendered as an input widget then it won't be
affected during population.

This is what the RequestProcessor does. Tags that render form fields
will add parameter names to the RequestProcessor. After 
sendPageAndWait() returns you call RequestProcessor.process() which will
use this map to only retrieve the parameters that has been registered.

The registration of parameters would be done in the widget code that
actually renders the widget.

Another part of the framework that stands alone from the rest is the
event handling. One place I think it would make sense to add it is in
FOM_Cocoon so that you could do:

-----------------------------------------------
function public_myWizard() {

  cocoon.setEventHandler(new WizardHandler());
  cocoon.sendPageAndWait(...);
  cocoon.sendPage("finished.html");
}

function WizardHandler() {}

WizardHandler.prototype.action_previous = function(...) {...}
WizardHandler.prototype.action_next = function(...) {...}
-----------------------------------------------

The sendPageAndWait function would check if an event handler has been
set. If so it will further check if the request parameter "cocoon-event"
is set and then fire off the corresponding function.

// Jonas


--
> Reinhard
-- 
---------------------------------------------------------------
Address  :   Rackarbergsg 74, 752 32 UPPSALA
Phone    :   018-50 69 28, 0768-767 747



Re: A widget framework (long)

Posted by Reinhard Poetz <re...@apache.org>.
Interesting paper that needs some time to read AND understand.

After reading it once, one question: Do you see any way to integrate your ideas 
into Cocoon Forms?

--
Reinhard

Re: A widget framework (long)

Posted by Jonas Ekstedt <ek...@ibg.uu.se>.
Hello

Thanks for the comments, they are much appreciated. I've inlined some
comments myself below.

On Wed, 2004-10-27 at 23:02, Antonio Gallardo wrote:
> > WIDGETS
> >
> > "Widgets" is a widget framework for Cocoon based on the taglib block.
> 
> It remains me XSP. We are moving out of that because the mixing of concerns.

I don't understand why you think this is similar to XSP.

> Knowing that is clear the form definition is an abstract definition of the
> form without taking care of the redering view.

The main point I'm trying to make is that I think this is an abstraction
that harms more than helps. For example, we have to use bindings to map
data between the view model and the model. If we let the view talk
directly to the model the need for bindings completely disappears. The
abstraction of the view into a view model increases the complexity both
of the framework itself as well as the applications you write against
it.

> > Having a ViewModel as a buffer between the Model and the View
> > introduces a number of problems: First of all any changes made to the
> > model has to be synhronized with changes to the ViewModel (eg if a list
> > containg beans is expanded with one more bean then the ViewModel has to
> > create widgets for the added bean).
> 
> Not exactly, you can have a bean with 10 attributes and a given CForm can
> only use 5 of them. Of ourse you can build other CForms that can use 1 to
> 10 of them. You don't need to map everything in a 1-1 relation.

This is true for bean attributes, but if I have a bean containing a list
of other beans, then adding a new bean to that list will require
invoking the binding to update the view model.
  
> > Another problem is if I have several
> > widgets mapped to the same bean value. Then a change of value in one
> > widget won't update the values of the other widgets.
> 
> Do you know the CForms binding framework? We also used of some JS client
> side to manage that.

Using client-side JS is of course a solution but it is error-prone and a
hassle.

> > Another set of problems has to do with the View. I can't use viewlogic
> > (eg. JXTemplate) to alter which widgets are rendered.
> 
> You can use JXTemplate with CForms. Also there is a very interesting
> presentation that Sylvain did in GT2004:
> 
> http://wiki.apache.org/cocoon/GT2004Sylvain

The problem isn't so much hiding the widgets. The problem is in the
subsequent population of the view model. If I choose to use JXTemplate
to hide certain widgets then the view model has no way of knowing that
those widgets should be ignored during population. Thus the recent
discussions about widget states where you inform the view model which
widgets are visible, active etc.
 
> > It would be a
> > piece of cake to make a wizard using JXTemplate but because the
> > ViewModel has to know which widgets are rendered any changes to the
> > visibility of the widgets has to go through the ViewModel. This means
> > that to make a wizard (or tab-layout etc) I have to create them as
> > Cocoon Form widgets using Java which is cumbersome.
> 
> As above, the point is to have a clear Separation Of Concerns. We can
> easily render the same for to diferent devices in diferent formats.

I have no experience in device independant web programming so I
shouldn't really comment on the virtues of the view model in regards to
that. However my gut feeling is that if I would want to make my forms
accessible on other devices, the physical limitations of those devices
would necessitate using separate view models anyway. Eg a gigantic form
suitable for web viewing has to be split up as a wizard style form on a
cellular phone. In this case the abstraction of the form has gained me
nothing.

> > <?xml version="1.0"?>
> >
> > <html xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
> >   xmlns:wt="urn:widgets"
> >   xmlns:wi="http://widget/instance"
> >   >
> >
> >   <body>
> >     <h1>Calculator wizard</h1>
> >     <jx:if test="${wizard == 'term1'}">
> >       Page term1
> >       <a href="${continuation.id}.kont?cocoon-event=action:next">Next</a>
> >     </jx:if>
> >
> >     <jx:if test="${wizard == 'term2'}">
> >       Page term2
> >       <a
> > href="${continuation.id}.kont?cocoon-event=action:previous">Previous</a>
> >       <a href="${continuation.id}.kont?cocoon-event=action:next">Next</a>
> >     </jx:if>
> >
> >     <jx:if test="${wizard == 'sum'}">
> >       Page sum
> >       <a
> > href="${continuation.id}.kont?cocoon-event=action:previous">Previous</a>
> >     </jx:if>
> >     <a href="${continuation.id}.kont?cocoon-event=action:exit">Exit</a>
> >   </body>
> > </html>
> > ---------------------------------------------------
> >
> > The JXTemplate is nothing out of the ordinary. It will simply hide the
> > pages of the wizard that are not active (as set in ${wizard}).
> 
> AFAIK, the above is a mixing of concerns between the businness logic and
> the view in the same file. Much the same style of XSP. Is more clear if
> you have 3 diferent forms and let the controller decide wich of them will
> use in a particular case.

Again, I don't understand why you think this is similar to XSP. Also, I
don't think there is any business logic included in this page (I might
have misunderstood what you mean by business logic). It simply displays
the current wizard page (a view concern) that has been set in the
controller.

> ....
> 
> > ADDING THE MODEL
> >
> > The widget framework introduces the concept of Models. A Model is a
> > wrapper around your data so that the View can access it in a uniform
> > way. Currently there are four different type of Models:
> >
> > * BeanModel: wraps a POJO
> > * DOMModel: wraps a DOM
> > * MapModel: wraps a java.util.Map
> > * ResultSetModel: wraps a database query
> >
> > As we decided to represent the calculator as a bean we will choose to
> > wrap it in a BeanModel.
> 
> The binding framework is used in CForms for doing that.

The problem with the binding framework, as I've said above, is that the
binding has to be invoked everytime I change the model. This is not
problematic for simple forms. Consider, however, the usecase of derived
values. Suppose I have a bean that contains two values and a getter that
returns the sum of them. When I change one of the terms in the form I
will have to use the bindings to save then reload the view model in case
I want the updated sum.

> 
> > In order to show the model in the view we change the JXTemplate:
> >
> > ---------------------------------------------------
> > <?xml version="1.0"?>
> >
> > <html xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
> >   xmlns:wt="urn:widgets"
> >   xmlns:wi="http://widget/instance"
> >   >
> >
> >   <body>
> >     <h1>Calculator wizard</h1>
> >     <form action="${continuation.id}.kont" method="post">
> >       <wt:model id="calculator">
> > 	<jx:if test="${wizard == 'term1'}">
> > 	  <p>
> > 	    <b>Term 1:</b>
> > 	    <wt:field type="text" attribute="term1"/>
> > 	  </p>
> > 	  <p>
> > 	    <wt:form-button action="next">Next</wt:form-button>
> > 	  </p>
> > 	</jx:if>
> >
> > 	<jx:if test="${wizard == 'term2'}">
> > 	  <p>
> > 	    <b>Term 2:</b>
> > 	    <wt:field type="text" attribute="term2"/>
> > 	  </p>
> > 	  <p>
> > 	    <wt:form-button action="previous">Previous</wt:form-button>
> > 	    <wt:form-button action="next">Next</wt:form-button>
> > 	  </p>
> > 	</jx:if>
> >
> > 	<jx:if test="${wizard == 'sum'}">
> > 	  <p>
> > 	    <b>Sum:</b>
> > 	    <wt:out attribute="sum"/>
> > 	  </p>
> > 	  <p>
> > 	    <wt:form-button action="previous">Previous</wt:form-button>
> > 	  </p>
> > 	</jx:if>
> >       </wt:model>
> >       <a href="${continuation.id}.kont?cocoon-event=action:exit">Exit</a>
> >     </form>
> >   </body>
> > </html>
> > ---------------------------------------------------
> 
> More mixing of concerns. It is going very similar to XSP.

This is pretty much what your average CForms template look like. With
the added bonus that you decide at render time what form element should
be used to display the data. I don't understand why you think this is a
mixing of concerns. There is nothing here that isn't explicitly related
to how to render the view.

> Hmm.. See Sylvain GT2004 presentation. The problem is that most people
> think that the cform rendering stylesheets in cocoon samples are the only
> way to render the CForms. This is not true.

It is easy to change how widgets are rendered using jxtemplate or xslt
but the problem comes when the view model is populated. The view model
doesn't know that in a particular view rendering the widget was rendered
as a non-editable field and thus shouldn't be populated. The view model
always has to know the state of the widgets.

> > VALIDATION
> >
> > A very simple calculator validator can look like this:
> >
> > ---------------------------------------------------
> > package widgets.samples;
> >
> > import org.apache.commons.validator.GenericValidator;
> > import widgets.model.BeanModel;
> >
> > public class CalculatorValidator {
> >
> >     public boolean validatePhase1(BeanModel model) {
> >
> > 	Calculator calculator = (Calculator) model.getBean();
> >
> > 	if (! GenericValidator.isInt(calculator.getTerm1())) {
> > 	    model.setError("term1", "Value must be an integer");
> > 	    return false;
> > 	}
> > 	return true;
> >     }
> >
> >     public boolean validatePhase2(BeanModel model) {
> >
> > 	Calculator calculator = (Calculator) model.getBean();
> >
> > 	if (! GenericValidator.isInt(calculator.getTerm2())) {
> > 	    model.setError("term2", "Value must be an integer");
> > 	    return false;
> > 	}
> > 	return true;
> >     }
> > }
> > ---------------------------------------------------
> 
> CForms validation is more elegant to me.

This was only an example of how validation can be done. Of course there
should be more user friedly ways of creating validation specifications.
Currently I'm working on a XMLSchemaValidator to use against a DOMModel.
Another alternative is RelaxNG. For BeanModel one could use
commons-validator.

Cheers Jonas


-- 
---------------------------------------------------------------
Address  :   Rackarbergsg 74, 752 32 UPPSALA
Phone    :   018-50 69 28, 0768-767 747



Re: A widget framework (long)

Posted by Antonio Gallardo <ag...@agssa.net>.
Hi Jonas:

I found the lecture interesting. Showed me that we need to put more effort
in documenting the CForms framework. You are making some assertions about
the CForms framework that are no exactly true....

Please don't take bad the comments between lines, because we are
learning..... ;-)

> ------------------------------------------------------------------------
>
> WIDGETS
>
> "Widgets" is a widget framework for Cocoon based on the taglib block.

It remains me XSP. We are moving out of that because the mixing of concerns.

> THE PROBLEM WITH COCOON FORMS
>
> The problem (as I see it) with Cocoon Forms is its concept of a
> ViewModel (the widget tree) that sits in between the model and the view:
>     Model (eg. bean) <-> ViewModel <-> View (eg. JXTemplate)

AFAIK, in the above statement, we tend to saw the form definition just as
a "mapping" between the model and the view. That is not true:

The so called ViewModel in CForms is the "form definition" the idea of
that is very similar to "XForms":

http://w3.org/MarkUp/Forms/arch1.png

<snip from="XForms specs" src="http://w3.org/MarkUp/Forms/">
a specification of Web forms that can be used with a wide variety of
platforms including desktop computers, hand helds, information appliances,
and even paper.
</snip>

Knowing that is clear the form definition is an abstract definition of the
form without taking care of the redering view.

> Having a ViewModel as a buffer between the Model and the View
> introduces a number of problems: First of all any changes made to the
> model has to be synhronized with changes to the ViewModel (eg if a list
> containg beans is expanded with one more bean then the ViewModel has to
> create widgets for the added bean).

Not exactly, you can have a bean with 10 attributes and a given CForm can
only use 5 of them. Of ourse you can build other CForms that can use 1 to
10 of them. You don't need to map everything in a 1-1 relation.

>From my experience, we have some Beans that need to be transformed before
passing it to a given CForm. To load the bean into de CForm we use
form.load(...).


> Another problem is if I have several
> widgets mapped to the same bean value. Then a change of value in one
> widget won't update the values of the other widgets.

Do you know the CForms binding framework? We also used of some JS client
side to manage that.

> Another set of problems has to do with the View. I can't use viewlogic
> (eg. JXTemplate) to alter which widgets are rendered.

You can use JXTemplate with CForms. Also there is a very interesting
presentation that Sylvain did in GT2004:

http://wiki.apache.org/cocoon/GT2004Sylvain

> It would be a
> piece of cake to make a wizard using JXTemplate but because the
> ViewModel has to know which widgets are rendered any changes to the
> visibility of the widgets has to go through the ViewModel. This means
> that to make a wizard (or tab-layout etc) I have to create them as
> Cocoon Form widgets using Java which is cumbersome.

As above, the point is to have a clear Separation Of Concerns. We can
easily render the same for to diferent devices in diferent formats.

> THE FRAMEWORK

...
>
> THE PAGE OBJECT
>
> <?xml version="1.0"?>
>
> <html xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
>   xmlns:wt="urn:widgets"
>   xmlns:wi="http://widget/instance"
>   >
>
>   <body>
>     <h1>Calculator wizard</h1>
>     <jx:if test="${wizard == 'term1'}">
>       Page term1
>       <a href="${continuation.id}.kont?cocoon-event=action:next">Next</a>
>     </jx:if>
>
>     <jx:if test="${wizard == 'term2'}">
>       Page term2
>       <a
> href="${continuation.id}.kont?cocoon-event=action:previous">Previous</a>
>       <a href="${continuation.id}.kont?cocoon-event=action:next">Next</a>
>     </jx:if>
>
>     <jx:if test="${wizard == 'sum'}">
>       Page sum
>       <a
> href="${continuation.id}.kont?cocoon-event=action:previous">Previous</a>
>     </jx:if>
>     <a href="${continuation.id}.kont?cocoon-event=action:exit">Exit</a>
>   </body>
> </html>
> ---------------------------------------------------
>
> The JXTemplate is nothing out of the ordinary. It will simply hide the
> pages of the wizard that are not active (as set in ${wizard}).

AFAIK, the above is a mixing of concerns between the businness logic and
the view in the same file. Much the same style of XSP. Is more clear if
you have 3 diferent forms and let the controller decide wich of them will
use in a particular case.

....

> ADDING THE MODEL
>
> The widget framework introduces the concept of Models. A Model is a
> wrapper around your data so that the View can access it in a uniform
> way. Currently there are four different type of Models:
>
> * BeanModel: wraps a POJO
> * DOMModel: wraps a DOM
> * MapModel: wraps a java.util.Map
> * ResultSetModel: wraps a database query
>
> As we decided to represent the calculator as a bean we will choose to
> wrap it in a BeanModel.

The binding framework is used in CForms for doing that.

> In order to show the model in the view we change the JXTemplate:
>
> ---------------------------------------------------
> <?xml version="1.0"?>
>
> <html xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
>   xmlns:wt="urn:widgets"
>   xmlns:wi="http://widget/instance"
>   >
>
>   <body>
>     <h1>Calculator wizard</h1>
>     <form action="${continuation.id}.kont" method="post">
>       <wt:model id="calculator">
> 	<jx:if test="${wizard == 'term1'}">
> 	  <p>
> 	    <b>Term 1:</b>
> 	    <wt:field type="text" attribute="term1"/>
> 	  </p>
> 	  <p>
> 	    <wt:form-button action="next">Next</wt:form-button>
> 	  </p>
> 	</jx:if>
>
> 	<jx:if test="${wizard == 'term2'}">
> 	  <p>
> 	    <b>Term 2:</b>
> 	    <wt:field type="text" attribute="term2"/>
> 	  </p>
> 	  <p>
> 	    <wt:form-button action="previous">Previous</wt:form-button>
> 	    <wt:form-button action="next">Next</wt:form-button>
> 	  </p>
> 	</jx:if>
>
> 	<jx:if test="${wizard == 'sum'}">
> 	  <p>
> 	    <b>Sum:</b>
> 	    <wt:out attribute="sum"/>
> 	  </p>
> 	  <p>
> 	    <wt:form-button action="previous">Previous</wt:form-button>
> 	  </p>
> 	</jx:if>
>       </wt:model>
>       <a href="${continuation.id}.kont?cocoon-event=action:exit">Exit</a>
>     </form>
>   </body>
> </html>
> ---------------------------------------------------

More mixing of concerns. It is going very similar to XSP.

> The wt:model tag is used to setup the context that subsequent widget
> tags will operate on. In this case we identify the model as "calculator"
> (which is the key we used in the page.show() method call. This means
> that any descendant tags will look up their values in our calculator
> bean. It is possible to supply the view with several models.
>
> When the wt:field tag is executed (during taglib transformation) it will
> fetch the value of attribute "term1" from the containing model (in this
> case the calculator bean). Subsequent xslt transformation will turn it
> into a html input text tag.
>
> The wt:form-button will translate into a html button element. The name
> of the buttons will be "cocoon-event" and its value will be set to eg.
> "action:next" so that a button press will trigger the event handling in
> the Page object.
>
> Contrary to Cocoon Forms we can choose at render time the type of widget
> used to show model values. In Cocoon Forms it is very difficult to
> change how we represent data, eg. once you have decided that term1
> should be rendered as a text field you cannot change that very easy.

Hmm.. See Sylvain GT2004 presentation. The problem is that most people
think that the cform rendering stylesheets in cocoon samples are the only
way to render the CForms. This is not true.

BTW, the are some RFE to build new samples for cform using XUL and another
using WML as the target format....

> Here we can use a text field, a select box or whatever to render term1,
> we can even render the value of term1 several times using different
> types of widgets.

Same in CForms.

>
> VALIDATION
>
> A very simple calculator validator can look like this:
>
> ---------------------------------------------------
> package widgets.samples;
>
> import org.apache.commons.validator.GenericValidator;
> import widgets.model.BeanModel;
>
> public class CalculatorValidator {
>
>     public boolean validatePhase1(BeanModel model) {
>
> 	Calculator calculator = (Calculator) model.getBean();
>
> 	if (! GenericValidator.isInt(calculator.getTerm1())) {
> 	    model.setError("term1", "Value must be an integer");
> 	    return false;
> 	}
> 	return true;
>     }
>
>     public boolean validatePhase2(BeanModel model) {
>
> 	Calculator calculator = (Calculator) model.getBean();
>
> 	if (! GenericValidator.isInt(calculator.getTerm2())) {
> 	    model.setError("term2", "Value must be an integer");
> 	    return false;
> 	}
> 	return true;
>     }
> }
> ---------------------------------------------------

CForms validation is more elegant to me.

Best Regards,

Antonio Gallardo

Re: A widget framework (long)

Posted by Sylvain Wallez <sy...@apache.org>.
Jonas Ekstedt wrote:

>Hello
>
>I have created a widget framework that I would like to donate to Cocoon
>(if you want it). I think it has several advantages to the current
>Cocoon Forms framework in that it is more straightforward to use and
>that it is easier to extend.
>

Interesting stuff. I'll take the time to study it in depth and give some 
feedback.

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }