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: import oracle.toplink.essentials.expressions.*;
040: import oracle.toplink.essentials.queryframework.*;
041: import java.util.*;
042:
043: /**
044: * INTERNAL:
045: * This node holds a list of all the updates that will occur in an Update Query.
046: * Slightly differnt from other nodes since holds more than two children in a list.
047: */
048: public class SetNode extends MajorNode {
049: private List assignmentNodes = null;
050:
051: public SetNode() {
052: super ();
053: assignmentNodes = new Vector();
054: }
055:
056: /**
057: * Iterate through the updates in this query and build expressions for them. Set the
058: * built expressions on the query.
059: */
060: public void addUpdatesToQuery(UpdateAllQuery theQuery,
061: GenerationContext context) {
062: Iterator iterator = assignmentNodes.iterator();
063: while (iterator.hasNext()) {
064: EqualsAssignmentNode node = (EqualsAssignmentNode) iterator
065: .next();
066: Expression leftExpression = getExpressionForNode(node
067: .getLeft(), theQuery.getReferenceClass(), context);
068: Expression rightExpression = getExpressionForNode(node
069: .getRight(), theQuery.getReferenceClass(), context);
070: theQuery.addUpdate(leftExpression, rightExpression);
071: }
072: }
073:
074: /**
075: * INTERNAL
076: * Check the update item node for a path expression starting with a
077: * unqualified field access and if so, replace it by a qualified field
078: * access.
079: */
080: public Node qualifyAttributeAccess(ParseTreeContext context) {
081: for (Iterator i = assignmentNodes.iterator(); i.hasNext();) {
082: Node item = (Node) i.next();
083: item.qualifyAttributeAccess(context);
084: }
085: return this ;
086: }
087:
088: /**
089: * INTERNAL
090: * Validate node.
091: */
092: public void validate(ParseTreeContext context) {
093: for (Iterator i = assignmentNodes.iterator(); i.hasNext();) {
094: Node item = (Node) i.next();
095: item.validate(context);
096: }
097: }
098:
099: /**
100: * Create an expression to represent one of the nodes on a SetToNode.
101: * We will assume that set_to nodes change elements that are direct mappings on the reference
102: * class of the query.
103: */
104: protected Expression getExpressionForNode(Node node,
105: Class referenceClass, GenerationContext context) {
106: Expression expression = null;
107: if (node.isAttributeNode()) {
108: // look up a preexisting expression based on the reference class of the query.
109: String classVariable = context.getParseTreeContext()
110: .getVariableNameForClass(referenceClass, context);
111: expression = context.expressionFor(classVariable);
112: if (expression == null) {
113: expression = new ExpressionBuilder();
114: context.addExpression(expression, classVariable);
115: }
116: expression = node.addToExpression(expression, context);
117: } else {
118: expression = node.generateExpression(context);
119: }
120: return expression;
121: }
122:
123: /**
124: * INTERNAL
125: */
126: public void setAssignmentNodes(List nodes) {
127: assignmentNodes = nodes;
128: }
129:
130: }
|