KickJava   Java API By Example, From Geeks To Geeks.

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


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 // Java imports
25
import java.util.*;
26
27 // TopLink Imports
28
import oracle.toplink.essentials.exceptions.EJBQLException;
29 import oracle.toplink.essentials.queryframework.ReportQuery;
30 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
31
32 /**
33  * INTERNAL
34  * <p><b>Purpose</b>: Represent an GROUP BY
35  * <p><b>Responsibilities</b>:<ul>
36  * <li> Generate the correct expression for an GROUP BY
37  * </ul>
38  */

39 public class GroupByNode extends MajorNode {
40
41     List groupByItems = null;
42
43     /**
44      * Return a new GroupByNode.
45      */

46     public GroupByNode() {
47         super();
48     }
49
50     /**
51      * INTERNAL
52      * Validate the current node.
53      */

54     public void validate(ParseTreeContext context, SelectNode selectNode) {
55         for (Iterator i = groupByItems.iterator(); i.hasNext(); ) {
56             Node item = (Node)i.next();
57             item.validate(context);
58         }
59
60         List selectExprs = selectNode.getSelectExpressions();
61         // check select expressions
62
for (Iterator i = selectExprs.iterator(); i.hasNext(); ) {
63             Node selectExpr = (Node)i.next();
64             if (!isValidSelectExpr(selectExpr)) {
65                 throw EJBQLException.invalidSelectForGroupByQuery(
66                     selectExpr.getAsString(), getAsString());
67             }
68         }
69     }
70
71     /**
72      * INTERNAL
73      * Add an Group By Item to this node
74      */

75     private void addGroupByItem(Object JavaDoc theNode) {
76         getGroupByItems().add(theNode);
77     }
78
79     /**
80      * INTERNAL
81      * Add the grouping expressions to the passed query
82      */

83     public void addGroupingToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) {
84         if (theQuery.isReportQuery()) {
85             Iterator iter = getGroupByItems().iterator();
86             while (iter.hasNext()) {
87                 Node nextNode = (Node)iter.next();
88                 ((ReportQuery)theQuery).addGrouping(nextNode.generateExpression(context));
89             }
90         }
91     }
92
93     /**
94      * INTERNAL
95      * Returns true if the sp
96      */

97     public boolean isValidHavingExpr(Node expr) {
98         if (expr.isDotNode() || expr.isVariableNode()) {
99             return isGroupbyItem(expr);
100         } else {
101             // delegate to child node if any
102
Node left = expr.getLeft();
103             Node right = expr.getRight();
104             return ((left == null) || isValidHavingExpr(left)) &&
105                 ((right == null) || isValidHavingExpr(right));
106         }
107     }
108
109     /**
110      * INTERNAL
111      * Returns true if the specified expr is a valid SELECT clause expression.
112      */

113     private boolean isValidSelectExpr(Node expr) {
114         if (expr.isAggregateNode()) {
115             return true;
116         } else if (expr.isConstructorNode()) {
117             List args = ((ConstructorNode)expr).getConstructorItems();
118             for (Iterator i = args.iterator(); i.hasNext(); ) {
119                 Node arg = (Node)i.next();
120                 if (!isValidSelectExpr(arg)) {
121                     return false;
122                 }
123             }
124             return true;
125         }
126         return isGroupbyItem(expr);
127     }
128
129     /**
130      * INTERNAL
131      * Return true if the specified expr is a groupby item.
132      */

133     private boolean isGroupbyItem(Node expr) {
134         if (expr.isDotNode() || expr.isVariableNode()) {
135             String JavaDoc exprRepr = expr.getAsString();
136             for (Iterator i = groupByItems.iterator(); i.hasNext();) {
137                 Node item = (Node)i.next();
138                 String JavaDoc itemRepr = item.getAsString();
139                 if (exprRepr.equals(itemRepr)) {
140                     return true;
141                 }
142             }
143         }
144         return false;
145     }
146
147     /**
148      * INTERNAL
149      * Return the GROUP BY statements
150      */

151     public List getGroupByItems() {
152         if (groupByItems == null) {
153             setGroupByItems(new Vector());
154         }
155         return groupByItems;
156     }
157
158     /**
159      * INTERNAL
160      * Set the GROUP BY statements
161      */

162     public void setGroupByItems(List newItems) {
163         groupByItems = newItems;
164     }
165
166     /**
167      * INTERNAL
168      * Get the string representation of this node.
169      */

170     public String JavaDoc getAsString() {
171         StringBuffer JavaDoc repr = new StringBuffer JavaDoc();
172         for (Iterator i = groupByItems.iterator(); i.hasNext(); ) {
173             Node expr = (Node)i.next();
174             if (repr.length() > 0) {
175                 repr.append(", ");
176             }
177             repr.append(expr.getAsString());
178         }
179         return "GROUP BY " + repr.toString();
180     }
181     
182 }
183
Popular Tags