KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > examples > data > UserGateway


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.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: UserGateway.java,v 1.3 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.examples.data;
21
22 import java.util.*;
23 import javax.servlet.http.*;
24 import javax.servlet.*;
25
26 import org.enhydra.barracuda.core.comp.*;
27 import org.enhydra.barracuda.core.event.*;
28 import org.enhydra.barracuda.core.event.helper.*;
29 import org.enhydra.barracuda.contrib.dbroggisch.display.*;
30 import org.enhydra.barracuda.contrib.sam.xmlform.*;
31 import org.enhydra.barracuda.contrib.sam.data.*;
32 import org.enhydra.barracuda.contrib.sam.models.*;
33 import org.apache.log4j.*;
34 import org.enhydra.barracuda.contrib.sam.examples.events.*;
35 import org.enhydra.barracuda.contrib.sam.examples.xmlc.*;
36
37 public class UserGateway extends DataObjectGateway {
38
39     //public constants
40
protected static Logger logger = Logger.getLogger(UserGateway.class.getName());
41     protected Class JavaDoc getDefaultEvent() { return org.enhydra.barracuda.contrib.sam.examples.events.User.class; }
42     protected Class JavaDoc getDefaultHandler() { return UserHandler.class; }
43     protected Class JavaDoc getDeleteEvent() { return UserDelete.class; }
44     protected Class JavaDoc getDeleteHandler() { return UserDeleteHandler.class; }
45     protected Class JavaDoc getListEvent() { return UserList.class; }
46     protected Class JavaDoc getListHandler() { return UserListHandler.class; }
47     protected Class JavaDoc getNewEvent() { return UserNew.class; }
48     protected Class JavaDoc getNewHandler() { return UserNewHandler.class; }
49     protected Class JavaDoc getSaveEvent() { return UserSave.class; }
50     protected Class JavaDoc getSaveHandler() { return UserSaveHandler.class; }
51     protected Class JavaDoc getRenderEvent() { return RenderUser.class; }
52     protected Class JavaDoc getRenderHandler() { return RenderUserHandler.class; }
53
54     /**
55      * Public constructor
56      */

57     public UserGateway() {
58         super();
59         logger.info("registering UserGateway's listeners");
60         specifyLocalEventInterests(new EventConnectorFactory(UserListHandler.class), UserList.class);
61         specifyLocalEventInterests(new EventConnectorFactory(RenderUserListHandler.class), RenderUserList.class);
62         specifyLocalEventInterests(new EventConnectorFactory(UserSaveEmailHandler.class), UserSaveEmail.class);
63     }
64
65     public static class UserConfig implements Config {
66
67         public DataObject getDataObject(String JavaDoc oid) throws DataObjectException {
68             return org.enhydra.barracuda.contrib.sam.examples.data.User.getUser(oid);
69         }
70
71         public String JavaDoc getFormName() { return "user";}
72         public String JavaDoc getMapName() { return "map"; }
73         public String JavaDoc getOidName() { return "oid"; }
74         public ViewEvent getViewEvent() { return new RenderUser(); }
75         public String JavaDoc getXmlFormName() { return "/WEB-INF/userform.xml"; }
76     }
77
78     public static class UserNewHandler extends DataObjectNewHandler {
79         public Config getConfig() { return new UserConfig(); }
80     }
81
82     public static class UserDeleteHandler extends DataObjectDeleteHandler {
83         public Config getConfig() { return new UserConfig(); }
84     }
85
86     public static class UserSaveHandler extends DataObjectSaveHandler {
87         public Config getConfig() { return new UserConfig(); }
88         protected Object JavaDoc modifyFormData(String JavaDoc key, Object JavaDoc value ) {
89             if (key.equals("password") && ((value==null) || ((String JavaDoc)value).length()==0)) return "sample";
90             return value;
91         }
92
93     }
94
95     public static class UserSaveEmailHandler extends DefaultBaseEventListener {
96         protected static Logger logger = Logger.getLogger(UserSaveEmailHandler.class.getName());
97         public void handleControlEvent(ControlEventContext context) throws EventException, javax.servlet.ServletException JavaDoc, java.io.IOException JavaDoc {
98             Config config = new UserConfig();
99
100             ErrorModel.create(context, new Exception JavaDoc("the email feature is currently not implemented. This is just an example how you can override default behavior of the SaveHandler."));
101             // placeholder for additional code when saving a dataobject e.g. send email with credentials....
102

103         }
104     }
105
106     public static class UserHandler extends DataObjectHandler {
107         public Config getConfig() { return new UserConfig(); }
108     }
109
110     public static class RenderUserHandler extends DataObjectViewHandler {
111
112         public Config getConfig() { return new UserConfig(); }
113
114         public Object JavaDoc getTemplateModels(ViewContext vc) {
115             List l = (List)super.getTemplateModels(vc);
116             l.add(new ErrorModel(vc));
117             HashMapModel hhm = new HashMapModel("Submit");
118             hhm.setItem("Save", new BAction(new UserSave()));
119             hhm.setItem("Delete", new BAction(new UserDelete()));
120             hhm.setItem("New", new BAction(new UserNew()));
121             hhm.setItem("SaveEmail", new BAction(new UserSaveEmail()));
122             l.add(hhm);
123             return l;
124         }
125
126         public Class JavaDoc getTemplateClass(ViewContext vc) {return userHTML.class;}
127     }
128
129     public static class UserListHandler extends DefaultBaseEventListener {
130
131         public void handleControlEvent(ControlEventContext context) throws EventException, javax.servlet.ServletException JavaDoc, java.io.IOException JavaDoc {
132             BaseEvent event = context.getEvent();
133             HttpServletRequest request = context.getRequest();
134             HttpSession session = request.getSession();
135
136             event.setHandled(true);
137             context.getQueue().addEvent(new RenderUserList());
138         }
139     }
140
141     public static class RenderUserListHandler extends BTemplateViewHandler {
142         public Object JavaDoc getTemplateModels() { return new UserListModel(); }
143         public Class JavaDoc getTemplateClass() {return userlistHTML.class;}
144     }
145
146     public static class UserListModel extends IterativeCollectionModel {
147
148         public UserListModel() {
149             super("Nav");
150             Collection l = org.enhydra.barracuda.contrib.sam.examples.data.User.getAll();
151             setModels(l);
152         }
153
154         public Object JavaDoc getItem(String JavaDoc key) {
155             try {
156                 org.enhydra.barracuda.contrib.sam.examples.data.User user = (org.enhydra.barracuda.contrib.sam.examples.data.User)_current;
157                 if (key.equals("Show")) {
158                     BLink r = new BLink((String JavaDoc)user.get("login"), new org.enhydra.barracuda.contrib.sam.examples.events.User());
159                     r.setParam("oid", (String JavaDoc)user.get("oid"));
160                     return r;
161                 }
162                 else return super.getItem(key);
163             } catch (DataObjectException ex) {
164                 logger.error(ex.getMessage(),ex);
165                 return ex.getMessage();
166             }
167         }
168     }
169 }
170
Popular Tags