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 14, 2005
014: * @author wseyler
015: */
016: package org.pentaho.data.connection.mdx;
017:
018: import java.util.List;
019:
020: import mondrian.olap.Hierarchy;
021: import mondrian.olap.Member;
022: import mondrian.olap.Result;
023:
024: import org.pentaho.commons.connection.AbstractPentahoMetaData;
025:
026: /**
027: * @author wseyler
028: *
029: * TODO To change the template for this generated type comment go to Window -
030: * Preferences - Java - Code Style - Code Templates
031: */
032: public class MDXMetaData extends AbstractPentahoMetaData {
033: private static final int AXIS_COLUMN = 0;
034:
035: private Object[][] columnHeaders;
036:
037: private Object[][] rowHeaders;
038:
039: private String[] columnNames;
040:
041: private static final int AXIS_ROW = 1;
042:
043: Result nativeResultSet = null;
044:
045: /**
046: * @param connection
047: */
048: public MDXMetaData(Result nativeResultSet) {
049: super ();
050: this .nativeResultSet = nativeResultSet;
051: columnHeaders = createColumnHeaders();
052: rowHeaders = createRowHeaders();
053: columnNames = createColumnNames();
054: }
055:
056: private Object[][] createColumnHeaders() {
057: int rowCount = 0;
058: int colCount = 0;
059:
060: List positions = nativeResultSet.getAxes()[AXIS_COLUMN]
061: .getPositions();
062: if (positions != null && positions.size() > 0) {
063: rowCount = ((List) positions.get(0)).size() + 1;
064: colCount = positions.size();
065: }
066: Object[][] result = new Object[rowCount][colCount];
067: for (int c = 0; c < colCount; c++) {
068: List members = (List) positions.get(c);
069: Member member = null;
070: for (int r = 0; r < rowCount - 1; r++) {
071: member = (Member) members.get(r);
072: result[r][c] = member.getCaption();
073: }
074: result[rowCount - 1][c] = member.getHierarchy()
075: .getCaption();
076: }
077: return result;
078: }
079:
080: private Object[][] createRowHeaders() {
081: int rowCount = 0;
082: int colCount = 0;
083:
084: List positions = nativeResultSet.getAxes()[AXIS_ROW]
085: .getPositions();
086: if (positions != null && positions.size() > 0) {
087: rowCount = positions.size();
088: colCount = ((List) positions.get(0)).size() + 1;
089: }
090: Object[][] result = new Object[rowCount][colCount];
091: for (int r = 0; r < rowCount; r++) {
092: List members = (List) positions.get(r);
093: Member member = null;
094: for (int c = 0; c < colCount - 1; c++) {
095: member = (Member) members.get(c);
096: result[r][c] = member.getCaption();
097: }
098: result[r][colCount - 1] = member.getHierarchy()
099: .getCaption();
100: }
101: return result;
102: }
103:
104: /**
105: * Flattens the row headers into column names (where the useful columns have
106: * useful names and the unuseful columns have unusful names).
107: * @return the row headers in a String array
108: */
109: private String[] createColumnNames() {
110: String[] colNames = null;
111:
112: if (nativeResultSet != null) {
113: colNames = new String[getColumnCount()];
114:
115: // Flatten out the column headers into one column-name
116: for (int i = 0; i < colNames.length; ++i) {
117: List positions = nativeResultSet.getAxes()[AXIS_ROW]
118: .getPositions();
119: if (i < ((List) positions.get(0)).size()) {
120: Member member = (Member) ((List) positions.get(0))
121: .get(i);
122: Hierarchy hierarchy = member.getHierarchy();
123: colNames[i] = hierarchy.getCaption();
124: } else {
125: colNames[i] = ((Member) ((List) positions.get(0))
126: .get(((List) positions.get(0)).size() - 1))
127: .getHierarchy().getName()
128: + "{" + i + "}"; //$NON-NLS-1$ //$NON-NLS-2$
129: }
130: }
131: }
132:
133: return colNames;
134: }
135:
136: public String getColumnName(int columnNumber) {
137: return (columnNames != null && columnNumber >= 0
138: && columnNumber < columnNames.length ? columnNames[columnNumber]
139: : ""); //$NON-NLS-1$
140: }
141:
142: /*
143: * (non-Javadoc)
144: *
145: * @see org.pentaho.connection.IPentahoMetaData#getColumnCount()
146: */
147: public int getColumnCount() {
148: return nativeResultSet.getAxes()[AXIS_COLUMN].getPositions()
149: .size();
150: }
151:
152: /*
153: * (non-Javadoc)
154: *
155: * @see org.pentaho.connection.IPentahoMetaData#getColumnHeaders()
156: */
157: public Object[][] getColumnHeaders() {
158: return columnHeaders;
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see org.pentaho.connection.IPentahoMetaData#getRowHeaders()
165: */
166: public Object[][] getRowHeaders() {
167: return rowHeaders;
168: }
169:
170: }
|