Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Use Continuations to Develop Complex Web Applications

By Abhijit Belapurkar
2005-04-22


The Application Sitemap

My next step is to write the application logic into my FlowControl file -- in this case, pos.js. As I mentioned, the file contains a function called sellItem that kicks off the application flow, as shown in Listing 4:

Listing 4. Application flow implemented in JavaScript

function sellItem()

{
var rate, qty, zone, amount, discount, total, discrate, savings, delOpt, delCost, Webcon;

var url = "page/getRateAmt";
cocoon.sendPageAndWait(url);
rate = parseFloat(cocoon.request.getParameter("rate"));
qty = parseInt(cocoon.request.getParameter("qty"));
amount = rate*qty;

url="page/getZone";
Webcon = cocoon.sendPageAndWait(url, {"rate":rate, "qty":qty});
zone = cocoon.request.getParameter("zone");

discount=0.02;

if (zone=="A")
{
if (qty >= 100)
{
discount=0.1;
}
}
else if (zone=="B")
{
if (qty >= 200)
{
discount=0.2;
}
}

discrate = 100*discount;
savings = discount*amount;

delCost=0.0;
delOpt = cocoon.request.getParameter("delOpt");
if (delOpt=="S")
{
url="page/getShipOpt";
cocoon.sendPageAndWait(url);
delCost = parseInt(cocoon.request.getParameter("delCost"));
}

total = amount + delCost - savings;
url="page/displayResult";
cocoon.sendPageAndWait(url, {"discrate":discrate, "total":total, "savings":savings,
"delCost":delCost, "amount":amount, "discount":discount, "zone":zone});
}
The cocoon object and its functions
You will notice in this example I have used an object called cocoon without having first declared it elsewhere. I didn't need to declare cocoon because it is part of a set of default system objects provided by Cocoon for use in Flowscripts. This set of objects is called the Flow Object Model (FOM).

The cocoon object is probably the most important and used object in the FOM set and is also the point of entry into the FOM. It is a global variable that represents the current sitemap. It provides two important functions called sendPage and sendPageAndWait. Both functions pass control to the Cocoon sitemap for generating the output page.

The former function takes two arguments, one the sitemap URI of the page to be sent back to the client and the other a context object containing data that can be extracted and used to replace placeholders within the generated page.

The latter function, which I've used in my application logic in Listing 4, takes the same two arguments as the former, but with a difference. After the page has been generated and sent back to the client, the sendPageAndWait function generates and returns a new continuation object (also part of the FOM). At this point, the Cocoon infrastructure also internally generates a unique continuation id and stores the mapping between the two in a global structure.

The sendPageAndWait function can also be passed a function that will be automatically executed after the pipeline processing is complete, but before the continuation is generated. This is an important feature in cases where the pipeline processing requires expensive or contentious resources that should not be bundled with the rest of the execution context (because the user may ask for this continuation to be resumed after a certain amount of think time, and it wouldn't make sense to hold on to these resources the entire time). However, we haven't used that version of this function in our code.

Tutorial Pages:
» A Programming Paradigm to Simplify MVC for the Web
» What is a Continuation, Anyway?
» Problems in Conventional Web development
» The Case for Continuations
» User-Centered Navigation
» You Make the Rules!
» The Continuations Repository
» An Example Application
» Web Continuations in Apache Cocoon
» The Application Sitemap
» The Application Logic
» Understanding the Application Logic
» Resuming the Continuation
» JavaScript vs. the Java language
» Continuations in Java code
» Pros and Cons of Continuations
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources