001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Sep 23, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.data;
018:
019: import java.util.HashMap;
020:
021: import org.dom4j.Document;
022: import org.dom4j.DocumentHelper;
023: import org.dom4j.Element;
024: import org.pentaho.commons.connection.IPentahoMetaData;
025: import org.pentaho.commons.connection.IPentahoResultSet;
026: import org.pentaho.messages.util.LocaleHelper;
027:
028: public class DataGrid {
029:
030: public static final int STYLE_ROWS = 1;
031:
032: public static final int STYLE_TREE = 2;
033:
034: Document gridDocument;
035:
036: int documentStyle;
037:
038: public DataGrid(int documentStyle) {
039: this .documentStyle = documentStyle;
040: }
041:
042: public Document getDataDocument() {
043: return gridDocument;
044: }
045:
046: public int getDocumentStyle() {
047: return documentStyle;
048: }
049:
050: public void populate(IPentahoResultSet results) {
051:
052: switch (documentStyle) {
053: case STYLE_ROWS:
054: populateRowStyle(results);
055: }
056:
057: }
058:
059: protected void populateRowStyle(IPentahoResultSet results) {
060: gridDocument = DocumentHelper.createDocument();
061: gridDocument.setXMLEncoding(LocaleHelper.getSystemEncoding());
062: Element root = gridDocument.addElement("datagrid"); //$NON-NLS-1$
063:
064: // add metadata about the headers
065: Element metadataNode = root.addElement("metadata"); //$NON-NLS-1$
066: HashMap headerMap = new HashMap();
067: IPentahoMetaData metadata = results.getMetaData();
068: // first process the column headers
069: Object headers[][] = metadata.getColumnHeaders();
070: addHeaderMetadata(headers, metadataNode, headerMap);
071:
072: // now process the rows in the data set
073: Element rowsNode = root.addElement("data"); //$NON-NLS-1$
074: Object row[] = results.next();
075:
076: while (row != null) {
077: // create a new node tree for every row
078: Element rowNode = rowsNode.addElement("row"); //$NON-NLS-1$
079: Element currentNode = rowNode;
080: String headerId;
081: for (int columnNo = 0; columnNo < row.length; columnNo++) {
082: // TODO make sure the nodes are encoded well
083: Object columnHeader;
084: for (int headerNo = 0; headerNo < headers.length; headerNo++) {
085: columnHeader = headers[headerNo][columnNo];
086: headerId = (String) headerMap.get(columnHeader);
087: currentNode = currentNode.addElement(headerId);
088: if (headerNo < row.length) {
089: currentNode
090: .addElement("value").setText(row[headerNo].toString()); //$NON-NLS-1$
091: }
092: // TODO support formatters
093: // currentNode.addElement( "formatted" ).setText( row[
094: // headerNo ].toString() );
095: }
096: }
097: row = results.next();
098: }
099: // System .out.println(gridDocument.asXML());
100: }
101:
102: protected void addHeaderMetadata(Object headers[][],
103: Element metadataNode, HashMap headerMap) {
104: if (headers == null) {
105: return;
106: }
107: // use a map to ensure we only add this information once
108: HashMap metadataMap = new HashMap();
109: for (int x = 0; x < headers.length; x++) {
110: for (int y = 0; y < headers[x].length; y++) {
111: Object header = headers[x][y];
112: if (header instanceof String) {
113: String headerStr = (String) header;
114: metadataMap.put(headerStr, headerStr);
115: createMetadata(headerStr, metadataNode, headerMap);
116: }
117: // TODO suppport heavier metadata objects
118: else {
119: String headerStr = header.toString();
120: metadataMap.put(headerStr, headerStr);
121: createMetadata(headerStr, metadataNode, headerMap);
122: }
123:
124: }
125: }
126: }
127:
128: protected void createMetadata(String header, Element metadataNode,
129: HashMap headerMap) {
130: Element node = metadataNode.addElement("header"); //$NON-NLS-1$
131: String id = "header" + headerMap.keySet().size(); //$NON-NLS-1$
132: node.addAttribute("id", id); //$NON-NLS-1$
133: node.addElement("name").setText(header); //$NON-NLS-1$
134: node.addAttribute("format", ""); //$NON-NLS-1$ //$NON-NLS-2$
135: Element title = node.addElement("title"); //$NON-NLS-1$
136: title.setText(header);
137: headerMap.put(header, id);
138: }
139:
140: // TODO support creating metadata nodes for other metadata types
141: }
|