You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Varun Mehta <va...@persistent.co.in> on 2005/11/15 09:58:18 UTC

Spring + Hibernate + Tapestry + Hivemind [if required]

Hi All,

 

I've been reading some of "Tapestry + Hibernate" and did some basic CRUD
applications using them as given on the wiki (brown bag). I've found many
applications that use, "Tapestry + Spring + Hibernate" hand in hand for
development (The best example is trails, which sounds promising), but I
could not get a satisfactory reason why people use them.

 

Also every .page/.java file which has to called/redirect has to manually
specify the page file to call, as in

 

AddEditBook nextPage = (AddEditBook) cycle.getPage("AddEditBook");

 

Now we don't have a centralized location to specify such configuration (or
I'm not aware about it, like struts-config.xml), is this a reason people
plug it with Spring. Meanwhile I'll try to study some of trails.

 

Regards
Varun Mehta

Member of Technical Staff,
Persistent Systems Pvt. Ltd.,
Direct: +91 20 3023 4656
Board: +91 20 3023 4000 Ext: 4656
Visit PSPL at

*	 http://persistentsys.com <http://persistentsys.com/>    

 

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
imagination is more important than knowledge - albert einstein 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

 

Visit Varun at 

*	http://varun.cjb.net <http://varun.cjb.net/>   [HTML] 
*	http://varuninfo.cjb.net <http://varuninfo.cjb.net/>   [Blogged] 
*	http://varunmehta.cjb.net <http://varunmehta.cjb.net/>   [Flash] 

 

Persistent Systems -Software Development Partner for Competitive Advantage.
Persistent Systems provides custom software product development services.
With over 15 years, 140 customers, and 700+ release cycles experience, we
deliver unmatched value through high quality, faster time to market and
lower total costs.

 

 


Re: Spring + Hibernate + Tapestry + Hivemind [if required]

Posted by Markus Joschko <ma...@gmail.com>.
mhm, have you ever looked at the spring webflow project?
It seems like they have already done a lot in this direction. The only
thing missing is the integration with tapestry :-(

markus

On 11/15/05, John Coleman <jo...@ntlworld.com> wrote:
> > Now we don't have a centralized location to specify such configuration (or
> > I'm not aware about it, like struts-config.xml), is this a reason people
> > plug it with Spring. Meanwhile I'll try to study some of trails.
>
> On the larger projects I have worked on, the team often come into conflict
> with using struts-config - most people seem to want it most of the time,
> especially at the start of the project. With Tapestry developers can at
> least deal with well decoupled units of work.
>
> Having said that I did start work on an idea to drive the navigation using
> Drools. This would operate like struts-config, but would also have potential
> to create far more complex navigation experiences, ideal for the more
> sophisticated web shops, who need to tailor for a clients individual needs.
> Of course, it does mean you have some untestable loosly bounds script doing
> a significant job! This is satisfying from an architectural perspective,
> because it farms out business logic, and lets the page be really simple. Nav
> rules are really part of the business logic, yet another reason why Struts
> is pants IMHO. Ideally pages should not contain any logic for what comes
> next.
>
> I started by extending BasePage, then you can implement a next page method
> and let Drools handle the actual value used. Here is an example of the
> good'ol max login attempt handling. Rather too simple to justify the method,
> but just an example. I'm conscious now that this should be handled by a
> hivemind service, but this code was written for Tapestry 3 standalone, and
> is rather yukky. A much nicer approach would be for a simple service that
> returns the name of the next page. The unpleasantry is in marshalling the
> required data for the working memory - so that the method is not fully
> decoupled. It would be nice for something to do that as required by the
> rules.
>
> You could also write your own digester to implement a struts-config like
> navigation service. You could even copy the relivant parts of the
> struts-config dtd. And you could make the digester accomodate multiple
> files, each containing its own nav rules, and merge them, thus avoiding file
> sharing conflicts. The Drools approach gives you far more flexibility
> though.
>
> John
>
> public class DRLBasePage extends BasePage {
>
> static URL url;
>
> static RuleBase ruleBase;
>
> WorkingMemory workingMemory;
>
> static {
>
> try {
>
> url = DRLBasePage.class.getResource("cycle.drl");
>
> ruleBase = RuleBaseBuilder.buildFromUrl(url);
>
> } catch (Exception e) {e.printStackTrace();}
>
> }
>
>
> public DRLBasePage() {
>
> workingMemory = ruleBase.newWorkingMemory();
>
> }
>
> // method to obtain the next page for the given working memory and cycle
>
> public void nextPage(IRequestCycle cycle) throws FactException {
>
> workingMemory.assertObject(this); // pass in this page object
>
> workingMemory.setApplicationData("cycle", cycle); // pass in the cycle to
> allow Drools to perform the forward
>
> // you may want to load additional data into working memory here which sadly
> starts to create loose coupling!
>
> // a suggestion is to ensure those data are page properties instead, so they
> can be picked up with the assert object method
>
> workingMemory.fireAllRules(); // this will implement the next page nav
>
> }
>
> }
>
>
>
> /*
>
> * If the user enters valid credentials they are stored in the visit
>
> * and an attempt is made to go to the next page.
>
> *
>
> */
>
> package examples;
>
> import org.apache.tapestry.IRequestCycle;
>
> import html.DRLBasePage;
>
> /**
>
> * Login page.
>
> */
>
> public abstract class Login extends DRLBasePage
>
> {
>
> public abstract String getUserName();
>
> public abstract String getPassword();
>
> public abstract void setMessage(String message);
>
> public void login(IRequestCycle cycle)
>
> {
>
> if (isValidLogin(getUserName(), getPassword()))
>
> {
>
> // Get the Visit and cast it to our application-specific
>
> // class.
>
> ((Visit) getVisit()).doLogin(getUserName(), getPassword());
>
> // use Drools to obtain next page
>
> try {
>
> nextPage(cycle);
>
> } catch (Exception e) {e.printStackTrace();}
>
> return;
>
> }
>
> // this sets the message property for the page to display
>
> setMessage("invalid user name or password - attempt "
>
> + ((Visit) getVisit()).getLoginAttempts());
>
> }
>
> private boolean isValidLogin(String userName, String password)
>
> {
>
> ((examples.Visit) getVisit()).incLoginAttempts();
>
> return "tapestry".equalsIgnoreCase(userName);
>
> }
>
> }
>
>
>
>
>
>
>
> // a problem with this method is the need of the page to load any data that
> the rules need, so that there is some yukky loose coupling
>
> // any ideas on how to get that code out much appreciated, but initially it
> is best to use page properties
>
> <?xml version="1.0"?>
>
> <rule-set name="Cycle" xmlns="http://drools.org/rules"
> xmlns:java="http://drools.org/semantics/java">
>
> <rule name="login_success">
>
> <parameter identifier="page">
>
> <java:class>html.DRLBasePage</java:class>
>
> </parameter>
>
> <java:condition>page instanceof examples.Login</java:condition>
>
> <java:condition>((examples.Visit) page.getVisit()).getLoginAttempts() &lt;
> 4</java:condition>
>
> <java:consequence>
>
> ((examples.Visit) page.getVisit()).logAccess();
>
> cycle.activate("Main");
>
> </java:consequence>
>
> </rule>
>
> <rule name="login_failure">
>
> <parameter identifier="page">
>
> <java:class>html.DRLBasePage</java:class>
>
> </parameter>
>
> <java:condition>page instanceof examples.Login</java:condition>
>
> <java:consequence>
>
> ((examples.Login) page).setMessage("Too many login attempts!");
>
> </java:consequence>
>
> </rule>
>
> </rule-set>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Spring + Hibernate + Tapestry + Hivemind [if required]

Posted by Varun Mehta <va...@persistent.co.in>.
Thanks for your feedback guys
 
Regards
Varun Mehta
Member of Technical Staff,
Persistent Systems Pvt. Ltd.,
Direct: +91 20 3023 4656
Board: +91 20 3023 4000 Ext: 4656
Visit PSPL at
*	 http://persistentsys.com   
 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
imagination is more important than knowledge - albert einstein 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 
Visit Varun at 
*	http://varun.cjb.net  [HTML] 
*	http://varuninfo.cjb.net  [Blogged] 
*	http://varunmehta.cjb.net  [Flash] 
 
 
-----Original Message-----
From: John Coleman [mailto:john.s.coleman@ntlworld.com] 
Sent: Tuesday, November 15, 2005 5:45 PM
To: Tapestry users
Subject: Re: Spring + Hibernate + Tapestry + Hivemind [if required]

Varun,

Tapestry seems to be unique in the way it seperates page presentation from
implementation, and that may appeal to Spring users who have invested a lot
of time in their business layer code, but would like a Tapestry front end.
Of course we have Hivemind to implement services, but Spring is still a
great way to do the container and popular.

You don't have to use Spring, I guess compatibility just makes it easier for
Spring projects to be redone using Tapestry. If you are starting off a
project from scratch, just forget Spring and use Hivemind or EJBs instead.
At the moment, for our dev team find even that is too much, so they just did
DAOs and code those straight into the page classes. On a trivial webapp that
is probably acceptable, but for bigger projects, you need some container
technology to decouple things nicely, so you can implement business objects
in an orderly fashion. I'm hoping to use Hivemind plus Cayenne in our next
project. Hivemind will provide both business object service, like session
beans, as well as internal services to provide persistence with Cayenne,
like entity beans.

It's all possible to achieve the above, but getting the navigation done in a
well decoupled fashion, with flexibility is ambitious. I'm not keen on the
struts-config model because it is only a way of allowing you some
flexibility in routing at design time, that in reality is not effective,
bcause the business rules wind up in the actions. Maybe configurable actions
delivered somehow as services are an idea?

John

----- Original Message ----- 
From: "Varun Mehta" <va...@persistent.co.in>
To: "'Tapestry users'" <ta...@jakarta.apache.org>
Sent: Tuesday, November 15, 2005 10:56 AM
Subject: RE: Spring + Hibernate + Tapestry + Hivemind [if required]


> Hey John,
>
> Thanks for the example.
>
> I'm not bent upon using struts-config.xml(just used it as an example), but
> am looking at a 'better' option (if any) to mark the next page to be
called
> incase of Tapestry.
>
> My prime question still remains unanswered, why do we need to use other
> frameworks like Spring etc... to be plugged in the Tapestry.
>
> Regards
> Varun Mehta
> Member of Technical Staff,
> Persistent Systems Pvt. Ltd.,
> Direct: +91 20 3023 4656
> Board: +91 20 3023 4000 Ext: 4656
> Visit PSPL at
> * http://persistentsys.com
>
> *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
> imagination is more important than knowledge - albert einstein
> *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
>
> Visit Varun at
> * http://varun.cjb.net  [HTML]
> * http://varuninfo.cjb.net  [Blogged]
> * http://varunmehta.cjb.net  [Flash]
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Spring + Hibernate + Tapestry + Hivemind [if required]

Posted by John Coleman <jo...@ntlworld.com>.
Varun,

Tapestry seems to be unique in the way it seperates page presentation from
implementation, and that may appeal to Spring users who have invested a lot
of time in their business layer code, but would like a Tapestry front end.
Of course we have Hivemind to implement services, but Spring is still a
great way to do the container and popular.

You don't have to use Spring, I guess compatibility just makes it easier for
Spring projects to be redone using Tapestry. If you are starting off a
project from scratch, just forget Spring and use Hivemind or EJBs instead.
At the moment, for our dev team find even that is too much, so they just did
DAOs and code those straight into the page classes. On a trivial webapp that
is probably acceptable, but for bigger projects, you need some container
technology to decouple things nicely, so you can implement business objects
in an orderly fashion. I'm hoping to use Hivemind plus Cayenne in our next
project. Hivemind will provide both business object service, like session
beans, as well as internal services to provide persistence with Cayenne,
like entity beans.

It's all possible to achieve the above, but getting the navigation done in a
well decoupled fashion, with flexibility is ambitious. I'm not keen on the
struts-config model because it is only a way of allowing you some
flexibility in routing at design time, that in reality is not effective,
bcause the business rules wind up in the actions. Maybe configurable actions
delivered somehow as services are an idea?

John

----- Original Message ----- 
From: "Varun Mehta" <va...@persistent.co.in>
To: "'Tapestry users'" <ta...@jakarta.apache.org>
Sent: Tuesday, November 15, 2005 10:56 AM
Subject: RE: Spring + Hibernate + Tapestry + Hivemind [if required]


> Hey John,
>
> Thanks for the example.
>
> I'm not bent upon using struts-config.xml(just used it as an example), but
> am looking at a 'better' option (if any) to mark the next page to be
called
> incase of Tapestry.
>
> My prime question still remains unanswered, why do we need to use other
> frameworks like Spring etc... to be plugged in the Tapestry.
>
> Regards
> Varun Mehta
> Member of Technical Staff,
> Persistent Systems Pvt. Ltd.,
> Direct: +91 20 3023 4656
> Board: +91 20 3023 4000 Ext: 4656
> Visit PSPL at
> * http://persistentsys.com
>
> *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
> imagination is more important than knowledge - albert einstein
> *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
>
> Visit Varun at
> * http://varun.cjb.net  [HTML]
> * http://varuninfo.cjb.net  [Blogged]
> * http://varunmehta.cjb.net  [Flash]
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Spring + Hibernate + Tapestry + Hivemind [if required]

Posted by Gunna Satria Hijrah Kusumah <gn...@nwa.iao.co.id>.
Hi Varun,

IMHO you don't have to use Spring if you feel you don't need to use it. It 
only add the complexity.
I learn that we can not use one solution to every problem.
So, when you need to use Spring? well, you can read documentations about it, 
maybe Spring in Action or etc, there you can find the benefits of Spring 
framework.
As i remember one of the advantage of Spring is the Inversion of Control.

regards,

Gunna
On Tuesday 15 November 2005 17:56, Varun Mehta wrote:
> Hey John,
>
> Thanks for the example.
>
> I'm not bent upon using struts-config.xml(just used it as an example), but
> am looking at a 'better' option (if any) to mark the next page to be called
> incase of Tapestry.
>
> My prime question still remains unanswered, why do we need to use other
> frameworks like Spring etc... to be plugged in the Tapestry.
>
> Regards
> Varun Mehta
> Member of Technical Staff,
> Persistent Systems Pvt. Ltd.,
> Direct: +91 20 3023 4656
> Board: +91 20 3023 4000 Ext: 4656
> Visit PSPL at
> *	 http://persistentsys.com
>
> *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
> imagination is more important than knowledge - albert einstein
> *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
>
> Visit Varun at
> *	http://varun.cjb.net  [HTML]
> *	http://varuninfo.cjb.net  [Blogged]
> *	http://varunmehta.cjb.net  [Flash]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Spring + Hibernate + Tapestry + Hivemind [if required]

Posted by Varun Mehta <va...@persistent.co.in>.
Hey John,

Thanks for the example.

I'm not bent upon using struts-config.xml(just used it as an example), but
am looking at a 'better' option (if any) to mark the next page to be called
incase of Tapestry.

My prime question still remains unanswered, why do we need to use other
frameworks like Spring etc... to be plugged in the Tapestry.

Regards
Varun Mehta
Member of Technical Staff,
Persistent Systems Pvt. Ltd.,
Direct: +91 20 3023 4656
Board: +91 20 3023 4000 Ext: 4656
Visit PSPL at
*	 http://persistentsys.com   
 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
imagination is more important than knowledge - albert einstein 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 
Visit Varun at 
*	http://varun.cjb.net  [HTML] 
*	http://varuninfo.cjb.net  [Blogged] 
*	http://varunmehta.cjb.net  [Flash] 
 
 

-----Original Message-----
From: John Coleman [mailto:john.s.coleman@ntlworld.com] 
Sent: Tuesday, November 15, 2005 4:04 PM
To: Tapestry users
Subject: Re: Spring + Hibernate + Tapestry + Hivemind [if required]

> Now we don't have a centralized location to specify such configuration (or
> I'm not aware about it, like struts-config.xml), is this a reason people
> plug it with Spring. Meanwhile I'll try to study some of trails.

On the larger projects I have worked on, the team often come into conflict
with using struts-config - most people seem to want it most of the time,
especially at the start of the project. With Tapestry developers can at
least deal with well decoupled units of work.

Having said that I did start work on an idea to drive the navigation using
Drools. This would operate like struts-config, but would also have potential
to create far more complex navigation experiences, ideal for the more
sophisticated web shops, who need to tailor for a clients individual needs.
Of course, it does mean you have some untestable loosly bounds script doing
a significant job! This is satisfying from an architectural perspective,
because it farms out business logic, and lets the page be really simple. Nav
rules are really part of the business logic, yet another reason why Struts
is pants IMHO. Ideally pages should not contain any logic for what comes
next.

I started by extending BasePage, then you can implement a next page method
and let Drools handle the actual value used. Here is an example of the
good'ol max login attempt handling. Rather too simple to justify the method,
but just an example. I'm conscious now that this should be handled by a
hivemind service, but this code was written for Tapestry 3 standalone, and
is rather yukky. A much nicer approach would be for a simple service that
returns the name of the next page. The unpleasantry is in marshalling the
required data for the working memory - so that the method is not fully
decoupled. It would be nice for something to do that as required by the
rules.

You could also write your own digester to implement a struts-config like
navigation service. You could even copy the relivant parts of the
struts-config dtd. And you could make the digester accomodate multiple
files, each containing its own nav rules, and merge them, thus avoiding file
sharing conflicts. The Drools approach gives you far more flexibility
though.

John

public class DRLBasePage extends BasePage {

static URL url;

static RuleBase ruleBase;

WorkingMemory workingMemory;

static {

try {

url = DRLBasePage.class.getResource("cycle.drl");

ruleBase = RuleBaseBuilder.buildFromUrl(url);

} catch (Exception e) {e.printStackTrace();}

}


public DRLBasePage() {

workingMemory = ruleBase.newWorkingMemory();

}

// method to obtain the next page for the given working memory and cycle

public void nextPage(IRequestCycle cycle) throws FactException {

workingMemory.assertObject(this); // pass in this page object

workingMemory.setApplicationData("cycle", cycle); // pass in the cycle to
allow Drools to perform the forward

// you may want to load additional data into working memory here which sadly
starts to create loose coupling!

// a suggestion is to ensure those data are page properties instead, so they
can be picked up with the assert object method

workingMemory.fireAllRules(); // this will implement the next page nav

}

}



/*

* If the user enters valid credentials they are stored in the visit

* and an attempt is made to go to the next page.

*

*/

package examples;

import org.apache.tapestry.IRequestCycle;

import html.DRLBasePage;

/**

* Login page.

*/

public abstract class Login extends DRLBasePage

{

public abstract String getUserName();

public abstract String getPassword();

public abstract void setMessage(String message);

public void login(IRequestCycle cycle)

{

if (isValidLogin(getUserName(), getPassword()))

{

// Get the Visit and cast it to our application-specific

// class.

((Visit) getVisit()).doLogin(getUserName(), getPassword());

// use Drools to obtain next page

try {

nextPage(cycle);

} catch (Exception e) {e.printStackTrace();}

return;

}

// this sets the message property for the page to display

setMessage("invalid user name or password - attempt "

+ ((Visit) getVisit()).getLoginAttempts());

}

private boolean isValidLogin(String userName, String password)

{

((examples.Visit) getVisit()).incLoginAttempts();

return "tapestry".equalsIgnoreCase(userName);

}

}







// a problem with this method is the need of the page to load any data that
the rules need, so that there is some yukky loose coupling

// any ideas on how to get that code out much appreciated, but initially it
is best to use page properties

<?xml version="1.0"?>

<rule-set name="Cycle" xmlns="http://drools.org/rules"
xmlns:java="http://drools.org/semantics/java">

<rule name="login_success">

<parameter identifier="page">

<java:class>html.DRLBasePage</java:class>

</parameter>

<java:condition>page instanceof examples.Login</java:condition>

<java:condition>((examples.Visit) page.getVisit()).getLoginAttempts() &lt;
4</java:condition>

<java:consequence>

((examples.Visit) page.getVisit()).logAccess();

cycle.activate("Main");

</java:consequence>

</rule>

<rule name="login_failure">

<parameter identifier="page">

<java:class>html.DRLBasePage</java:class>

</parameter>

<java:condition>page instanceof examples.Login</java:condition>

<java:consequence>

((examples.Login) page).setMessage("Too many login attempts!");

</java:consequence>

</rule>

</rule-set>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Spring + Hibernate + Tapestry + Hivemind [if required]

Posted by John Coleman <jo...@ntlworld.com>.
> Now we don't have a centralized location to specify such configuration (or
> I'm not aware about it, like struts-config.xml), is this a reason people
> plug it with Spring. Meanwhile I'll try to study some of trails.

On the larger projects I have worked on, the team often come into conflict
with using struts-config - most people seem to want it most of the time,
especially at the start of the project. With Tapestry developers can at
least deal with well decoupled units of work.

Having said that I did start work on an idea to drive the navigation using
Drools. This would operate like struts-config, but would also have potential
to create far more complex navigation experiences, ideal for the more
sophisticated web shops, who need to tailor for a clients individual needs.
Of course, it does mean you have some untestable loosly bounds script doing
a significant job! This is satisfying from an architectural perspective,
because it farms out business logic, and lets the page be really simple. Nav
rules are really part of the business logic, yet another reason why Struts
is pants IMHO. Ideally pages should not contain any logic for what comes
next.

I started by extending BasePage, then you can implement a next page method
and let Drools handle the actual value used. Here is an example of the
good'ol max login attempt handling. Rather too simple to justify the method,
but just an example. I'm conscious now that this should be handled by a
hivemind service, but this code was written for Tapestry 3 standalone, and
is rather yukky. A much nicer approach would be for a simple service that
returns the name of the next page. The unpleasantry is in marshalling the
required data for the working memory - so that the method is not fully
decoupled. It would be nice for something to do that as required by the
rules.

You could also write your own digester to implement a struts-config like
navigation service. You could even copy the relivant parts of the
struts-config dtd. And you could make the digester accomodate multiple
files, each containing its own nav rules, and merge them, thus avoiding file
sharing conflicts. The Drools approach gives you far more flexibility
though.

John

public class DRLBasePage extends BasePage {

static URL url;

static RuleBase ruleBase;

WorkingMemory workingMemory;

static {

try {

url = DRLBasePage.class.getResource("cycle.drl");

ruleBase = RuleBaseBuilder.buildFromUrl(url);

} catch (Exception e) {e.printStackTrace();}

}


public DRLBasePage() {

workingMemory = ruleBase.newWorkingMemory();

}

// method to obtain the next page for the given working memory and cycle

public void nextPage(IRequestCycle cycle) throws FactException {

workingMemory.assertObject(this); // pass in this page object

workingMemory.setApplicationData("cycle", cycle); // pass in the cycle to
allow Drools to perform the forward

// you may want to load additional data into working memory here which sadly
starts to create loose coupling!

// a suggestion is to ensure those data are page properties instead, so they
can be picked up with the assert object method

workingMemory.fireAllRules(); // this will implement the next page nav

}

}



/*

* If the user enters valid credentials they are stored in the visit

* and an attempt is made to go to the next page.

*

*/

package examples;

import org.apache.tapestry.IRequestCycle;

import html.DRLBasePage;

/**

* Login page.

*/

public abstract class Login extends DRLBasePage

{

public abstract String getUserName();

public abstract String getPassword();

public abstract void setMessage(String message);

public void login(IRequestCycle cycle)

{

if (isValidLogin(getUserName(), getPassword()))

{

// Get the Visit and cast it to our application-specific

// class.

((Visit) getVisit()).doLogin(getUserName(), getPassword());

// use Drools to obtain next page

try {

nextPage(cycle);

} catch (Exception e) {e.printStackTrace();}

return;

}

// this sets the message property for the page to display

setMessage("invalid user name or password - attempt "

+ ((Visit) getVisit()).getLoginAttempts());

}

private boolean isValidLogin(String userName, String password)

{

((examples.Visit) getVisit()).incLoginAttempts();

return "tapestry".equalsIgnoreCase(userName);

}

}







// a problem with this method is the need of the page to load any data that
the rules need, so that there is some yukky loose coupling

// any ideas on how to get that code out much appreciated, but initially it
is best to use page properties

<?xml version="1.0"?>

<rule-set name="Cycle" xmlns="http://drools.org/rules"
xmlns:java="http://drools.org/semantics/java">

<rule name="login_success">

<parameter identifier="page">

<java:class>html.DRLBasePage</java:class>

</parameter>

<java:condition>page instanceof examples.Login</java:condition>

<java:condition>((examples.Visit) page.getVisit()).getLoginAttempts() &lt;
4</java:condition>

<java:consequence>

((examples.Visit) page.getVisit()).logAccess();

cycle.activate("Main");

</java:consequence>

</rule>

<rule name="login_failure">

<parameter identifier="page">

<java:class>html.DRLBasePage</java:class>

</parameter>

<java:condition>page instanceof examples.Login</java:condition>

<java:consequence>

((examples.Login) page).setMessage("Too many login attempts!");

</java:consequence>

</rule>

</rule-set>



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org