KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > metainfo > lib > BasicNameRef


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 package org.objectweb.jorm.metainfo.lib;
24
25 import org.objectweb.jorm.metainfo.api.CompositeName;
26 import org.objectweb.jorm.metainfo.api.MetaObject;
27 import org.objectweb.jorm.metainfo.api.NameRef;
28 import org.objectweb.jorm.metainfo.api.Package;
29 import org.objectweb.jorm.util.api.Loggable;
30 import org.objectweb.util.monolog.api.Logger;
31 import org.objectweb.util.monolog.api.LoggerFactory;
32 import org.objectweb.jorm.metainfo.api.Manager;
33
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 /**
38  * The BasicNameRef is used to reference a composite name.
39  * nameref are used in namedef to define pname with multiple fields.
40  * @author N. De Palma
41  */

42 public class BasicNameRef extends BasicMetaObject implements NameRef {
43
44     /**
45      * the name of the class reference
46      */

47     private String JavaDoc name;
48
49     /**
50      * name of the schema for the compositename referenced by this nameref
51      */

52     private String JavaDoc cnSchemaName = null;
53
54     /**
55      * the meta object describing the composite name referenced by this nameref.
56      */

57     private CompositeName compositeNameClass;
58
59     /**
60      * the list of the projection definition declared by the user in the
61      * current reference to the class.
62      * key: the name of the nameField
63      * value: the name of the classfield
64      */

65     private HashMap JavaDoc projections;
66
67     /**
68      * Builds a new BasicNameRef object.
69      * @param cn_Class the composite name referenced by the nameref
70      * @param parent the parent meta-object of the current nameref (this is a namedef).
71      */

72     public BasicNameRef(CompositeName cn_Class, MetaObject parent) {
73         super(parent);
74         this.name = cn_Class.getName();
75         this.compositeNameClass = cn_Class;
76         projections = new HashMap JavaDoc();
77     }
78
79     /**
80      * Add a projection between a field in the composite name structure and a field in the class.
81      * @param namefield the name of the field in the composite name
82      * @param classfield the name of the field in the class
83      */

84     public void addProjection(String JavaDoc namefield, String JavaDoc classfield) {
85         projections.put(namefield, classfield);
86     }
87
88     /**
89      * Retrieve the list of projections between the fields of the composite name and the field of the class.
90      * @return a map object describing the projection.
91      */

92     public Map JavaDoc getProjection() {
93         return projections;
94     }
95
96     /**
97      * Return the class field name associated with the composite name field name.
98      * @param fieldName the name of a field in the composite name.
99      * @return a String object containing a class field name.
100      */

101     public String JavaDoc getClassFieldName(String JavaDoc fieldName) {
102         return (String JavaDoc) projections.get(fieldName);
103     }
104
105     /**
106      * Retrieve the name of the nameref. This name is equal to the name of the referenced composite name.
107      * @return the nameof the nameref used to describe the current pname.
108      */

109     public String JavaDoc getName() {
110         return name;
111     }
112
113     /**
114      * Retrieve the metaobject describing the composite name referenced by this nameref.
115      * @return the composite name.
116      */

117     public CompositeName getCompositeName() {
118         return compositeNameClass;
119     }
120
121     public boolean equals(Object JavaDoc nr) {
122         if (nr instanceof NameRef)
123             return ((NameRef) nr).getName().equals(this.name);
124         else
125             return false;
126     }
127
128
129     public String JavaDoc getCNPackageName() {
130         if (cnSchemaName == null) {
131             MetaObject mo = compositeNameClass;
132             while (!(mo instanceof Package JavaDoc)) {
133                 mo = mo.getParent();
134                 if (mo instanceof Manager) return null;
135             }
136             cnSchemaName = ((Package JavaDoc) mo).getName();
137         }
138         return cnSchemaName;
139     }
140
141     /**
142      * test if the type of the compositename referenced in this nameref is equals to the type
143      * referenced by the nameref passed as parameter.
144      * @obj the nameref to compare to
145      * @return true if the schema name ans the compositename name are equal.
146      */

147     public boolean equalCNType(Object JavaDoc obj) {
148         if (!(obj instanceof NameRef)) return false;
149         return ((name.equals(((NameRef) obj).getName())) &&
150                 (getCNPackageName().equals(((NameRef) obj).getCNPackageName())));
151     }
152 }
153
Popular Tags