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 * Licensed to the Apache Software Foundation (ASF) under one or more
0003 * contributor license agreements. See the NOTICE file distributed with
0004 * this work for additional information regarding copyright ownership.
0005 * The ASF licenses this file to You under the Apache License, Version 2.0
0006 * (the "License"); you may not use this file except in compliance with
0007 * the License. You may obtain a copy of the License at
0008 *
0009 * http://www.apache.org/licenses/LICENSE-2.0
0010 *
0011 * Unless required by applicable law or agreed to in writing, software
0012 * distributed under the License is distributed on an "AS IS" BASIS,
0013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 * See the License for the specific language governing permissions and
0015 * limitations under the License.
0016 */

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

0020 var calculator = null;

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

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

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

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

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

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

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

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.