KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > expressions > DataExpression


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.expressions;
23
24 import java.util.*;
25 import java.io.*;
26 import oracle.toplink.essentials.expressions.*;
27 import oracle.toplink.essentials.internal.helper.*;
28 import oracle.toplink.essentials.mappings.*;
29 import oracle.toplink.essentials.querykeys.*;
30 import oracle.toplink.essentials.descriptors.ClassDescriptor;
31
32 /**
33  * Superclass for all expression that have a context.
34  * i.e. a base expression.
35  */

36 public abstract class DataExpression extends Expression {
37     protected Vector derivedTables;
38     protected Vector derivedFields;
39     protected Expression baseExpression;
40     protected boolean hasBeenNormalized = false;
41     protected TableAliasLookup tableAliases;
42
43     /**
44      * DataExpression constructor comment.
45      */

46     public DataExpression() {
47         super();
48     }
49
50     public void addDerivedField(Expression addThis) {
51         if (derivedFields == null) {
52             derivedFields = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(4);
53         }
54         derivedFields.addElement(addThis);
55     }
56
57     public void addDerivedTable(Expression addThis) {
58         if (derivedTables == null) {
59             derivedTables = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(4);
60         }
61         derivedTables.addElement(addThis);
62     }
63
64     /**
65      * INTERNAL:
66      * Find the alias for a given table
67      */

68     public DatabaseTable aliasForTable(DatabaseTable table) {
69         if (tableAliases == null) {
70             if (getBaseExpression() == null) {
71                 return null;
72             }
73             return getBaseExpression().aliasForTable(table);
74         }
75
76         return tableAliases.keyAtValue(table);
77     }
78
79     /**
80      * INTERNAL:
81      * Alias a particular table within this node
82      */

83     protected void assignAlias(String JavaDoc name, DatabaseTable table) {
84         assignAlias(new DatabaseTable(name), table);
85     }
86
87     /**
88      * INTERNAL:
89      * Alias a particular table within this node
90      */

91     protected void assignAlias(DatabaseTable alias, DatabaseTable table) {
92         if (tableAliases == null) {
93             tableAliases = new TableAliasLookup();
94         }
95         tableAliases.put(alias, table);
96     }
97
98     /**
99      * INTERNAL:
100      */

101     public void clearAliases() {
102         tableAliases = null;
103     }
104
105     public Vector copyCollection(Vector in, Dictionary alreadyDone) {
106         if (in == null) {
107             return null;
108         }
109         Vector result = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(in.size());
110         for (Enumeration e = in.elements(); e.hasMoreElements();) {
111             Expression exp = (Expression)e.nextElement();
112             result.addElement(exp.copiedVersionFrom(alreadyDone));
113         }
114         return result;
115     }
116
117     /**
118      * INTERNAL:
119      */

120     public Expression existingDerivedField(DatabaseField field) {
121         if (derivedFields == null) {
122             return null;
123         }
124         for (Enumeration e = derivedFields.elements(); e.hasMoreElements();) {
125             FieldExpression exp = (FieldExpression)e.nextElement();
126             if (exp.getField().equals(field)) {
127                 return exp;
128             }
129         }
130         return null;
131
132     }
133
134     /**
135      * INTERNAL:
136      */

137     public Expression existingDerivedTable(DatabaseTable table) {
138         if (derivedTables == null) {
139             return null;
140         }
141         for (Enumeration e = derivedTables.elements(); e.hasMoreElements();) {
142             TableExpression exp = (TableExpression)e.nextElement();
143             if (exp.getTable().equals(table)) {
144                 return exp;
145             }
146         }
147         return null;
148
149     }
150
151     /**
152      * INTERNAL:
153      * Return the field appropriately aliased
154      */

155     public DatabaseField getAliasedField() {
156         return null;
157
158     }
159
160     public Expression getBaseExpression() {
161         return baseExpression;
162     }
163
164     /**
165      * Return the expression builder which is the ultimate base of this expression, or
166      * null if there isn't one (shouldn't happen if we start from a root)
167      */

168     public ExpressionBuilder getBuilder() {
169         if (getBaseExpression() == null) {
170             return null;
171         }
172         return getBaseExpression().getBuilder();
173     }
174
175     public ClassDescriptor getDescriptor() {
176         return null;
177
178     }
179
180     /**
181      * INTERNAL:
182      */

183     public DatabaseField getField() {
184         return null;
185     }
186
187     public Expression getField(String JavaDoc fieldName) {
188         DatabaseField field = new DatabaseField(fieldName);
189         return getField(field);
190
191     }
192
193     public Expression getField(DatabaseField field) {
194         Expression existing = existingDerivedField(field);
195         if (existing != null) {
196             return existing;
197         }
198         return newDerivedField(field);
199
200     }
201
202     public DatabaseMapping getMapping() {
203         if (getBaseExpression() == null) {
204             return null;
205         }
206         ClassDescriptor aDescriptor = ((DataExpression)getBaseExpression()).getDescriptor();
207         if (aDescriptor == null) {
208             return null;
209         }
210         return aDescriptor.getObjectBuilder().getMappingForAttributeName(getName());
211     }
212
213     /**
214      * INTERNAL:
215      */

216     public Vector getOwnedTables() {
217         if (getDescriptor() == null) {
218             return null;
219         } else {
220             if (getDescriptor().isAggregateDescriptor()) {
221                 return null;
222             } else {
223                 return getDescriptor().getTables();
224             }
225         }
226     }
227
228     public QueryKey getQueryKeyOrNull() {
229         return null;
230
231     }
232
233     public Expression getTable(String JavaDoc tableName) {
234         DatabaseTable table = new DatabaseTable(tableName);
235         return getTable(table);
236     }
237
238     public Expression getTable(DatabaseTable table) {
239         Expression existing = existingDerivedTable(table);
240         if (existing != null) {
241             return existing;
242         }
243         return newDerivedTable(table);
244
245     }
246
247     /**
248      * INTERNAL:
249      * Return the aliases used. For CR#2456 must never lazily initialize as also used for Expression identity.
250      */

251     public TableAliasLookup getTableAliases() {
252         return tableAliases;
253
254     }
255
256     /**
257      * INTERNAL:
258      * Did the normalizer already add additional joins to the where clause due to
259      * this query key representing a foreign reference mapping?
260      * This insures that join criteria (for any query key expression) is not
261      * added twice.
262      * <p>
263      * New meaning: DataExpressions are often iterated on multiple times during
264      * normalize, but Function/Relation expressions only once. Adding a has
265      * been normalized flag improves performance and is required in some
266      * applications, such as have temporal query criteria been added.
267      */

268     public boolean hasBeenNormalized() {
269         return hasBeenNormalized;
270     }
271
272     public boolean hasBeenAliased() {
273         return ((tableAliases != null) && (tableAliases.size() != 0));
274
275     }
276
277     protected boolean hasDerivedFields() {
278         return derivedFields != null;
279     }
280
281     protected boolean hasDerivedTables() {
282         return derivedTables != null;
283     }
284
285     /**
286      * INTERNAL:
287      */

288     public boolean isAttribute() {
289         return false;
290     }
291
292     public boolean isDataExpression() {
293         return true;
294     }
295
296     /**
297      * INTERNAL:
298      * For iterating using an inner class
299      */

300     public void iterateOn(ExpressionIterator iterator) {
301         super.iterateOn(iterator);
302         if (baseExpression != null) {
303             baseExpression.iterateOn(iterator);
304         }
305     }
306
307     public Expression mappingCriteria() {
308         return null;
309     }
310
311     /**
312      * INTERNAL:
313      */

314     public Expression newDerivedField(DatabaseField field) {
315         FieldExpression result = new FieldExpression(field, this);
316         addDerivedField(result);
317         return result;
318
319     }
320
321     /**
322      * INTERNAL:
323      */

324     public Expression newDerivedTable(DatabaseTable table) {
325         TableExpression result = new TableExpression(table);
326         result.setBaseExpression(this);
327         addDerivedTable(result);
328         return result;
329
330     }
331
332     /**
333      * INTERNAL:
334      * Normalize the expression into a printable structure.
335      * Any joins must be added to form a new root.
336      */

337     public Expression normalize(ExpressionNormalizer normalizer) {
338         if (getBaseExpression() != null) {
339             // First normalize the base.
340
setBaseExpression(getBaseExpression().normalize(normalizer));
341         }
342
343         return super.normalize(normalizer);
344     }
345
346     /**
347      * INTERNAL:
348      * Used for cloning.
349      */

350     protected void postCopyIn(Dictionary alreadyDone) {
351         super.postCopyIn(alreadyDone);
352         clearAliases();
353         if (baseExpression != null) {
354             baseExpression = baseExpression.copiedVersionFrom(alreadyDone);
355         }
356         derivedFields = copyCollection(derivedFields, alreadyDone);
357         derivedTables = copyCollection(derivedTables, alreadyDone);
358     }
359
360     /**
361      * INTERNAL:
362      * Print SQL onto the stream, using the ExpressionPrinter for context
363      */

364     public void printSQL(ExpressionSQLPrinter printer) {
365         printer.printField(getAliasedField());
366     }
367
368     protected void setBaseExpression(Expression e) {
369         baseExpression = e;
370     }
371
372     public void setHasBeenNormalized(boolean value) {
373         hasBeenNormalized = value;
374     }
375
376     /**
377      * INTERNAL:
378      * For CR#2456, Table identity involves having two tables sharing the same
379      * aliasing table.
380      */

381     public void setTableAliases(TableAliasLookup tableAliases) {
382         if (this.tableAliases == null) {
383             this.tableAliases = tableAliases;
384         }
385     }
386
387     public String JavaDoc tableAliasesDescription() {
388         if (tableAliases == null) {
389             return "";
390         }
391         return tableAliases.toString();
392     }
393
394     /**
395      * Print the base for debuggin purposes.
396      */

397     public void writeSubexpressionsTo(BufferedWriter writer, int indent) throws IOException {
398         if (getBaseExpression() != null) {
399             getBaseExpression().toString(writer, indent);
400         }
401     }
402 }
403
Popular Tags