KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.PException;
27 import org.objectweb.jorm.metainfo.api.CommonClassMapping;
28 import org.objectweb.jorm.metainfo.api.MetaObject;
29 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
30 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * As a meta-object, the parent is the main table
40  * @author S.Chassande-Barrioz
41  */

42 public class RdbExternalTable extends RdbTable {
43
44     // the list of joins {RdbJoin}
45
List JavaDoc joins = null;
46
47     /**
48      * Create a meta-info object representing an external table.
49      * The parent is supposed to implement the RdbMappingInfos interface.
50      * @param parent parent in the meta-info tree (from which is taken the main table)
51      * @param linkedMO meta-info object referenced by this meta-info object
52      * @param name name of the table in the relational schema
53      */

54
55     public RdbExternalTable(MetaObject parent, MetaObject linkedMO, String JavaDoc name) {
56         super(parent, linkedMO, name);
57         if (!(parent instanceof RdbMappingInfos)) {
58             throw new InternalError JavaDoc("parent must implements the RdbMappingInfos");
59         }
60         joins = new ArrayList JavaDoc();
61     }
62
63     public RdbJoin createRdbJoin(String JavaDoc jn) {
64         RdbJoin j = new RdbJoin(this, jn);
65         joins.add(j);
66         //sort the list of joins according to their names
67
Collections.sort(joins);
68         return j;
69     }
70
71     public RdbJoin removeRdbJoin(String JavaDoc j) {
72         for (Iterator JavaDoc itJoin = joins.iterator(); itJoin.hasNext();) {
73             RdbJoin rdbJoin = (RdbJoin) itJoin.next();
74             if (rdbJoin.getName().equals(name)) {
75                 itJoin.remove();
76                 return rdbJoin;
77             }
78         }
79         return null;
80     }
81
82     public RdbJoin getRdbJoin(String JavaDoc name) {
83         for (Iterator JavaDoc itJoin = joins.iterator(); itJoin.hasNext();) {
84             RdbJoin rdbJoin = (RdbJoin) itJoin.next();
85             if (rdbJoin.getName().equals(name)) return rdbJoin;
86         }
87         return null;
88     }
89
90     public RdbTable getMainTable() {
91         return ((RdbMappingInfos) parent).getMainTable();
92     }
93
94     public Collection JavaDoc getRdbJoins() {
95         return joins;
96     }
97
98     public RdbPrimitiveElementMapping createPrimitiveElementMapping(
99             PrimitiveElement pe,
100             String JavaDoc columnName,
101             String JavaDoc sqlType,
102             boolean notNull,
103             RdbJoin join) throws PException {
104         RdbPrimitiveElementMapping pem = (RdbPrimitiveElementMapping)
105                 colName2pem.get(columnName);
106         if (pem != null) {
107             PrimitiveElement p = pem.lookupPrimitiveElement(join);
108             if (p != null && p != pe) {
109                 throw new PException("The column '" + columnName
110                                      + "' of the table '" + name + "' is already bound to"
111                                      + " another primitive element ("
112                                      + (p == null ? null : "'" + p.getName() + "'") + ")"
113                                      + " by the join '" + join.getName()
114                                      + "' than the specified (" + pe.getName() + ")");
115             }
116             pem.bindPrimitiveElement(join, pe);
117             if (pem.getType() == null && sqlType != null) {
118                 pem.setType(sqlType);
119             }
120             pem.setIsNotNull(pem.isNotNull() || notNull);
121         }
122         RdbPrimitiveElementMapping pem2 = new RdbPrimitiveElementMapping(
123                 columnName, sqlType, notNull, pe, this, join);
124         ((CommonClassMapping) getParent()).addPrimitiveElementMapping(pem2);
125         pem2.setLogger(getLogger());
126         pem2.setLoggerFactory(getLoggerFactory());
127         if (pem == null) {
128             colName2pem.put(columnName, pem2);
129             return pem2;
130         }
131         return pem;
132     }
133
134     public PrimitiveElementMapping getPrimitiveElementMappingByField(String JavaDoc fieldName) {
135         for (Iterator JavaDoc it = colName2pem.values().iterator(); it.hasNext();) {
136             RdbPrimitiveElementMapping pem =
137                     (RdbPrimitiveElementMapping) it.next();
138             Iterator JavaDoc itPem = pem.getPrimitiveElementByRdbJoin().values().iterator();
139             while (itPem.hasNext()) {
140                 PrimitiveElement pe = (PrimitiveElement) itPem.next();
141                 if (pe.getName().equals(fieldName))
142                     return pem;
143             }
144         }
145         return null;
146     }
147 }
148
Popular Tags