KickJava   Java API By Example, From Geeks To Geeks.

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


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.naming.api.PBinder;
27 import org.objectweb.jorm.naming.api.PName;
28 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrRegistry;
29 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgr;
30 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrImpl;
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.PMapCluster;
35 import org.objectweb.jorm.api.PBinding;
36 import org.objectweb.util.monolog.api.Logger;
37 import org.objectweb.util.monolog.api.LoggerFactory;
38
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 import examples.invoice.applications.JormInit;
43
44 /**
45  * @author P. Dechamboux
46  */

47 public class ProductHelper implements MapHelper {
48     public final static String JavaDoc CN = "examples.invoice.persistclass.Product";
49     private LoggerFactory loggerFactory;
50     private HashMap JavaDoc pobjMap = new HashMap JavaDoc();
51
52     /**
53      * Prepares the mapping of the Address persistent class for the
54      * AddressAppli or other applications.
55      */

56     public void map(PMapper m, LoggerFactory lf, byte clact)
57         throws PException {
58         loggerFactory = lf;
59         Logger logger = loggerFactory.getLogger(CN);
60         // LongGenIncr initialization
61
PClassMapping pcm = m.lookup(CN);
62         // Perform mapping only if it has not been already done
63
if (pcm == null) {
64             PolymorphIdMgr pim = PolymorphIdMgrRegistry.getPolymorphIdMgr(m);
65             if (pim == null) {
66                 pim = new PolymorphIdMgrImpl();
67                 pim.init(m, clact);
68                 PolymorphIdMgrRegistry.registerPolymorphIdMgr(pim);
69             }
70             PBinder binder = pim.getPBinder(CN);
71             try {
72                 pcm = (PClassMapping) Class.forName(m.cn2mn(CN) + PMapper.PCLASSMAPPINGAPPENDER).newInstance();
73             } catch (Exception JavaDoc e) {
74                 throw new PException("Cannot create PClassMapping (probably a ClassLoader problem).");
75             }
76             pcm.setPBinder(binder);
77             binder.setPClassMapping(pcm);
78             binder.setCacheManager(JormInit.getInstance().getCacheManager());
79             m.map(pcm);
80         }
81         PMapCluster cl = m.getPMapCluster(CN);
82         switch (clact) {
83         case PClassMapping.CREATE_STRUCTURE_IF_NEEDED:
84             cl.createMappingStructures(false);
85             break;
86         case PClassMapping.CLEANUP_REMOVEALL:
87             cl.deleteMappingStructures();
88             cl.createMappingStructures(false);
89             break;
90         case PClassMapping.CLEANUP_REMOVEDATA:
91             cl.deleteData();
92             break;
93         case PClassMapping.CLEANUP_DONOTHING:
94             break;
95         }
96     }
97
98     /**
99      * Tries to retrieve an Address knowing its name.
100      */

101     public Product getProduct(PMapper m, PName pn) throws PException {
102         Product res;
103         if (pn == null)
104             return null;
105         PClassMapping pcm = m.lookup(CN);
106         PBinding pb = pcm.getPBinder().lookup(pn);
107         if (pb == null) {
108             Object JavaDoc conn = m.getConnection();
109             try {
110                 pb = pcm.createPBinding();
111                 pb.bind(pn);
112                 res = new Product(conn, pb);
113                 pobjMap.put(pb, res);
114                 m.closeConnection(conn);
115             } catch (PException pe) {
116                 m.closeConnection(conn);
117                 throw pe;
118             }
119         } else {
120             res = (Product) pobjMap.get(pb);
121         }
122         return res;
123     }
124
125     /**
126      * Creates a new persistent Product.
127      */

128     public Product createProduct(PMapper m, String JavaDoc n, char s, String JavaDoc c, float w, float p) throws PException {
129         Product res;
130         PBinding pb = m.lookup(CN).createPBinding();
131         Object JavaDoc conn = m.getConnection();
132         try {
133             res = new Product(conn, pb, n, s, c, w, p);
134             m.closeConnection(conn);
135             pobjMap.put(pb, res);
136         } catch (PException pe) {
137             pe.getNestedException().printStackTrace();
138             m.closeConnection(conn);
139             throw pe;
140         }
141         return res;
142     }
143
144     /**
145      * Deletes a persistent Product.
146      */

147     public boolean deleteProduct(PMapper m, String JavaDoc id) throws PException {
148         PName pn = m.lookup(CN).getPBinder().decodeString(id);
149         Product p = getProduct(m, pn);
150         if (p == null)
151             return false;
152         Object JavaDoc conn = m.getConnection();
153         try {
154             PBinding pb = p.getPBinding();
155             p.delete(conn);
156             m.closeConnection(conn);
157             pobjMap.remove(pb);
158         } catch (PException pe) {
159             m.closeConnection(conn);
160             throw pe;
161         }
162         return true;
163     }
164
165     /**
166      * Lists all existing Products.
167      */

168     public Iterator JavaDoc listProducts(PMapper m) throws PException {
169         Iterator JavaDoc res;
170         Object JavaDoc conn = m.getConnection();
171         try {
172             res = m.lookup(CN).getPNameIterator(conn);
173             m.closeConnection(conn);
174         } catch (PException pe) {
175             m.closeConnection(conn);
176             throw pe;
177         }
178         return res;
179     }
180 }
181
Popular Tags