KickJava   Java API By Example, From Geeks To Geeks.

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


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.Class;
27 import org.objectweb.jorm.metainfo.api.ClassMapping;
28 import org.objectweb.jorm.metainfo.api.ClassProject;
29 import org.objectweb.jorm.metainfo.api.GenClassRef;
30 import org.objectweb.jorm.metainfo.api.Mapping;
31 import org.objectweb.jorm.metainfo.api.MetaObject;
32 import org.objectweb.jorm.metainfo.api.ParentClassMapping;
33 import org.objectweb.jorm.metainfo.lib.BasicMapping;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 import java.util.Collection JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39
40
41 /**
42  * Description
43  */

44 public class RdbMapping extends BasicMapping {
45     /**
46      * Builds a new BasicRdbMapping object.
47      * This object contains the mapping structures of a class.
48      * The parent object is a Project object.
49      * @param mapperName the mapper name,
50      * parent the parent of the current object.
51      */

52     public RdbMapping(String JavaDoc mapperName, MetaObject parent) {
53         super(mapperName, parent);
54     }
55
56     ///////////////////////////////////////////////////////////////////
57
// from RdbMapping interface
58
///////////////////////////////////////////////////////////////////
59

60     /**
61      * NB: OL: Always create multi mapping since only multimapping are expected
62      * when parsing the meta-info
63      * Creates a new BasicRdbClassMapping object.
64      * @param ruleName the name of the rule used to map the class,
65      * linkedMO the Class object referenced by the current object,
66      * parent the parent object of the current object.
67      * @return an RdbClassMapping object.
68      */

69     public ClassMapping createClassMapping(String JavaDoc ruleName) {
70         return createClassMultiMapping(ruleName);
71     }
72
73     /**
74      * Creates a new BasicRdbClassMultiMapping object.
75      * @param ruleName the name of the rule used to map the class,
76      * linkedMO the Class object referenced by the current object,
77      * parent the parent object of the current object.
78      * @return an RdbClassMultiMapping object.
79      */

80     public RdbClassMultiMapping createClassMultiMapping(String JavaDoc ruleName) {
81         RdbClassMultiMapping res = (RdbClassMultiMapping) getClassMapping();
82         if (res == null) {
83             res = new RdbClassMultiMapping(
84                     ruleName, getParent().getParent(), this);
85             setClassMapping(res);
86             res.setLogger(logger);
87         } else {
88             res.setRuleName(ruleName);
89         }
90         return res;
91     }
92
93     /**
94      * Creates a new BasicRdbGenClassMapping object.
95      * @param ruleName the name of the rule used to map the class,
96      * linkedMO the GenClassRef object referenced by the current
97      * object,
98      * parent the parent object of the current object.
99      * @return an RdbGenClassMapping object.
100      */

101     public RdbGenClassMapping createGenClassMapping(String JavaDoc ruleName,
102                                                     MetaObject linkedMO) {
103         String JavaDoc gcid = ((GenClassRef) linkedMO).getGenClassId();
104         if (logger.isLoggable(BasicLevel.DEBUG)) {
105             logger.log(BasicLevel.DEBUG,
106                     "(gcid " + gcid + ")");
107         }
108         RdbGenClassMapping res = (RdbGenClassMapping) getGenClassMapping(gcid);
109         if (res == null) {
110             res = new RdbGenClassMapping(ruleName, linkedMO, this);
111             res.setLogger(logger);
112             addGenClassMapping(gcid, res);
113         } else {
114             res.setRuleName(ruleName);
115             res.setLinkedMO(linkedMO);
116         }
117         return res;
118     }
119
120
121     public boolean isFilteredMapping() {
122         //lookup if the current class a filtered parent class mapping
123
boolean isFilteredMapping = isFilteredMapping(this);
124         //if the class has children with filtered parent class mapping
125
if (!isFilteredMapping) {
126             Class JavaDoc clazz = (Class JavaDoc) getClassMapping().getLinkedMO();
127             Collection JavaDoc cs = clazz.getSubClasses();
128             if (cs.isEmpty()) {
129                 return false;
130             }
131             String JavaDoc proj = ((ClassProject) getParent()).getProjectName();
132             for (Iterator JavaDoc it = cs.iterator(); it.hasNext() && !isFilteredMapping;) {
133                 Mapping m = ((Class JavaDoc) it.next()).getClassProject(proj)
134                         .getMapping(getMapperName());
135                 isFilteredMapping = isFilteredMapping(m);
136             }
137         }
138         return isFilteredMapping;
139     }
140
141     private boolean isFilteredMapping(Mapping m) {
142         Collection JavaDoc pcms = m.getClassMapping().getParentClassMappings();
143         if (pcms.isEmpty()) {
144             return false;
145         }
146         boolean isFilteredMapping = false;
147         for(Iterator JavaDoc it=pcms.iterator(); it.hasNext() && !isFilteredMapping;) {
148             ParentClassMapping pcm = (ParentClassMapping) it.next();
149             isFilteredMapping =
150                 RdbClassMapping.MAP_NEW_FIELDS_TO_EXTENDED_STRUCTURES
151                 .equalsIgnoreCase(pcm.getRuleName());
152         }
153         return isFilteredMapping;
154     }
155
156 }
157
Popular Tags