From 567c871a38da6e3fe4581c7e404e6c3c85dd9d17 Mon Sep 17 00:00:00 2001 From: Julian Hyde Date: Mon, 10 Mar 2008 20:45:29 +0000 Subject: [PATCH] Apply hierarchy lookup logic in one other place. git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@79 c6a108a4-781c-0410-a6c6-c2d559e19af0 --- .../olap4j/driver/xmla/XmlaOlap4jCellSet.java | 128 +++++++++++++----- 1 file changed, 96 insertions(+), 32 deletions(-) diff --git a/src/org/olap4j/driver/xmla/XmlaOlap4jCellSet.java b/src/org/olap4j/driver/xmla/XmlaOlap4jCellSet.java index fa3b870..0061f1e 100644 --- a/src/org/olap4j/driver/xmla/XmlaOlap4jCellSet.java +++ b/src/org/olap4j/driver/xmla/XmlaOlap4jCellSet.java @@ -46,20 +46,30 @@ abstract class XmlaOlap4jCellSet implements CellSet { private final List immutableAxisList = Olap4jUtil.cast(Collections.unmodifiableList(axisList)); private XmlaOlap4jCellSetAxis filterAxis; - private static final boolean DEBUG = true; + private static final boolean DEBUG = false; private static final List standardProperties = Arrays.asList( "UName", "Caption", "LName", "LNum", "DisplayInfo"); + /** + * Creates an XmlaOlap4jCellSet. + * + * @param olap4jStatement Statement + */ XmlaOlap4jCellSet( XmlaOlap4jStatement olap4jStatement) - throws OlapException { assert olap4jStatement != null; this.olap4jStatement = olap4jStatement; this.closed = false; } + /** + * Gets response from the XMLA request and populates cell set axes and cells + * with it. + * + * @throws OlapException on error + */ void populate() throws OlapException { byte[] bytes = olap4jStatement.getBytes(); @@ -150,7 +160,7 @@ void populate() throws OlapException { ((XmlaOlap4jPreparedStatement) olap4jStatement) .cellSetMetaData; } else { - this.metaData = createMetaData(olap4jStatement, root); + this.metaData = createMetaData(root); } // todo: use CellInfo element to determine mapping of cell properties @@ -166,7 +176,7 @@ void populate() throws OlapException { final Element axesNode = findChild(root, MDDATASET_NS, "Axes"); for (Element axisNode : findChildren(axesNode, MDDATASET_NS, "Axis")) { final String axisName = axisNode.getAttribute("name"); - final Axis axis = xx(axisName); + final Axis axis = lookupAxis(axisName); final XmlaOlap4jCellSetAxis cellSetAxis = new XmlaOlap4jCellSetAxis(this, axis); switch (axis) { @@ -224,7 +234,7 @@ void populate() throws OlapException { stringElement(memberNode, "Caption"); final int lnum = integerElement(memberNode, "LNum"); final Hierarchy hierarchy = - metaData.cube.getHierarchies().get(hierarchyName); + lookupHierarchy(metaData.cube, hierarchyName); final Level level = hierarchy.getLevels().get(lnum); member = new XmlaOlap4jSurpriseMember( level, hierarchy, lnum, caption, uname); @@ -264,6 +274,7 @@ void populate() throws OlapException { final String value = stringElement(cell, "Value"); final String formattedValue = stringElement(cell, "FmtValue"); final String formatString = stringElement(cell, "FormatString"); + Olap4jUtil.discard(formatString); for (Element element : childElements(cell)) { String tag = element.getLocalName(); final Property property = @@ -283,9 +294,15 @@ void populate() throws OlapException { } } - private XmlaOlap4jCellSetMetaData createMetaData( - XmlaOlap4jStatement olap4jStatement, - Element root) throws OlapException + /** + * Creates metadata for a cell set, given the DOM of the XMLA result. + * + * @param root Root node of XMLA result + * @return Metadata describing this cell set + * @throws OlapException on error + */ + private XmlaOlap4jCellSetMetaData createMetaData(Element root) + throws OlapException { final Element olapInfo = findChild(root, MDDATASET_NS, "OlapInfo"); @@ -312,7 +329,7 @@ private XmlaOlap4jCellSetMetaData createMetaData( XmlaOlap4jCellSetAxisMetaData filterAxisMetaData = null; for (Element axisInfo : axisInfos) { final String axisName = axisInfo.getAttribute("name"); - Axis axis = xx(axisName); + Axis axis = lookupAxis(axisName); final List hierarchyInfos = findChildren(axisInfo, MDDATASET_NS, "HierarchyInfo"); final List hierarchyList = @@ -341,24 +358,8 @@ private XmlaOlap4jCellSetMetaData createMetaData( final List propertyList = new ArrayList(); for (Element hierarchyInfo : hierarchyInfos) { - final String hierarchyName = - hierarchyInfo.getAttribute("name"); - Hierarchy hierarchy = - cube.getHierarchies().get(hierarchyName); - if (hierarchy == null) { - for (Hierarchy hierarchy1 : cube.getHierarchies()) { - if (hierarchy1.getUniqueName().equals(hierarchyName)) { - hierarchy = hierarchy1; - break; - } - } - if (hierarchy == null) { - throw olap4jStatement.olap4jConnection.helper - .createException( - "Internal error: hierarchy '" + hierarchyName - + "' not found in cube '" + cubeName + "'"); - } - } + final String hierarchyName = hierarchyInfo.getAttribute("name"); + Hierarchy hierarchy = lookupHierarchy(cube, hierarchyName); hierarchyList.add(hierarchy); for (Element childNode : childElements(hierarchyInfo)) { String tag = childNode.getLocalName(); @@ -409,16 +410,51 @@ private XmlaOlap4jCellSetMetaData createMetaData( cellProperties); } - private Axis xx(String axisName) { - Axis axis; + /** + * Looks up a hierarchy in a cube with a given name or, failing that, a + * given unique name. Throws if not found. + * + * @param cube Cube + * @param hierarchyName Name (or unique name) of hierarchy. + * @return Hierarchy + * @throws OlapException + */ + private Hierarchy lookupHierarchy(XmlaOlap4jCube cube, String hierarchyName) + throws OlapException + { + Hierarchy hierarchy = cube.getHierarchies().get(hierarchyName); + if (hierarchy == null) { + for (Hierarchy hierarchy1 : cube.getHierarchies()) { + if (hierarchy1.getUniqueName().equals(hierarchyName)) { + hierarchy = hierarchy1; + break; + } + } + if (hierarchy == null) { + throw this.olap4jStatement.olap4jConnection.helper + .createException( + "Internal error: hierarchy '" + hierarchyName + + "' not found in cube '" + cube.getName() + + "'"); + } + } + return hierarchy; + } + + /** + * Looks up an Axis with a given name. + * + * @param axisName Name of axis + * @return Axis + */ + private Axis lookupAxis(String axisName) { if (axisName.startsWith("Axis")) { final Integer ordinal = Integer.valueOf(axisName.substring("Axis".length())); - axis = Axis.values()[Axis.COLUMNS.ordinal() + ordinal]; + return Axis.values()[Axis.COLUMNS.ordinal() + ordinal]; } else { - axis = Axis.FILTER; + return Axis.FILTER; } - return axis; } public CellSetMetaData getMetaData() { @@ -445,6 +481,13 @@ public Cell getCell(Position... positions) { return getCell(coords); } + /** + * Returns a cell given its ordinal. + * + * @param pos Ordinal + * @return Cell + * @throws IndexOutOfBoundsException if ordinal is not in range + */ private Cell getCellInternal(int pos) { final Cell cell = cellMap.get(pos); if (cell == null) { @@ -461,6 +504,12 @@ private Cell getCellInternal(int pos) { return cell; } + /** + * Returns a string describing the maximum coordinates of this cell set; + * for example "2, 3" for a cell set with 2 columns and 3 rows. + * + * @return description of cell set bounds + */ private String getBoundsAsString() { StringBuilder buf = new StringBuilder(); int k = 0; @@ -481,6 +530,12 @@ public CellSetAxis getFilterAxis() { return filterAxis; } + /** + * Returns the ordinal of the last cell in this cell set. This is the + * product of the cardinalities of all axes. + * + * @return ordinal of last cell in cell set + */ private int maxOrdinal() { int modulo = 1; for (CellSetAxis axis : axisList) { @@ -1125,6 +1180,15 @@ private static class XmlaOlap4jSurpriseMember implements Member { private final String caption; private final String uname; + /** + * Creates an XmlaOlap4jSurpriseMember. + * + * @param level Level + * @param hierarchy Hierarchy + * @param lnum Level number + * @param caption Caption + * @param uname Member unique name + */ XmlaOlap4jSurpriseMember( Level level, Hierarchy hierarchy,