Java shapes: using Java objects from Flow

In this example, java classes are used to calculate the area and perimeter of shapes. Various Java classes are instantiated and used from Flow.

Start the example here.

The sitemap

There's nothing new in the sitemap, our use of variables allows the exact same sitemap to be reused for both our Flow examples.

The only specific thing is the importing of the java-shapes.js flowscript, but this was already present for the previous example:

<map:flow id="flow" language="javascript">
<map:script src="number-guess/guess-number.js"/>
<map:script src="multi-page/multi-page.js"/>
<map:script src="java-shapes/java-shapes.js"/>
</map:flow>

Java code

The java code (interface Shape, classes Rectangular, Square, Circle) is fairly trivial, and the computations could easily be done in javascript for such a simple case.

However, our goal is to show interactions between Flow and java classes, so you shouldn't pay too much attention to the java code, except to note that it has no knowledge of Avalon - our java objects are just POJOs: Plain Old Java Objects.

Flowscript code

Here's the Flowscript which has three steps.

  1. Get the selected shape from user and prepare the next page.
  2. Get shape's informations (width, height or radius) and instantiate the correct java class for the selected shape.
  3. Use java class to calculate area and perimeter and display the results
java-shapes.js
0001 /*
0002 * Copyright 1999-2004 The Apache Software Foundation.
0003 *
0004 * Licensed under the Apache License, Version 2.0 (the "License");
0005 * you may not use this file except in compliance with the License.
0006 * You may obtain a copy of the License at
0007 *
0008 * http://www.apache.org/licenses/LICENSE-2.0
0009 *
0010 * Unless required by applicable law or agreed to in writing, software
0011 * distributed under the License is distributed on an "AS IS" BASIS,
0012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 * See the License for the specific language governing permissions and
0014 * limitations under the License.
0015 */

0017 // Shape's area and perimeter calculation example.

0019 var calculator = null;

0021 function public_startShape() {
0022 var hint = "Calculate shape's area and perimeter using logic in java. ";

0024 // let user select shape
0025 cocoon.sendPageAndWait("java-shapes/views/select", {"hint" : hint});
0026 var shapeId = cocoon.request.get("shape");

0028 // send shape-specific view
0029 cocoon.sendPageAndWait("java-shapes/views/" + shapeId, {"shapeId" : shapeId});

0031 // get request parameters (of which some are null depending on shape, that's not a problem)
0032 var h = parseInt( cocoon.request.get("h") );
0033 var b = parseInt( cocoon.request.get("b") );
0034 var r = parseInt( cocoon.request.get("r") );

0036 // instantiate appropriate calculator
0037 if(shapeId == "square") {
0038 calculator = new Packages.org.apache.cocoon.samples.tour.shapes.Square(b);
0039 } else if(shapeId=="rectangular") {
0040 calculator = new Packages.org.apache.cocoon.samples.tour.shapes.Rectangular(b,h);
0041 } else if(shapeId=="circle") {
0042 calculator = new Packages.org.apache.cocoon.samples.tour.shapes.Circle(r);
0043 } else {
0044 throw new java.lang.Exception("No calculator found for shape '" + shapeId + "'");
0045 }

0047 // compute results
0048 // (accessing bean-like properties like "getArea()" using property names like "area")
0049 var a = calculator.area;
0050 var p = calculator.perimeter;

0052 cocoon.sendPage("java-shapes/views/results", {"area" : a, "perimeter" : p, "shape" : shapeId} );
0053 }

JXTemplate view

Shape selection

<page id="page">
<title> Flow example: Shapes </title>
<content>
<h2> ${hint} </h2>
<form method="post" action="${continuation.id}.continue">
<p> Select shape </p>
<select name="shape">
<optgroup label="Select shape">
<option value="square"> Square </option>
<option value="rectangular"> Rectangular </option>
<option value="circle"> Circle </option>
</optgroup>
</select>
<input type="submit"/>
</form>
<p class="footer">
<a href="../docs/index.html"> Flow examples </a>
</p>
</content>
</page>

Here the user can select a shape.