001: package com.calipso.reportgenerator.common;
002:
003: import net.sf.jasperreports.engine.JRDataSource;
004: import net.sf.jasperreports.engine.JRException;
005: import net.sf.jasperreports.engine.JRField;
006:
007: import javax.swing.table.TableModel;
008: import javax.swing.table.DefaultTableModel;
009:
010: import com.calipso.reportgenerator.common.QueryMetric;
011: import com.calipso.reportgenerator.common.QueryDimension;
012:
013: import java.util.Vector;
014:
015: /**
016: *
017: * User: soliveri
018: * Date: Dec 16, 2003
019: * Time: 2:59:52 PM
020: *
021: */
022:
023: public abstract class ReportTableModel implements JRDataSource {
024:
025: private TableModel reportTableModel;
026: private Vector tableColumnNames;
027: private Vector tableData;
028: private int groupingDimCount;
029: private int nonGroupingDimCount;
030: private int commonMetricsCount;
031: private int accMetricsCount;
032: private int index = -1;
033: protected Vector occurrences;
034:
035: public ReportTableModel() {
036: }
037:
038: public boolean next() throws JRException {
039: index++;
040: return (index < reportTableModel.getRowCount());
041: }
042:
043: public Object getFieldValue(JRField jrField) throws JRException {
044: Object value = null;
045: int col = 0;
046: String fieldName = jrField.getName();
047:
048: for (int i = 0; i < tableColumnNames.size(); i++, col++) {
049: if (tableColumnNames.elementAt(i).toString().equals(
050: fieldName)) {
051: value = reportTableModel.getValueAt(index, col);
052: }
053: }
054: return value;
055: }
056:
057: protected void setTableColumnNames(Vector tableColumnNames) {
058: this .tableColumnNames = tableColumnNames;
059: }
060:
061: protected void setTableData(Vector tableData) {
062: this .tableData = tableData;
063: }
064:
065: /**
066: * Devuelve un vector que contiene los nombres de las columnas para el modelo
067: * @param groupingDimensions dimensiones que agrupan
068: * @param nonGroupingDimensions dimensiones que no agrupan
069: * @param metricsArray metricas
070: * @return nombres de las columnas para el modelo
071: */
072: protected Vector getTableModelColumns(Object[] groupingDimensions,
073: Object[] nonGroupingDimensions, Object[] metricsArray) {
074: Vector columnNames = new Vector();
075: for (int i = 0; i < groupingDimensions.length; i++) {
076: QueryDimension qd = (QueryDimension) groupingDimensions[i];
077: columnNames.add(qd.getName());
078: }
079: for (int i = 0; i < nonGroupingDimensions.length; i++) {
080: QueryDimension qd = (QueryDimension) nonGroupingDimensions[i];
081: columnNames.add(qd.getName());
082: }
083: for (int i = 0; i < metricsArray.length; i++) {
084: QueryMetric qm = (QueryMetric) metricsArray[i];
085: columnNames.add(qm.getName());
086: }
087: return columnNames;
088: }
089:
090: protected void newDefaultTableModel() {
091: reportTableModel = new DefaultTableModel(tableData,
092: tableColumnNames);
093: }
094:
095: protected void newNonDataTableModel() {
096: reportTableModel = new DefaultTableModel(new Vector(),
097: tableColumnNames);
098: }
099:
100: public TableModel getModel() {
101: return reportTableModel;
102: }
103:
104: public int getGroupingDimCount() {
105: return groupingDimCount;
106: }
107:
108: protected void setGroupingDimCount(int groupingDimCount) {
109: this .groupingDimCount = groupingDimCount;
110: }
111:
112: public int getNonGroupingDimCount() {
113: return nonGroupingDimCount;
114: }
115:
116: protected void setNonGroupingDimCount(int nonGroupingDimCount) {
117: this .nonGroupingDimCount = nonGroupingDimCount;
118: }
119:
120: public int getCommonMetricsCount() {
121: return commonMetricsCount;
122: }
123:
124: protected void setCommonMetricsCount(int commonMetricsCount) {
125: this .commonMetricsCount = commonMetricsCount;
126: }
127:
128: public int getAccMetricsCount() {
129: return accMetricsCount;
130: }
131:
132: protected void setAccMetricsCount(int accMetricsCount) {
133: this .accMetricsCount = accMetricsCount;
134: }
135:
136: public Vector getOccurrences() {
137: return occurrences;
138: }
139: }
|