KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > parsing > ConstructorNode


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.parsing;
23
24 // Java imports
25
import java.util.*;
26
27 import oracle.toplink.essentials.exceptions.EJBQLException;
28 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
29 import oracle.toplink.essentials.queryframework.ReportQuery;
30
31 /**
32  * INTERNAL
33  * <p><b>Purpose</b>: Represent a constructor node (NEW)
34  * <p><b>Responsibilities</b>:<ul>
35  * <li> Generate the correct expression for a constructor
36  * </ul>
37  */

38 public class ConstructorNode extends Node {
39
40     /** The name of the constructor class. */
41     private String JavaDoc className = null;
42     
43     /** The list of constructor call argument nodes */
44     public List constructorItems = new ArrayList();
45
46     /**
47      * Return a new ConstructorNode
48      */

49     public ConstructorNode(String JavaDoc className) {
50         this.className = className;
51     }
52
53     /**
54      * INTERNAL
55      * Apply this node to the passed query
56      */

57     public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) {
58         if (theQuery instanceof ReportQuery) {
59             SelectGenerationContext selectContext = (SelectGenerationContext)context;
60             ReportQuery reportQuery = (ReportQuery)theQuery;
61             reportQuery.beginAddingConstructorArguments(getConstructorClass());
62             for (Iterator i = constructorItems.iterator(); i.hasNext();) {
63                 Node node = (Node)i.next();
64                 if (selectingRelationshipField(node, context)) {
65                     selectContext.useOuterJoins();
66                 }
67                 node.applyToQuery(reportQuery, context);
68                 selectContext.dontUseOuterJoins();
69             }
70             reportQuery.endAddingToConstructorItem();
71         }
72     }
73     
74     /**
75      * INTERNAL
76      * Validate node and calculate its type.
77      */

78     public void validate(ParseTreeContext context) {
79         for (Iterator i = constructorItems.iterator(); i.hasNext();) {
80             Node item = (Node)i.next();
81             item.validate(context);
82         }
83
84         // Resolve constructor class
85
TypeHelper typeHelper = context.getTypeHelper();
86         Object JavaDoc type = typeHelper.resolveTypeName(className);
87         if (type == null) {
88             String JavaDoc name = className;
89             // check for inner classes
90
int index = name.lastIndexOf('.');
91             if (index != -1) {
92                 name = name.substring(0, index) + '$' + name.substring(index+1);
93                 type = typeHelper.resolveTypeName(name);
94             }
95         }
96         setType(type);
97     }
98     
99     /**
100      * INTERNAL
101      * Is this node a ConstructorNode
102      */

103     public boolean isConstructorNode() {
104         return true;
105     }
106
107     /**
108      * INTERNAL
109      * Add an Order By Item to this node
110      */

111     public void addConstructorItem(Object JavaDoc theNode) {
112         constructorItems.add(theNode);
113     }
114
115     /**
116      * INTERNAL
117      * Set the list of constructor items of this node.
118      */

119     public void setConstructorItems(List items) {
120         this.constructorItems = items;
121     }
122
123     /**
124      * INTERNAL
125      * Get the list of constructor items of this node.
126      */

127     public List getConstructorItems() {
128         return this.constructorItems;
129     }
130
131     /**
132      * Check the specifid constructor class and return its class instance.
133      * @exception EJBQLException if the specified constructor class could not
134      * be found.
135      */

136     private Class JavaDoc getConstructorClass() {
137         Object JavaDoc type = getType();
138         if (type == null) {
139             throw EJBQLException.constructorClassNotFound(className);
140         }
141         return (Class JavaDoc)type;
142     }
143
144     /**
145      * INTERNAL
146      */

147     private boolean selectingRelationshipField(Node node, GenerationContext context) {
148         if ((node == null) || !node.isDotNode()) {
149             return false;
150         }
151         return !((DotNode)node).endsWithDirectToField(context);
152     }
153
154     /**
155      * INTERNAL
156      * Get the string representation of this node.
157      */

158     public String JavaDoc getAsString() {
159         StringBuffer JavaDoc repr = new StringBuffer JavaDoc();
160         repr.append("NEW ").append(className);
161         repr.append("(");
162         for (Iterator i = constructorItems.iterator(); i.hasNext();) {
163             Node node = (Node)i.next();
164             repr.append(node.getAsString());
165             if (i.hasNext()) {
166                 repr.append(", ");
167             }
168         }
169         repr.append(")");
170         return repr.toString();
171     }
172 }
173
Popular Tags