1 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 ; 40 41 42 43 49 public class DatabaseEventFactoryImpl extends DatabaseEventFactory { 50 51 private static final Class [] keyInfoConstructorArgsTypes = new Class [] { 52 Integer .class, 53 String .class, 54 HttpServletRequest .class, 55 DbFormsConfig.class 56 }; 57 58 59 private static DatabaseEventFactory instance = null; 60 private static Log logCat = LogFactory.getLog(NavEventFactoryImpl.class); 61 62 67 public static synchronized DatabaseEventFactory instance() { 68 if (instance == null) { 69 instance = new DatabaseEventFactoryImpl(); 70 } 71 72 return instance; 73 } 74 75 76 85 public WebEvent createEvent(String action, 86 HttpServletRequest request, 87 DbFormsConfig config) { 88 WebEvent event = null; 89 Object [] constructorArgs = null; 90 91 String eventId = getEventIdFromDestinationTable(request, action); 93 EventInfo einfo = getEventInfo(eventId); 94 95 logCat.info("::createEvent - got event [" + einfo + "] from action [" 97 + action + "]"); 98 99 if (!isKeyInfoEvent(action)) { 101 constructorArgs = new Object [] { 102 action, 103 request, 104 config 105 }; 106 event = getEvent(einfo, constructorArgsTypes, constructorArgs); 107 } 108 else { 110 KeyInfo kInfo = getKeyInfo(action, request); 111 112 constructorArgs = new Object [] { 114 new Integer (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 136 public DatabaseEvent createInsertEvent(int tableId, 137 String keyId, 138 HttpServletRequest request, 139 DbFormsConfig config) { 140 DatabaseEvent event = null; 141 Object [] constructorArgs = new Object [] { 142 new Integer (tableId), 143 keyId, 144 request, 145 config 146 }; 147 Table table = config.getTable(tableId); 148 TableEvents tableEvents = table.getTableEvents(); 149 String 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 168 public DatabaseEvent createUpdateEvent(int tableId, 169 String keyId, 170 HttpServletRequest request, 171 DbFormsConfig config) { 172 DatabaseEvent event = null; 173 Object [] constructorArgs = new Object [] { 174 new Integer (tableId), 175 keyId, 176 request, 177 config 178 }; 179 Table table = config.getTable(tableId); 180 TableEvents tableEvents = table.getTableEvents(); 181 String 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 195 protected void initializeEvents() throws Exception { 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 215 private KeyInfo getKeyInfo(String action, 216 HttpServletRequest request) { 217 KeyInfo keyInfo = null; 218 String associatedRadioName = ParseUtil.getParameter(request, 219 "data" + action 220 + "_arname"); 221 String keyString = ParseUtil.getParameter(request, associatedRadioName); 222 223 if (keyString != null) { 224 int tableId = StringUtil.getEmbeddedStringAsInteger(keyString, 0, '_'); 225 String 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 242 250 private boolean isKeyInfoEvent(String action) { 251 boolean keyInfo = false; 252 253 if ((action.startsWith("ac_updatear_")) 254 || action.startsWith("ac_deletear_")) { 255 keyInfo = true; 256 } 257 258 return keyInfo; 260 } 261 } 262 | Popular Tags |