View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/DatabaseEventFactoryImpl.java,v 1.19 2006/02/17 11:36:28 hkollmann Exp $
3    * $Revision: 1.19 $
4    * $Date: 2006/02/17 11:36:28 $
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  import org.dbforms.util.Util;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  /***
43   * DatabaseEventFactoryImpl class. Create DatabaseEvent objects.
44   * 
45   * @author Luca Fossato
46   * 
47   */
48  public class DatabaseEventFactoryImpl extends AbstractDatabaseEventFactory {
49  	/*** classes used as "keyInfo" constructor arguments types */
50  	private static final Class[] keyInfoConstructorArgsTypes = new Class[] {
51  			Integer.class, String.class, HttpServletRequest.class,
52  			DbFormsConfig.class };
53  
54  	/*** an handle to the unique DatabaseEventFactory instance */
55  	private static AbstractDatabaseEventFactory instance = null;
56  
57  	private static Log logCat = LogFactory.getLog(NavEventFactoryImpl.class);
58  
59  	/***
60  	 * Get the instance of DatabaseEventFactory class.
61  	 * 
62  	 * @return the instance of DAO class.
63  	 */
64  	public static synchronized AbstractDatabaseEventFactory instance() {
65  		if (instance == null) {
66  			instance = new DatabaseEventFactoryImpl();
67  		}
68  
69  		return instance;
70  	}
71  
72  	/***
73  	 * create and return a new database event
74  	 * 
75  	 * @param action
76  	 *            the action string that identifies the web event
77  	 * @param request
78  	 *            the HttpServletRequest object
79  	 * @param config
80  	 *            the DbForms config object
81  	 * 
82  	 * @return a new database event, or null if any error occurs
83  	 */
84  	public AbstractWebEvent createEvent(String action, HttpServletRequest request,
85  			DbFormsConfig config) {
86  		AbstractWebEvent event = null;
87  		Object[] constructorArgs = null;
88  
89  		// get the event id of the destination table
90  		String eventId = getEventIdFromDestinationTable(request, action);
91  		EventInfo einfo = getEventInfo(eventId);
92  
93  		// debug
94  		logCat.info("::createEvent - got event [" + einfo + "] from action ["
95  				+ action + "]");
96  
97  		// instance "normal" database events;
98  		if (!isKeyInfoEvent(action)) {
99  			constructorArgs = new Object[] { action, request, config };
100 			event = getEvent(einfo, constructorArgsTypes, constructorArgs);
101 		} else {
102 			// instance "keyInfo" database events;
103 			KeyInfo kInfo = getKeyInfo(action, request);
104 			if (kInfo == null) {
105 				// generate reload event if key is not found!
106 				// suggested by woodchudk5@sourceforge.net in
107 				// http://sourceforge.net/forum/message.php?msg_id=3340989
108 				int tableId = StringUtil.getEmbeddedStringAsInteger(action, 2,
109 						'_');
110 				event = new PageReloadEvent(tableId, request, config);
111 //				event.setType(EventType.EVENT_NAVIGATION_RELOAD);
112 			} else {
113 				// args are: tableId, keyId, request, config
114 				constructorArgs = new Object[] {
115 						new Integer(kInfo.getTableId()), kInfo.getKeyId(),
116 						request, config };
117 				event = getEvent(einfo, keyInfoConstructorArgsTypes,
118 						constructorArgs);
119 			}
120 		}
121 
122 		return event;
123 	}
124 
125 	/***
126 	 * Create and return a new InsertEvent as secondary event.
127 	 * 
128 	 * @param tableId
129 	 *            the table identifier
130 	 * @param keyId
131 	 *            the key identifier
132 	 * @param request
133 	 *            the HttpServletRequest object
134 	 * @param config
135 	 *            the DbForms config object
136 	 * 
137 	 * @return The updateEvent object
138 	 */
139 	public AbstractDatabaseEvent createInsertEvent(int tableId, String keyId,
140 			HttpServletRequest request, DbFormsConfig config) {
141 		AbstractDatabaseEvent event = null;
142 		Object[] constructorArgs = new Object[] { new Integer(tableId), keyId,
143 				request, config };
144 		Table table = config.getTable(tableId);
145 		TableEvents tableEvents = table.getTableEvents();
146 		String eventId = tableEvents
147 				.getEventId(EventType.EVENT_DATABASE_INSERT);
148 		EventInfo einfo = getEventInfo(eventId);
149 		event = (AbstractDatabaseEvent) getEvent(einfo, keyInfoConstructorArgsTypes,
150 				constructorArgs);
151 
152 		return event;
153 	}
154 
155 	/***
156 	 * Create and return a new UpdateEvent as secondary event.
157 	 * 
158 	 * @param tableId
159 	 *            the table identifier
160 	 * @param keyId
161 	 *            the key identifier
162 	 * @param request
163 	 *            the HttpServletRequest object
164 	 * @param config
165 	 *            the DbForms config object
166 	 * 
167 	 * @return The updateEvent object
168 	 */
169 	public AbstractDatabaseEvent createUpdateEvent(int tableId, String keyId,
170 			HttpServletRequest request, DbFormsConfig config) {
171 		AbstractDatabaseEvent event = null;
172 		Object[] constructorArgs = new Object[] { new Integer(tableId), keyId,
173 				request, config };
174 		Table table = config.getTable(tableId);
175 		TableEvents tableEvents = table.getTableEvents();
176 		String eventId = tableEvents
177 				.getEventId(EventType.EVENT_DATABASE_UPDATE);
178 		EventInfo einfo = getEventInfo(eventId);
179 		event = (AbstractDatabaseEvent) getEvent(einfo, keyInfoConstructorArgsTypes,
180 				constructorArgs);
181 
182 		return event;
183 	}
184 
185 	/***
186 	 * Initialize the default events.
187 	 * 
188 	 * @exception Exception
189 	 *                if any error occurs
190 	 */
191 	protected void initializeEvents() throws Exception {
192 		addEventInfo(new EventInfo(EventType.EVENT_DATABASE_DELETE,
193 				"org.dbforms.event.datalist.DeleteEvent"));
194 		addEventInfo(new EventInfo(EventType.EVENT_DATABASE_INSERT,
195 				"org.dbforms.event.datalist.InsertEvent"));
196 		addEventInfo(new EventInfo(EventType.EVENT_DATABASE_UPDATE,
197 				"org.dbforms.event.datalist.UpdateEvent"));
198 	}
199 
200 	/***
201 	 * Get key informations for "key info" database events
202 	 * 
203 	 * @param action
204 	 *            the action string
205 	 * @param request
206 	 *            the HttpServletRequest object
207 	 * 
208 	 * @return a keyInfo object containing the informations related to the
209 	 *         tableId and the keyId taken from the input action string, or null
210 	 *         if any error occurs
211 	 */
212 	private KeyInfo getKeyInfo(String action, HttpServletRequest request) {
213 		KeyInfo keyInfo = null;
214 		String associatedRadioName = ParseUtil.getParameter(request, "data"
215 				+ action + "_arname");
216 		String keyString = ParseUtil.getParameter(request, associatedRadioName);
217 
218 		if (!Util.isNull(keyString)) {
219 			int tableId = StringUtil.getEmbeddedStringAsInteger(keyString, 0, '_');
220 			String keyId = StringUtil.getEmbeddedString(keyString, 1, '_');
221 			keyInfo = new KeyInfo();
222 			keyInfo.setTableId(tableId);
223 			keyInfo.setKeyId(keyId);
224 		}
225 
226 		return keyInfo;
227 	}
228 
229 	/***
230 	 * PRIVATE METHODS here
231 	 * 
232 	 * @param action
233 	 *            DOCUMENT ME!
234 	 * 
235 	 * @return DOCUMENT ME!
236 	 */
237 	/***
238 	 * Checck if the input action string defines a "keyInfo event"
239 	 * 
240 	 * @param action
241 	 *            the action string
242 	 * 
243 	 * @return true if the input action string defines a "keyInfo event"; false
244 	 *         otherwise
245 	 */
246 	private boolean isKeyInfoEvent(String action) {
247 		boolean keyInfo = false;
248 
249 		if ((action.startsWith("ac_updatear_"))
250 				|| action.startsWith("ac_deletear_")) {
251 			keyInfo = true;
252 		}
253 
254 		// logCat.info("::isKeyInfoEvent - action [" + action + "] is related to
255 		// a keyInfo event ? [" + keyInfo + "]");
256 		return keyInfo;
257 	}
258 }