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.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 55 public abstract class EventFactory { 56 57 private static Log logCat = LogFactory.getLog(EventFactory.class); 58 59 60 static final Class [] constructorArgsTypes = new Class [] { 61 String .class, 62 HttpServletRequest .class, 63 DbFormsConfig.class 64 }; 65 66 67 protected HashMap eventInfoMap = null; 68 69 72 protected EventFactory() { 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 91 public abstract WebEvent createEvent(String action, 92 HttpServletRequest request, 93 DbFormsConfig config); 94 95 96 102 public void addEventInfo(EventInfo einfo) { 103 if (eventInfoMap != null) { 104 String id = einfo.getId(); 107 108 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 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 133 138 protected abstract void initializeEvents() throws Exception ; 139 140 141 150 protected WebEvent getEvent(EventInfo einfo, 151 Class [] aconstructorArgsTypes, 152 Object [] 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 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 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 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 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 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 } 276 | Popular Tags |