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.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 ; 41 import java.util.Vector ; 42 43 import javax.servlet.http.HttpServletRequest ; 44 45 46 47 57 public class EventEngine { 58 59 private static Log logCat = LogFactory.getLog(EventEngine.class.getName()); 60 61 62 private DatabaseEventFactory dbEventFactory = DatabaseEventFactoryImpl 63 .instance(); 64 private DbFormsConfig config; 65 private HttpServletRequest request; 66 67 68 private NavEventFactory navEventFactory = NavEventFactoryImpl.instance(); 69 70 75 private Vector involvedTables; 76 77 83 public EventEngine(HttpServletRequest request, 84 DbFormsConfig config) { 85 this.request = request; 86 this.config = config; 87 88 involvedTables = parseInvolvedTables(); 90 } 91 92 97 public Vector getInvolvedTables() { 98 return involvedTables; 99 } 100 101 102 108 public WebEvent generatePrimaryEvent() { 109 WebEvent e = null; 110 String action = ParseUtil.getFirstParameterStartingWith(request, "ac_"); 111 String customEvent = ParseUtil.getParameter(request, "customEvent"); 112 113 if (Util.isNull(action) && !Util.isNull(customEvent)) { 114 action = customEvent.trim(); 116 } 117 118 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 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 action = getImageButtonAction(action); 151 152 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 setEventFollowUp(e, action); 180 181 return e; 182 } 183 184 185 193 public Enumeration generateSecundaryEvents(Table actTable, 194 WebEvent exclude) { 195 Vector result = new Vector (); 196 int excludeTableId = -1; 197 String excludeKeyId = null; 198 boolean collissionDanger = false; 199 200 if (exclude instanceof DatabaseEvent) { 203 collissionDanger = true; 204 excludeTableId = exclude.getTable() 205 .getId(); 206 excludeKeyId = ((DatabaseEvent) exclude).getKeyId(); 207 } 208 209 String param = "autoupdate_" + String.valueOf(actTable.getId()); 210 String res = ParseUtil.getParameter(request, param); 211 212 if (res.equalsIgnoreCase("true") 214 || (res.equalsIgnoreCase("OnUpdateOnly") 215 && exclude.getType() 216 .equals("update"))) { 217 String paramStub = "k_" + actTable.getId() + "_"; 220 Enumeration keysOfCurrentTable = ParseUtil.getParametersStartingWith(request, 221 paramStub) 222 .elements(); 223 224 while (keysOfCurrentTable.hasMoreElements()) { 225 String aKeyParam = (String ) keysOfCurrentTable.nextElement(); 226 String 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 if (collissionDanger && (excludeTableId != actTable.getId())) { 246 paramStub = "f_" + actTable.getId() + "_" 247 + Constants.FIELDNAME_INSERTPREFIX; 248 249 Vector v = ParseUtil.getParametersStartingWith(request, paramStub); 250 251 if (v.size() > 0) { 252 String aKeyParam = (String ) v.firstElement(); 253 String 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 277 private void setEventFollowUp(WebEvent e, 278 String action) { 279 String followUp = ParseUtil.getParameter(request, "data" + action + "_fu"); 282 283 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 followUpOnError = ParseUtil.getParameter(request, 300 "data" + action + "_fue"); 301 302 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 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 338 private String getImageButtonAction(String 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 357 365 private String setSourcePath(String contextPath, 366 String sourcePath) { 367 if (!Util.isNull(contextPath) 368 && !Util.isNull(sourcePath) 369 && sourcePath.startsWith(contextPath)) { 370 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 387 private void initializeWebEvent(WebEvent e) { 388 String contextPath = request.getContextPath(); 389 String 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 406 private Vector parseInvolvedTables() { 407 String [] invTables = ParseUtil.getParameterValues(request, "invtable"); 408 409 if (invTables == null) { 411 return null; 412 } 413 414 Vector result = new Vector (); 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 |