001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.flow.apples.samples;
018:
019: import java.math.BigDecimal;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.cocoon.ProcessingException;
025: import org.apache.cocoon.components.flow.apples.AppleController;
026: import org.apache.cocoon.components.flow.apples.AppleRequest;
027: import org.apache.cocoon.components.flow.apples.AppleResponse;
028:
029: /**
030: * CalculationApple shows an easy Apple example implementation for a Calculator.
031: * <p>
032: * It is explicitely designed to show the difference with flowscript by
033: * remembering the 'lookahead' information from the previous path that entered
034: * already the other data.
035: * <p>
036: * In other words this shows that Apples are not building a complete tree of
037: * continuations like flowscript is doing. But the initial argument of course was
038: * that some cases simply don't need it.
039: */
040: public class CalculationApple extends AbstractLogEnabled implements
041: AppleController {
042:
043: BigDecimal inputA;
044: BigDecimal inputB;
045: String inputOp;
046: BigDecimal output;
047:
048: public String toString() {
049: return "CalculationApple[ a=" + this .inputA + " | b="
050: + this .inputB + " | op = " + this .inputOp
051: + " | result = " + this .output + "]";
052: }
053:
054: public void process(AppleRequest req, AppleResponse res)
055: throws ProcessingException {
056: String changeTo = processRequest(req);
057: getLogger().debug(toString());
058: showNextState(res, changeTo);
059: }
060:
061: private String processRequest(AppleRequest req) {
062: String changeRequest = req.getCocoonRequest().getParameter(
063: "change");
064:
065: String newA = req.getCocoonRequest().getParameter("a");
066: if (newA != null) {
067: this .inputA = new BigDecimal(newA);
068: // explicitely do not set inputB and inputOp to null !
069: }
070: String newB = req.getCocoonRequest().getParameter("b");
071: if (newB != null) {
072: this .inputB = new BigDecimal(newB);
073: // explicitely do not set inputOp to null !
074: }
075: String newOp = req.getCocoonRequest().getParameter("operator");
076: if (newOp != null) {
077: this .inputOp = newOp;
078: }
079: //explicitely always do the calculation
080: calculate();
081:
082: return changeRequest;
083: }
084:
085: private void calculate() {
086: if (this .inputA == null || this .inputB == null) {
087: this .output = null;
088: } else if ("plus".equals(this .inputOp)) {
089: this .output = this .inputA.add(this .inputB);
090: } else if ("minus".equals(this .inputOp)) {
091: this .output = this .inputA.add(this .inputB.negate());
092: } else if ("multiply".equals(this .inputOp)) {
093: this .output = this .inputA.multiply(this .inputB);
094: } else if ("divide".equals(this .inputOp)) {
095: this .output = this .inputA.divide(this .inputB,
096: BigDecimal.ROUND_HALF_EVEN);
097: } else { //not a valid operator
098: this .output = null;
099: }
100: }
101:
102: private void showNextState(AppleResponse res, String changeTo) {
103: Object bizdata = buildBizData();
104:
105: if (changeTo != null) {
106: res.sendPage("calc/get" + changeTo, bizdata);
107: } else if (this .inputA == null) {
108: res.sendPage("calc/getNumberA", null);
109: } else if (this .inputB == null) {
110: res.sendPage("calc/getNumberB", bizdata);
111: } else if (this .inputOp == null) {
112: res.sendPage("calc/getOperator", bizdata);
113: } else {
114: res.sendPage("calc/displayResult", bizdata);
115: }
116: }
117:
118: private Object buildBizData() {
119: Map bizdata = new HashMap();
120: bizdata.put("a", this .inputA);
121: bizdata.put("b", this .inputB);
122: bizdata.put("operator", this .inputOp);
123: bizdata.put("result", this.output);
124: return bizdata;
125: }
126:
127: }
|