KickJava   Java API By Example, From Geeks To Geeks.

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


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.PBinding;
28 import org.objectweb.jorm.api.PException;
29 import org.objectweb.jorm.naming.api.PName;
30 import org.objectweb.jorm.naming.api.PNamingContext;
31
32 /**
33  * @author P. Dechamboux
34  */

35 public class ProdUnits implements PAccessor, ProdUnitsAccessor {
36     private PName productPn;
37     private Product product;
38     private int quantity;
39     private PBinding pBinding;
40     private ProdUnitsHelper prodUnitsHelper;
41     private PNamingContext productNC;
42
43     public PBinding getPBinding() {
44         return pBinding;
45     }
46
47     /**
48      * Implements lazy dereferencing of address field.
49      */

50     private Product getProduct() throws PException {
51         try {
52             if (product == null)
53                 product = prodUnitsHelper.getProductHelper().getProduct(
54                     pBinding.getPClassMapping().getPMapper(), productPn);
55         } catch (Exception JavaDoc e) {
56             e.printStackTrace();
57             throw new PException(e, "Cannot getProduct within ProdUnits!");
58         }
59         return product;
60     }
61
62     /**
63      * Creates a new Address.
64      */

65     public ProdUnits(ProdUnitsHelper puh, Object JavaDoc conn, PBinding pb, PName pn, int quant) throws PException {
66         prodUnitsHelper = puh;
67         pBinding = pb;
68         productNC = (PNamingContext) pBinding.getPClassMapping().getPNameCoder("product");
69         productPn = pn;
70         quantity = quant;
71         pBinding.export(conn);
72         pBinding.write(conn, this);
73     }
74
75     /**
76      * Creates an Address that is already persistent.
77      */

78     public ProdUnits(ProdUnitsHelper puh, Object JavaDoc conn, PBinding pb) throws PException {
79         prodUnitsHelper = puh;
80         pBinding = pb;
81         productNC = (PNamingContext) pBinding.getPClassMapping().getPNameCoder("product");
82         pb.read(conn, this);
83     }
84
85     /**
86      * Turns this persistent object into a non-persistent one.
87      */

88     public void delete(Object JavaDoc conn) throws PException {
89         pBinding.unexport(conn);
90         pBinding.write(conn, this);
91         pBinding = null;
92     }
93
94     /**
95      * Yields a printable version a the full ProdUnits.
96      */

97     public String JavaDoc toPrintable() throws PException {
98         return "ID: [" + pBinding.getPName().encodeString() + "] - VALUE: [product: [" + getProduct().toPrintable() + "], quantity: " + quantity + "]";
99     }
100
101     // IMPLEMENTATION OF PAccessor INTERFACE
102

103     public Object JavaDoc getMemoryInstance() {
104         return this;
105     }
106
107     // IMPLEMENTATION OF AddressAccessor INTERFACE (generated by JORM)
108

109     // Accessors to the product field
110
public void paSetProduct(PName val, Object JavaDoc conn) throws PException {
111         productPn = productNC.resolve(conn, val);
112     }
113
114     public PName paGetProduct(Object JavaDoc conn) throws PException {
115         return productNC.export(conn, productPn);
116     }
117
118     // Accessors to the quantity field
119
public void paSetQuantity(int val) throws PException {
120         quantity = val;
121     }
122
123     public int paGetQuantity() throws PException {
124         return quantity;
125     }
126 }
127
Popular Tags