home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / CalculationApple.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  3.8 KB  |  93 lines

  1. package org.apache.cocoon.components.flow.apples.samples;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import org.apache.avalon.framework.logger.AbstractLogEnabled;
  7. import org.apache.cocoon.ProcessingException;
  8. import org.apache.cocoon.components.flow.apples.AppleController;
  9. import org.apache.cocoon.components.flow.apples.AppleRequest;
  10. import org.apache.cocoon.components.flow.apples.AppleResponse;
  11.  
  12. public class CalculationApple extends AbstractLogEnabled implements AppleController {
  13.    BigDecimal inputA;
  14.    BigDecimal inputB;
  15.    String inputOp;
  16.    BigDecimal output;
  17.  
  18.    public String toString() {
  19.       return "CalculationApple[ a=" + this.inputA + " | b=" + this.inputB + " | op = " + this.inputOp + " | result = " + this.output + "]";
  20.    }
  21.  
  22.    public void process(AppleRequest req, AppleResponse res) throws ProcessingException {
  23.       String changeTo = this.processRequest(req);
  24.       this.getLogger().debug(this.toString());
  25.       this.showNextState(res, changeTo);
  26.    }
  27.  
  28.    private String processRequest(AppleRequest req) {
  29.       String changeRequest = req.getCocoonRequest().getParameter("change");
  30.       String newA = req.getCocoonRequest().getParameter("a");
  31.       if (newA != null) {
  32.          this.inputA = new BigDecimal(newA);
  33.       }
  34.  
  35.       String newB = req.getCocoonRequest().getParameter("b");
  36.       if (newB != null) {
  37.          this.inputB = new BigDecimal(newB);
  38.       }
  39.  
  40.       String newOp = req.getCocoonRequest().getParameter("operator");
  41.       if (newOp != null) {
  42.          this.inputOp = newOp;
  43.       }
  44.  
  45.       this.calculate();
  46.       return changeRequest;
  47.    }
  48.  
  49.    private void calculate() {
  50.       if (this.inputA != null && this.inputB != null) {
  51.          if ("plus".equals(this.inputOp)) {
  52.             this.output = this.inputA.add(this.inputB);
  53.          } else if ("minus".equals(this.inputOp)) {
  54.             this.output = this.inputA.add(this.inputB.negate());
  55.          } else if ("multiply".equals(this.inputOp)) {
  56.             this.output = this.inputA.multiply(this.inputB);
  57.          } else if ("divide".equals(this.inputOp)) {
  58.             this.output = this.inputA.divide(this.inputB, 6);
  59.          } else {
  60.             this.output = null;
  61.          }
  62.       } else {
  63.          this.output = null;
  64.       }
  65.  
  66.    }
  67.  
  68.    private void showNextState(AppleResponse res, String changeTo) {
  69.       Object bizdata = this.buildBizData();
  70.       if (changeTo != null) {
  71.          res.sendPage("calc/get" + changeTo, bizdata);
  72.       } else if (this.inputA == null) {
  73.          res.sendPage("calc/getNumberA", (Object)null);
  74.       } else if (this.inputB == null) {
  75.          res.sendPage("calc/getNumberB", bizdata);
  76.       } else if (this.inputOp == null) {
  77.          res.sendPage("calc/getOperator", bizdata);
  78.       } else {
  79.          res.sendPage("calc/displayResult", bizdata);
  80.       }
  81.  
  82.    }
  83.  
  84.    private Object buildBizData() {
  85.       Map bizdata = new HashMap();
  86.       bizdata.put("a", this.inputA);
  87.       bizdata.put("b", this.inputB);
  88.       bizdata.put("operator", this.inputOp);
  89.       bizdata.put("result", this.output);
  90.       return bizdata;
  91.    }
  92. }
  93.