KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.math.BigDecimal JavaDoc;
25 import java.math.BigInteger JavaDoc;
26
27 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
28 import oracle.toplink.essentials.queryframework.ReportQuery;
29 import oracle.toplink.essentials.expressions.Expression;
30
31 /**
32  * INTERNAL
33  * <p><b>Purpose</b>: Model a SUM
34  * <p><b>Responsibilities</b>:<ul>
35  * <li> Apply itself to a query correctly
36  * </ul>
37  */

38 public class SumNode extends AggregateNode {
39
40     /**
41      * INTERNAL
42      * Apply this node to the passed query
43      */

44     public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) {
45         if (theQuery.isReportQuery()) {
46             ReportQuery reportQuery = (ReportQuery)theQuery;
47             reportQuery.addSum(resolveAttribute(),
48                                getArgumentExpression(context),
49                                calculateReturnType(context));
50             
51         }
52     }
53
54     /**
55      * INTERNAL
56      * Validate node and calculate its type.
57      */

58     public void validate(ParseTreeContext context) {
59         if (left != null) {
60             left.validate(context);
61             TypeHelper typeHelper = context.getTypeHelper();
62             setType(calculateReturnType(left.getType(), typeHelper));
63         }
64     }
65
66     /**
67      * INTERNAL
68      * Return a TopLink expression generated using the left node
69      */

70     public Expression generateExpression(GenerationContext context) {
71         return getArgumentExpression(context).sum();
72     }
73
74     /**
75      * INTERNAL
76      * This method calculates the return type of the SUM operation.
77      */

78     protected Class JavaDoc calculateReturnType(GenerationContext context) {
79         Class JavaDoc returnType = null;
80         if (getLeft().isDotNode()){
81             DotNode arg = (DotNode)getLeft();
82             Class JavaDoc fieldType = arg.getTypeOfDirectToField(context);
83             TypeHelper helper = context.getParseTreeContext().getTypeHelper();
84             returnType = (Class JavaDoc)calculateReturnType(fieldType, helper);
85         }
86         return returnType;
87     }
88
89     /**
90      * INTERNAL
91      * Helper method to calculate the return type of the SUM operation.
92      */

93     protected Object JavaDoc calculateReturnType(Object JavaDoc argType, TypeHelper helper) {
94         Object JavaDoc returnType = null;
95         if (helper.isIntegralType(argType)) {
96             returnType = helper.getLongClassType();
97         } else if (helper.isFloatingPointType(argType)) {
98             returnType = helper.getDoubleClassType();
99         } else if (helper.isBigIntegerType(argType)) {
100             returnType = helper.getBigIntegerType();
101         } else if (helper.isBigDecimalType(argType)) {
102             returnType = helper.getBigDecimalType();
103         }
104         return returnType;
105     }
106
107     /**
108      * INTERNAL
109      * Get the string representation of this node.
110      */

111     public String JavaDoc getAsString() {
112         return "SUM(" + left.getAsString() + ")";
113     }
114 }
115
Popular Tags