KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > DatabaseEventFactoryImpl


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/DatabaseEventFactoryImpl.java,v 1.14 2004/10/17 07:02:25 hkollmann Exp $
3  * $Revision: 1.14 $
4  * $Date: 2004/10/17 07:02:25 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.event;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.DbFormsConfig;
30 import org.dbforms.config.EventInfo;
31 import org.dbforms.config.Table;
32 import org.dbforms.config.TableEvents;
33
34 import org.dbforms.event.eventtype.EventType;
35
36 import org.dbforms.util.ParseUtil;
37 import org.dbforms.util.StringUtil;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40
41
42
43 /**
44  * DatabaseEventFactoryImpl class. Create DatabaseEvent objects.
45  *
46  * @author Luca Fossato
47  *
48  */

49 public class DatabaseEventFactoryImpl extends DatabaseEventFactory {
50    /** classes used as "keyInfo" constructor arguments types */
51    private static final Class JavaDoc[] keyInfoConstructorArgsTypes = new Class JavaDoc[] {
52                                                                  Integer JavaDoc.class,
53                                                                  String JavaDoc.class,
54                                                                  HttpServletRequest JavaDoc.class,
55                                                                  DbFormsConfig.class
56                                                               };
57
58    /** an handle to the unique DatabaseEventFactory instance */
59    private static DatabaseEventFactory instance = null;
60    private static Log logCat = LogFactory.getLog(NavEventFactoryImpl.class);
61
62    /**
63     * Get the instance of DatabaseEventFactory class.
64     *
65     * @return the instance of DAO class.
66     */

67    public static synchronized DatabaseEventFactory instance() {
68       if (instance == null) {
69          instance = new DatabaseEventFactoryImpl();
70       }
71
72       return instance;
73    }
74
75
76    /**
77     * create and return a new database event
78     *
79     * @param action the action string that identifies the web event
80     * @param request the HttpServletRequest object
81     * @param config the DbForms config object
82     *
83     * @return a new database event, or null if any error occurs
84     */

85    public WebEvent createEvent(String JavaDoc action,
86                                HttpServletRequest JavaDoc request,
87                                DbFormsConfig config) {
88       WebEvent event = null;
89       Object JavaDoc[] constructorArgs = null;
90
91       // get the event id of the destination table
92
String JavaDoc eventId = getEventIdFromDestinationTable(request, action);
93       EventInfo einfo = getEventInfo(eventId);
94
95       // debug
96
logCat.info("::createEvent - got event [" + einfo + "] from action ["
97                   + action + "]");
98
99       // instance "normal" database events;
100
if (!isKeyInfoEvent(action)) {
101          constructorArgs = new Object JavaDoc[] {
102                               action,
103                               request,
104                               config
105                            };
106          event = getEvent(einfo, constructorArgsTypes, constructorArgs);
107       }
108       // instance "keyInfo" database events;
109
else {
110          KeyInfo kInfo = getKeyInfo(action, request);
111
112          // args are: tableId, keyId, request, config
113
constructorArgs = new Object JavaDoc[] {
114                               new Integer JavaDoc(kInfo.getTableId()),
115                               kInfo.getKeyId(),
116                               request,
117                               config
118                            };
119          event = getEvent(einfo, keyInfoConstructorArgsTypes, constructorArgs);
120       }
121
122       return event;
123    }
124
125
126    /**
127     * Create and return a new InsertEvent as secondary event.
128     *
129     * @param tableId the table identifier
130     * @param keyId the key identifier
131     * @param request the HttpServletRequest object
132     * @param config the DbForms config object
133     *
134     * @return The updateEvent object
135     */

136    public DatabaseEvent createInsertEvent(int tableId,
137                                           String JavaDoc keyId,
138                                           HttpServletRequest JavaDoc request,
139                                           DbFormsConfig config) {
140       DatabaseEvent event = null;
141       Object JavaDoc[] constructorArgs = new Object JavaDoc[] {
142                                          new Integer JavaDoc(tableId),
143                                          keyId,
144                                          request,
145                                          config
146                                       };
147       Table table = config.getTable(tableId);
148       TableEvents tableEvents = table.getTableEvents();
149       String JavaDoc eventId = tableEvents.getEventId(EventType.EVENT_DATABASE_INSERT);
150       EventInfo einfo = getEventInfo(eventId);
151       event = (DatabaseEvent) getEvent(einfo, keyInfoConstructorArgsTypes,
152                                        constructorArgs);
153
154       return event;
155    }
156
157
158    /**
159     * Create and return a new UpdateEvent as secondary event.
160     *
161     * @param tableId the table identifier
162     * @param keyId the key identifier
163     * @param request the HttpServletRequest object
164     * @param config the DbForms config object
165     *
166     * @return The updateEvent object
167     */

168    public DatabaseEvent createUpdateEvent(int tableId,
169                                           String JavaDoc keyId,
170                                           HttpServletRequest JavaDoc request,
171                                           DbFormsConfig config) {
172       DatabaseEvent event = null;
173       Object JavaDoc[] constructorArgs = new Object JavaDoc[] {
174                                          new Integer JavaDoc(tableId),
175                                          keyId,
176                                          request,
177                                          config
178                                       };
179       Table table = config.getTable(tableId);
180       TableEvents tableEvents = table.getTableEvents();
181       String JavaDoc eventId = tableEvents.getEventId(EventType.EVENT_DATABASE_UPDATE);
182       EventInfo einfo = getEventInfo(eventId);
183       event = (DatabaseEvent) getEvent(einfo, keyInfoConstructorArgsTypes,
184                                        constructorArgs);
185
186       return event;
187    }
188
189
190    /**
191     * Initialize the default events.
192     *
193     * @exception Exception if any error occurs
194     */

195    protected void initializeEvents() throws Exception JavaDoc {
196       addEventInfo(new EventInfo(EventType.EVENT_DATABASE_DELETE,
197                                  "org.dbforms.event.datalist.DeleteEvent"));
198       addEventInfo(new EventInfo(EventType.EVENT_DATABASE_INSERT,
199                                  "org.dbforms.event.datalist.InsertEvent"));
200       addEventInfo(new EventInfo(EventType.EVENT_DATABASE_UPDATE,
201                                  "org.dbforms.event.datalist.UpdateEvent"));
202    }
203
204
205    /**
206     * Get key informations for "key info" database events
207     *
208     * @param action the action string
209     * @param request the HttpServletRequest object
210     *
211     * @return a keyInfo object containing the informations related to the
212     * tableId and the keyId taken from the input action string, or
213     * null if any error occurs
214     */

215    private KeyInfo getKeyInfo(String JavaDoc action,
216                               HttpServletRequest JavaDoc request) {
217       KeyInfo keyInfo = null;
218       String JavaDoc associatedRadioName = ParseUtil.getParameter(request,
219                                                            "data" + action
220                                                            + "_arname");
221       String JavaDoc keyString = ParseUtil.getParameter(request, associatedRadioName);
222
223       if (keyString != null) {
224          int tableId = StringUtil.getEmbeddedStringAsInteger(keyString, 0, '_');
225          String JavaDoc keyId = StringUtil.getEmbeddedString(keyString, 1, '_');
226          keyInfo = new KeyInfo();
227          keyInfo.setTableId(tableId);
228          keyInfo.setKeyId(keyId);
229       }
230
231       return keyInfo;
232    }
233
234
235    /**
236     * PRIVATE METHODS here
237     *
238     * @param action DOCUMENT ME!
239     *
240     * @return DOCUMENT ME!
241     */

242    /**
243     * Checck if the input action string defines a "keyInfo event"
244     *
245     * @param action the action string
246     *
247     * @return true if the input action string defines a "keyInfo event"; false
248     * otherwise
249     */

250    private boolean isKeyInfoEvent(String JavaDoc action) {
251       boolean keyInfo = false;
252
253       if ((action.startsWith("ac_updatear_"))
254                 || action.startsWith("ac_deletear_")) {
255          keyInfo = true;
256       }
257
258       //logCat.info("::isKeyInfoEvent - action [" + action + "] is related to a keyInfo event ? [" + keyInfo + "]");
259
return keyInfo;
260    }
261 }
262
Popular Tags