KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > apples > samples > CalculationApple


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.flow.apples.samples;
17
18 import java.math.BigDecimal JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.components.flow.apples.AppleController;
25 import org.apache.cocoon.components.flow.apples.AppleRequest;
26 import org.apache.cocoon.components.flow.apples.AppleResponse;
27
28 /**
29  * CalculationApple shows an easy Apple example implementation for a Calculator.
30  * <p>
31  * It is explicitely designed to show the difference with flowscript by
32  * remembering the 'lookahead' information from the previous path that entered
33  * already the other data.
34  * <p>
35  * In other words this shows that Apples are not building a complete tree of
36  * continuations like flowscript is doing. But the initial argument of course was
37  * that some cases simply don't need it.
38  */

39 public class CalculationApple extends AbstractLogEnabled implements AppleController {
40
41     BigDecimal JavaDoc inputA;
42     BigDecimal JavaDoc inputB;
43     String JavaDoc inputOp;
44     BigDecimal JavaDoc output;
45
46
47     public String JavaDoc toString() {
48         return "CalculationApple[ a=" + this.inputA + " | b=" + this.inputB
49                 + " | op = " + this.inputOp + " | result = " + this.output + "]";
50     }
51
52     public void process(AppleRequest req, AppleResponse res) throws ProcessingException {
53         String JavaDoc changeTo = processRequest(req);
54         getLogger().debug(toString());
55         showNextState(res, changeTo);
56     }
57
58     private String JavaDoc processRequest(AppleRequest req) {
59         String JavaDoc changeRequest = req.getCocoonRequest().getParameter("change");
60
61         String JavaDoc newA = req.getCocoonRequest().getParameter("a");
62         if (newA != null) {
63             this.inputA = new BigDecimal JavaDoc(newA);
64             // explicitely do not set inputB and inputOp to null !
65
}
66         String JavaDoc newB = req.getCocoonRequest().getParameter("b");
67         if (newB != null) {
68             this.inputB = new BigDecimal JavaDoc(newB);
69             // explicitely do not set inputOp to null !
70
}
71         String JavaDoc newOp = req.getCocoonRequest().getParameter("operator");
72         if (newOp != null) {
73             this.inputOp = newOp;
74         }
75         //explicitely always do the calculation
76
calculate();
77
78         return changeRequest;
79     }
80
81
82     private void calculate() {
83         if (this.inputA == null || this.inputB == null) {
84             this.output = null;
85         } else if("plus".equals(this.inputOp)) {
86             this.output = this.inputA.add(this.inputB);
87         } else if("minus".equals(this.inputOp)) {
88             this.output = this.inputA.add(this.inputB.negate());
89         } else if("multiply".equals(this.inputOp)) {
90             this.output = this.inputA.multiply(this.inputB);
91         } else if("divide".equals(this.inputOp)) {
92             this.output = this.inputA.divide(this.inputB, BigDecimal.ROUND_HALF_EVEN);
93         } else { //not a valid operator
94
this.output = null;
95         }
96     }
97
98     private void showNextState(AppleResponse res, String JavaDoc changeTo) {
99         Object JavaDoc bizdata = buildBizData();
100         
101         if (changeTo != null) {
102             res.sendPage("calc/get" + changeTo, bizdata);
103         } else if (this.inputA == null) {
104             res.sendPage("calc/getNumberA", null);
105         } else if (this.inputB == null) {
106             res.sendPage("calc/getNumberB", bizdata);
107         } else if (this.inputOp == null) {
108             res.sendPage("calc/getOperator", bizdata);
109         } else {
110             res.sendPage("calc/displayResult", bizdata);
111         }
112     }
113
114     private Object JavaDoc buildBizData() {
115         Map JavaDoc bizdata = new HashMap JavaDoc();
116         bizdata.put("a", this.inputA);
117         bizdata.put("b", this.inputB);
118         bizdata.put("operator", this.inputOp);
119         bizdata.put("result", this.output);
120         return bizdata;
121     }
122
123 }
124
Popular Tags