/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.cocoon.samples.flow.groovy import org.apache.cocoon.components.flow.java.AbstractContinuable public class CalculatorFlow extends AbstractContinuable { count = 1 public void doCalculator() { a = getNumber "a", 0, 0 // This is a method call b = getNumber("b", a, 0) op = getOperator(a, b) result = 0 switch (op) { case "plus": result= a + b break case "minus": result = a - b break case "multiply": result = a * b break case "divide": if (b != 0) { sendMessage "Error: Cannot divide by zero!" } else { result = a / b } break default: sendMessage "Error: Unkown operator!" } sendPage("page/calculator-result", ["a" : a, "b": b, "operator": op, "result": result, "count" : count]) count++ } private getNumber(name, a, b) { uri = "page/calculator-" + name.toLowerCase() sendPageAndWait(uri, ["a" : a, "b": b, "count" : count]) getRequest().getParameter(name) } private getOperator(a, b) { sendPageAndWait("page/calculator-operator", ["a" : a, "b": b, "count" : count]) getRequest().getParameter("operator") } private void sendMessage(message) { sendPageAndWait("page/calculator-message", ["message" : message, "count" : count]) } }