KickJava   Java API By Example, From Geeks To Geeks.

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


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.metainfo.lib;
25
26 import org.objectweb.jorm.metainfo.api.CompositeName;
27 import org.objectweb.jorm.metainfo.api.MetaObject;
28 import org.objectweb.jorm.metainfo.api.NameDef;
29 import org.objectweb.jorm.metainfo.api.NameRef;
30 import org.objectweb.jorm.util.api.Loggable;
31 import org.objectweb.util.monolog.api.Logger;
32 import org.objectweb.util.monolog.api.LoggerFactory;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Collections JavaDoc;
38
39 /**
40  * The BasicNameDef object describes the mapping of a reference field,
41  * It can be the PName of a Class. It can be a Field which is an attribute
42  * of the class and is a reference; in this case, the reference can be
43  * to another Class (ClassObject) or to a GenClass.
44  * @author X. Spengler
45  */

46 public class BasicNameDef extends BasicMetaObject implements NameDef {
47
48     /**
49      * isSystem defines is the current namedef is defined outside jorm or not.
50      * if true, the name is defined outside jorm,
51      * if false, the name is defined by jorm.
52      */

53     private boolean isSystem = false;
54
55     /**
56      * isNameRef defines if the namedef is describe by a composite name.
57      * true the namedef is a composite one.
58      */

59     private boolean isNameRef = false;
60
61     /**
62      * isFieldName defines the namedef is describe by a single field.
63      * true the namedef is a single one.
64      */

65     private boolean isFieldName = false;
66
67     /**
68      * nameRef contains a reference to a composite name.
69      */

70     private NameRef nameRef = null;
71
72     /**
73      * the fieldRefs contains FieldName which is iether an hiddenfield or a field
74      * added to the current list.
75      * element: a FieldName object
76      */

77     private String JavaDoc fieldName = null;
78
79     /**
80      * Each NameDef object has a distinct name.
81      */

82     private String JavaDoc name = null;
83
84     /**
85      * Builds a new BasicNameDef objects.
86      * This object is defined by its name, if it is defined outside jorm or not,
87      * and its parent.
88      *
89      * @param parent the parent object of the current NameDef object
90      */

91     public BasicNameDef(MetaObject parent) {
92         super(parent);
93         name = "";
94     }
95
96
97     ///////////////////////////////////////////////////////////////////
98
// from NameDef interface
99
///////////////////////////////////////////////////////////////////
100

101
102     /**
103      * Returns the number of hidden fields which composed the current object.
104      *
105      * @return the number of hidden fields
106      */

107     public int getFieldNumber() {
108         return (isNameRef() ? nameRef.getProjection().size() : 1);
109     }
110
111     /**
112      * Allows to know if the mapping of the referenced field is defined by
113      * another system (could be defined by Versant system for instance).
114      *
115      * @return true, if the mapping is defined by another system, else false,
116      * if the mapping is defined inside JORM (by ScalarField(s) and
117      * FieldName(s)).
118      */

119     public boolean isSystem() {
120         return isSystem;
121     }
122
123     /**
124      * Allows to set if the current NameDef is system defined (defined outside
125      * jorm) or not.
126      *
127      * @param system true, if the mapping is defined by another system, else
128      * false, if the mapping is defined inside JORM (by
129      * ScalarField(s) and FieldName(s)).
130      */

131     public void setSystem(boolean system) {
132         isSystem = system;
133     }
134
135     /**
136      * allow to know if the namedef is describe by a composite name.
137      * @return true the namedef is a composite one.
138      */

139     public boolean isNameRef() {
140         return isNameRef;
141     }
142
143     /**
144      * allow to know if the namedef is describe by a single field.
145      * @return true the namedef is a single one.
146      */

147     public boolean isFieldName() {
148         return isFieldName;
149     }
150
151     /**
152      * Build a reference toward a composite name describing the namedef.
153      * @param name the name of the compsoite name
154      * @param cn_Class the meta object representing the composite name.
155      * @return the nameref metaobject describing the reference.
156      */

157     public NameRef createNameRef(CompositeName cn_Class) {
158         isNameRef = true;
159         BasicNameRef bnr = new BasicNameRef(cn_Class, this);
160         this.nameRef = bnr;
161         return bnr;
162     }
163
164     /**
165      * Retrieves the nameref if the namedef is a composite one.
166      * @return the nameref or null if the namedef is a single one or system.
167      */

168     public NameRef getNameRef() {
169         return this.nameRef;
170     }
171
172
173     /**
174      * Retrieve the name of the field describing the namedef.
175      * The fieldname can be the name of a field or an hiddenfield.
176      * @return the fieldname or null is the namedef is a system or a composite one.
177      */

178     public String JavaDoc getFieldName() {
179         return this.fieldName;
180     }
181
182     /**
183      * Allow to set the fieldname describing the namedef single field.
184      * @param fieldname the fieldname
185      */

186     public void setFieldName(String JavaDoc fieldname) {
187         this.fieldName = fieldname;
188         this.isFieldName = true;
189     }
190
191     /**
192      * Returns the name of the NameDef object.
193      * @return the name of the NameDef object.
194      */

195     public String JavaDoc getName() {
196         return this.name;
197     }
198
199     /**
200      * Sets the name of the NameDef object.
201      * @param name the name of the NameDef object.
202      */

203     public void setName(String JavaDoc name) {
204         this.name = name;
205     }
206
207     /**
208      * Returns an iterator on all fields (HiddenFields and FieldRef).
209      * This iterator contains FieldName and ScalarField objects. If no object
210      * fields exists, an empty iterator is returned.
211      *
212      * @return an iterator on field
213      */

214
215     public Iterator JavaDoc iterateField() {
216         if (!(isNameRef())) {
217             ArrayList JavaDoc allInOne = new ArrayList JavaDoc();
218             allInOne.add(fieldName);
219             return allInOne.iterator();
220         } else
221             return nameRef.getProjection().values().iterator();
222     }
223
224     protected Collection JavaDoc getChildren() {
225         if (isNameRef && nameRef != null) {
226             return Collections.singleton(nameRef);
227         } else {
228             return super.getChildren();
229         }
230     }
231 }
232
Popular Tags