KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/EventEngine.java,v 1.37 2004/10/17 07:02:25 hkollmann Exp $
3  * $Revision: 1.37 $
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.Constants;
30 import org.dbforms.config.DbFormsConfig;
31 import org.dbforms.config.Table;
32
33 import org.dbforms.event.eventtype.EventType;
34 import org.dbforms.event.eventtype.EventTypeUtil;
35
36 import org.dbforms.util.ParseUtil;
37 import org.dbforms.util.StringUtil;
38 import org.dbforms.util.Util;
39
40 import java.util.Enumeration JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44
45
46
47 /**
48  * This class is invoked by the Controller-Servlet. It parses a request to find
49  * out which Event(s) need to be instanciated. The fine-grained parsing
50  * (parsing of additional data, etc) is done by the WebEvent-Object itself (in
51  * order to hide complexity from this class and to keep the framework open for
52  * implementations of new Event-classes)
53  *
54  * @author Joe Peer
55  *
56  */

57 public class EventEngine {
58    /** logging category for this class */
59    private static Log logCat = LogFactory.getLog(EventEngine.class.getName());
60
61    /** instance of DatabaseEventFactory */
62    private DatabaseEventFactory dbEventFactory = DatabaseEventFactoryImpl
63                                                  .instance();
64    private DbFormsConfig config;
65    private HttpServletRequest JavaDoc request;
66
67    /** instance of NavigationEventFactory */
68    private NavEventFactory navEventFactory = NavEventFactoryImpl.instance();
69
70    /**
71     * this vector will contain which tables where on the jsp page: one jsp file
72     * may contain multiple dbforms, and each forms could contain many subforms
73     * nested inside
74     */

75    private Vector JavaDoc involvedTables;
76
77    /**
78     * Constructor.
79     *
80     * @param request the request object
81     * @param config the configuration object
82     */

83    public EventEngine(HttpServletRequest JavaDoc request,
84                       DbFormsConfig config) {
85       this.request = request;
86       this.config = config;
87
88       // find out which tables where on the jsp
89
involvedTables = parseInvolvedTables();
90    }
91
92    /**
93     * Get the involvedTables attribute of the EventEngine class.
94     *
95     * @return the involvedTables attribute of the EventEngine class
96     */

97    public Vector JavaDoc getInvolvedTables() {
98       return involvedTables;
99    }
100
101
102    /**
103     * Generate the primary event object, depending on the data contained into
104     * the incoming http request object.
105     *
106     * @return a new WebEvent object
107     */

108    public WebEvent generatePrimaryEvent() {
109       WebEvent e = null;
110       String JavaDoc action = ParseUtil.getFirstParameterStartingWith(request, "ac_");
111       String JavaDoc customEvent = ParseUtil.getParameter(request, "customEvent");
112
113       if (Util.isNull(action) && !Util.isNull(customEvent)) {
114          // 2003-07-21-HKK: fixing NS 4.79 bug! (From bug list)
115
action = customEvent.trim();
116       }
117
118       // NOOP EVENT
119
//
120
// family: web event
121
if (Util.isNull(action)) {
122          logCat.info("##### N O O P ELEMENT ######");
123          e = new NoopEvent(-1, request, config);
124          initializeWebEvent(e);
125
126          return e;
127       }
128
129       // RELOAD EVENT
130
//
131
// family: web event
132
//
133
// ReloadEvent use to refresh field values from request object
134
// and to allow server side manipulation for these fields
135
//
136
// This event is created if customEvent is set to re_x_x!!
137
//
138
if (action.startsWith("re_")) {
139          logCat.info("##### RELOAD EVENT ######");
140          e = new PageReloadEvent(StringUtil.getEmbeddedStringAsInteger(action,
141                                                                       2, '_'),
142                                  request, config);
143          e.setType(EventType.EVENT_NAVIGATION_RELOAD);
144          initializeWebEvent(e);
145
146          return e;
147       }
148
149       // make the image button data (if any) look like a submit button;
150
action = getImageButtonAction(action);
151
152       // get the EventType class and identify the event type
153
// and use the related factory class to create the event;
154
EventType eventType = EventTypeUtil.getEventType(action);
155
156       switch (eventType.getEventGroup()) {
157          case EventType.EVENT_GROUP_DATABASE: {
158             logCat.info("::generatePrimaryEvent - generating a database event");
159             e = dbEventFactory.createEvent(action, request, config);
160
161             break;
162          }
163
164          case EventType.EVENT_GROUP_NAVIGATION: {
165             logCat.info("::generatePrimaryEvent - generating a navigation event");
166             e = navEventFactory.createEvent(action, request, config);
167
168             break;
169          }
170
171          default: {
172             logCat.error("::generatePrimaryEvent - WARNING: generating NO event. Why ?");
173
174             break;
175          }
176       }
177
178       // setting the followUp attributes for the generated event
179
setEventFollowUp(e, action);
180
181       return e;
182    }
183
184
185    /**
186     * Generate secundary events (update events)
187     *
188     * @param actTable DOCUMENT ME!
189     * @param exclude the parent web event (related to the main form)
190     *
191     * @return DOCUMENT ME!
192     */

193    public Enumeration JavaDoc generateSecundaryEvents(Table actTable,
194                                               WebEvent exclude) {
195       Vector JavaDoc result = new Vector JavaDoc();
196       int excludeTableId = -1;
197       String JavaDoc excludeKeyId = null;
198       boolean collissionDanger = false;
199
200       // first of all, we check if there is some real potential for collisions
201
// in the "to exclude"-event
202
if (exclude instanceof DatabaseEvent) {
203          collissionDanger = true;
204          excludeTableId = exclude.getTable()
205                                    .getId();
206          excludeKeyId = ((DatabaseEvent) exclude).getKeyId();
207       }
208
209       String JavaDoc param = "autoupdate_" + String.valueOf(actTable.getId());
210       String JavaDoc res = ParseUtil.getParameter(request, param);
211
212       // auto-updating may be disabled, so we have to check:
213
if (res.equalsIgnoreCase("true")
214                 || (res.equalsIgnoreCase("OnUpdateOnly")
215                 && exclude.getType()
216                                 .equals("update"))) {
217          // we can only update existing rowsets. so we just look for
218
// key-values
219
String JavaDoc paramStub = "k_" + actTable.getId() + "_";
220          Enumeration JavaDoc keysOfCurrentTable = ParseUtil.getParametersStartingWith(request,
221                                                                               paramStub)
222                                                    .elements();
223
224          while (keysOfCurrentTable.hasMoreElements()) {
225             String JavaDoc aKeyParam = (String JavaDoc) keysOfCurrentTable.nextElement();
226             String JavaDoc keyId = aKeyParam.substring(paramStub.length());
227
228             logCat.info("autoaupdate debug info: keyId=" + keyId
229                         + " excludeKeyId=" + excludeKeyId);
230
231             if (!collissionDanger
232                       || (excludeTableId != actTable.getId())
233                       || !keyId.equals(excludeKeyId)) {
234                DatabaseEvent e = dbEventFactory.createUpdateEvent(actTable
235                                                                   .getId(),
236                                                                   keyId,
237                                                                   request,
238                                                                   config);
239                result.addElement(e);
240             }
241          }
242
243          // now try the same with insert records
244
// but only if
245
if (collissionDanger && (excludeTableId != actTable.getId())) {
246             paramStub = "f_" + actTable.getId() + "_"
247                         + Constants.FIELDNAME_INSERTPREFIX;
248
249             Vector JavaDoc v = ParseUtil.getParametersStartingWith(request, paramStub);
250
251             if (v.size() > 0) {
252                String JavaDoc aKeyParam = (String JavaDoc) v.firstElement();
253                String JavaDoc keyId = aKeyParam.substring(paramStub.length());
254                keyId = StringUtil.getEmbeddedString(keyId, 0, '_');
255
256                DatabaseEvent e = dbEventFactory.createInsertEvent(actTable
257                                                                   .getId(),
258                                                                   keyId,
259                                                                   request,
260                                                                   config);
261                result.addElement(e);
262             }
263          }
264       }
265
266       return result.elements();
267    }
268
269
270    /**
271     * Sets the eventFollowUp and followUpOnError attributes of the input Event
272     * object
273     *
274     * @param e the event object
275     * @param action the action string
276     */

277    private void setEventFollowUp(WebEvent e,
278                                  String JavaDoc action) {
279       // now we have to find the followup-site the app-developer wants us to
280
// display.
281
String JavaDoc followUp = ParseUtil.getParameter(request, "data" + action + "_fu");
282
283       // if not...
284
// ...then check if 2-followup exists (should always exist!)
285
if (followUp == null) {
286          followUp = ParseUtil.getParameter(request,
287                                            "fu_"
288                                            + (((e == null)
289                                               || (e.getTable() == null)) ? (-1)
290                                                                          : e.getTable().getId()));
291       }
292
293       logCat.info("setting follow up to:" + followUp);
294
295       if (e != null) {
296          e.setFollowUp(followUp);
297       }
298
299       String JavaDoc followUpOnError = ParseUtil.getParameter(request,
300                                                       "data" + action + "_fue");
301
302       // if not...
303
// ...then check if 2-followup exists
304
if (followUpOnError == null) {
305          followUpOnError = ParseUtil.getParameter(request,
306                                                   "fue_"
307                                                   + (((e == null)
308                                                      || (e.getTable() == null))
309                                                      ? (-1)
310                                                      : e.getTable().getId()));
311       }
312
313       // Still no followup on error - use general followup
314
if (followUpOnError == null) {
315          followUpOnError = followUp;
316       }
317
318       logCat.info("setting follow up on Error to:" + followUpOnError);
319
320       if (e != null) {
321          e.setFollowUpOnError(followUpOnError);
322       }
323    }
324
325
326    /**
327     * Make the image button data look like a submit button. <br>
328     * Image buttons submit different parameters than submit buttons for submit
329     * the browser sends one value parameter:ac_insert_0_root=Submit this bug!
330     * for image buttons, the browser returns two values, showing the x and y
331     * position of the mouse parameter ac_insert_0_root.y=24 parameter
332     * ac_insert_0_root.x=34
333     *
334     * @param action Description of the Parameter
335     *
336     * @return The imageButtonAction value
337     */

338    private String JavaDoc getImageButtonAction(String JavaDoc action) {
339       if (action.endsWith(".y") || action.endsWith(".x")) {
340          action = action.substring(0, action.length() - 2);
341       }
342
343       logCat.info("::getImageButtonAction - action = [" + action + "]");
344
345       return action;
346    }
347
348
349    /**
350     * PRIVATE METHODS here
351     *
352     * @param contextPath DOCUMENT ME!
353     * @param sourcePath DOCUMENT ME!
354     *
355     * @return DOCUMENT ME!
356     */

357    /**
358     * Sets the sourcePath attribute of the EventEngine object
359     *
360     * @param contextPath The new sourcePath value
361     * @param sourcePath The new sourcePath value
362     *
363     * @return Description of the Return Value
364     */

365    private String JavaDoc setSourcePath(String JavaDoc contextPath,
366                                 String JavaDoc sourcePath) {
367       if (!Util.isNull(contextPath)
368                 && !Util.isNull(sourcePath)
369                 && sourcePath.startsWith(contextPath)) {
370          // shouldn't! just make sure!
371
if (contextPath.endsWith("/")) {
372             sourcePath = sourcePath.substring(contextPath.length() - 1);
373          } else {
374             sourcePath = sourcePath.substring(contextPath.length());
375          }
376       }
377
378       return sourcePath;
379    }
380
381
382    /**
383     * Initialize the input web event.
384     *
385     * @param e the web event to initialize
386     */

387    private void initializeWebEvent(WebEvent e) {
388       String JavaDoc contextPath = request.getContextPath();
389       String JavaDoc sourcePath = ParseUtil.getParameter(request, "source");
390
391       logCat.info("sourcePath = " + sourcePath);
392       sourcePath = setSourcePath(contextPath, sourcePath);
393
394       e.setFollowUp(sourcePath);
395       logCat.info("followup=" + e.getFollowUp());
396    }
397
398
399    /**
400     * Find out which tables where on the jsp (one jsp file may contain multiple
401     * dbforms, and each forms could contain many subforms nested inside!)
402     *
403     * @return the vector object containing the Table objects involved with the
404     * main table
405     */

406    private Vector JavaDoc parseInvolvedTables() {
407       String JavaDoc[] invTables = ParseUtil.getParameterValues(request, "invtable");
408
409       // in empty forms, for example, we don't have any involved tables..!
410
if (invTables == null) {
411          return null;
412       }
413
414       Vector JavaDoc result = new Vector JavaDoc();
415
416       for (int i = 0; i < invTables.length; i++) {
417          int tableIndex = Integer.parseInt(invTables[i]);
418          Table t = config.getTable(tableIndex);
419
420          result.addElement(t);
421       }
422
423       return result;
424    }
425 }
426
Popular Tags