001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * WizardFieldObject.java
028: *
029: * Created on January 27, 2006, 3:42 PM
030: *
031: */
032:
033: package it.businesslogic.ireport.crosstab.gui;
034:
035: import it.businesslogic.ireport.JRField;
036: import it.businesslogic.ireport.JRParameter;
037: import it.businesslogic.ireport.JRVariable;
038: import java.util.Vector;
039:
040: /**
041: *
042: * @author gtoffoli
043: */
044: public class WizardFieldObject {
045:
046: private Object obj = null;
047:
048: /** Creates a new instance of WizardFieldObject */
049: public WizardFieldObject(Object obj) {
050:
051: this .obj = obj;
052:
053: }
054:
055: public Vector getGroupByValues() {
056: String classtype = "java.lang.String";
057: Vector groupByValues = new Vector();
058:
059: groupByValues.add("Unique");
060:
061: if (obj instanceof it.businesslogic.ireport.JRField) {
062: classtype = ((JRField) obj).getClassType();
063: } else if (obj instanceof it.businesslogic.ireport.JRParameter) {
064: classtype = ((JRParameter) obj).getClassType();
065: } else if (obj instanceof it.businesslogic.ireport.JRVariable) {
066: classtype = ((JRVariable) obj).getClassType();
067: }
068:
069: try {
070: Class clazz = this .getClass().forName(classtype);
071: if (java.util.Date.class.isAssignableFrom(clazz)) {
072: groupByValues.add("Year");
073: groupByValues.add("Month");
074: groupByValues.add("Week");
075: groupByValues.add("Day");
076: }
077: } catch (Exception ex) {
078: }
079:
080: return groupByValues;
081: }
082:
083: public Vector getFunctions() {
084: String classtype = "java.lang.String";
085: Vector functions = new Vector();
086:
087: functions.add("Count");
088:
089: if (obj instanceof it.businesslogic.ireport.JRField) {
090: classtype = ((JRField) obj).getClassType();
091: } else if (obj instanceof it.businesslogic.ireport.JRParameter) {
092: classtype = ((JRParameter) obj).getClassType();
093: } else if (obj instanceof it.businesslogic.ireport.JRVariable) {
094: classtype = ((JRVariable) obj).getClassType();
095: }
096:
097: try {
098: Class clazz = this .getClass().forName(classtype);
099:
100: if (java.lang.Number.class.isAssignableFrom(clazz)) {
101: functions.add("Sum");
102: functions.add("Count");
103: functions.add("DistinctCount");
104: functions.add("Average");
105: functions.add("StandardDeviation");
106: functions.add("Variance");
107: }
108:
109: if (java.util.Date.class.isAssignableFrom(clazz)
110: || java.lang.Number.class.isAssignableFrom(clazz)) {
111: functions.add("Lowest");
112: functions.add("Highest");
113: }
114:
115: } catch (Exception ex) {
116: }
117:
118: functions.add("First");
119: functions.add("Nothing");
120:
121: return functions;
122: }
123:
124: public String toString() {
125: if (obj instanceof it.businesslogic.ireport.JRField) {
126: return ((JRField) obj).getName() + " (field)";
127: } else if (obj instanceof it.businesslogic.ireport.JRParameter) {
128: return ((JRParameter) obj).getName() + " (parameter)";
129: } else if (obj instanceof it.businesslogic.ireport.JRVariable) {
130: return ((JRVariable) obj).getName() + " (variable)";
131: }
132:
133: return "" + obj;
134: }
135:
136: public String getName() {
137: if (obj instanceof it.businesslogic.ireport.JRField) {
138: return ((JRField) obj).getName();
139: } else if (obj instanceof it.businesslogic.ireport.JRParameter) {
140: return ((JRParameter) obj).getName();
141: } else if (obj instanceof it.businesslogic.ireport.JRVariable) {
142: return ((JRVariable) obj).getName();
143: }
144:
145: return "" + obj;
146: }
147:
148: public String getExpression(String groupByType) {
149: String expression = "";
150:
151: if (obj instanceof it.businesslogic.ireport.JRField) {
152: expression = "$F{" + ((JRField) obj).getName() + "}";
153: } else if (obj instanceof it.businesslogic.ireport.JRParameter) {
154: expression = "$P{" + ((JRParameter) obj).getName() + "}";
155: } else if (obj instanceof it.businesslogic.ireport.JRVariable) {
156: expression = "$V{" + ((JRVariable) obj).getName() + "}";
157: }
158:
159: if (groupByType.equals("Year")) {
160: return "(new SimpleDateFormat(\"yyyy\")).format("
161: + expression + ")";
162: } else if (groupByType.equals("Month")) {
163: return "(new SimpleDateFormat(\"yyyy-MM\")).format("
164: + expression + ")";
165: } else if (groupByType.equals("Week")) {
166: return "(new SimpleDateFormat(\"yyyy-ww\")).format("
167: + expression + ")";
168: } else if (groupByType.equals("Day")) {
169: return "(new SimpleDateFormat(\"yyyy-MM-dd\")).format("
170: + expression + ")";
171: }
172:
173: return expression;
174: }
175:
176: public String getExpressionClass(String groupByType) {
177: if (groupByType != null && !groupByType.equals("Unique")) {
178: return "java.lang.String";
179: }
180:
181: if (obj instanceof it.businesslogic.ireport.JRField) {
182: return ((JRField) obj).getClassType();
183: } else if (obj instanceof it.businesslogic.ireport.JRParameter) {
184: return ((JRParameter) obj).getClassType();
185: } else if (obj instanceof it.businesslogic.ireport.JRVariable) {
186: return ((JRVariable) obj).getClassType();
187: }
188:
189: return "java.lang.String";
190: }
191: }
|