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 java.math.BigDecimal;
040: import java.math.BigInteger;
041:
042: import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
043: import oracle.toplink.essentials.queryframework.ReportQuery;
044: import oracle.toplink.essentials.expressions.Expression;
045:
046: /**
047: * INTERNAL
048: * <p><b>Purpose</b>: Model a SUM
049: * <p><b>Responsibilities</b>:<ul>
050: * <li> Apply itself to a query correctly
051: * </ul>
052: */
053: public class SumNode extends AggregateNode {
054:
055: /**
056: * INTERNAL
057: * Apply this node to the passed query
058: */
059: public void applyToQuery(ObjectLevelReadQuery theQuery,
060: GenerationContext context) {
061: if (theQuery.isReportQuery()) {
062: ReportQuery reportQuery = (ReportQuery) theQuery;
063: reportQuery.addAttribute(resolveAttribute(),
064: generateExpression(context),
065: calculateReturnType(context));
066:
067: }
068: }
069:
070: /**
071: * INTERNAL
072: * Validate node and calculate its type.
073: */
074: public void validate(ParseTreeContext context) {
075: if (left != null) {
076: left.validate(context);
077: TypeHelper typeHelper = context.getTypeHelper();
078: setType(calculateReturnType(left.getType(), typeHelper));
079: }
080: }
081:
082: /**
083: * INTERNAL
084: */
085: protected Expression addAggregateExression(Expression expr) {
086: return expr.sum();
087: }
088:
089: /**
090: * INTERNAL
091: * This method calculates the return type of the SUM operation.
092: */
093: protected Class calculateReturnType(GenerationContext context) {
094: Class returnType = null;
095: if (getLeft().isDotNode()) {
096: DotNode arg = (DotNode) getLeft();
097: Class fieldType = arg.getTypeOfDirectToField(context);
098: TypeHelper helper = context.getParseTreeContext()
099: .getTypeHelper();
100: returnType = (Class) calculateReturnType(fieldType, helper);
101: }
102: return returnType;
103: }
104:
105: /**
106: * INTERNAL
107: * Helper method to calculate the return type of the SUM operation.
108: */
109: protected Object calculateReturnType(Object argType,
110: TypeHelper helper) {
111: Object returnType = null;
112: if (helper.isIntegralType(argType)) {
113: returnType = helper.getLongClassType();
114: } else if (helper.isFloatingPointType(argType)) {
115: returnType = helper.getDoubleClassType();
116: } else if (helper.isBigIntegerType(argType)) {
117: returnType = helper.getBigIntegerType();
118: } else if (helper.isBigDecimalType(argType)) {
119: returnType = helper.getBigDecimalType();
120: }
121: return returnType;
122: }
123:
124: /**
125: * INTERNAL
126: * Get the string representation of this node.
127: */
128: public String getAsString() {
129: return "SUM(" + left.getAsString() + ")";
130: }
131: }
|