1 19 package org.netbeans.mdr.storagemodel; 20 21 import java.util.*; 22 23 import org.netbeans.mdr.persistence.*; 24 import org.netbeans.mdr.util.*; 25 26 31 public abstract class StorableBaseObject implements Streamable, StorageClient { 32 33 34 private org.netbeans.mdr.persistence.MOFID id; 35 36 protected org.netbeans.mdr.persistence.MOFID meta; 38 protected org.netbeans.mdr.persistence.MOFID immediatePackage; 40 private transient org.netbeans.mdr.persistence.MOFID outermostPackage = null; 42 private transient StorablePackage immediatePackageObj = null; 43 private transient StorablePackage outermostPackageObj = null; 44 45 46 private transient Storage storage; 47 48 49 private transient MdrStorage mdrStorage; 50 51 protected boolean initFinished; 53 54 private Object slot1 = null; 56 private Object slot2 = null; 57 private Object slot3 = null; 58 private Object slot4 = null; 59 private Map propertiesSlot = null; 60 private int slotsCount = 0; 62 63 67 public StorableBaseObject() { 68 super(); 69 initFinished = true; 70 } 71 72 StorableBaseObject (MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID immediatePackage, org.netbeans.mdr.persistence.MOFID meta) throws StorageException { 73 this(mdrStorage, immediatePackage, meta, null); 74 } 75 76 StorableBaseObject (MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID immediatePackage, org.netbeans.mdr.persistence.MOFID meta, String storageId) throws StorageException { 77 initFinished = false; 78 if (storageId == null) { 79 this.id = mdrStorage.generateMOFID (immediatePackage); 80 } 81 else { 82 this.id = mdrStorage.generateMOFID (storageId); 83 } 84 this.meta = meta; 85 this.immediatePackage = immediatePackage; 86 this.mdrStorage = mdrStorage; 87 if (immediatePackage != null) { 89 org.netbeans.mdr.persistence.MOFID parentOutermost = mdrStorage.getObject(immediatePackage).getOutermostPackageId(); 90 if (parentOutermost == null) { 91 this.outermostPackage = immediatePackage; 92 } else { 93 this.outermostPackage = parentOutermost; 94 } 95 } else { 96 this.outermostPackage = this.id; 97 } 98 } 99 100 104 public StorableObject getMetaObject() throws StorageException { 105 return (StorableObject) getMdrStorage().getObject(meta); 106 } 107 108 112 public StorablePackage getImmediatePackage() throws StorageException { 113 if (immediatePackageObj == null) { 114 immediatePackageObj = (StorablePackage) getMdrStorage().getObject(immediatePackage); 115 } 116 return immediatePackageObj; 117 } 118 119 122 public org.netbeans.mdr.persistence.MOFID getImmediatePackageId() { 123 return immediatePackage; 124 } 125 126 129 public org.netbeans.mdr.persistence.MOFID getMetaObjectId() { 130 return meta; 131 } 132 133 136 public org.netbeans.mdr.persistence.MOFID getOutermostPackageId() { 137 if (outermostPackage == null) { 138 if (immediatePackage == null) { 140 outermostPackage = getMofId(); } else { 142 StorableBaseObject temp, pkg; 143 try { 144 pkg = mdrStorage.getObject(immediatePackage); 145 do { 146 temp = pkg.getImmediatePackage(); 147 if (temp != null) pkg = temp; 148 else break; 149 } while (temp != null); 150 outermostPackage = pkg.getMofId(); 151 } catch (StorageException e) { 152 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 153 } 154 } } return outermostPackage; 157 } 158 159 163 public StorablePackage getOutermostPackage() throws StorageException { 164 if (outermostPackageObj == null) { 165 outermostPackageObj = (StorablePackage) getMdrStorage().getObject(getOutermostPackageId()); 166 } 167 return outermostPackageObj; 168 } 169 170 173 public org.netbeans.mdr.persistence.MOFID getMofId() { 174 return id; 175 } 176 177 180 public String toString () { 181 return getClass().getName() + "("+id.toString()+")"; 182 } 183 184 187 public boolean equals(Object o) { 188 return (o instanceof StorableBaseObject) && (((StorableBaseObject)o).getMofId().equals(this.getMofId())); 189 } 190 191 194 public int hashCode() { 195 return id.hashCode(); 196 } 197 198 protected void deleteRecursive() throws StorageException { 199 getMdrStorage().removeObject(this); 200 } 201 202 205 public void write (java.io.OutputStream outputStream) { 206 try { 208 IOUtils.writeMOFID (outputStream, id,this.getMdrStorage(), this.id); 209 outputStream.write(slotsCount); 210 switch (slotsCount) { 211 case 5: IOUtils.write(outputStream, this.propertiesSlot, this); 212 case 4: IOUtils.write(outputStream, slot4, this); 213 case 3: IOUtils.write(outputStream, slot3, this); 214 case 2: IOUtils.write(outputStream, slot2, this); 215 case 1: IOUtils.write(outputStream, slot1, this); 216 } 217 } catch (java.io.IOException e) { 218 throw (DebugException) Logger.getDefault().annotate(new RuntimeException (), e); 219 } 220 } 221 222 225 public void read (java.io.InputStream inputStream) { 226 try { 227 id = IOUtils.readMOFID (inputStream, storage); 228 slotsCount = inputStream.read(); 229 switch (slotsCount) { 230 case 5: this.propertiesSlot = (Map) IOUtils.read (inputStream, this, null); 231 case 4: slot4 = IOUtils.read(inputStream, this, null); 232 case 3: slot3 = IOUtils.read(inputStream, this, null); 233 case 2: slot2 = IOUtils.read(inputStream, this, null); 234 case 1: slot1 = IOUtils.read(inputStream, this, null); 235 } 236 outermostPackage = null; } catch (java.io.IOException e) { 238 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 239 } 240 } 241 242 public void setStorage(Storage storage) { 243 this.storage = storage; 244 mdrStorage = MdrStorage.getInstance(storage); 245 } 246 247 250 public MdrStorage getMdrStorage() { 251 return mdrStorage; 252 } 253 254 260 public void putProperty (Object key, Object value) { 261 if (key == null || value == null ) 263 throw new IllegalArgumentException (); 264 this.objectWillChange(); 265 if (this.propertiesSlot == null) { 266 this.propertiesSlot = new HashMap (); 267 slotsCount = 5; 268 } 269 boolean failed = true; 270 this.mdrStorage.getRepository().beginTrans(true); 271 try { 272 this.propertiesSlot.put (key, value); 273 this.objectChanged (); 274 failed = false; 275 } finally { 276 this.mdrStorage.getRepository().endTrans(failed); 277 } 278 } 279 280 284 public Object removeProperty (Object key) { 285 if (this.propertiesSlot == null) 286 return null; 287 boolean failed = true; 288 this.mdrStorage.getRepository().beginTrans(true); 289 try { 290 this.objectWillChange(); 291 Object result = this.propertiesSlot.remove (key); 292 if (result != null) { 293 this.objectChanged (); 294 } 295 if (this.propertiesSlot.size () == 0) 296 this.propertiesSlot = null; 297 failed = false; 298 return result; 299 } finally { 300 this.mdrStorage.getRepository().endTrans(failed); 301 } 302 } 303 304 308 public Object getProperty (Object key) { 309 if (this.propertiesSlot == null) 310 return null; 311 else 312 return this.propertiesSlot.get (key); 313 } 314 315 public Object setSlot1(Object value) { 316 Object result = slot1; 317 objectWillChange(); 318 slot1 = value; 319 if (slotsCount < 1) slotsCount = 1; 320 objectChanged(); 321 return result; 322 } 323 324 public Object getSlot1() { 325 return slot1; 326 } 327 328 public Object setSlot2(Object value) { 329 Object result = slot2; 330 objectWillChange(); 331 slot2 = value; 332 if (slotsCount < 2) slotsCount = 2; 333 objectChanged(); 334 return result; 335 } 336 337 public Object getSlot2() { 338 return slot2; 339 } 340 341 public Object setSlot3(Object value) { 342 Object result = slot3; 343 objectWillChange(); 344 slot3 = value; 345 if (slotsCount < 3) slotsCount = 3; 346 objectChanged(); 347 return result; 348 } 349 350 public Object getSlot3() { 351 return slot3; 352 } 353 354 public Object setSlot4(Object value) { 355 Object result = slot4; 356 objectWillChange(); 357 slot4 = value; 358 if (slotsCount < 4) slotsCount = 4; 359 objectChanged(); 360 return result; 361 } 362 363 public Object getSlot4() { 364 return slot4; 365 } 366 367 370 protected void replaceValues(Map table) { 371 meta = (org.netbeans.mdr.persistence.MOFID) table.get(meta); 372 } 373 374 376 public void objectWillChange() { 377 if (initFinished) 378 try { 379 getMdrStorage().objectStateWillChange(id); 380 } catch (StorageException e) { 381 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 382 } 383 } 384 385 387 public void objectChanged() { 388 if (initFinished) 389 try { 390 getMdrStorage().objectStateChanged(id); 391 } catch (StorageException e) { 392 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 393 } 394 } 395 396 } 397 | Popular Tags |