KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > generator > RdbGenTable


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.generator;
25
26 import org.objectweb.jorm.type.api.PTypeSpace;
27 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
28 import org.objectweb.jorm.metainfo.api.ScalarField;
29 import org.objectweb.jorm.metainfo.api.Class;
30 import org.objectweb.jorm.api.PException;
31 import org.objectweb.jorm.mapper.rdb.metainfo.*;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * Defines one of the table that is used to mapped a particular JORM class.
41  * In case of the main table (see RdbGenInfos), no join information is
42  * defined.
43  * @author P. Dechamboux
44  */

45 public class RdbGenTable {
46     public RdbGenInfos genInfos = null;
47     /**
48      * The name of the table involved in this mapping.
49      */

50     public String JavaDoc tableName = null;
51     /**
52      * The list of columns of this table used in this mapping.
53      */

54     public List JavaDoc columns = new ArrayList JavaDoc();
55
56     /**
57      * The list of inherited columns of this table used in this mapping.
58      */

59     public List JavaDoc inheritedColumns = new ArrayList JavaDoc();
60
61     /**
62      * The filter.
63      */

64     // public RdbFilter filter = null;
65

66     /**
67      * The list of columns of this table that are shared with a colocated
68      * object (i.e., an object stored into the same table along with this
69      * object.
70      */

71     public List JavaDoc colocatedColumns = null;
72     /**
73      * Tells that this table stores colocated objects.
74      */

75     public boolean colocatedTable = false;
76
77     public List JavaDoc joins = null;
78     public Map JavaDoc jn2join = null;
79
80     /**
81      * indicates if the table is a read-only table
82      */

83     public boolean readOnly = false;
84
85     /**
86      * indicates if code must be generated to update the table
87      * when updating an instance
88      */

89     public boolean mustGenerateUpdate = true;
90
91     /**
92      * indicates if the table is the main table of a the class
93      */

94     public boolean isMainTable;
95
96     public RdbGenTable() {
97     }
98
99     /**
100      * Creates an RdbGenTable from an external table.
101      * @param extTable the external table
102      * @param mainTable the associated main table
103      * @param clazz the associated table
104      * @throws PException
105      */

106     public RdbGenTable(RdbExternalTable extTable,
107                        RdbGenTable mainTable,
108                        Class JavaDoc clazz,
109                        RdbGenInfos genInfos) throws PException {
110
111         constructFromTable(extTable, clazz, genInfos);
112         isMainTable = false;
113         readOnly = extTable.isReadOnly();
114         joins = new ArrayList JavaDoc();
115         jn2join = new HashMap JavaDoc();
116         int joinIdx = 0;
117         for (Iterator JavaDoc itj = extTable.getRdbJoins().iterator(); itj.hasNext();) {
118             RdbJoin j = (RdbJoin) itj.next();
119             RdbGenJoin rgj = new RdbGenJoin(mainTable, this, j, joinIdx);
120             joinIdx++;
121             jn2join.put(j.getName(), rgj);
122             joins.add(rgj);
123         }
124         if ( extTable.getPrimitiveElementMappings() != null) {
125             for (Iterator JavaDoc it = extTable.getPrimitiveElementMappings().iterator(); it.hasNext();) {
126                 RdbPrimitiveElementMapping rpem = (RdbPrimitiveElementMapping) it.next();
127                 Iterator JavaDoc entryIt = rpem.getPrimitiveElementByRdbJoin().entrySet().iterator();
128                 if (!entryIt.hasNext()) {
129                     continue;
130                 }
131                 if (rpem.getName() == null) {
132                     throw new PException("Primitive element "
133                                         + ((PrimitiveElement) rpem.getLinkedMO()).getName()
134                                         + " has a null column name");
135                 }
136                 RdbGenColumn rgc = getColumn(rpem.getName());
137                 rgc.pes = new ArrayList JavaDoc();
138                 rgc.joins = new ArrayList JavaDoc();
139                 PrimitiveElement pe = null;
140                 while (entryIt.hasNext()) {
141                     Map.Entry JavaDoc me = (Map.Entry JavaDoc) entryIt.next();
142                     RdbJoin j = (RdbJoin) me.getKey();
143                     pe = (PrimitiveElement) me.getValue();
144                     rgc.pes.add(pe);
145                     rgc.joins.add(jn2join.get(j.getName()));
146                 }
147                 rgc.hiddenField = pe instanceof ScalarField;
148                 rgc.columnType = pe.getType();
149             }
150         }
151     }
152
153     /**
154      * Creates an RdbGenTable from a main table.
155      * @param mainTable the associated main table
156      * @param clazz the associated table
157      * @throws PException
158      */

159     public RdbGenTable(RdbTable mainTable, Class JavaDoc clazz, RdbGenInfos genInfos) throws PException {
160         constructFromTable(mainTable, clazz, genInfos);
161         isMainTable = true;
162     }
163
164
165     private void constructFromTable(RdbTable table, Class JavaDoc clazz,
166                                     RdbGenInfos genInfos) throws PException {
167         this.genInfos = genInfos;
168         tableName = table.getName();
169         colocatedTable = table.isColocated() && !table.isColocatedMaster();
170
171         for (Iterator JavaDoc it = table.getPrimitiveElementMappings().iterator(); it.hasNext();) {
172             RdbPrimitiveElementMapping rpem = (RdbPrimitiveElementMapping) it.next();
173             RdbGenColumn rgc = new RdbGenColumn();
174             rgc.columnName = rpem.getName();
175             if (rgc.columnName == null) {
176                 throw new PException("Primitive element "
177                                      + ((PrimitiveElement) rpem.getLinkedMO()).getName()
178                                      + " has a null column name");
179             }
180             rgc.columnNotNull = rpem.isNotNull();
181             rgc.table = this;
182             PrimitiveElement pe = (PrimitiveElement) rpem.getLinkedMO();
183             if (pe != null) {
184                 rgc.columnType = pe.getType();
185                 rgc.columnSize = pe.getSize();
186                 rgc.columnScale = pe.getScale();
187                 rgc.hiddenField = pe instanceof ScalarField;
188                 if (pe.isConstant()) {
189                     //fetch the constant value on the current class
190
rgc.constant = clazz.getConstantValue(pe.getName());
191                 }
192                 rgc.fieldName = pe.getName();
193             }
194             rgc.columnSqlType = rpem.getType();
195             if (rgc.columnSqlType != null && rgc.columnSqlType.length() == 0) {
196                 rgc.columnSqlType = null;
197             }
198             columns.add(rgc);
199         }
200
201     }
202     /**
203      * Fix the mustGenerateUpdate attribute.
204      * One must generate code to update the table when an instance of the jorm
205      * class (to which the genTable is associated) is updated.
206      * The table is concerned only if there are columns which do not participate
207      * in the join with the main table nor in the identifier
208      */

209     public void fixMustGenerateUpdate() {
210         mustGenerateUpdate = false;
211         // loop on columns
212
for (int i = 0; i < columns.size() && !mustGenerateUpdate; i++) {
213             RdbGenColumn rgc = (RdbGenColumn) columns.get(i);
214
215             // column must not participate in the join with the main table
216
if (rgc.joinCol != null) {
217                 continue;
218             }
219
220             // column must not participate in the identifier
221
if (joins == null && genInfos.colInGenId(rgc)) {
222                 continue;
223             }
224             // otherwise we must generate some code that update the table
225
mustGenerateUpdate = true;
226         }
227     }
228
229     public boolean getMustGenerateUpdate() {
230         return mustGenerateUpdate;
231     }
232
233     public boolean getReadOnly() {
234         return readOnly;
235     }
236
237     public List JavaDoc getJoins() {
238         return joins;
239     }
240
241     public RdbGenInfos getGenInfos() {
242         return genInfos;
243     }
244
245     public String JavaDoc getTableName() {
246         return tableName;
247     }
248
249     public String JavaDoc getTableNameNoDot() {
250         return tableName.replace('.', '_');
251     }
252
253     public List JavaDoc getColumns() {
254         return columns;
255     }
256
257     public List JavaDoc getInheritedColumns() {
258         return inheritedColumns;
259     }
260
261     public List JavaDoc getColocatedColumns() {
262         return colocatedColumns;
263     }
264
265     public boolean getColocatedTable() {
266         return colocatedTable;
267     }
268
269     public boolean isMainTable() {
270         return isMainTable;
271     }
272
273     /**
274      * Specifies if only prepared statement can be used with this table mapping.
275      */

276     private Boolean JavaDoc psMand = null;
277
278     /**
279      * Tells if only PreparedStatement can be used to store data associated
280      * with this table.
281      * @return true if the use of PreparedStatement is mandatory.
282      */

283     public boolean preparedStatementMandatory() {
284         if (psMand == null) {
285             for (int i = 0; i < columns.size(); i++) {
286                 if (((RdbGenColumn) columns.get(i)).columnType == PTypeSpace.SERIALIZED) {
287                     psMand = new Boolean JavaDoc(true);
288                     return true;
289                 }
290             }
291             psMand = new Boolean JavaDoc(false);
292         }
293         return psMand.booleanValue();
294     }
295
296     /**
297      * Tells if the given column belongs to a colocated ones, which means that
298      * it is shared with the colocated object and must not be touched (e.g.
299      * should not be nullified).
300      * @param rgc The involved column.
301      * @return true if it is a colocated column.
302      */

303     boolean colocatedColumn(RdbGenColumn rgc) {
304         if (colocatedColumns != null) {
305             for (int i = 0; i < colocatedColumns.size(); i++) {
306                 if (colocatedColumns.get(i) == rgc) {
307                     return true;
308                 }
309             }
310         }
311         return false;
312     }
313     /**
314      * get the genColumn corresponding to the given column columnName. If
315      * not found in this table definition, search in the table definition of the
316      * super classes
317      * @param columnName the name of a column in the table
318      * @return the genColumn corresponding to the column name
319      */

320     public RdbGenColumn getColumn(String JavaDoc columnName) {
321         RdbGenColumn rgc = null;
322         for (int i = 0; i < columns.size(); i++) {
323             rgc = (RdbGenColumn) columns.get(i);
324             if (rgc.columnName.equals(columnName)) {
325                 return rgc;
326             }
327         }
328         for (int i = 0; i < inheritedColumns.size(); i++) {
329             rgc = (RdbGenColumn) inheritedColumns.get(i);
330             if (rgc.columnName.equals(columnName)) {
331                 return rgc;
332             }
333         }
334         
335         return null;
336     }
337 }
338
Popular Tags