1 package de.webman.acl; 2 3 import java.util.Hashtable ; 4 import java.util.Vector ; 5 import com.teamkonzept.lib.DatabaseErrorCodes; 6 import com.teamkonzept.lib.ErrorCodes; 7 import com.teamkonzept.lib.PropertyManager; 8 import com.teamkonzept.lib.TKConfigurationException; 9 import com.teamkonzept.lib.TKException; 10 import com.teamkonzept.lib.TKVector; 11 import de.webman.acl.db.ObjectCollectionDBData; 12 import de.webman.acl.db.ObjectDBData; 13 import de.webman.acl.db.ObjectDBInterface; 14 import com.teamkonzept.webman.mainint.WebmanExceptionHandler; 15 import com.teamkonzept.webman.mainint.events.TKUserException; 16 import com.teamkonzept.webman.mainint.events.UserCodes; 17 import org.apache.log4j.Category; 18 19 20 29 public abstract class ObjectFactoryBase 30 implements ObjectFactory 31 { 32 33 static Category cat = Category.getInstance(ObjectFactoryBase.class); 34 35 36 38 40 44 private boolean cacheLoadedObjects = false; 45 46 50 private boolean deferAssociationLoading = false; 51 52 55 private boolean preLoadObjects = false; 56 57 60 private boolean allObjectsCached = false; 61 62 65 private Hashtable cache = null; 66 67 68 70 73 public ObjectFactoryBase () 74 { 75 } 77 78 79 81 86 public abstract ObjectDBInterface getDBInterface (); 87 88 94 public abstract ObjectDBData getDBData (Integer id); 95 96 102 public abstract ObjectDBData getDBData (WMObject object); 103 104 110 public abstract WMObject buildObject (ObjectDBData data); 111 112 113 115 120 public synchronized void configurationChanged () 121 throws TKException 122 { 123 try 125 { 126 PropertyManager manager = PropertyManager.getPropertyManager(PROPERTY_GROUP_NAME); 128 129 this.cacheLoadedObjects = Boolean.valueOf(manager.getValue(PROPERTY_CACHE_LOADED_OBJECTS, 131 DEFAULT_CACHE_LOADED_OBJECTS)).booleanValue(); 132 133 this.deferAssociationLoading = Boolean.valueOf(manager.getValue(PROPERTY_DEFER_ASSOCIATION_LOADING, 135 DEFAULT_DEFER_ASSOCIATION_LOADING)).booleanValue(); 136 137 this.preLoadObjects = Boolean.valueOf(manager.getValue(PROPERTY_PRE_LOAD_OBJECTS, 139 DEFAULT_PRE_LOAD_OBJECTS)).booleanValue(); 140 } 141 catch (TKConfigurationException tkce) 142 { 143 this.cacheLoadedObjects = Boolean.valueOf(DEFAULT_CACHE_LOADED_OBJECTS).booleanValue(); 145 this.deferAssociationLoading = Boolean.valueOf(DEFAULT_DEFER_ASSOCIATION_LOADING).booleanValue(); 146 this.preLoadObjects = Boolean.valueOf(DEFAULT_PRE_LOAD_OBJECTS).booleanValue(); 147 } 148 149 if (! this.cacheLoadedObjects || 151 ! this.deferAssociationLoading) 152 { 153 this.preLoadObjects = false; 156 } 157 158 if (this.cacheLoadedObjects && this.cache == null) 160 { 161 this.cache = new Hashtable (); 163 } 164 165 if (! this.cacheLoadedObjects && this.cache != null) 166 { 167 this.cache = null; 169 } 170 171 if (this.preLoadObjects) 172 { 173 getObjects(); 175 } 176 } 177 178 179 181 188 public WMObject getObject (Integer id) 189 throws TKException 190 { 191 WMObject object = null; 192 193 try 194 { 195 object = this.cacheLoadedObjects 197 ? (WMObject) cache.get(id) 198 : null; 199 200 if (object == null) 201 { 202 ObjectDBData data = getDBData(id); 204 205 getDBInterface().selectPrimary(data); 207 208 if (! data.isEmpty()) 209 { 210 object = buildObject(data); 212 213 if (! this.deferAssociationLoading) 214 { 215 object.loadAssociations(); 217 } 218 219 if (this.cacheLoadedObjects) 220 { 221 cache.put(object.getID(), object); 223 } 224 } 225 } 226 } 227 catch (Exception x) 228 { 229 throw WebmanExceptionHandler.getException(x); 230 } 231 232 return object; 233 } 234 235 241 public TKVector getObjects () 242 throws TKException 243 { 244 TKVector objects = null; 245 246 try 247 { 248 if (this.allObjectsCached) 249 { 250 objects = new TKVector(this.cache.size()); 252 objects.fill(this.cache.elements()); 253 } 254 else 255 { 256 objects = getObjects(getObjectIDs()); 257 258 if (this.cacheLoadedObjects) 259 { 260 this.allObjectsCached = true; 262 } 263 } 264 } 265 catch (Exception x) 266 { 267 throw WebmanExceptionHandler.getException(x); 268 } 269 270 return objects; 271 } 272 273 280 public TKVector getObjects (TKVector ids) 281 throws TKException 282 { 283 TKVector objects = null; 284 285 try 286 { 287 if (ids != null) 288 { 289 int index = 0; 290 int size = ids.size(); 291 292 objects = new TKVector(size); 293 294 while (index < size) 295 { 296 objects.addElement(getObject((Integer ) ids.elementAt(index++))); 298 } 299 } 300 else 301 { 302 objects = new TKVector(0); 303 } 304 } 305 catch (Exception x) 306 { 307 throw WebmanExceptionHandler.getException(x); 308 } 309 310 return objects; 311 } 312 313 319 public TKVector getObjectIDs () 320 throws TKException 321 { 322 TKVector proxies = null; 323 324 try 325 { 326 ObjectDBData data = getDBData((Integer ) null); 328 data.setQuery(getDBInterface().getSelectAllQuery()); 329 data.setPrototype(new ObjectCollectionDBData(null, 330 null, 331 getDBInterface().getPrimaryKeyName(), 332 null)); 333 334 getDBInterface().selectDependent(data); 336 proxies = data.getCollection(); 337 } 338 catch (Exception x) 339 { 340 throw WebmanExceptionHandler.getException(x); 341 } 342 343 return proxies; 344 } 345 346 353 public TKVector getObjectIDs (ObjectDBData data) 354 throws TKException 355 { 356 TKVector proxies = null; 357 358 try 359 { 360 getDBInterface().selectDependent(data); 362 proxies = data.getCollection(); 363 } 364 catch (Exception x) 365 { 366 throw WebmanExceptionHandler.getException(x); 367 } 368 369 return proxies; 370 } 371 372 379 public TKVector getObjectAssociations (WMObject object) 380 throws TKException 381 { 382 TKVector proxies = null; 383 384 try 385 { 386 ObjectDBData data = getDBData(object.getID()); 388 389 if (! data.isIgnoreTable(null)) 390 { 391 data.setQuery(getDBInterface().getSelectDependentQuery()); 392 data.setPrototype(new ObjectCollectionDBData(getDBInterface().getPrimaryKeyName(), 393 object.getID(), 394 getDBInterface().getDependentKeyName(), 395 null)); 396 397 getDBInterface().selectDependent(data); 399 } 400 401 proxies = data.getCollection(); 402 } 403 catch (Exception x) 404 { 405 throw WebmanExceptionHandler.getException(x); 406 } 407 408 return proxies; 409 } 410 411 418 public WMObject createObject (ObjectDBData data) 419 throws TKException 420 { 421 WMObject object = null; 422 423 try 424 { 425 getDBInterface().insert(data); 427 428 object = buildObject(data); 430 431 if (this.cacheLoadedObjects) 432 { 433 cache.put(object.getID(), object); 435 } 436 } 437 catch (Exception x) 438 { 439 if (x instanceof TKException && 440 ((TKException) x).getErrorCode() == DatabaseErrorCodes.FK_CONSTRAINT_VIOLATION) 441 { 442 throw new TKUserException("An object with the given attributes already exists.", 443 UserCodes.DUPLICATE_OBJECT, 444 ErrorCodes.USER_SEVERITY, 445 true, 446 null); 447 } 448 449 throw WebmanExceptionHandler.getException(x); 450 } 451 452 return object; 453 } 454 455 461 public void modifyObject (WMObject object) 462 throws TKException 463 { 464 Integer id = null; 465 466 try 467 { 468 id = object.getID(); 469 470 if (object.isModifiedAttributes() || 471 object.isModifiedAssociations()) 472 { 473 ObjectDBData data = getDBData(object); 475 476 if (object.isModifiedAssociations()) 477 { 478 data.setQuery(getDBInterface().getInsertDependentQuery()); 480 data.setPrototype(new ObjectCollectionDBData(getDBInterface().getPrimaryKeyName(), 481 object.getID(), 482 getDBInterface().getDependentKeyName(), 483 null)); 484 data.setCollection(object.getAssociations()); 485 486 if (object.isModifiedAttributes()) 487 { 488 getDBInterface().updateFull(data); 490 } 491 else 492 { 493 getDBInterface().updateDependent(data); 495 } 496 } 497 else 498 { 499 getDBInterface().updatePrimary(data); 501 } 502 503 object.updatedAttributes(); 505 object.updatedAssociations(); 506 } 507 } 508 catch (NullPointerException npe) 509 { 510 } 512 catch (Exception x) 513 { 514 throw WebmanExceptionHandler.getException(x); 515 } 516 } 517 518 524 public void deleteObject (WMObject object) 525 throws TKException 526 { 527 Integer id = null; 528 529 try 530 { 531 id = object.getID(); 532 533 getDBInterface().delete(getDBData(id)); 535 536 if (this.cacheLoadedObjects) 537 { 538 cache.remove(id); 540 } 541 } 542 catch (NullPointerException npe) 543 { 544 } 546 catch (Exception x) 547 { 548 throw WebmanExceptionHandler.getException(x); 549 } 550 } 551 552 } 553 | Popular Tags |