KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > jorm > lib > PNameField


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 package org.objectweb.medor.query.jorm.lib;
25
26 import org.objectweb.jorm.metainfo.api.Class;
27 import org.objectweb.jorm.metainfo.api.Manager;
28 import org.objectweb.jorm.metainfo.api.MetaObject;
29 import org.objectweb.jorm.metainfo.api.Reference;
30 import org.objectweb.jorm.metainfo.api.TypedElement;
31 import org.objectweb.jorm.metainfo.api.GenClassRef;
32 import org.objectweb.jorm.metainfo.api.NameDef;
33 import org.objectweb.jorm.metainfo.api.ClassRef;
34 import org.objectweb.jorm.metainfo.api.Mapping;
35 import org.objectweb.jorm.metainfo.api.ClassProject;
36 import org.objectweb.jorm.metainfo.api.ReferenceMapping;
37 import org.objectweb.jorm.lib.JormPathHelper;
38 import org.objectweb.medor.query.api.QueryTree;
39 import org.objectweb.medor.query.jorm.api.JormExtent;
40 import org.objectweb.medor.api.MedorException;
41 import org.objectweb.util.monolog.api.BasicLevel;
42
43 import java.util.Iterator JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * This class represents Fields corresponding to the PName of a class or a
48  * Genclass(the NameDef of the class itself) or the the PName associated to a
49  * reference field or to a genclass element.
50  * The name of such a Field is a constant.
51  *
52  * Inherited field te:
53  * ===================
54  * In case where this PNameField represents the a reference field, this
55  * field is not null and references the Jorm Meta object Reference.
56  *
57  * In case where this PNameField represents GenClass identifier or the
58  * genclass element this field is not null and references the Jorm meta
59  * object GenClassRef.
60  *
61  * @author Alexandre Lefebvre, Sebastien Chassande-Barrioz
62  */

63 public class PNameField extends BasicJormField {
64
65     /**
66      * In case where this PNameField represents the PName of a Jorm class, this
67      * field is not null and references the Jorm Meta object Class.
68      */

69     protected Class JavaDoc clazz;
70
71     /**
72      * If the current PNameField represents a PName of a Jorm class or genclass,
73      * this field is equals to 'true'. Otherwise the current PNameField
74      * represents a reference field or the genclass element then this field is
75      * equals to 'false'.
76      */

77     protected boolean isClassPName;
78
79     /**
80      * This fiedl returns 'true' if the current PNameField has been built to
81      * describe the PName identifier of a GenClass or the PName of the GenClass
82      * element.
83      */

84     protected boolean isInGenClass;
85
86     public PNameField() {
87     }
88
89     /**
90      * This constructor must be used to built a PNameField associated to a
91      * GenClass identifier or the genclass element.
92      * @param fieldName is the name which must be given to the field.
93      * @param gcr is the jorm meta object which describes the genclass.
94      * @param isGenClassPName indicates if this PNameField will represent a
95      * genclass identifier(value='true'). Otherwise the this PNameField will
96      * represent a genclass element(value='false').
97      * @param qt is the QueryTree which is associated to this QueryTree field.
98      */

99     public PNameField(String JavaDoc fieldName,
100                       GenClassRef gcr,
101                       boolean isGenClassPName,
102                       QueryTree qt) throws MedorException {
103         super(fieldName, qt, gcr);
104         isInGenClass = true;
105         isClassPName = isGenClassPName;
106     }
107
108     /**
109      * This constructor must be used to built a PNameField associated to a
110      * classRef.
111      * @param cr is the jorm meta object ClassRef associated to this PNameField.
112      * @param qt is the QueryTree which is associated to this QueryTree field.
113      */

114     public PNameField(String JavaDoc fieldName, ClassRef cr, QueryTree qt) throws MedorException {
115         super(fieldName, qt, cr);
116         isInGenClass = cr.getParent() instanceof GenClassRef;
117         isClassPName = false;
118     }
119
120     /**
121      * This constructor must be used to built a PNameField associated to a Class
122      * identifier or a reference field.
123      * @param fn is the name which must be given to the field.
124      * @param cl is the jorm meta object Class associated to this PNameField. In
125      * case of the class contains a field which the name is equals to @fn then
126      * this PNameField represents a reference field.
127      * @param qt is the QueryTree which is associated to this QueryTree field.
128      */

129     public PNameField(String JavaDoc fn, Class JavaDoc cl, QueryTree qt) throws MedorException {
130         super(fn, qt);
131         isInGenClass = false;
132         TypedElement te = getTypedElement(cl, fn);
133         if (te != null && te instanceof Reference) {
134             type = te.getType();
135             isClassPName = false;
136             this.te = te;
137         }
138         else {
139             clazz = cl;
140             isClassPName = true;
141             type = clazz.getPType();
142         }
143     }
144
145     public Object JavaDoc clone(Object JavaDoc clone,
146                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
147         clone = super.clone(clone, obj2clone);
148         ((PNameField) clone).clazz = clazz;
149         ((PNameField) clone).isClassPName = isClassPName;
150         ((PNameField) clone).isInGenClass = isInGenClass;
151         return clone;
152     }
153
154     /**
155      * It returns 'true' when this PNameField represents the identifier of a jorm
156      * class or a jorm genclass.
157      */

158     public boolean isClassPName() {
159         return isClassPName;
160     }
161
162     /**
163      * It returns the jorm meta object Reference in case of this PNameField
164      * represents a reference field in a jorm class.
165      */

166     public Reference getReference() {
167         return (Reference) te;
168     }
169
170     /**
171      * It returns the jorm meta object Class in case of this PNameField
172      * represents an identifier of a jorm class.
173      */

174     public Class JavaDoc getMetaObjectClass() {
175         return clazz;
176     }
177
178     /**
179      * It returns the jorm meta object GenClassRef in case of this PNameField
180      * represents a GenClass identifier or a GenClass element.
181      */

182     public GenClassRef getGenClassRef() {
183         return (GenClassRef)te;
184     }
185
186     /**
187      * It returns 'true' if the current PNameField has been built to
188      * describe the PName identifier of a GenClass or the PName of the GenClass
189      * element.
190      */

191     public boolean isInGenClass() {
192         return isInGenClass;
193     }
194
195     private TypedElement getTypedElement(Class JavaDoc clazz, String JavaDoc fn) {
196         int idx = fn.length();
197         TypedElement te = null;
198         te = clazz.getTypedElement(fn);
199         while ((idx = fn.lastIndexOf(".", idx)) != -1 && te == null) {
200             te = clazz.getTypedElement(fn.substring(idx + 1, fn.length()));
201             idx--;
202         }
203         return te;
204     }
205
206     /**
207      * Retrieves the NameDef corresponding to this PNameField
208      * @param extent attached to this PNameField
209      * @throws MedorException if it is not possible to fetch the Mapping for
210      * the project name and the mapper attached to the JormExtent.
211      */

212     public NameDef getNameDef(JormExtent extent) throws MedorException {
213         if (isClassPName) {
214             if (isInGenClass) {
215                 //Pname of the gen class identifier
216
return getRefNameDef((GenClassRef) extent.getMetaObject(), extent);
217             } else {
218                 //PName of the class identifier
219
return getClassNameDef(extent);
220             }
221         } else {
222             if (isInGenClass) {
223                 return getElemNameDef((Reference) te, extent);
224             } else {
225                 //PName of the class reference
226
return getRefNameDef((Reference) te, extent);
227             }
228         }
229     }
230
231     /**
232      * Retrieves the name of the ParameterOperand containing the PNamingContext.
233      */

234     public String JavaDoc getPNamingContextParameter() {
235         if (isClassPName) {
236             if (isInGenClass) {
237                 //Pname of the gen class identifier
238
return JormPathHelper.getPath((GenClassRef) te, false);
239             } else {
240                 //PName of the class identifier
241
return JormPathHelper.getPath(clazz);
242             }
243         } else {
244             if (te instanceof GenClassRef) {
245                 //reference to a GenClass from a Class, or a GenClass
246
return JormPathHelper.getPath((GenClassRef) te,
247                         !isInGenClass);
248             }
249             else
250                 //reference to a Class from a Class, or a GenClass
251
return JormPathHelper.getPath((ClassRef) te);
252          }
253     }
254
255     /**
256      * Retrieves the Jorm meta object of the Class represented by this
257      * PNameField. If the parameter is
258      * a Class then the same value is returned. If the parameter is a ClassRef
259      * or a GenClassRef then referenced Class is returned. Otherwise a null
260      * value is returned.
261      */

262     public MetaObject getTargetedMetaObject() {
263         if (isInGenClass) {
264             if (isClassPName) {
265                 return te;
266             } else {
267                 GenClassRef gcr = (GenClassRef) te;
268                 if (gcr.isClassRef()) {
269                     return gcr.getClassRef().getMOClass();
270                 } else if (gcr.isGenClassRef()) {
271                     return gcr.getGenClassRef();
272                 } else {
273                     return null;
274                 }
275             }
276         } else {
277             if (isClassPName) {
278                 return clazz;
279             } else {
280                 return te;
281             }
282         }
283     }
284     /**
285      * Retrieves the NameDef representing the identifier of a GenClass/
286      * @param gcr is the jorm meta object representing the GenClass
287      * @param extent is the GenClassExtent corresponding to the 'gcr'
288      * @throws MedorException if it is not possible to fetch the Mapping for
289      * the project name and the mapper attached to the JormExtent.
290      */

291     private NameDef getRefNameDef(GenClassRef gcr,
292                                  JormExtent extent) throws MedorException {
293         return (NameDef) getMapping(extent)
294                 .getGenClassMapping(gcr.getGenClassId())
295                 .getIdentifierMapping().getLinkedMO();
296     }
297
298
299     /**
300      * Retrieves the NameDef representing the element of a GenClass/
301      * @param r is the jorm meta object representing the reference element of
302      * the GenClass
303      * @param extent is the GenClassExtent corresponding to the 'gcr'
304      * @throws MedorException if it is not possible to fetch the Mapping for
305      * the project name and the mapper attached to the JormExtent.
306      */

307     private NameDef getElemNameDef(Reference r,
308                                  JormExtent extent) throws MedorException {
309         return (NameDef) getMapping(extent)
310                 .getGenClassMapping(((GenClassRef) r.getParent()).getGenClassId())
311                 .getReferenceMapping().getLinkedMO();
312     }
313
314     /**
315      * Retrieves the NameDef representing the reference to a Class. This
316      * reference is hosted by a Class (not a GenClass see getElemNameDef)
317      * @param r is the jorm meta object representing the class reference
318      * @param extent is the ClassExtent corresponding to the 'cr'
319      * @throws MedorException if it is not possible to fetch the Mapping for
320      * the project name and the mapper attached to the JormExtent.
321      */

322     private NameDef getRefNameDef(Reference r,
323                                  JormExtent extent) throws MedorException {
324         if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) {
325             logger.log(BasicLevel.DEBUG, "Extent class mapping " + getMapping(extent).getClassMapping());
326             Iterator JavaDoc it = getMapping(extent).getClassMapping().getReferenceMappings().iterator();
327             while (it.hasNext()) {
328                 logger.log(BasicLevel.DEBUG, "Reference rule name " + ((ReferenceMapping)it.next()).getRuleName());
329             }
330             logger.log(BasicLevel.DEBUG, "+ reference mapping " + getMapping(extent).getClassMapping().getReferenceMapping(r.getName()));
331             logger.log(BasicLevel.DEBUG, "+ rule name " + getMapping(extent).getClassMapping().getReferenceMapping(r.getName()).getRuleName());
332             logger.log(BasicLevel.DEBUG, "+ linkedMO " + getMapping(extent).getClassMapping().getReferenceMapping(r.getName()).getLinkedMO());
333         }
334         return (NameDef) getMapping(extent).getClassMapping()
335                 .getReferenceMapping(r.getName()).getLinkedMO();
336     }
337
338     /**
339      * Retrieves the Jorm meta object representing a Class. If the parameter is
340      * a Class then the same value is returned. If the parameter is a ClassRef
341      * or a GenClassRef then the hosting Class is returned. Otherwise a null
342      * value is returned.
343      */

344     private Class JavaDoc getClass(MetaObject mo) {
345         if (mo instanceof Class JavaDoc) {
346             return (Class JavaDoc) mo;
347         } else if (mo instanceof Package JavaDoc || mo instanceof Manager) {
348             return null;
349         } else {
350             return getClass(mo.getParent());
351         }
352     }
353
354     /**
355      * It retrieves the Mapping of a JormExtent.
356      * @param extent represents a persitent object which the Mapping is expected
357      * @throws MedorException if no mapping exists for the project name and the
358      * mapper specified by the extent.
359      */

360     private Mapping getMapping(JormExtent extent) throws MedorException {
361         Class JavaDoc c = getClass(extent.getMetaObject());
362         ClassProject cp = c.getClassProject(extent.getProjectName());
363         if (cp == null) {
364             throw new MedorException("No classproject found for the class "
365                     + c.getFQName() + " and the project "
366                     + extent.getProjectName());
367         }
368         String JavaDoc mapperName = extent.getPMapper().getMapperName();
369         int idx = mapperName.indexOf('.');
370         Mapping m = cp.getMapping(idx == -1
371                 ? mapperName
372                 : mapperName.substring(0, idx));
373         if (m == null) {
374             throw new MedorException("No mapping found for the class "
375                     + c.getFQName() + ", the project " + extent.getProjectName()
376                     + " and the mapper " + mapperName);
377         }
378         return m;
379     }
380
381     /**
382      * Retrieves the NameDef representing the identifier of a Class.
383      * @param extent is the ClassExtent representing the class
384      * @throws MedorException if it is not possible to fetch the Mapping for
385      * the project name and the mapper attached to the JormExtent.
386      */

387     private NameDef getClassNameDef(JormExtent extent) throws MedorException {
388         return (NameDef) getMapping(extent).getClassMapping()
389                 .getIdentifierMapping().getLinkedMO();
390     }
391
392 }
393
Popular Tags