KickJava   Java API By Example, From Geeks To Geeks.

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


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.facility.naming.basidir.BasidBinder;
27 import org.objectweb.jorm.naming.api.PBinder;
28 import org.objectweb.jorm.naming.api.PName;
29 import org.objectweb.jorm.naming.api.PNamingContext;
30 import org.objectweb.jorm.util.api.Loggable;
31 import org.objectweb.jorm.api.PMapper;
32 import org.objectweb.jorm.api.PException;
33 import org.objectweb.jorm.api.PClassMapping;
34 import org.objectweb.jorm.api.PClassMappingCtrl;
35 import org.objectweb.jorm.api.PMapCluster;
36 import org.objectweb.jorm.api.PBinding;
37 import org.objectweb.util.monolog.api.Logger;
38 import org.objectweb.util.monolog.api.LoggerFactory;
39
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 import examples.invoice.applications.JormInit;
44
45 /**
46  * @author P. Dechamboux
47  */

48 public class InvoiceHelper implements MapHelper {
49     public final static String JavaDoc CN = "examples.invoice.persistclass.Invoice";
50     private LoggerFactory loggerFactory;
51     private HashMap JavaDoc pobjMap = new HashMap JavaDoc();
52     private AddressHelper addressHelper;
53     private ProductHelper productHelper;
54     private ProdUnitsHelper prodUnitsHelper;
55
56     public InvoiceHelper(AddressHelper ah, ProductHelper ph, ProdUnitsHelper puh) {
57         addressHelper = ah;
58         productHelper = ph;
59         prodUnitsHelper = puh;
60     }
61
62     /**
63      * Prepares the mapping of the Address persistent class for the
64      * AddressAppli or other applications.
65      */

66     public void map(PMapper m, LoggerFactory lf, byte clact)
67             throws PException {
68         addressHelper.map(m, lf, clact);
69         productHelper.map(m, lf, clact);
70         prodUnitsHelper.map(m, lf, clact);
71         loggerFactory = lf;
72         Logger logger = loggerFactory.getLogger(CN);
73         PClassMapping pcm = m.lookup(CN);
74         // Perform mapping only if it has not been already done
75
if (pcm == null) {
76             // puset Set mapping definition
77
PBinder sbinder = new BasidBinder(PNamingContext.CTINT);
78             PClassMapping sm;
79             sm = m.createGenClassMapping();
80             ((Loggable) sm).setLoggerFactory(loggerFactory);
81             sm.setPBinder(sbinder);
82             sbinder.setPClassMapping(sm);
83             sbinder.setCacheManager(JormInit.getInstance().getCacheManager());
84             ((PClassMappingCtrl) sm).setPNameCoder(m.lookup(prodUnitsHelper.CN).getPBinder());
85             // Invoice mapping definition
86
PBinder binder = new BasidBinder(PNamingContext.CTINT);
87             try {
88                 pcm = (PClassMapping) Class.forName(m.cn2mn(CN) +
89                                                     PMapper.PCLASSMAPPINGAPPENDER)
90                         .newInstance();
91             } catch (Exception JavaDoc e) {
92                 throw new PException("Cannot create PClassMapping (probably a ClassLoader problem).");
93             }
94             pcm.setPBinder(binder);
95             binder.setPClassMapping(pcm);
96             binder.setCacheManager(JormInit.getInstance().getCacheManager());
97             ((PClassMappingCtrl) pcm).setPNameCoder("address", m.lookup(addressHelper.CN).getPBinder());
98             ((PClassMappingCtrl) pcm).setGenClassMapping("puset", sm);
99             ((PClassMappingCtrl) pcm).setPNameCoder("puset", sm.getPBinder());
100             m.map(pcm);
101             PMapCluster cl = m.getPMapCluster(CN);
102             switch (clact) {
103             case PClassMapping.CREATE_STRUCTURE_IF_NEEDED:
104                 cl.createMappingStructures(false);
105                 break;
106             case PClassMapping.CLEANUP_REMOVEALL:
107                 cl.deleteMappingStructures();
108                 cl.createMappingStructures(false);
109                 break;
110             case PClassMapping.CLEANUP_REMOVEDATA:
111                 cl.deleteData();
112                 break;
113             case PClassMapping.CLEANUP_DONOTHING:
114                 break;
115             }
116         }
117     }
118
119     public AddressHelper getAddressHelper() {
120         if (addressHelper == null)
121             System.out.println("WARNING: addressHelper is NULL in invoiceHelper!!");
122         return addressHelper;
123     }
124
125     public ProdUnitsHelper getProdUnitsHelper() {
126         if (prodUnitsHelper == null)
127             System.out.println("WARNING: prodUnitsHelper is NULL in invoiceHelper!!");
128         return prodUnitsHelper;
129     }
130
131     /**
132      * Tries to retrieve an Address knowing its name.
133      */

134     public Invoice getInvoice(PMapper m, PName pn) throws PException {
135         Invoice res;
136         if (pn == null)
137             return null;
138         PClassMapping pcm = m.lookup(CN);
139         PBinding pb = pcm.getPBinder().lookup(pn);
140         if (pb == null) {
141             Object JavaDoc conn = m.getConnection();
142             try {
143                 pb = pcm.createPBinding();
144                 pb.bind(pn);
145                 res = new Invoice(this, conn, pb);
146                 pobjMap.put(pb, res);
147                 m.closeConnection(conn);
148             } catch (PException pe) {
149                 m.closeConnection(conn);
150                 throw pe;
151             }
152         } else {
153             res = (Invoice) pobjMap.get(pb);
154         }
155         return res;
156     }
157
158     /**
159      * Creates a new persistent Invoice.
160      */

161     public Invoice createInvoice(PMapper m, int n, String JavaDoc ar, String JavaDoc[] purs)
162             throws PException {
163         Invoice res;
164         PBinding pb = m.lookup(CN).createPBinding();
165         Object JavaDoc conn = m.getConnection();
166         try {
167             PName pn = m.lookup(CN).getPNameCoder("address").decodeString(ar);
168             PName[] pus = new PName[purs.length];
169             for (int i = 0; i < purs.length; i++)
170                 pus[i] = m.lookup(CN).getGenClassMapping("puset").getPNameCoder().decodeString(purs[i]);
171             res = new Invoice(this, conn, pb, n, pn, pus);
172             m.closeConnection(conn);
173             pobjMap.put(pb, res);
174         } catch (PException pe) {
175             m.closeConnection(conn);
176             throw pe;
177         }
178         return res;
179     }
180
181     /**
182      * Deletes a persistent Invoice.
183      */

184     public boolean deleteInvoice(PMapper m, String JavaDoc id) throws Exception JavaDoc {
185         PName pn = m.lookup(CN).getPBinder().decodeString(id);
186         Invoice p = getInvoice(m, pn);
187         if (p == null)
188             return false;
189         Object JavaDoc conn = m.getConnection();
190         try {
191             PBinding pb = p.getPBinding();
192             p.delete(conn);
193             m.closeConnection(conn);
194             pobjMap.remove(pb);
195         } catch (PException pe) {
196             m.closeConnection(conn);
197             throw pe;
198         }
199         return true;
200     }
201
202     /**
203      * Lists all existing Invoices.
204      */

205     public Iterator JavaDoc listInvoices(PMapper m) throws Exception JavaDoc {
206         Iterator JavaDoc res;
207         Object JavaDoc conn = m.getConnection();
208         try {
209             res = m.lookup(CN).getPNameIterator(conn);
210             m.closeConnection(conn);
211         } catch (PException pe) {
212             m.closeConnection(conn);
213             throw pe;
214         }
215         return res;
216     }
217
218     /**
219      * This methods returns the word specified in parameter with the first
220      * character in upper-case. This method is used for the generation of
221      * accessor methods.
222      * @param word The String which the first character must be set in
223      * upper-case
224      * @return The String whith the first character in upper-case
225      */

226     private String JavaDoc upperFL(String JavaDoc word) {
227         if (word != null && word.length() > 0)
228             return word.substring(0, 1).toUpperCase() + word.substring(1);
229         else
230             return word;
231     }
232 }
233
Popular Tags