1 23 24 package examples.invoice.persistclass; 25 26 import org.objectweb.jorm.api.PAccessor; 27 import org.objectweb.jorm.api.PGenClassAccessor; 28 import org.objectweb.jorm.api.PBinding; 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.jorm.api.PIndexedElem; 31 import org.objectweb.jorm.naming.api.PName; 32 33 import java.io.Serializable ; 34 import java.util.Date ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.Set ; 38 import java.math.BigDecimal ; 39 import java.math.BigInteger ; 40 41 44 public class Invoice implements PAccessor, InvoiceAccessor { 45 private int number; 46 private PName addressPn; 47 private Address address = null; 48 private PName pusetPn; 49 private Set puset = null; 50 private SetAccessor pusetAcc; 51 private PBinding pusetBinding = null; 52 private PBinding pBinding; 53 private InvoiceHelper invoiceHelper; 54 55 public PBinding getPBinding() { 56 return pBinding; 57 } 58 59 62 private Address getAddress() throws PException { 63 try { 64 if (address == null) 65 address = invoiceHelper.getAddressHelper().getAddress( 66 pBinding.getPClassMapping().getPMapper(), 67 addressPn); 68 } catch (Exception e) { 69 e.printStackTrace(); 70 throw new PException(e, "Cannot getAddress within Invoice!"); 71 } 72 return address; 73 } 74 75 78 private Set getPuSet() throws PException { 79 if (pusetPn == null) 80 return null; 81 if (pusetBinding == null) { 82 pusetBinding = pBinding.getPClassMapping().getGenClassMapping("puset").createPBinding(); 83 pusetBinding.bind(pusetPn); 84 puset = new HashSet (); 85 pusetAcc = new SetAccessor(puset); 86 Object conn = null; 87 try { 88 conn = pBinding.getPClassMapping().getPMapper().getConnection(); 89 pusetBinding.read(conn, pusetAcc); 90 } catch (PException pe) { 91 throw pe; 92 } catch (Exception e) { 93 throw new PException(e, "Cannot getPuSet-1"); 94 } finally { 95 try { 96 pBinding.getPClassMapping().getPMapper().closeConnection(conn); 97 } catch (Exception e) { 98 throw new PException(e, "Cannot getPuSet-2"); 99 } 100 } 101 } 102 return puset; 103 } 104 105 108 public Invoice(InvoiceHelper ih, Object conn, PBinding pb, int n, 109 PName ad, PName[] pus) throws PException { 110 invoiceHelper = ih; 111 pBinding = pb; 112 number = n; 113 addressPn = ad; 114 try { 115 pusetBinding = pBinding.getPClassMapping().getGenClassMapping("puset").createPBinding(); 116 pusetPn = pusetBinding.export(conn, new Integer (number)); 117 puset = new HashSet (); 118 pusetAcc = new SetAccessor(puset); 119 for (int i = 0; i < pus.length; i++) { 120 ProdUnits pu = invoiceHelper.getProdUnitsHelper().getProdUnits( 121 pBinding.getPClassMapping().getPMapper(), pus[i]); 122 puset.add(pu); 123 } 124 pusetBinding.write(conn, pusetAcc); 125 pBinding.export(conn, new Integer (number)); 126 pBinding.write(conn, this); 127 } catch (PException pe) { 128 pe.getNestedException().printStackTrace(); 129 throw pe; 130 } catch (Exception e) { 131 e.printStackTrace(); 132 throw new PException(e, "Pb while creating an Invoice."); 133 } 134 } 135 136 139 public Invoice(InvoiceHelper ih, Object conn, PBinding pb) throws PException { 140 invoiceHelper = ih; 141 pBinding = pb; 142 pb.read(conn, this); 143 } 144 145 149 public void delete(Object conn) throws PException { 150 if (pusetBinding == null) { 151 pusetBinding = pBinding.getPClassMapping().getGenClassMapping("puset").createPBinding(); 152 } 153 pusetBinding.unexport(conn); 154 pusetBinding.write(conn, pusetAcc); 155 pusetBinding = null; 156 pBinding.unexport(conn); 157 pBinding.write(conn, this); 158 pBinding = null; 159 } 160 161 164 public String toPrintable() throws PException { 165 String res = "ID: [" + number + "]\n\t\tVALUE: [\n\t\t\tnumber: " + number + ",\n\t\t\taddress: [" + getAddress().toPrintable() + "],\n\t\t\tpuset: {"; 166 Set s = getPuSet(); 167 Iterator it = s.iterator(); 168 String sep = "\n\t\t\t\t"; 169 while (it.hasNext()) { 170 ProdUnits pu = (ProdUnits) it.next(); 171 res += sep + pu.toPrintable(); 172 sep = ",\n\t\t\t\t"; 173 } 174 res += "\n\t\t\t}\n\t\t]"; 175 return res; 176 } 177 178 180 public Object getMemoryInstance() { 181 return this; 182 } 183 184 186 public void paSetNumber(int val) throws PException { 188 number = val; 189 } 190 191 public int paGetNumber() throws PException { 192 return number; 193 } 194 195 public void paSetAddress(PName val, Object conn) throws PException { 197 addressPn = val; 198 } 199 200 public PName paGetAddress(Object conn) throws PException { 201 return addressPn; 202 } 203 204 public void paSetPuset(PName val, Object connection) throws PException { 206 pusetPn = val; 207 } 208 209 public PName paGetPuset(Object connection) throws PException { 210 return pusetPn; 211 } 212 213 218 class SetAccessor implements PGenClassAccessor { 219 private Set setData; 220 221 SetAccessor(Set sd) { 222 setData = sd; 223 } 224 225 public void paAdd(PIndexedElem elem, Object conn) throws PException { 226 try { 227 ProdUnits pu = invoiceHelper.getProdUnitsHelper().getProdUnits( 228 pBinding.getPClassMapping().getPMapper(), elem.pieGetRefElem()); 229 setData.add(pu); 230 } catch (Exception e) { 231 throw new PException(e, "Pb to Add in Invoice/SetAccessor."); 232 } 233 } 234 235 public Object getMemoryInstance() { 236 return setData; 237 } 238 239 public PIndexedElem createPIndexedElem() { 240 return new SetIndexedElem(); 241 } 242 243 public boolean paDeltaSupported() { 244 return false; 245 } 246 247 public int paGetNbElem() { 248 return setData.size(); 249 } 250 251 public Iterator paIterator() { 252 return new SetAccessorIterator(setData.iterator()); 253 } 254 255 public void paSetNbElem(int nbelem) { 256 } 257 258 262 class SetIndexedElem implements PIndexedElem { 263 private final static String ERRNOINDEX = "Set generic class has no index for its elements."; 264 private final static String ERRTYPEUNSUPP = "This is a set of reference (PName)."; 265 private byte status = PIndexedElem.ELEM_CREATED; 266 private PName ref; 267 268 public byte getElemStatus() { 269 return status; 270 } 271 272 public boolean pieGetBooleanElem() throws PException { 273 throw new PException(ERRTYPEUNSUPP); 274 } 275 276 public Boolean pieGetObooleanElem() throws PException { 277 throw new PException(ERRTYPEUNSUPP); 278 } 279 280 public byte pieGetByteElem() throws PException { 281 throw new PException(ERRTYPEUNSUPP); 282 } 283 284 public Byte pieGetObyteElem() throws PException { 285 throw new PException(ERRTYPEUNSUPP); 286 } 287 288 public byte pieGetByteIndexField(String fn) throws PException { 289 throw new PException(ERRNOINDEX); 290 } 291 292 public Byte pieGetObyteIndexField(String fn) throws PException { 293 throw new PException(ERRNOINDEX); 294 } 295 296 public char pieGetCharElem() throws PException { 297 throw new PException(ERRTYPEUNSUPP); 298 } 299 300 public Character pieGetOcharElem() throws PException { 301 throw new PException(ERRTYPEUNSUPP); 302 } 303 304 public char pieGetCharIndexField(String fn) throws PException { 305 throw new PException(ERRNOINDEX); 306 } 307 308 public Character pieGetOcharIndexField(String fn) throws PException { 309 throw new PException(ERRNOINDEX); 310 } 311 312 public short pieGetShortElem() throws PException { 313 throw new PException(ERRTYPEUNSUPP); 314 } 315 316 public Short pieGetOshortElem() throws PException { 317 throw new PException(ERRTYPEUNSUPP); 318 } 319 320 public short pieGetShortIndexField(String fn) throws PException { 321 throw new PException(ERRNOINDEX); 322 } 323 324 public Short pieGetOshortIndexField(String fn) throws PException { 325 throw new PException(ERRNOINDEX); 326 } 327 328 public int pieGetIntElem() throws PException { 329 throw new PException(ERRTYPEUNSUPP); 330 } 331 332 public Integer pieGetOintElem() throws PException { 333 throw new PException(ERRTYPEUNSUPP); 334 } 335 336 public int pieGetIntIndexField(String fn) throws PException { 337 throw new PException(ERRNOINDEX); 338 } 339 340 public Integer pieGetOintIndexField(String fn) throws PException { 341 throw new PException(ERRNOINDEX); 342 } 343 344 public long pieGetLongElem() throws PException { 345 throw new PException(ERRTYPEUNSUPP); 346 } 347 348 public Long pieGetOlongElem() throws PException { 349 throw new PException(ERRTYPEUNSUPP); 350 } 351 352 public long pieGetLongIndexField(String fn) throws PException { 353 throw new PException(ERRNOINDEX); 354 } 355 356 public Long pieGetOlongIndexField(String fn) throws PException { 357 throw new PException(ERRNOINDEX); 358 } 359 360 public float pieGetFloatElem() throws PException { 361 throw new PException(ERRTYPEUNSUPP); 362 } 363 364 public Float pieGetOfloatElem() throws PException { 365 throw new PException(ERRTYPEUNSUPP); 366 } 367 368 public double pieGetDoubleElem() throws PException { 369 throw new PException(ERRTYPEUNSUPP); 370 } 371 372 public Double pieGetOdoubleElem() throws PException { 373 throw new PException(ERRTYPEUNSUPP); 374 } 375 376 public String pieGetStringElem() throws PException { 377 throw new PException(ERRTYPEUNSUPP); 378 } 379 380 public String pieGetStringIndexField(String fn) throws PException { 381 throw new PException(ERRNOINDEX); 382 } 383 384 public BigDecimal pieGetBigDecimalElem() throws PException { 385 throw new PException(ERRTYPEUNSUPP); 386 } 387 388 public BigInteger pieGetBigIntegerElem() throws PException { 389 throw new PException(ERRTYPEUNSUPP); 390 } 391 392 public Date pieGetDateElem() throws PException { 393 throw new PException(ERRTYPEUNSUPP); 394 } 395 396 public Date pieGetDateIndexField(String fn) throws PException { 397 throw new PException(ERRNOINDEX); 398 } 399 400 public char[] pieGetCharArrayElem() throws PException { 401 throw new PException(ERRTYPEUNSUPP); 402 } 403 404 public byte[] pieGetByteArrayElem() throws PException { 405 throw new PException(ERRTYPEUNSUPP); 406 } 407 408 public Serializable pieGetSerializedElem() throws PException { 409 throw new PException(ERRTYPEUNSUPP); 410 } 411 412 public PName pieGetRefElem() throws PException { 413 return ref; 414 } 415 416 public void pieSetBooleanElem(boolean value) throws PException { 417 throw new PException(ERRTYPEUNSUPP); 418 } 419 420 public void pieSetObooleanElem(Boolean value) throws PException { 421 throw new PException(ERRTYPEUNSUPP); 422 } 423 424 public void pieSetByteElem(byte value) throws PException { 425 throw new PException(ERRTYPEUNSUPP); 426 } 427 428 public void pieSetObyteElem(Byte value) throws PException { 429 throw new PException(ERRTYPEUNSUPP); 430 } 431 432 public void pieSetByteIndexField(String fn, byte value) throws PException { 433 throw new PException(ERRNOINDEX); 434 } 435 436 public void pieSetObyteIndexField(String fn, Byte value) throws PException { 437 throw new PException(ERRNOINDEX); 438 } 439 440 public void pieSetCharElem(char value) throws PException { 441 throw new PException(ERRTYPEUNSUPP); 442 } 443 444 public void pieSetOcharElem(Character value) throws PException { 445 throw new PException(ERRTYPEUNSUPP); 446 } 447 448 public void pieSetCharIndexField(String fn, char value) throws PException { 449 throw new PException(ERRNOINDEX); 450 } 451 452 public void pieSetOcharIndexField(String fn, Character value) throws PException { 453 throw new PException(ERRNOINDEX); 454 } 455 456 public void pieSetShortElem(short value) throws PException { 457 throw new PException(ERRTYPEUNSUPP); 458 } 459 460 public void pieSetOshortElem(Short value) throws PException { 461 throw new PException(ERRTYPEUNSUPP); 462 } 463 464 public void pieSetShortIndexField(String fn, short value) throws PException { 465 throw new PException(ERRNOINDEX); 466 } 467 468 public void pieSetOshortIndexField(String fn, Short value) throws PException { 469 throw new PException(ERRNOINDEX); 470 } 471 472 public void pieSetIntElem(int value) throws PException { 473 throw new PException(ERRTYPEUNSUPP); 474 } 475 476 public void pieSetOintElem(Integer value) throws PException { 477 throw new PException(ERRTYPEUNSUPP); 478 } 479 480 public void pieSetIntIndexField(String fn, int value) throws PException { 481 throw new PException(ERRNOINDEX); 482 } 483 484 public void pieSetOintIndexField(String fn, Integer value) throws PException { 485 throw new PException(ERRNOINDEX); 486 } 487 488 public void pieSetLongElem(long value) throws PException { 489 throw new PException(ERRTYPEUNSUPP); 490 } 491 492 public void pieSetOlongElem(Long value) throws PException { 493 throw new PException(ERRTYPEUNSUPP); 494 } 495 496 public void pieSetLongIndexField(String fn, long value) throws PException { 497 throw new PException(ERRNOINDEX); 498 } 499 500 public void pieSetOlongIndexField(String fn, Long value) throws PException { 501 throw new PException(ERRNOINDEX); 502 } 503 504 public void pieSetFloatElem(float value) throws PException { 505 throw new PException(ERRTYPEUNSUPP); 506 } 507 508 public void pieSetOfloatElem(Float value) throws PException { 509 throw new PException(ERRTYPEUNSUPP); 510 } 511 512 public void pieSetDoubleElem(double value) throws PException { 513 throw new PException(ERRTYPEUNSUPP); 514 } 515 516 public void pieSetOdoubleElem(Double value) throws PException { 517 throw new PException(ERRTYPEUNSUPP); 518 } 519 520 public void pieSetStringElem(String value) throws PException { 521 throw new PException(ERRTYPEUNSUPP); 522 } 523 524 public void pieSetStringIndexField(String fn, String value) throws PException { 525 throw new PException(ERRNOINDEX); 526 } 527 528 public void pieSetBigDecimalElem(BigDecimal value) throws PException { 529 throw new PException(ERRTYPEUNSUPP); 530 } 531 532 public void pieSetBigIntegerElem(BigInteger value) throws PException { 533 throw new PException(ERRTYPEUNSUPP); 534 } 535 536 public void pieSetDateElem(Date value) throws PException { 537 throw new PException(ERRTYPEUNSUPP); 538 } 539 540 public void pieSetDateIndexField(String fn, Date value) throws PException { 541 throw new PException(ERRNOINDEX); 542 } 543 544 public void pieSetCharArrayElem(char[] value) throws PException { 545 throw new PException(ERRTYPEUNSUPP); 546 } 547 548 public void pieSetByteArrayElem(byte[] value) throws PException { 549 throw new PException(ERRTYPEUNSUPP); 550 } 551 552 public void pieSetSerializedElem(Serializable value) throws PException { 553 throw new PException(ERRTYPEUNSUPP); 554 } 555 556 public void pieSetRefElem(PName value) throws PException { 557 ref = value; 558 } 559 } 560 561 564 class SetAccessorIterator implements Iterator { 565 Iterator pusetIt; 566 SetIndexedElem ie; 567 568 SetAccessorIterator(Iterator it) { 569 pusetIt = it; 570 ie = new SetIndexedElem(); 571 } 572 573 public boolean hasNext() { 574 return pusetIt.hasNext(); 575 } 576 577 public Object next() { 578 if (!hasNext()) 579 return null; 580 try { 581 ie.pieSetRefElem(((ProdUnits) pusetIt.next()).getPBinding().getPName()); 582 } catch (PException pe) { 583 pe.printStackTrace(); 584 return null; 585 } 586 return ie; 587 } 588 589 public void remove() { 590 } 591 } 592 } 593 } 594 | Popular Tags |