KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > metainfo > RdbTable


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.metainfo;
25
26 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
27 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping;
28 import org.objectweb.jorm.metainfo.api.MetaObject;
29 import org.objectweb.jorm.metainfo.api.CommonClassMapping;
30 import org.objectweb.jorm.metainfo.lib.BasicMappingStructure;
31 import org.objectweb.jorm.api.PException;
32
33 import java.util.Collection JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * Define the mapping of primitive elements onto the columns of a table.
41  * Note that the mapping definition of the fields of a class onto the columns
42  * of a table may be sparsed between several objects of this class. This can
43  * espacially happen with inheritance with the 'extended' or 'added' rules.
44  *
45  *
46  * @author S.Chassande-Barrioz
47  */

48 public class RdbTable extends BasicMappingStructure {
49     /**
50      * Name of the table in the relational schema
51      */

52     String JavaDoc name = null;
53     /**
54      * TODO
55      */

56     boolean colocated = false;
57     /**
58      * TODO
59      */

60     boolean colocatedMaster = false;
61
62     /**
63      * Map use to register the primitive elements mapping indexed with their
64      * column name. < String,PrimitiveElementMapping >
65      */

66     Map JavaDoc colName2pem = null;
67
68     /**
69      * Indicates if the table is read-only
70      */

71     boolean readOnly;
72
73     public RdbTable(MetaObject parent, MetaObject linkedMO, String JavaDoc name) {
74         super(parent, linkedMO);
75         colName2pem = new HashMap JavaDoc();
76         this.name = name;
77     }
78
79
80
81     public boolean isReadOnly() {
82         return readOnly;
83     }
84
85     public void setReadOnly(boolean readOnly) {
86         this.readOnly = readOnly;
87     }
88
89     public String JavaDoc getName() {
90         return name;
91     }
92
93     public void setName(String JavaDoc name) {
94         this.name = name;
95     }
96
97     public Collection JavaDoc getPrimitiveElementMappings() {
98         return colName2pem.values();
99     }
100
101     /**
102      * Creates and registers a primitive element mapping, that is how
103      * a primitive element is mapped onto a column of the table. It implictly
104      * defines a new column name in the table, the type and the 'not-null'
105      * property being passed as parameters.
106      * @param pe the primitive element
107      * @param columnName the name of the column onto the primitive element is mapped
108      * @param sqlType the SQL type of the column
109      * @param notNull if true the column cannot have null values
110      * @return a new primitive element registered with this table mapping
111      * @throws PException if a primitive element is already mapped to the column
112      */

113     public RdbPrimitiveElementMapping createPrimitiveElementMapping(
114             PrimitiveElement pe,
115             String JavaDoc columnName,
116             String JavaDoc sqlType,
117             boolean notNull) throws PException {
118
119         // checks for existence of the mapping
120
RdbPrimitiveElementMapping pem = (RdbPrimitiveElementMapping)
121                 colName2pem.get(columnName);
122         if (pem != null) {
123             throw new PException("Column " + columnName
124                                  + " already defined in the table " + name);
125         }
126         // creates a new primitive element mapping
127
pem = new RdbPrimitiveElementMapping(
128                 columnName, sqlType, notNull, pe, this);
129         ((CommonClassMapping) getParent()).addPrimitiveElementMapping(pem);
130         pem.setLoggerFactory(getLoggerFactory());
131         // and registers it
132
colName2pem.put(columnName, pem);
133         return pem;
134     }
135
136     /**
137      * Creates and registers a primitive element mapping, that is how
138      * a primitive element is mapped onto a column of the table. It implictly
139      * defines a new column name in the table, the type being undefined
140      * and the 'not-null' property being set to false.
141      * @param pe the primitive element
142      * @param columnName the name of the column onto the primitive element is mapped
143      * @return a new primitive element registered with this table mapping
144      * @throws PException if a primitive element is already mapped to the column
145      */

146     public RdbPrimitiveElementMapping createPrimitiveElementMapping(
147             PrimitiveElement pe,
148             String JavaDoc columnName) throws PException {
149         return createPrimitiveElementMapping(pe, columnName, null, false);
150     }
151
152     /**
153      * Removes a primitive element mapping from the table mapping definition
154      * @param pem the primitive element mapping to remove
155      * @return null if the pem was not part of the table mapping definition, the
156      * pem itself otherwise
157      */

158     public PrimitiveElementMapping removePrimitiveElementMapping(PrimitiveElementMapping pem) {
159         return (PrimitiveElementMapping) colName2pem.remove(pem);
160     }
161
162     /**
163      * Gets the primitive element mapping by its column name
164      * @param columnName the name of the column
165      * @return the primitive element mapping definition on this column
166      */

167     public PrimitiveElementMapping getPrimitiveElementMappingByCol(String JavaDoc columnName) {
168         return (PrimitiveElementMapping) colName2pem.get(columnName);
169     }
170
171     /**
172      * Gets the primitive element mapping by its field name
173      * @param fieldName the name of the field
174      * @return the primitive element mapping definition of this field
175      */

176     public PrimitiveElementMapping getPrimitiveElementMappingByField(String JavaDoc fieldName) {
177         for (Iterator JavaDoc it = colName2pem.values().iterator(); it.hasNext();) {
178             RdbPrimitiveElementMapping pem =
179                     (RdbPrimitiveElementMapping) it.next();
180             if (((PrimitiveElement) pem.getLinkedMO()).getName().equals(fieldName))
181                 return pem;
182         }
183         return null;
184     }
185
186     /**
187      * Gets the set of column names of the table definition
188      * @return the set of column names of the table definition (String)
189      */

190     public Set JavaDoc getColumns() {
191         return colName2pem.keySet();
192     }
193
194     /**
195      * Gets the collocated attribute
196      * @return the collocated attribute
197      */

198     public boolean isColocated() {
199         return colocated;
200     }
201
202     /**
203      * Sets the collocated attribute
204      * @param c true if the table is collocated
205      */

206     public void setColocated(boolean c) {
207         colocated = c;
208     }
209
210     public boolean isColocatedMaster() {
211         return colocatedMaster;
212     }
213
214     public void setColocatedMaster(boolean cm) {
215         colocatedMaster = cm;
216     }
217 }
218
Popular Tags