KickJava   Java API By Example, From Geeks To Geeks.

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


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.Reference;
28 import org.objectweb.jorm.metainfo.api.TypedElement;
29 import org.objectweb.jorm.metainfo.api.MetaObject;
30 import org.objectweb.medor.api.MedorException;
31 import org.objectweb.medor.query.api.QueryTreeField;
32
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * This class represents the extent of a JORM class.
38  * It contains and implements its associated TupleStructure.
39  */

40 public class ClassExtent extends BasicJormExtent {
41
42     private Class JavaDoc myClass;
43
44     //this parameter is set to false by default
45
//it is set to true via the addPrefetchFields method in the JormQueryTreeHelper class
46
private boolean prefetch = false;
47     
48     public ClassExtent() {
49     }
50
51     public ClassExtent(String JavaDoc _name, String JavaDoc pnFieldName) {
52         super(_name, pnFieldName);
53     }
54
55     /**
56      * Constructs an extent for the JORM class, in the form of a QueryLeaf.
57      * Two options are possible, depending on the value of the classPNameOnly
58      * parameter:
59      * <ol><li>either all attributes of the JORM class are present in the
60      * QueryLeaf</li>
61      * <li>or only the PName attribute is present in the QueryLeaf</li>
62      * </ol>
63      * @param jormClass is the JORM full class name.
64      * @param _name is the name of the node (null value => empty string)
65      * @param classPNameOnly indicates whether the extent should contain only
66      * the PName for the class (if true) or all attributes for that class
67      * (if false).
68      *
69      */

70     public ClassExtent(Class JavaDoc jormClass, String JavaDoc _name,
71                        String JavaDoc pnameFieldName,
72                        boolean classPNameOnly) throws MedorException {
73         super(_name, pnameFieldName);
74         myClass = jormClass;
75         
76         //First, create a Field for that Class
77
identifier = new PNameField(getFieldName(name, pnameFieldName), myClass, this);
78         this.fields.add(identifier);
79         name2field.put(identifier.getName(), identifier);
80
81         if (!classPNameOnly) {
82
83             //Fields are PrimitiveElement, ClassRef or GenClassRef
84
for (Iterator JavaDoc it = jormClass.getAllFields().iterator(); it.hasNext();) {
85                 addField((TypedElement) it.next());
86             }
87         }
88     }
89
90     /**
91      * Constructs an extent for the JORM class, in the form of a QueryLeaf.
92      * The field names for those fields which are part of the extent are
93      * passed as a parameter. The fact that the PName is part of the extent
94      * is optional.
95      * @param jormClass is the JORM full class name.
96      * @param _name is the name of the node (null value => empty string)
97      * @param fieldNames is an array of JORM field names which should be present
98      * in the extent
99      * @param addPName is a boolean which is true if the PName should be part
100      * of the extent, false otherwise.
101      * @throws MedorException if one of the requested fieldNames does not
102      * correspond to a field of the JORM class.
103      */

104     public ClassExtent(Class JavaDoc jormClass, String JavaDoc _name,
105                        String JavaDoc[] fieldNames,
106                        boolean addPName,
107                        String JavaDoc pnameFieldName)
108         throws MedorException {
109         super(_name, pnameFieldName);
110         myClass = jormClass;
111
112         //First, create a Field for that Class if requested
113
if (addPName) {
114             PNameField nameField = new PNameField(getFieldName(name, pnameFieldName), myClass, this);
115             this.fields.add(nameField);
116             name2field.put(nameField.getName(), nameField);
117         }
118         //for each requested fieldName, adds the corresponding
119
for (int i = 0; i < fieldNames.length; i++) {
120             TypedElement te = jormClass.getTypedElement(fieldNames[i]);
121             if (te == null)
122                 throw new MedorException("No field named " + fieldNames[i] + " in JORM class " + jormClass.getName());
123             addField(te);
124         }
125     }
126
127     public Object JavaDoc clone(Object JavaDoc clone,
128                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
129         clone = super.clone(clone, obj2clone);
130         ((ClassExtent) clone).myClass = myClass;
131         return clone;
132     }
133
134     public QueryTreeField addField(String JavaDoc fieldName) throws MedorException {
135         if (fieldName == null) {
136             throw new java.lang.IllegalArgumentException JavaDoc("Null field name");
137         }
138         TypedElement te = myClass.getTypedElement(fieldName);
139         if (te == null) {
140             throw new MedorException("No field '" + fieldName
141                 + "' found on the class '" + myClass.getName() +"'.");
142         }
143         return addField(te);
144     }
145     /**
146      * It adds a new Field into the tuple structure.
147      * @param te is the jorm Meta object TypedElement which is the source of
148      * the field
149      * pname or the gen class pname.
150      */

151     public QueryTreeField addField(TypedElement te) throws MedorException {
152         QueryTreeField basicF = null;
153         String JavaDoc fn = getFieldName(name, te.getName());
154         basicF = (QueryTreeField) name2field.get(fn);
155         if (basicF != null)
156             throw new MedorException("the field " + te.getName()
157                 + " is already defined (" + fn + ")");
158         if (te instanceof Reference)
159             basicF = new PNameField(fn, myClass, this);
160         else
161             basicF = new BasicJormField(fn, this, te);
162         fields.add(basicF);
163         name2field.put(basicF.getName(), basicF);
164         return basicF;
165     }
166
167     public MetaObject getMetaObject() {
168         return myClass;
169     }
170
171     public String JavaDoc getJormName() {
172         return myClass.getFQName();
173     }
174     
175     public boolean isPrefetch() {
176         return prefetch;
177     }
178     public void setPrefetch(boolean prefetch) {
179         this.prefetch = prefetch;
180     }
181 }
182
Popular Tags