KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > invoice > persistclass > Invoice


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

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 JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.math.BigDecimal JavaDoc;
39 import java.math.BigInteger JavaDoc;
40
41 /**
42  * @author P. Dechamboux
43  */

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 JavaDoc 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     /**
60      * Implements lazy dereferencing of address field.
61      */

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 JavaDoc e) {
69             e.printStackTrace();
70             throw new PException(e, "Cannot getAddress within Invoice!");
71         }
72         return address;
73     }
74
75     /**
76      * Implements lazy dereferencing of puset field.
77      */

78     private Set JavaDoc 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 JavaDoc();
85             pusetAcc = new SetAccessor(puset);
86             Object JavaDoc 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 JavaDoc e) {
93                 throw new PException(e, "Cannot getPuSet-1");
94             } finally {
95                 try {
96                     pBinding.getPClassMapping().getPMapper().closeConnection(conn);
97                 } catch (Exception JavaDoc e) {
98                     throw new PException(e, "Cannot getPuSet-2");
99                 }
100             }
101         }
102         return puset;
103     }
104
105     /**
106      * Creates a new Invoice.
107      */

108     public Invoice(InvoiceHelper ih, Object JavaDoc 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 JavaDoc(number));
117             puset = new HashSet JavaDoc();
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 JavaDoc(number));
126             pBinding.write(conn, this);
127         } catch (PException pe) {
128             pe.getNestedException().printStackTrace();
129             throw pe;
130         } catch (Exception JavaDoc e) {
131             e.printStackTrace();
132             throw new PException(e, "Pb while creating an Invoice.");
133         }
134     }
135
136     /**
137      * Creates an Invoice that is already persistent.
138      */

139     public Invoice(InvoiceHelper ih, Object JavaDoc conn, PBinding pb) throws PException {
140         invoiceHelper = ih;
141         pBinding = pb;
142         pb.read(conn, this);
143     }
144
145     /**
146      * Turns this persistent object into a non-persistent one as well as the
147      * referenced "puset".
148      */

149     public void delete(Object JavaDoc 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     /**
162      * Yields a printable version a the full Address.
163      */

164     public String JavaDoc toPrintable() throws PException {
165         String JavaDoc res = "ID: [" + number + "]\n\t\tVALUE: [\n\t\t\tnumber: " + number + ",\n\t\t\taddress: [" + getAddress().toPrintable() + "],\n\t\t\tpuset: {";
166         Set JavaDoc s = getPuSet();
167         Iterator JavaDoc it = s.iterator();
168         String JavaDoc 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     // IMPLEMENTATION OF PAccessor INTERFACE
179

180     public Object JavaDoc getMemoryInstance() {
181         return this;
182     }
183
184     // IMPLEMENTATION OF AddressAccessor INTERFACE (generated by JORM)
185

186     // Accessors to the name field
187
public void paSetNumber(int val) throws PException {
188         number = val;
189     }
190
191     public int paGetNumber() throws PException {
192         return number;
193     }
194
195     // Accessors to the size field
196
public void paSetAddress(PName val, Object JavaDoc conn) throws PException {
197         addressPn = val;
198     }
199
200     public PName paGetAddress(Object JavaDoc conn) throws PException {
201         return addressPn;
202     }
203
204     //Accessors to the puset field
205
public void paSetPuset(PName val, Object JavaDoc connection) throws PException {
206         pusetPn = val;
207     }
208
209     public PName paGetPuset(Object JavaDoc connection) throws PException {
210         return pusetPn;
211     }
212
213     /**
214      * Provided as the PGenClassAccessor for accessing elements of the "puset"
215      * HashSet (that are ProdUnits objects) as PIndexedElem in order to be able
216      * to store them with JORM.
217      */

218     class SetAccessor implements PGenClassAccessor {
219         private Set JavaDoc setData;
220
221         SetAccessor(Set JavaDoc sd) {
222             setData = sd;
223         }
224
225         public void paAdd(PIndexedElem elem, Object JavaDoc conn) throws PException {
226             try {
227                 ProdUnits pu = invoiceHelper.getProdUnitsHelper().getProdUnits(
228                     pBinding.getPClassMapping().getPMapper(), elem.pieGetRefElem());
229                 setData.add(pu);
230             } catch (Exception JavaDoc e) {
231                 throw new PException(e, "Pb to Add in Invoice/SetAccessor.");
232             }
233         }
234
235         public Object JavaDoc 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 JavaDoc paIterator() {
252             return new SetAccessorIterator(setData.iterator());
253         }
254
255         public void paSetNbElem(int nbelem) {
256         }
257
258         /**
259          * Provided to present elements of the set (ProdUnits) as PIndexedElem
260          * that manages a reference shown as a PName.
261          */

262         class SetIndexedElem implements PIndexedElem {
263             private final static String JavaDoc ERRNOINDEX = "Set generic class has no index for its elements.";
264             private final static String JavaDoc 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 JavaDoc 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 JavaDoc pieGetObyteElem() throws PException {
285                 throw new PException(ERRTYPEUNSUPP);
286             }
287
288             public byte pieGetByteIndexField(String JavaDoc fn) throws PException {
289                 throw new PException(ERRNOINDEX);
290             }
291
292             public Byte JavaDoc pieGetObyteIndexField(String JavaDoc 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 JavaDoc pieGetOcharElem() throws PException {
301                 throw new PException(ERRTYPEUNSUPP);
302             }
303
304             public char pieGetCharIndexField(String JavaDoc fn) throws PException {
305                 throw new PException(ERRNOINDEX);
306             }
307
308             public Character JavaDoc pieGetOcharIndexField(String JavaDoc 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 JavaDoc pieGetOshortElem() throws PException {
317                 throw new PException(ERRTYPEUNSUPP);
318             }
319
320             public short pieGetShortIndexField(String JavaDoc fn) throws PException {
321                 throw new PException(ERRNOINDEX);
322             }
323
324             public Short JavaDoc pieGetOshortIndexField(String JavaDoc 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 JavaDoc pieGetOintElem() throws PException {
333                 throw new PException(ERRTYPEUNSUPP);
334             }
335
336             public int pieGetIntIndexField(String JavaDoc fn) throws PException {
337                 throw new PException(ERRNOINDEX);
338             }
339
340             public Integer JavaDoc pieGetOintIndexField(String JavaDoc 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 JavaDoc pieGetOlongElem() throws PException {
349                 throw new PException(ERRTYPEUNSUPP);
350             }
351
352             public long pieGetLongIndexField(String JavaDoc fn) throws PException {
353                 throw new PException(ERRNOINDEX);
354             }
355
356             public Long JavaDoc pieGetOlongIndexField(String JavaDoc 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 JavaDoc 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 JavaDoc pieGetOdoubleElem() throws PException {
373                 throw new PException(ERRTYPEUNSUPP);
374             }
375
376             public String JavaDoc pieGetStringElem() throws PException {
377                 throw new PException(ERRTYPEUNSUPP);
378             }
379
380             public String JavaDoc pieGetStringIndexField(String JavaDoc fn) throws PException {
381                 throw new PException(ERRNOINDEX);
382             }
383
384             public BigDecimal JavaDoc pieGetBigDecimalElem() throws PException {
385                 throw new PException(ERRTYPEUNSUPP);
386             }
387
388             public BigInteger JavaDoc pieGetBigIntegerElem() throws PException {
389                 throw new PException(ERRTYPEUNSUPP);
390             }
391
392             public Date JavaDoc pieGetDateElem() throws PException {
393                 throw new PException(ERRTYPEUNSUPP);
394             }
395
396             public Date JavaDoc pieGetDateIndexField(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc value) throws PException {
429                 throw new PException(ERRTYPEUNSUPP);
430             }
431
432             public void pieSetByteIndexField(String JavaDoc fn, byte value) throws PException {
433                 throw new PException(ERRNOINDEX);
434             }
435
436             public void pieSetObyteIndexField(String JavaDoc fn, Byte JavaDoc 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 JavaDoc value) throws PException {
445                 throw new PException(ERRTYPEUNSUPP);
446             }
447
448             public void pieSetCharIndexField(String JavaDoc fn, char value) throws PException {
449                 throw new PException(ERRNOINDEX);
450             }
451
452             public void pieSetOcharIndexField(String JavaDoc fn, Character JavaDoc 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 JavaDoc value) throws PException {
461                 throw new PException(ERRTYPEUNSUPP);
462             }
463
464             public void pieSetShortIndexField(String JavaDoc fn, short value) throws PException {
465                 throw new PException(ERRNOINDEX);
466             }
467
468             public void pieSetOshortIndexField(String JavaDoc fn, Short JavaDoc 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 JavaDoc value) throws PException {
477                 throw new PException(ERRTYPEUNSUPP);
478             }
479
480             public void pieSetIntIndexField(String JavaDoc fn, int value) throws PException {
481                 throw new PException(ERRNOINDEX);
482             }
483
484             public void pieSetOintIndexField(String JavaDoc fn, Integer JavaDoc 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 JavaDoc value) throws PException {
493                 throw new PException(ERRTYPEUNSUPP);
494             }
495
496             public void pieSetLongIndexField(String JavaDoc fn, long value) throws PException {
497                 throw new PException(ERRNOINDEX);
498             }
499
500             public void pieSetOlongIndexField(String JavaDoc fn, Long JavaDoc 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 JavaDoc 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 JavaDoc value) throws PException {
517                 throw new PException(ERRTYPEUNSUPP);
518             }
519
520             public void pieSetStringElem(String JavaDoc value) throws PException {
521                 throw new PException(ERRTYPEUNSUPP);
522             }
523
524             public void pieSetStringIndexField(String JavaDoc fn, String JavaDoc value) throws PException {
525                 throw new PException(ERRNOINDEX);
526             }
527
528             public void pieSetBigDecimalElem(BigDecimal JavaDoc value) throws PException {
529                 throw new PException(ERRTYPEUNSUPP);
530             }
531
532             public void pieSetBigIntegerElem(BigInteger JavaDoc value) throws PException {
533                 throw new PException(ERRTYPEUNSUPP);
534             }
535
536             public void pieSetDateElem(Date JavaDoc value) throws PException {
537                 throw new PException(ERRTYPEUNSUPP);
538             }
539
540             public void pieSetDateIndexField(String JavaDoc fn, Date JavaDoc 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 JavaDoc 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         /**
562          * Provided to iterate over referenced ProdUnits shown as SetIndexedElem.
563          */

564         class SetAccessorIterator implements Iterator JavaDoc {
565             Iterator JavaDoc pusetIt;
566             SetIndexedElem ie;
567
568             SetAccessorIterator(Iterator JavaDoc it) {
569                 pusetIt = it;
570                 ie = new SetIndexedElem();
571             }
572
573             public boolean hasNext() {
574                 return pusetIt.hasNext();
575             }
576
577             public Object JavaDoc 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