001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.parsing;
038:
039: // Java imports
040: import java.util.*;
041:
042: import oracle.toplink.essentials.exceptions.EJBQLException;
043: import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
044: import oracle.toplink.essentials.queryframework.ReportQuery;
045:
046: /**
047: * INTERNAL
048: * <p><b>Purpose</b>: Represent a constructor node (NEW)
049: * <p><b>Responsibilities</b>:<ul>
050: * <li> Generate the correct expression for a constructor
051: * </ul>
052: */
053: public class ConstructorNode extends Node {
054:
055: /** The name of the constructor class. */
056: private String className = null;
057:
058: /** The list of constructor call argument nodes */
059: public List constructorItems = new ArrayList();
060:
061: /**
062: * Return a new ConstructorNode
063: */
064: public ConstructorNode(String className) {
065: this .className = className;
066: }
067:
068: /**
069: * INTERNAL
070: * Apply this node to the passed query
071: */
072: public void applyToQuery(ObjectLevelReadQuery theQuery,
073: GenerationContext context) {
074: if (theQuery instanceof ReportQuery) {
075: SelectGenerationContext selectContext = (SelectGenerationContext) context;
076: ReportQuery reportQuery = (ReportQuery) theQuery;
077: reportQuery
078: .beginAddingConstructorArguments(getConstructorClass(context
079: .getParseTreeContext()));
080: for (Iterator i = constructorItems.iterator(); i.hasNext();) {
081: Node node = (Node) i.next();
082: if (selectingRelationshipField(node, context)) {
083: selectContext.useOuterJoins();
084: }
085: node.applyToQuery(reportQuery, context);
086: selectContext.dontUseOuterJoins();
087: }
088: reportQuery.endAddingToConstructorItem();
089: }
090: }
091:
092: /**
093: * INTERNAL
094: * Validate node and calculate its type.
095: */
096: public void validate(ParseTreeContext context) {
097: for (Iterator i = constructorItems.iterator(); i.hasNext();) {
098: Node item = (Node) i.next();
099: item.validate(context);
100: }
101:
102: // Resolve constructor class
103: TypeHelper typeHelper = context.getTypeHelper();
104: Object type = typeHelper.resolveTypeName(className);
105: if (type == null) {
106: String name = className;
107: // check for inner classes
108: int index = name.lastIndexOf('.');
109: if (index != -1) {
110: name = name.substring(0, index) + '$'
111: + name.substring(index + 1);
112: type = typeHelper.resolveTypeName(name);
113: }
114: }
115: setType(type);
116: }
117:
118: /**
119: * INTERNAL
120: * Is this node a ConstructorNode
121: */
122: public boolean isConstructorNode() {
123: return true;
124: }
125:
126: /**
127: * INTERNAL
128: * Add an Order By Item to this node
129: */
130: public void addConstructorItem(Object theNode) {
131: constructorItems.add(theNode);
132: }
133:
134: /**
135: * INTERNAL
136: * Set the list of constructor items of this node.
137: */
138: public void setConstructorItems(List items) {
139: this .constructorItems = items;
140: }
141:
142: /**
143: * INTERNAL
144: * Get the list of constructor items of this node.
145: */
146: public List getConstructorItems() {
147: return this .constructorItems;
148: }
149:
150: /**
151: * Check the specifid constructor class and return its class instance.
152: * @exception EJBQLException if the specified constructor class could not
153: * be found.
154: */
155: private Class getConstructorClass(ParseTreeContext context) {
156: Object type = getType();
157: if (type == null) {
158: throw EJBQLException.constructorClassNotFound(context
159: .getQueryInfo(), getLine(), getColumn(), className);
160: }
161: return (Class) type;
162: }
163:
164: /**
165: * INTERNAL
166: */
167: private boolean selectingRelationshipField(Node node,
168: GenerationContext context) {
169: if ((node == null) || !node.isDotNode()) {
170: return false;
171: }
172: return !((DotNode) node).endsWithDirectToField(context);
173: }
174:
175: /**
176: * INTERNAL
177: * Get the string representation of this node.
178: */
179: public String getAsString() {
180: StringBuffer repr = new StringBuffer();
181: repr.append("NEW ").append(className);
182: repr.append("(");
183: for (Iterator i = constructorItems.iterator(); i.hasNext();) {
184: Node node = (Node) i.next();
185: repr.append(node.getAsString());
186: if (i.hasNext()) {
187: repr.append(", ");
188: }
189: }
190: repr.append(")");
191: return repr.toString();
192: }
193: }
|