KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > data > DataGrid


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 Sep 23, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.data;
18
19 import java.util.HashMap JavaDoc;
20
21 import org.dom4j.Document;
22 import org.dom4j.DocumentHelper;
23 import org.dom4j.Element;
24 import org.pentaho.core.connection.IPentahoMetaData;
25 import org.pentaho.core.connection.IPentahoResultSet;
26 import org.pentaho.messages.util.LocaleHelper;
27
28 public class DataGrid {
29
30     public static final int STYLE_ROWS = 1;
31
32     public static final int STYLE_TREE = 2;
33
34     Document gridDocument;
35
36     int documentStyle;
37
38     public DataGrid(int documentStyle) {
39         this.documentStyle = documentStyle;
40     }
41
42     public Document getDataDocument() {
43         return gridDocument;
44     }
45
46     public int getDocumentStyle() {
47         return documentStyle;
48     }
49
50     public void populate(IPentahoResultSet results) {
51
52         switch (documentStyle) {
53         case STYLE_ROWS:
54             populateRowStyle(results);
55         }
56
57     }
58
59     protected void populateRowStyle(IPentahoResultSet results) {
60         gridDocument = DocumentHelper.createDocument();
61         gridDocument.setXMLEncoding(LocaleHelper.getSystemEncoding());
62         Element root = gridDocument.addElement("datagrid"); //$NON-NLS-1$
63

64         // add metadata about the headers
65
Element metadataNode = root.addElement("metadata"); //$NON-NLS-1$
66
HashMap JavaDoc headerMap = new HashMap JavaDoc();
67         IPentahoMetaData metadata = results.getMetaData();
68         // first process the column headers
69
Object JavaDoc headers[][] = metadata.getColumnHeaders();
70         addHeaderMetadata(headers, metadataNode, headerMap);
71
72         // now process the rows in the data set
73
Element rowsNode = root.addElement("data"); //$NON-NLS-1$
74
Object JavaDoc row[] = results.next();
75
76         while (row != null) {
77             // create a new node tree for every row
78
Element rowNode = rowsNode.addElement("row"); //$NON-NLS-1$
79
Element currentNode = rowNode;
80             String JavaDoc headerId;
81             for (int columnNo = 0; columnNo < row.length; columnNo++) {
82                 // TODO make sure the nodes are encoded well
83
Object JavaDoc columnHeader;
84                 for (int headerNo = 0; headerNo < headers.length; headerNo++) {
85                     columnHeader = headers[headerNo][columnNo];
86                     headerId = (String JavaDoc) headerMap.get(columnHeader);
87                     currentNode = currentNode.addElement(headerId);
88                     if (headerNo < row.length) {
89                         currentNode.addElement("value").setText(row[headerNo].toString()); //$NON-NLS-1$
90
}
91                     // TODO support formatters
92
// currentNode.addElement( "formatted" ).setText( row[
93
// headerNo ].toString() );
94
}
95             }
96             row = results.next();
97         }
98         // System .out.println(gridDocument.asXML());
99
}
100
101     protected void addHeaderMetadata(Object JavaDoc headers[][], Element metadataNode, HashMap JavaDoc headerMap) {
102         if (headers == null) {
103             return;
104         }
105         // use a map to ensure we only add this information once
106
HashMap JavaDoc metadataMap = new HashMap JavaDoc();
107         for (int x = 0; x < headers.length; x++) {
108             for (int y = 0; y < headers[x].length; y++) {
109                 Object JavaDoc header = headers[x][y];
110                 if (header instanceof String JavaDoc) {
111                     String JavaDoc headerStr = (String JavaDoc) header;
112                     metadataMap.put(headerStr, headerStr);
113                     createMetadata(headerStr, metadataNode, headerMap);
114                 }
115                 // TODO suppport heavier metadata objects
116
else {
117                     String JavaDoc headerStr = header.toString();
118                     metadataMap.put(headerStr, headerStr);
119                     createMetadata(headerStr, metadataNode, headerMap);
120                 }
121
122             }
123         }
124     }
125
126     protected void createMetadata(String JavaDoc header, Element metadataNode, HashMap JavaDoc headerMap) {
127         Element node = metadataNode.addElement("header"); //$NON-NLS-1$
128
String JavaDoc id = "header" + headerMap.keySet().size(); //$NON-NLS-1$
129
node.addAttribute("id", id); //$NON-NLS-1$
130
node.addElement("name").setText(header); //$NON-NLS-1$
131
node.addAttribute("format", ""); //$NON-NLS-1$ //$NON-NLS-2$
132
Element title = node.addElement("title"); //$NON-NLS-1$
133
title.setText(header);
134         headerMap.put(header, id);
135     }
136
137     // TODO support creating metadata nodes for other metadata types
138
}
139
Popular Tags