KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > lib > RdbPMappingStructuresManager


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 org.objectweb.jorm.mapper.rdb.lib;
25
26 import org.objectweb.jorm.api.PExceptionIO;
27 import org.objectweb.jorm.api.PMappingStructuresManager;
28 import org.objectweb.jorm.api.PMapper;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.api.PExceptionProtocol;
31 import org.objectweb.jorm.api.PMapCluster;
32 import org.objectweb.util.monolog.api.Logger;
33
34 import java.io.FileWriter JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Collection JavaDoc;
39
40 /**
41  * Defines the way to manage relational schema for the RDB mappers.
42  * @author P. Dechamboux
43  */

44 public class RdbPMappingStructuresManager implements PMappingStructuresManager {
45     private PMapper pMapper = null;
46     private ArrayList JavaDoc clusterList = new ArrayList JavaDoc();
47     protected Logger logger = null;
48     
49     /**
50      * Closes a mapper connection.
51      * @param conn The mapper connection.
52      * @throws PException
53      */

54     void closeConnection(Object JavaDoc conn) throws PException {
55         pMapper.closeConnection(conn);
56     }
57
58     /**
59      * Gets a connection from the mapper.
60      * @return The allocated connection.
61      * @throws PException
62      */

63     Object JavaDoc getConnection() throws PException {
64         Object JavaDoc conn = pMapper.getConnection();
65         if (conn == null)
66             throw new PExceptionProtocol("Impossible to initialize the mapping structures without connection");
67         return conn;
68     }
69
70     /**
71      * Retrieves or creates an RdbPMapCluster associated with this JORM class.
72      * @param jcname The name of this JORM class.
73      * @param tname A table name within which the class is mapped.
74      * @return The associated RdbPMapCluster.
75      */

76     private RdbPMapCluster getAlwaysPMapCluster(String JavaDoc jcname,
77                                                 String JavaDoc tname)
78             throws PException {
79         RdbPMapCluster cl = null;
80         Iterator JavaDoc it = clusterList.iterator();
81         while (it.hasNext()) {
82             cl = (RdbPMapCluster) it.next();
83             if (cl.containTable(tname)) {
84                 break;
85             }
86             cl = null;
87         }
88         if (cl == null) {
89             // no map cluster use tname
90
cl = (RdbPMapCluster) getPMapCluster(jcname);
91             if (cl == null) {
92                 // no map cluster for jcname : creates a new one
93
cl = new RdbPMapCluster(jcname, tname, this);
94                 clusterList.add(cl);
95             }
96             // there is already a map cluster for jcname
97
} else {
98             // there is already a map cluster for tname
99
RdbPMapCluster cl2 = (RdbPMapCluster) getPMapCluster(jcname);
100             if ((cl != cl2) && (cl2 != null)) {
101                 // there is also a map cluster for jcname : merge both into cl
102
cl.merge(cl2);
103                 clusterList.remove(cl2);
104             } else {
105                 // no map cluster for jcname : declare jcname into cl
106
cl.classDefined(jcname);
107             }
108         }
109         return cl;
110     }
111
112     /**
113      * Adds a table column into these RDB mapping structures.
114      * @param jcname The JORM class name.
115      * @param tname The table name.
116      * @param cname The column name.
117      * @param notnull true if column value should never be null.
118      * @throws PException
119      */

120     synchronized public void addTableColumn(String JavaDoc jcname,
121                                             String JavaDoc tname,
122                                             String JavaDoc cname,
123                                             String JavaDoc type,
124                                             boolean notnull,
125                                             boolean ispkcol,
126                                             boolean isMaster) throws PException {
127         RdbPMapCluster cl = getAlwaysPMapCluster(jcname, tname);
128         cl.addTableColumn(tname, cname, type, notnull, ispkcol, isMaster);
129     }
130
131
132     public synchronized void declareClass(String JavaDoc jcname) {
133         Iterator JavaDoc it = clusterList.iterator();
134         RdbPMapCluster cl;
135         while (it.hasNext()) {
136             cl = (RdbPMapCluster) it.next();
137             if (cl.containClass(jcname)) {
138                 return;
139             }
140         }
141         cl = new RdbPMapCluster(jcname, this);
142         clusterList.add(cl);
143     }
144
145     public PMapper getPMapper() {
146         return pMapper;
147     }
148
149     // IMPLEMENTATION OF METHODS FROM THE PMappingStructuresManager INTERFACE
150

151     /**
152      * Asks for the map cluster associated with a JORM class.
153      * @return The relevant map cluster.
154      * @throws PException This class has not been mapped.
155      */

156     synchronized public PMapCluster getPMapCluster(String JavaDoc jcname) throws PException {
157         Iterator JavaDoc it = clusterList.iterator();
158         while (it.hasNext()) {
159             RdbPMapCluster cl = (RdbPMapCluster) it.next();
160             if (cl.containClass(jcname)
161                     || cl.getUnResolvedDependencies().contains(jcname)) {
162                 return cl;
163             }
164         }
165         return null;
166     }
167
168     /**
169      * Asks for all the map clusters defined within this mapper. The returned
170      * Collection is not thread safe.
171      * @return The Iterator over the collection of map clusters.
172      */

173     public Collection JavaDoc getPMapClusters() {
174         return clusterList;
175     }
176
177     synchronized public void addDependency(String JavaDoc jcname1, String JavaDoc jcname2)
178             throws PException {
179         RdbPMapCluster cl1 = (RdbPMapCluster) getPMapCluster(jcname1);
180         if (cl1 == null) {
181             throw new PException("You must define the class " + jcname1
182                                  + " before adding its dependcies (no cluster found).");
183         }
184         RdbPMapCluster cl2 = (RdbPMapCluster) getPMapCluster(jcname2);
185         if (cl2 == null) {
186             cl1.addDependency(jcname2);
187         } else if (cl2 != cl1) {
188             cl1.merge(cl2);
189             clusterList.remove(cl2);
190         }
191     }
192
193     public void classDefined(String JavaDoc jcname) throws PException {
194         RdbPMapCluster cl = (RdbPMapCluster) getPMapCluster(jcname);
195         if (cl == null) {
196             throw new PException("No cluster found for the class " + jcname);
197         }
198         cl.classDefined(jcname);
199     }
200
201     /**
202      * Assigns a mapper to this schema manager.
203      * @param pm The concerned mapper.
204      */

205     public void setPMapper(PMapper pm) {
206         pMapper = pm;
207     }
208
209     /**
210      * Assigns a logger to this schema manager.
211      * @param l The concerned logger.
212      */

213     public void setLogger(Logger l) {
214         logger = l;
215     }
216 }
217
Popular Tags