KickJava   Java API By Example, From Geeks To Geeks.

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


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  * INTERNAL
26  * <p><b>Purpose</b>: Represent the MEMBER-OF operator
27  * <p><b>Responsibilities</b>:<ul>
28  * <li> MEMBER OF is not supported.
29  * </ul>
30  * @author Jon Driscoll and Joel Lucuik
31  * @since since July 2003
32  */

33 import oracle.toplink.essentials.expressions.*;
34
35 public class MemberOfNode extends BinaryOperatorNode {
36     private boolean notIndicated = false;
37
38     //If we're dealing with a NOT, we store the expression for the left.
39
//When we get to the one-to-many on the right, it will handle the noneOf using
40
//the receiver stored in the context.
41
//(i.e. secondLastRightExpression.noneOf(lastRightVariable, leftExpression)
42
private Expression leftExpression = null;
43
44     /**
45      * Return a new MemberOfNode
46      */

47     public MemberOfNode() {
48         super();
49     }
50
51     /**
52      * INTERNAL makeNodeOneToMany:
53      * Traverse to the leaf on theNode and mark as one to many
54      */

55     public void makeNodeOneToMany(Node theNode) {
56         Node currentNode = theNode;
57         do {
58             if (!currentNode.hasRight()) {
59                 ((AttributeNode)currentNode).setRequiresCollectionAttribute(true);
60                 return;
61             }
62             currentNode = currentNode.getRight();
63         } while (true);
64     }
65
66     /**
67      * INTERNAL
68      * Validate node and calculates its type.
69      */

70     public void validate(ParseTreeContext context) {
71         super.validate(context);
72         Node left = getLeft();
73         if (left.isVariableNode() && ((VariableNode)left).isAlias(context)) {
74             context.usedVariable(((VariableNode)left).getCanonicalVariableName());
75         }
76         left.validateParameter(context, right.getType());
77         TypeHelper typeHelper = context.getTypeHelper();
78         setType(typeHelper.getBooleanType());
79     }
80     
81     public Expression generateExpression(GenerationContext context) {
82         //BUG 3107061 - Handle if left node is declared in FROM
83
//i.e. "SELECT OBJECT(emp) FROM Employee emp, SmallProject s", and the left
84
//node is the "s"
85
if (getLeft().isVariableNode() && ((VariableNode)getLeft()).isAlias(context)) {
86             VariableNode leftVariableNode = (VariableNode)getLeft();
87             makeNodeOneToMany(getRight());
88             return getRight().generateExpression(context).equal(new ExpressionBuilder(leftVariableNode.resolveClass(context)));
89         }
90
91         // Need to make sure one of the node is marked as a one to many
92
if (getRight().isParameterNode()) {
93             makeNodeOneToMany(getLeft());
94         } else {
95             makeNodeOneToMany(getRight());
96         }
97
98         //Handle NOT. Store the expression for the left, let VariableNode handle it.
99
if (notIndicated()) {
100             Expression resultFromRight = null;
101             context.setMemberOfNode(this);
102             this.setLeftExpression(getLeft().generateExpression(context));
103             resultFromRight = getRight().generateExpression(context);
104             //clean up
105
context.setMemberOfNode(null);
106             this.setLeftExpression(null);
107             return resultFromRight;
108         } else {
109             //otherwise, handle like normal anyOf()
110
return getRight().generateExpression(context).equal(getLeft().generateExpression(context));
111         }
112     }
113
114     /**
115      * INTERNAL
116      * Indicate if a NOT was found in the WHERE clause.
117      * Examples:
118      * ...WHERE ... NOT MEMBER OF
119      */

120     public void indicateNot() {
121         notIndicated = true;
122     }
123
124     public boolean notIndicated() {
125         return notIndicated;
126     }
127
128     //set and get the leftExpression. This is for NOT MEMBER OF.
129
public void setLeftExpression(Expression newLeftExpression) {
130         leftExpression = newLeftExpression;
131     }
132
133     public Expression getLeftExpression() {
134         return leftExpression;
135     }
136 }
137
Popular Tags