KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > connection > DataUtilities


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Oct 25, 2005
14  * @author wseyler
15  */

16 package org.pentaho.core.connection;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.sql.Date JavaDoc;
20 import java.sql.Timestamp JavaDoc;
21
22 import org.dom4j.Document;
23 import org.dom4j.dom.DOMDocumentFactory;
24 import org.dom4j.io.OutputFormat;
25 import org.dom4j.io.XMLWriter;
26
27 public class DataUtilities implements IPentahoDataTypes {
28     public DataUtilities() {
29         super();
30         // TODO Auto-generated constructor stub
31
}
32
33     /**
34      * Pivots a 2D array such that rows become columns and columns become rows. The source array is not modified.
35      *
36      * @param source
37      * 2D array to pivot
38      * @return a 2D array that represents the pivoted source
39      *
40      * ie source: |Data0,0 |Data0,1 |Data0,2 |Data 0,3 | |Data1,0 |Data1,1 |Data1,2 |Data 1,3 | |Data2,0 |Data2,1 |Data2,2 |Data 2,3 | |Data3,0 |Data3,1 |Data3,2 |Data 3,3 | |Data4,0 |Data4,1 |Data4,2 |Data 4,3 | |Data5,0 |Data5,1 |Data5,2
41      * |Data 5,3 |
42      *
43      * becomes: |Data0,0 |Data1,0 |Data2,0 |Data3,0 |Data4,0 |Data5,0 | |Data0,1 |Data1,1 |Data2,1 |Data3,1 |Data4,1 |Data5,1 | |Data0,2 |Data1,2 |Data2,2 |Data3,2 |Data4,2 |Data5,2 | |Data0,3 |Data1,3 |Data2,3 |Data3,3 |Data4,3 |Data5,3 |
44      */

45     public static Object JavaDoc[][] pivotDimensions(Object JavaDoc[][] source) {
46         Object JavaDoc[][] result = null;
47         if (source.length > 0) {
48             result = new Object JavaDoc[source[0].length][source.length];
49             for (int row = 0; row < source.length; row++) {
50                 for (int column = 0; column < source[row].length; column++) {
51                     result[column][row] = source[row][column];
52                 }
53             }
54         } else {
55             result = source;
56         }
57         return result;
58     }
59
60     /**
61      * Selects rows from a 2d array and returns a 2d array that contains only those rows
62      *
63      * @param data
64      * 2D data to filter
65      * @param rowsToInclude
66      * Interger array of rows to include in results
67      * @return 2D array of data filtered by rows
68      */

69     public static Object JavaDoc[][] filterDataByRows(Object JavaDoc[][] data, Integer JavaDoc[] rowsToInclude) {
70         if (rowsToInclude == null) { // none to select so just return the
71
// data
72
return data;
73         }
74         Object JavaDoc[][] result = new Object JavaDoc[rowsToInclude.length][data[0].length];
75         for (int row = 0; row < rowsToInclude.length; row++) {
76             Object JavaDoc[] rowHeaderRow = data[rowsToInclude[row].intValue()];
77             for (int column = 0; column < rowHeaderRow.length; column++) {
78                 result[row][column] = rowHeaderRow[column];
79             }
80         }
81         return result;
82     }
83
84     /**
85      * Selects columns from a 2d array and returns a 2d array that contains only those columns
86      *
87      * @param data
88      * 2D data to filter
89      * @param columnsToInclude
90      * Integer array of rows to include in results
91      * @return 2D array of data filtered by columns
92      */

93     public static Object JavaDoc[][] filterDataByColumns(Object JavaDoc[][] data, Integer JavaDoc[] columnsToInclude) {
94         if (columnsToInclude == null) {
95             return data;
96         }
97         Object JavaDoc[][] result = new Object JavaDoc[data.length][columnsToInclude.length];
98         for (int column = 0; column < columnsToInclude.length; column++) {
99             for (int row = 0; row < data.length; row++) {
100                 result[row][column] = data[row][columnsToInclude[column].intValue()];
101             }
102         }
103         return result;
104     }
105
106     /**
107      * Filters the data such that only the rowsToInclude and columnsToInclude are in the dataset
108      *
109      * @param data -
110      * 2D data to filter
111      * @param rowsToInclude -
112      * Integer array of row numbers to include a null value indicates all rows
113      * @param columnsToInclude
114      * Integer array of column numbers to include a null values indicates all columns
115      * @return - 2D array of only those rows and columns specified
116      */

117     public static Object JavaDoc[][] filterData(Object JavaDoc[][] data, Integer JavaDoc[] rowsToInclude, Integer JavaDoc[] columnsToInclude) {
118         Object JavaDoc[][] result = filterDataByColumns(data, columnsToInclude);
119         result = filterDataByRows(result, rowsToInclude);
120         return result;
121     }
122
123     /**
124      * Get an XML representation of the resultset
125      *
126      * @return String containing XML which represents the data (eg for use with xquery)
127      */

128     public static String JavaDoc getXMLString(IPentahoResultSet resultSet) {
129         Document document = DOMDocumentFactory.getInstance().createDocument();
130         org.dom4j.Element resultSetNode = document.addElement("result-set"); //$NON-NLS-1$
131
Object JavaDoc[] colHeaders = resultSet.getMetaData().getColumnHeaders()[0];
132         String JavaDoc metaDataStr = ""; //$NON-NLS-1$
133
Object JavaDoc firstDataRow[] = resultSet.getDataRow(0);
134         for (int i = 0; i < firstDataRow.length; i++) {
135             metaDataStr += firstDataRow[i].getClass().getName() + (i == firstDataRow.length - 1 ? "" : ","); //$NON-NLS-1$//$NON-NLS-2$
136
}
137         resultSetNode.addComment(metaDataStr);
138         int rowCount = resultSet.getRowCount();
139         for (int i = 0; i < rowCount; i++) {
140             Object JavaDoc row[] = resultSet.getDataRow(i);
141             org.dom4j.Element rowNode = resultSetNode.addElement("row"); //$NON-NLS-1$
142
for (int j = 0; j < row.length; j++) {
143                 String JavaDoc column = colHeaders[j].toString();
144                 String JavaDoc value = row[j] != null ? row[j].toString() : ""; //$NON-NLS-1$
145
if (row[j] instanceof Timestamp JavaDoc || row[j] instanceof Date JavaDoc) {
146                     value = String.valueOf(((Timestamp JavaDoc) row[j]).getTime());
147                 }
148                 org.dom4j.Element dataNode = rowNode.addElement(column);
149                 dataNode.setText(value);
150             }
151         }
152         OutputFormat format = OutputFormat.createPrettyPrint();
153         ByteArrayOutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc();
154         try {
155             XMLWriter writer = new XMLWriter(outStream, format);
156             writer.write(document);
157             return outStream.toString();
158         } catch (Exception JavaDoc e) {
159             // fall through and try to return the xml unformatted
160
}
161         return document.asXML();
162     }
163 }
164
Popular Tags