KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/EventFactory.java,v 1.28 2005/02/19 21:26:29 hkollmann Exp $
3  * $Revision: 1.28 $
4  * $Date: 2005/02/19 21:26:29 $
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.DbFormsConfigRegistry;
31 import org.dbforms.config.EventInfo;
32 import org.dbforms.config.Table;
33 import org.dbforms.config.TableEvents;
34
35 import org.dbforms.event.eventtype.EventTypeUtil;
36
37 import org.dbforms.util.ReflectionUtil;
38 import org.dbforms.util.Util;
39
40 import java.util.HashMap JavaDoc;
41 import java.util.Properties JavaDoc;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44
45
46
47 /**
48  * The EventFactory abstract class provides the interface and the
49  * implementation of protected methods used by eventFactory subclasses
50  * (see NavEventFactory and DatabaseEventFactory).
51  *
52  * @author Luca Fossato
53  *
54  */

55 public abstract class EventFactory {
56    /** logging category */
57    private static Log logCat = LogFactory.getLog(EventFactory.class);
58
59    /** classes used as "non keyInfo" constructor arguments types */
60    static final Class JavaDoc[] constructorArgsTypes = new Class JavaDoc[] {
61                                                             String JavaDoc.class,
62                                                             HttpServletRequest JavaDoc.class,
63                                                             DbFormsConfig.class
64                                                          };
65
66    /** map of supported event */
67    protected HashMap JavaDoc eventInfoMap = null;
68
69    /**
70     * Creates a new EventFactory object.
71     */

72    protected EventFactory() {
73       eventInfoMap = new HashMap JavaDoc();
74
75       try {
76          initializeEvents();
77       } catch (Exception JavaDoc e) {
78          logCat.error("::EventFactory - cannot initialize the factory events", e);
79       }
80    }
81
82    /**
83     * Create and return a new event.
84     *
85     * @param action the action string that identifies the web event
86     * @param request the HttpServletRequest object
87     * @param config the DbForms config object
88     *
89     * @return a new navigation event
90     */

91    public abstract WebEvent createEvent(String JavaDoc action,
92                                         HttpServletRequest JavaDoc request,
93                                         DbFormsConfig config);
94
95
96    /**
97     * Add a new EventInfo object into the factory. <br>
98     * The EventInfo name must be unique.
99     *
100     * @param einfo the EventInfo object to add to
101     */

102    public void addEventInfo(EventInfo einfo) {
103       if (eventInfoMap != null) {
104          // note: events are registered using their id value (if that value is not null),
105
// or their type value (if the id is null or empty).
106
String JavaDoc id = einfo.getId();
107
108          // event info override;
109
if (eventInfoMap.containsKey(id)) {
110             EventInfo prevEinfo = (EventInfo) eventInfoMap.get(id);
111
112             if (prevEinfo != null) {
113                String JavaDoc prevClassName = prevEinfo.getClassName();
114                logCat.warn(new StringBuffer JavaDoc("::addEventInfo - the event information having id, class [").append(id).append(", ").append(einfo
115                                                                                                                                         .getClassName()).append("] overrides the event class [").append(prevClassName).append("]").toString());
116             }
117          }
118
119          // event info registration;
120
eventInfoMap.put(id, einfo);
121          logCat.info(new StringBuffer JavaDoc("::addEventInfo - event info having id, type, class [").append(id).append(", ").append(einfo
122                                                                                                                              .getType()).append(", ").append(einfo
123                                                                                                                                                              .getClassName()).append("] registered"));
124       }
125    }
126
127
128    /**
129     * PROTECTED methods here
130     *
131     * @throws Exception DOCUMENT ME!
132     */

133    /**
134     * Initialize the default events.
135     *
136     * @exception Exception if any error occurs
137     */

138    protected abstract void initializeEvents() throws Exception JavaDoc;
139
140
141    /**
142     * Instance a new DatabaseEvent object.
143     *
144     * @param einfo the EventInfo object
145     * @param constructorArgsTypes array of constructor argument classes
146     * @param constructorArgs array of constructor argument objects
147     *
148     * @return the event object, or null if any problem occurs
149     */

150    protected WebEvent getEvent(EventInfo einfo,
151                                Class JavaDoc[] aconstructorArgsTypes,
152                                Object JavaDoc[] constructorArgs) {
153       WebEvent event = null;
154
155       if (einfo != null) {
156          try {
157             event = (WebEvent) ReflectionUtil.newInstance(einfo.getClassName(),
158                                                           aconstructorArgsTypes,
159                                                           constructorArgs);
160
161             // set a new Properties object into the event;
162
event.setProperties(new Properties JavaDoc(einfo.getProperties()));
163             event.setType(einfo.getType());
164          } catch (Exception JavaDoc e) {
165             logCat.error("::getEvent - cannot create the new event [" + einfo
166                          + "]", e);
167          }
168       }
169
170       return event;
171    }
172
173
174    /**
175     * Get the Event identifier from the destination table related to the input
176     * action string
177     *
178     * @param request the request object
179     * @param action the action string
180     *
181     * @return the Event identifier from the destination table, or null if any
182     * error occurs
183     */

184    protected String JavaDoc getEventIdFromDestinationTable(HttpServletRequest JavaDoc request,
185                                                    String JavaDoc action) {
186       DbFormsConfig config = null;
187       Table table = null;
188       String JavaDoc eventId = null;
189       String JavaDoc eventType = EventTypeUtil.getEventType(action)
190                                              .getEventType();
191
192       try {
193          config = DbFormsConfigRegistry.instance()
194                                        .lookup();
195       } catch (Exception JavaDoc e) {
196          logCat.error("::getEventIdFromDestinationTable - cannot get the config object from the DbFormsConfigRegistry");
197       }
198
199       if (config != null) {
200          // try to retrieve a valid target table name from the request;
201
// if it's null, try to retrieve the table id from the action string.
202
String JavaDoc tableName = EventHelper.getDestinationTableName(request, action);
203
204          if (!Util.isNull(tableName)) {
205             table = config.getTableByName(tableName);
206          } else {
207             int tableId = EventHelper.getTableId(action);
208             table = config.getTable(tableId);
209          }
210
211          if (!Util.isNull(eventType)) {
212             TableEvents tableEvents;
213
214             if (table != null) {
215                tableEvents = table.getTableEvents();
216             } else {
217                tableEvents = new TableEvents();
218             }
219
220             eventId = tableEvents.getEventId(eventType);
221          }
222       }
223
224       logCat.info("::getEventIdFromDestinationTable - eventId = [" + eventId
225                   + "]");
226
227       return eventId;
228    }
229
230
231    /**
232     * Get the Event identifier from the destination table related to the input
233     * action string
234     *
235     * @param table the table object
236     * @param action the action string
237     *
238     * @return the Event identifier from the destination table, or null if any
239     * error occurs
240     */

241    protected String JavaDoc getEventIdFromDestinationTable(Table table,
242                                                    String JavaDoc action) {
243       TableEvents tableEvents = table.getTableEvents();
244       String JavaDoc eventType = EventTypeUtil.getEventType(action)
245                                            .getEventType();
246       String JavaDoc eventId = tableEvents.getEventId(eventType);
247
248       logCat.info("::getEventIdFromDestinationTable - eventId = [" + eventId
249                   + "]");
250
251       return eventId;
252    }
253
254
255    /**
256     * Get the EventInfo object having the input identifier.
257     *
258     * @param id the EventInfo identifier
259     *
260     * @return the EventInfo object having the input identifier, or null if that
261     * object does not exist
262     */

263    protected EventInfo getEventInfo(String JavaDoc id) {
264       EventInfo einfo = null;
265
266       if ((eventInfoMap != null) && (eventInfoMap.containsKey(id))) {
267          einfo = (EventInfo) eventInfoMap.get(id);
268       } else {
269          logCat.error("::getEventInfo - event having id [" + id
270                       + "] is not registered into the factory, returning a NULL event");
271       }
272
273       return einfo;
274    }
275 }
276
Popular Tags