KickJava   Java API By Example, From Geeks To Geeks.

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


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

44 public class ProdUnitsHelper implements MapHelper {
45     public final static String JavaDoc CN = "examples.invoice.persistclass.ProdUnits";
46     private LoggerFactory loggerFactory;
47     private HashMap JavaDoc pobjMap = new HashMap JavaDoc();
48     private ProductHelper productHelper;
49
50     public ProdUnitsHelper(ProductHelper ph) {
51         productHelper = ph;
52     }
53
54     /**
55      * Prepares the mapping of the ProdUnits persistent class for the
56      * ProdUnitsAppli or other applications.
57      */

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

112     public ProdUnits getProdUnits(PMapper m, PName pn) throws Exception JavaDoc {
113         ProdUnits res;
114         if (pn == null) {
115             System.out.println("WARNING: PName is null in getProdUnits within ProdUnitsHelper!!");
116             return null;
117         }
118         PClassMapping pcm = m.lookup(CN);
119         PBinding pb = pcm.getPBinder().lookup(pn);
120         if (pb == null) {
121             Object JavaDoc conn = m.getConnection();
122             try {
123                 pb = pcm.createPBinding();
124                 pb.bind(pn);
125                 res = new ProdUnits(this, conn, pb);
126                 m.closeConnection(conn);
127                 pobjMap.put(pb, res);
128             } catch (PException pe) {
129                 m.closeConnection(conn);
130                 throw pe;
131             }
132         } else {
133             res = (ProdUnits) pobjMap.get(pb);
134         }
135         return res;
136     }
137
138     /**
139      * Creates a new persistent ProdUnits.
140      */

141     public ProdUnits createProdUnits(PMapper m, String JavaDoc pr, int quant) throws PException {
142         ProdUnits res;
143         PBinding pb = m.lookup(CN).createPBinding();
144         Object JavaDoc conn = m.getConnection();
145         try {
146             PName pn = m.lookup(ProductHelper.CN).getPBinder().decodeString(pr);
147             res = new ProdUnits(this, conn, pb, pn, quant);
148             m.closeConnection(conn);
149             pobjMap.put(pb, res);
150         } catch (PException pe) {
151             m.closeConnection(conn);
152             throw pe;
153         }
154         return res;
155     }
156
157     /**
158      * Deletes a persistent ProdUnits.
159      */

160     public boolean deleteProdUnits(PMapper m, long id) throws Exception JavaDoc {
161         PName pn = m.lookup(CN).getPBinder().decodeLong(id);
162         ProdUnits ad = getProdUnits(m, pn);
163         if (ad == null)
164             return false;
165         Object JavaDoc conn = m.getConnection();
166         try {
167             PBinding pb = ad.getPBinding();
168             ad.delete(conn);
169             m.closeConnection(conn);
170             pobjMap.remove(pb);
171         } catch (PException pe) {
172             m.closeConnection(conn);
173             throw pe;
174         }
175         return true;
176     }
177
178     /**
179      * Lists all existing ProdUnitses.
180      */

181     public Iterator JavaDoc listProdUnits(PMapper m) throws Exception JavaDoc {
182         Iterator JavaDoc res;
183         Object JavaDoc conn = m.getConnection();
184         try {
185             res = m.lookup(CN).getPNameIterator(conn);
186             m.closeConnection(conn);
187         } catch (PException pe) {
188             m.closeConnection(conn);
189             throw pe;
190         }
191         return res;
192     }
193 }
194
Popular Tags