KickJava   Java API By Example, From Geeks To Geeks.

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


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, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.parsing;
23
24 import oracle.toplink.essentials.expressions.*;
25 import oracle.toplink.essentials.queryframework.*;
26 import java.util.*;
27
28 /**
29  * INTERNAL:
30  * This node holds a list of all the updates that will occur in an Update Query.
31  * Slightly differnt from other nodes since holds more than two children in a list.
32  */

33 public class SetNode extends MajorNode {
34     private List assignmentNodes = null;
35
36     public SetNode() {
37         super();
38         assignmentNodes = new Vector();
39     }
40
41     /**
42      * Iterate through the updates in this query and build expressions for them. Set the
43      * built expressions on the query.
44      */

45     public void addUpdatesToQuery(UpdateAllQuery theQuery, GenerationContext context) {
46         Iterator iterator = assignmentNodes.iterator();
47         while (iterator.hasNext()) {
48             EqualsAssignmentNode node = (EqualsAssignmentNode)iterator.next();
49             Expression leftExpression = getExpressionForNode(node.getLeft(), theQuery.getReferenceClass(), context);
50             Expression rightExpression = getExpressionForNode(node.getRight(), theQuery.getReferenceClass(), context);
51             theQuery.addUpdate(leftExpression, rightExpression);
52         }
53     }
54
55     /**
56      * INTERNAL
57      * Validate node.
58      */

59     public void validate(ParseTreeContext context) {
60         for (Iterator i = assignmentNodes.iterator(); i.hasNext(); ) {
61             Node item = (Node)i.next();
62             item.validate(context);
63         }
64     }
65
66     /**
67      * Create an expression to represent one of the nodes on a SetToNode.
68      * We will assume that set_to nodes change elements that are direct mappings on the reference
69      * class of the query.
70      */

71     protected Expression getExpressionForNode(Node node, Class JavaDoc referenceClass, GenerationContext context) {
72         Expression expression = null;
73         if (node.isAttributeNode()) {
74             // look up a preexisting expression based on the reference class of the query.
75
String JavaDoc classVariable = context.getParseTreeContext().getVariableNameForClass(referenceClass, context);
76             expression = context.expressionFor(classVariable);
77             if (expression == null) {
78                 expression = new ExpressionBuilder();
79                 context.addExpression(expression, classVariable);
80             }
81             expression = node.addToExpression(expression, context);
82         } else {
83             expression = node.generateExpression(context);
84         }
85         return expression;
86     }
87
88     /**
89      * INTERNAL
90      */

91     public void setAssignmentNodes(List nodes) {
92         assignmentNodes = nodes;
93     }
94
95 }
96
Popular Tags