KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > examples > controller > handlers > RepopulationHandler


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: RepopulationHandler.java,v 1.2 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.examples.controller.handlers;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import org.enhydra.barracuda.core.event.ControlEventContext;
28 import org.enhydra.barracuda.core.event.EventException;
29 import org.enhydra.barracuda.core.forms.FormElement;
30 import org.enhydra.barracuda.core.forms.ValidationException;
31 import org.enhydra.barracuda.contrib.dbroggisch.page.Page;
32 import org.enhydra.barracuda.contrib.dbroggisch.page.PageEventListener;
33 import org.apache.log4j.Logger;
34
35 import org.enhydra.barracuda.contrib.dbroggisch.examples.model.forms.*;
36 import org.enhydra.barracuda.contrib.dbroggisch.examples.view.pages.*;
37
38
39 public class RepopulationHandler extends PageEventListener {
40
41     private static final Logger logger = Logger.getLogger(RepopulationHandler.class.getName());
42
43     public void handleControlEvent(ControlEventContext context, Page p)
44             throws EventException, ServletException JavaDoc, IOException JavaDoc {
45         HttpServletRequest JavaDoc request = context.getRequest();
46         ExampleFormMap cfm = new ExampleFormMap();
47         RepopulationPage rPage = (RepopulationPage)p;
48         rPage.setState(ExampleBasePage.REPOPULATION);
49
50         // We put the form map into the page
51
// so it can act as template model for
52
// the rendering process.
53
rPage.setRFM(cfm);
54
55         // The request is only mapped and validated if a special hidden parameter is present.
56
// I usually call it "visited". Be careful to assign a unique name to it, or you might
57
// expirience strange errors when the application event flow is routed to a different event
58
// handling form maps.
59
if (ExampleFormMap.MODEL_NAME.equals(request.getParameter("visited"))) {
60             try {
61                 // This is all you have to do - mapping and validation.
62
cfm.map(request).validate(true);
63
64                 // Print all values the elements have.
65
if (logger.isDebugEnabled()) {
66                     Map JavaDoc els = cfm.getElements();
67
68                     for (Iterator JavaDoc it = els.entrySet().iterator(); it.hasNext();) {
69                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
70                         String JavaDoc name = (String JavaDoc)entry.getKey();
71                         FormElement fel = (FormElement)entry.getValue();
72                         logger.debug("Element " + name + ":");
73                         Object JavaDoc val = fel.getVal();
74
75                         if (val != null) {
76                             logger.debug("Value class: " + val.getClass().getName());
77                             logger.debug("Value: " + val);
78                         } else {
79                             logger.debug("Has a null-value");
80                         }
81                     }
82                 }
83             } catch (ValidationException ex) {
84                 logger.error("Form Validation Exception", ex);
85             }
86         }
87         context.getEvent().setHandled(true);
88         //context.getQueue().addEvent(new RenderRepopulation());
89
}
90
91 }
92
Popular Tags