01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.flow.apples.samples;
18:
19: import java.util.HashMap;
20: import java.util.Map;
21: import java.util.Random;
22:
23: import org.apache.avalon.framework.logger.AbstractLogEnabled;
24: import org.apache.cocoon.ProcessingException;
25: import org.apache.cocoon.components.flow.apples.AppleController;
26: import org.apache.cocoon.components.flow.apples.AppleRequest;
27: import org.apache.cocoon.components.flow.apples.AppleResponse;
28:
29: /**
30: * GuessGameApple shows an easy Apples implementation for a number guessing game.
31: */
32: public class GuessGameApple extends AbstractLogEnabled implements
33: AppleController {
34:
35: private final int random = (new Random()).nextInt(10) + 1;
36: private int guesses = 0;
37:
38: public String toString() {
39: return "GuessGameApple[ random=" + this .random + " | guesses="
40: + this .guesses + "]";
41: }
42:
43: public void process(AppleRequest req, AppleResponse res)
44: throws ProcessingException {
45:
46: String hint = "No hints yet.";
47: String targetURI = "guess/guess.jx";
48:
49: int newGuess = -1;
50: String newGuessString = req.getCocoonRequest().getParameter(
51: "guess");
52:
53: if (newGuessString != null) {
54: newGuess = Integer.parseInt(newGuessString);
55: this .guesses++;
56:
57: if (this .random == newGuess) {
58: targetURI = "guess/success.jx";
59: } else {
60: if (this .random < newGuess) {
61: hint = "Try lower.";
62: } else {
63: hint = "Try higher.";
64: }
65: }
66: }
67:
68: getLogger().debug(toString());
69:
70: Map bizdata = new HashMap();
71: bizdata.put("random", "" + this .random);
72: bizdata.put("guesses", "" + this .guesses);
73: bizdata.put("hint", hint);
74:
75: res.sendPage(targetURI, bizdata);
76: }
77:
78: }
|