KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 // Java imports
26
import java.util.*;
27
28 // TopLink imports
29
import oracle.toplink.essentials.expressions.*;
30 import oracle.toplink.essentials.queryframework.ReportQuery;
31 import oracle.toplink.essentials.exceptions.EJBQLException;
32
33 /**
34  * INTERNAL
35  * <p><b>Purpose</b>: Represent an IN in EJBQL
36  * <p><b>Responsibilities</b>:<ul>
37  * <li> Generate the correct expression for an IN
38  * </ul>
39  * @author Jon Driscoll and Joel Lucuik
40  * @since TopLink 4.0
41  */

42 public class InNode extends SimpleConditionalExpressionNode {
43
44     private List theObjects = null;
45
46     //Was NOT indicated? "WHERE emp.lastName NOT IN (...)
47
private boolean notIndicated = false;
48
49     /**
50      * InNode constructor comment.
51      */

52     public InNode() {
53         super();
54     }
55
56     /**
57      * INTERNAL
58      * Add the passed node value to the collection of object for this node
59      */

60     public void addNodeToTheObjects(Node theNode) {
61         getTheObjects().add(theNode);
62     }
63
64     /**
65      * INTERNAL
66      * Validate the current node and calculates its type.
67      */

68     public void validate(ParseTreeContext context) {
69         Object JavaDoc leftType = null;
70         TypeHelper typeHelper = context.getTypeHelper();
71
72         if (left != null) {
73             left.validate(context);
74             leftType = left.getType();
75         }
76         for (Iterator i = getTheObjects().iterator(); i.hasNext();) {
77             Node node = (Node)i.next();
78             node.validate(context);
79             node.validateParameter(context, leftType);
80             Object JavaDoc nodeType = node.getType();
81             if ((leftType != null) && !typeHelper.isAssignableFrom(leftType, nodeType))
82                 throw EJBQLException.invalidExpressionArgument("IN", node.getAsString(), typeHelper.getTypeName(leftType));
83         }
84
85         setType(typeHelper.getBooleanType());
86     }
87
88     /**
89      * INTERNAL
90      * Return the TopLink expression for this node
91      */

92     public Expression generateExpression(GenerationContext context) {
93         Expression whereClause = getLeft().generateExpression(context);
94         List arguments = getTheObjects();
95         Node firstArg = (Node)arguments.get(0);
96         if (firstArg.isSubqueryNode()) {
97             SubqueryNode subqueryNode = (SubqueryNode)firstArg;
98             ReportQuery reportQuery = subqueryNode.getReportQuery(context);
99             if (notIndicated()) {
100                 whereClause = whereClause.notIn(reportQuery);
101             }
102             else {
103                 whereClause = whereClause.in(reportQuery);
104             }
105         }
106         else {
107             Vector inArguments = new Vector(arguments.size());
108             for (Iterator iter = arguments.iterator(); iter.hasNext();) {
109                 Node nextNode = (Node)iter.next();
110                 inArguments.add(nextNode.generateExpression(context));
111             }
112             if (inArguments.size() > 0) {
113                 if (notIndicated()) {
114                     whereClause = whereClause.notIn(inArguments);
115                 } else {
116                     whereClause = whereClause.in(inArguments);
117                 }
118             }
119         }
120         return whereClause;
121     }
122
123     /**
124      * INTERNAL
125      * Return the collection of the objects used as parameters for this node
126      */

127     public List getTheObjects() {
128         if (theObjects == null) {
129             setTheObjects(new Vector());
130         }
131         return theObjects;
132     }
133
134     /**
135      * INTERNAL
136      * Set this node's object collection to the passed value
137      */

138     public void setTheObjects(List newTheObjects) {
139         theObjects = newTheObjects;
140     }
141
142     /**
143      * INTERNAL
144      * Indicate if a NOT was found in the WHERE clause.
145      * Examples:
146      * ...WHERE ... NOT IN(...)
147      */

148     public void indicateNot() {
149         notIndicated = true;
150     }
151
152     public boolean notIndicated() {
153         return notIndicated;
154     }
155 }
156
Popular Tags