KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
27 import org.objectweb.jorm.metainfo.api.Class;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * Implementation of the RdbClassMultiMapping interface.
36  *
37  * BasicRdbClassMultiMapping contains mapping information referring to:
38  * - the primary table,
39  * - external tables,
40  * - join relationships between the primary table and an external table.
41  */

42 public class RdbClassMultiMapping extends RdbClassMapping
43         implements RdbMappingInfos {
44     List JavaDoc tableNames = null;
45     List JavaDoc tables = null;
46
47     /**
48      * Builds a new BasicRdbClassMultiMapping object.
49      *
50      * This object contains the mapping structure of the class it refers to.
51      * The parent object is a Mapping object that contains the mapper name.
52      *
53      * @param ruleName the name of the rule used to map the class,
54      * linkedMo the Class object referenced by the current object,
55      * parent the parent of the current object.
56      */

57     public RdbClassMultiMapping(String JavaDoc ruleName, MetaObject linkedMO,
58                                      MetaObject parent) {
59         super(ruleName, linkedMO, parent);
60         tableNames = new ArrayList JavaDoc();
61         tables = new ArrayList JavaDoc();
62     }
63
64     public PrimitiveElementMapping getPrimitiveElementMapping(String JavaDoc fieldName) {
65         PrimitiveElementMapping pem = super.getPrimitiveElementMapping(fieldName);
66         if (pem == null) {
67             Iterator JavaDoc it = tables.iterator();
68             while (pem == null && it.hasNext()) {
69                 pem = ((RdbExternalTable) it.next())
70                     .getPrimitiveElementMappingByField(fieldName);
71             }
72         }
73         return pem;
74     }
75
76     ///////////////////////////////////////////////////////////////////
77
// from RdbClassMultiMapping interface
78
///////////////////////////////////////////////////////////////////
79

80     public RdbExternalTable createRdbExternalTable(String JavaDoc tableName) {
81         int idx = tableNames.indexOf(tableName);
82         RdbExternalTable t;
83         if (idx == -1) {
84             t = new RdbExternalTable(this, getLinkedMO(), tableName);
85             t.setLogger(getLogger());
86             t.setLoggerFactory(getLoggerFactory());
87             tableNames.add(tableName);
88             tables.add(t);
89         }
90         else {
91             t = (RdbExternalTable) tables.get(idx);
92         }
93         return t;
94     }
95
96     public RdbExternalTable removeRdbExternalTable(String JavaDoc tableName) {
97         int idx = tableNames.indexOf(tableName);
98         if (idx != -1) {
99             tableNames.remove(idx);
100             return (RdbExternalTable) tables.remove(idx);
101         }
102         return null;
103     }
104
105     public Collection JavaDoc getRdbExternalTables() {
106         return tables;
107     }
108
109
110     public void getAllRdbExternalTables(ArrayList JavaDoc res) {
111         if ( !getParentClassMappings().isEmpty()) {
112             Iterator JavaDoc it = getParentClassMappings().iterator();
113             while (it.hasNext()) {
114                 ParentClassMapping pcm = (ParentClassMapping) it.next();
115                 if (pcm.getRuleName().equals(REMAP_FIELDS_TO_NEW_STRUCTURES)) {
116                     Class JavaDoc parentClass = pcm.getMOClass();
117                     //System.out.println("rule-name = vertical =<" + parentClass.getFQName() + ">");
118
RdbClassMultiMapping classmapping =
119                         (RdbClassMultiMapping) parentClass.getClassMapping(getProjectName(),getMapperName());
120                     classmapping.getAllRdbExternalTables(res);
121                     // add the main table ?
122
// turns first the main table into an external table
123
//res.add(table);
124
// add the external tables
125
res.addAll(tables);
126                 }
127                 else {
128                     // There is a filtered or an horizontal mapping.
129
//System.out.println("rule-name = " + pcm.getRuleName());
130
res.addAll(getRdbExternalTables());
131                 }
132             }
133         }
134         else {
135             // There is no extension.
136
res.addAll(tables);
137         }
138     }
139
140
141     public RdbExternalTable getRdbExternalTable(String JavaDoc tableName) {
142         int idx = tableNames.indexOf(tableName);
143         if (idx != -1) {
144             return (RdbExternalTable) tables.get(idx);
145         }
146         return null;
147     }
148
149     protected Collection JavaDoc getChildren() {
150         Collection JavaDoc col = super.getChildren();
151         col.addAll(tables);
152         return col;
153     }
154
155     public RdbTable getMainTable() {
156         return table;
157     }
158
159     public List JavaDoc getExternalTables() {
160         return tables;
161     }
162
163     public List JavaDoc getExternalTableNames() {
164         return tableNames;
165     }
166
167 }
168
Popular Tags