KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.PMapper;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.api.PClassMapping;
31 import org.objectweb.jorm.api.PMapCluster;
32 import org.objectweb.jorm.api.PBinding;
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 AddressHelper implements MapHelper {
45     public final static String JavaDoc CN = "examples.invoice.persistclass.Address";
46     private LoggerFactory loggerFactory;
47     private HashMap JavaDoc pobjMap = new HashMap JavaDoc();
48
49     /**
50      * Prepares the mapping of the Address persistent class for the
51      * AddressAppli or other applications.
52      */

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

93     public Address getAddress(PMapper m, PName pn) throws PException {
94         Address res;
95         if (pn == null) {
96             System.out.println("WARNING: PName is null in getAddress within AddressHelper!!");
97             return null;
98         }
99         PClassMapping pcm = m.lookup(CN);
100         PBinding pb = pcm.getPBinder().lookup(pn);
101         if (pb == null) {
102             Object JavaDoc conn = m.getConnection();
103             try {
104                 pb = pcm.createPBinding();
105                 pb.bind(pn);
106                 res = new Address(conn, pb);
107                 pobjMap.put(pb, res);
108                 m.closeConnection(conn);
109             } catch (PException pe) {
110                 m.closeConnection(conn);
111                 throw pe;
112             }
113         } else {
114             res = (Address) pobjMap.get(pb);
115         }
116         return res;
117     }
118
119     /**
120      * Creates a new persistent Address.
121      */

122     public Address createAddress(PMapper m, String JavaDoc n, String JavaDoc s, String JavaDoc z,
123                                  String JavaDoc t, String JavaDoc c) throws PException {
124         Address res;
125         PBinding pb = m.lookup(CN).createPBinding();
126         Object JavaDoc conn = m.getConnection();
127         try {
128             res = new Address(conn, pb, n, s, z, t, c);
129             m.closeConnection(conn);
130             pobjMap.put(pb, res);
131         } catch (PException pe) {
132             m.closeConnection(conn);
133             throw pe;
134         }
135         return res;
136     }
137
138     /**
139      * Deletes a persistent Address.
140      */

141     public boolean deleteAddress(PMapper m, String JavaDoc id) throws PException {
142         PName pn = m.lookup(CN).getPBinder().decodeString(id);
143         Address ad = getAddress(m, pn);
144         if (ad == null)
145             return false;
146         Object JavaDoc conn = m.getConnection();
147         try {
148             PBinding pb = ad.getPBinding();
149             ad.delete(conn);
150             m.closeConnection(conn);
151             pobjMap.remove(pb);
152         } catch (PException pe) {
153             m.closeConnection(conn);
154             throw pe;
155         }
156         return true;
157     }
158
159     /**
160      * Lists all existing Addresses.
161      */

162     public Iterator JavaDoc listAddress(PMapper m) throws Exception JavaDoc {
163         Iterator JavaDoc res;
164         Object JavaDoc conn = m.getConnection();
165         try {
166             res = m.lookup(CN).getPNameIterator(conn);
167             m.closeConnection(conn);
168         } catch (PException pe) {
169             m.closeConnection(conn);
170             throw pe;
171         }
172         return res;
173     }
174 }
175
Popular Tags