1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
41 import java.util.Properties;
42
43 import javax.servlet.http.HttpServletRequest;
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 AbstractEventFactory {
56 /*** logging category */
57 private static Log logCat = LogFactory.getLog(AbstractEventFactory.class);
58
59 /*** classes used as "non keyInfo" constructor arguments types */
60 static final Class[] constructorArgsTypes = new Class[] {
61 String.class,
62 HttpServletRequest.class,
63 DbFormsConfig.class
64 };
65
66 /*** map of supported event */
67 protected HashMap eventInfoMap = null;
68
69 /***
70 * Creates a new EventFactory object.
71 */
72 protected AbstractEventFactory() {
73 eventInfoMap = new HashMap();
74
75 try {
76 initializeEvents();
77 } catch (Exception 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 AbstractWebEvent createEvent(String action,
92 HttpServletRequest 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
105
106 String id = einfo.getId();
107
108
109 if (eventInfoMap.containsKey(id)) {
110 EventInfo prevEinfo = (EventInfo) eventInfoMap.get(id);
111
112 if (prevEinfo != null) {
113 String prevClassName = prevEinfo.getClassName();
114 logCat.warn(new StringBuffer("::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
120 eventInfoMap.put(id, einfo);
121 logCat.info(new StringBuffer("::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;
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 AbstractWebEvent getEvent(EventInfo einfo,
151 Class[] aconstructorArgsTypes,
152 Object[] constructorArgs) {
153 AbstractWebEvent event = null;
154
155 if (einfo != null) {
156 try {
157 event = (AbstractWebEvent) ReflectionUtil.newInstance(einfo.getClassName(),
158 aconstructorArgsTypes,
159 constructorArgs);
160
161
162 event.setProperties(new Properties(einfo.getProperties()));
163 event.setType(einfo.getType());
164 } catch (Exception 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 getEventIdFromDestinationTable(HttpServletRequest request,
185 String action) {
186 DbFormsConfig config = null;
187 Table table = null;
188 String eventId = null;
189 String eventType = EventTypeUtil.getEventType(action)
190 .getEventType();
191
192 try {
193 config = DbFormsConfigRegistry.instance()
194 .lookup();
195 } catch (Exception e) {
196 logCat.error("::getEventIdFromDestinationTable - cannot get the config object from the DbFormsConfigRegistry");
197 }
198
199 if (config != null) {
200
201
202 String 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 getEventIdFromDestinationTable(Table table,
242 String action) {
243 TableEvents tableEvents = table.getTableEvents();
244 String eventType = EventTypeUtil.getEventType(action)
245 .getEventType();
246 String 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 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 }