home *** CD-ROM | disk | FTP | other *** search
- package org.apache.cocoon.components.flow.apples.samples;
-
- import java.math.BigDecimal;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.avalon.framework.logger.AbstractLogEnabled;
- import org.apache.cocoon.ProcessingException;
- import org.apache.cocoon.components.flow.apples.AppleController;
- import org.apache.cocoon.components.flow.apples.AppleRequest;
- import org.apache.cocoon.components.flow.apples.AppleResponse;
-
- public class CalculationApple extends AbstractLogEnabled implements AppleController {
- BigDecimal inputA;
- BigDecimal inputB;
- String inputOp;
- BigDecimal output;
-
- public String toString() {
- return "CalculationApple[ a=" + this.inputA + " | b=" + this.inputB + " | op = " + this.inputOp + " | result = " + this.output + "]";
- }
-
- public void process(AppleRequest req, AppleResponse res) throws ProcessingException {
- String changeTo = this.processRequest(req);
- this.getLogger().debug(this.toString());
- this.showNextState(res, changeTo);
- }
-
- private String processRequest(AppleRequest req) {
- String changeRequest = req.getCocoonRequest().getParameter("change");
- String newA = req.getCocoonRequest().getParameter("a");
- if (newA != null) {
- this.inputA = new BigDecimal(newA);
- }
-
- String newB = req.getCocoonRequest().getParameter("b");
- if (newB != null) {
- this.inputB = new BigDecimal(newB);
- }
-
- String newOp = req.getCocoonRequest().getParameter("operator");
- if (newOp != null) {
- this.inputOp = newOp;
- }
-
- this.calculate();
- return changeRequest;
- }
-
- private void calculate() {
- if (this.inputA != null && this.inputB != null) {
- if ("plus".equals(this.inputOp)) {
- this.output = this.inputA.add(this.inputB);
- } else if ("minus".equals(this.inputOp)) {
- this.output = this.inputA.add(this.inputB.negate());
- } else if ("multiply".equals(this.inputOp)) {
- this.output = this.inputA.multiply(this.inputB);
- } else if ("divide".equals(this.inputOp)) {
- this.output = this.inputA.divide(this.inputB, 6);
- } else {
- this.output = null;
- }
- } else {
- this.output = null;
- }
-
- }
-
- private void showNextState(AppleResponse res, String changeTo) {
- Object bizdata = this.buildBizData();
- if (changeTo != null) {
- res.sendPage("calc/get" + changeTo, bizdata);
- } else if (this.inputA == null) {
- res.sendPage("calc/getNumberA", (Object)null);
- } else if (this.inputB == null) {
- res.sendPage("calc/getNumberB", bizdata);
- } else if (this.inputOp == null) {
- res.sendPage("calc/getOperator", bizdata);
- } else {
- res.sendPage("calc/displayResult", bizdata);
- }
-
- }
-
- private Object buildBizData() {
- Map bizdata = new HashMap();
- bizdata.put("a", this.inputA);
- bizdata.put("b", this.inputB);
- bizdata.put("operator", this.inputOp);
- bizdata.put("result", this.output);
- return bizdata;
- }
- }
-