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: // TopLink imports
043: import oracle.toplink.essentials.expressions.*;
044: import oracle.toplink.essentials.queryframework.ReportQuery;
045: import oracle.toplink.essentials.exceptions.EJBQLException;
046:
047: /**
048: * INTERNAL
049: * <p><b>Purpose</b>: Represent an IN in EJBQL
050: * <p><b>Responsibilities</b>:<ul>
051: * <li> Generate the correct expression for an IN
052: * </ul>
053: * @author Jon Driscoll and Joel Lucuik
054: * @since TopLink 4.0
055: */
056: public class InNode extends SimpleConditionalExpressionNode {
057:
058: private List theObjects = null;
059:
060: //Was NOT indicated? "WHERE emp.lastName NOT IN (...)
061: private boolean notIndicated = false;
062:
063: /**
064: * InNode constructor comment.
065: */
066: public InNode() {
067: super ();
068: }
069:
070: /**
071: * INTERNAL
072: * Add the passed node value to the collection of object for this node
073: */
074: public void addNodeToTheObjects(Node theNode) {
075: getTheObjects().add(theNode);
076: }
077:
078: /**
079: * INTERNAL
080: * Validate the current node and calculates its type.
081: */
082: public void validate(ParseTreeContext context) {
083: Object leftType = null;
084: TypeHelper typeHelper = context.getTypeHelper();
085:
086: if (left != null) {
087: left.validate(context);
088: leftType = left.getType();
089: }
090: for (Iterator i = getTheObjects().iterator(); i.hasNext();) {
091: Node node = (Node) i.next();
092: node.validate(context);
093: node.validateParameter(context, leftType);
094: Object nodeType = node.getType();
095: if ((leftType != null)
096: && !typeHelper.isAssignableFrom(leftType, nodeType))
097: throw EJBQLException.invalidExpressionArgument(context
098: .getQueryInfo(), node.getLine(), node
099: .getColumn(), "IN", node.getAsString(),
100: typeHelper.getTypeName(leftType));
101: }
102:
103: setType(typeHelper.getBooleanType());
104: }
105:
106: /**
107: * INTERNAL
108: * Return the TopLink expression for this node
109: */
110: public Expression generateExpression(GenerationContext context) {
111: Expression whereClause = getLeft().generateExpression(context);
112: List arguments = getTheObjects();
113: Node firstArg = (Node) arguments.get(0);
114: if (firstArg.isSubqueryNode()) {
115: SubqueryNode subqueryNode = (SubqueryNode) firstArg;
116: ReportQuery reportQuery = subqueryNode
117: .getReportQuery(context);
118: if (notIndicated()) {
119: whereClause = whereClause.notIn(reportQuery);
120: } else {
121: whereClause = whereClause.in(reportQuery);
122: }
123: } else {
124: Vector inArguments = new Vector(arguments.size());
125: for (Iterator iter = arguments.iterator(); iter.hasNext();) {
126: Node nextNode = (Node) iter.next();
127: inArguments.add(nextNode.generateExpression(context));
128: }
129: if (inArguments.size() > 0) {
130: if (notIndicated()) {
131: whereClause = whereClause.notIn(inArguments);
132: } else {
133: whereClause = whereClause.in(inArguments);
134: }
135: }
136: }
137: return whereClause;
138: }
139:
140: /**
141: * INTERNAL
142: * Return the collection of the objects used as parameters for this node
143: */
144: public List getTheObjects() {
145: if (theObjects == null) {
146: setTheObjects(new Vector());
147: }
148: return theObjects;
149: }
150:
151: /**
152: * INTERNAL
153: * Set this node's object collection to the passed value
154: */
155: public void setTheObjects(List newTheObjects) {
156: theObjects = newTheObjects;
157: }
158:
159: /**
160: * INTERNAL
161: * Indicate if a NOT was found in the WHERE clause.
162: * Examples:
163: * ...WHERE ... NOT IN(...)
164: */
165: public void indicateNot() {
166: notIndicated = true;
167: }
168:
169: public boolean notIndicated() {
170: return notIndicated;
171: }
172: }
|