001/* 002// This software is subject to the terms of the Eclipse Public License v1.0 003// Agreement, available at the following URL: 004// http://www.eclipse.org/legal/epl-v10.html. 005// You must accept the terms of that agreement to use this software. 006// 007// Copyright (C) 2001-2005 Julian Hyde 008// Copyright (C) 2005-2011 Pentaho and others 009// All Rights Reserved. 010*/ 011package mondrian.olap; 012 013import mondrian.server.Execution; 014import mondrian.server.Statement; 015 016import org.apache.log4j.Logger; 017 018import java.io.PrintWriter; 019import java.util.List; 020 021/** 022 * Skeleton implementation of {@link Result}. 023 * 024 * @author jhyde 025 * @since 10 August, 2001 026 */ 027public abstract class ResultBase implements Result { 028 protected final Execution execution; 029 protected final Statement statement; 030 protected final Query query; 031 protected final Axis[] axes; 032 protected Axis slicerAxis; 033 034 protected ResultBase(Execution execution, Axis[] axes) { 035 this.execution = execution; 036 this.statement = execution.getMondrianStatement(); 037 this.query = statement.getQuery(); 038 assert query != null; 039 this.axes = 040 axes == null 041 ? new Axis[query.getAxes().length] 042 : axes; 043 } 044 045 protected abstract Logger getLogger(); 046 047 public Query getQuery() { 048 return statement.getQuery(); 049 } 050 051 // implement Result 052 public Axis[] getAxes() { 053 return axes; 054 } 055 056 // implement Result 057 public Axis getSlicerAxis() { 058 return slicerAxis; 059 } 060 061 // implement Result 062 public void print(PrintWriter pw) { 063 for (int i = -1; i < axes.length; i++) { 064 pw.println("Axis #" + (i + 1) + ":"); 065 printAxis(pw, i < 0 ? slicerAxis : axes[i]); 066 } 067 // Usually there are 3 axes: {slicer, columns, rows}. Position is a 068 // {column, row} pair. We call printRows with axis=2. When it recurses 069 // to axis=-1, it prints. 070 int[] pos = new int[axes.length]; 071 printRows(pw, axes.length - 1, pos); 072 } 073 074 private void printRows(PrintWriter pw, int axis, int[] pos) { 075 if (axis < 0) { 076 printCell(pw, pos); 077 } else { 078 Axis _axis = axes[axis]; 079 List<Position> positions = _axis.getPositions(); 080 for (int i = 0; i < positions.size(); i++) { 081 pos[axis] = i; 082 if (axis == 0) { 083 int row = 084 axis + 1 < pos.length 085 ? pos[axis + 1] 086 : 0; 087 pw.print("Row #" + row + ": "); 088 } 089 printRows(pw, axis - 1, pos); 090 if (axis == 0) { 091 pw.println(); 092 } 093 } 094 } 095 } 096 097 private void printAxis(PrintWriter pw, Axis axis) { 098 List<Position> positions = axis.getPositions(); 099 for (Position position : positions) { 100 boolean firstTime = true; 101 pw.print("{"); 102 for (Member member : position) { 103 if (member.getDimension().isHighCardinality()) { 104 pw.println(" -- High cardinality dimension --}"); 105 return; 106 } 107 if (! firstTime) { 108 pw.print(", "); 109 } 110 pw.print(member.getUniqueName()); 111 firstTime = false; 112 } 113 pw.println("}"); 114 } 115 } 116 117 private void printCell(PrintWriter pw, int[] pos) { 118 Cell cell = getCell(pos); 119 pw.print(cell.getFormattedValue()); 120 } 121 122 /** 123 * Returns the current member of a given hierarchy at a given location. 124 * 125 * @param pos Coordinates in cell set 126 * @param hierarchy Hierarchy 127 * @return current member of given hierarchy 128 */ 129 public Member getMember(int[] pos, Hierarchy hierarchy) { 130 for (int i = -1; i < axes.length; i++) { 131 Axis axis = slicerAxis; 132 int index = 0; 133 if (i >= 0) { 134 axis = axes[i]; 135 index = pos[i]; 136 } 137 List<Position> positions = axis.getPositions(); 138 Position position = positions.get(index); 139 for (Member member : position) { 140 if (member.getHierarchy() == hierarchy) { 141 return member; 142 } 143 } 144 } 145 return hierarchy.getHierarchy().getDefaultMember(); 146 } 147 148 public void close() { 149 } 150} 151 152// End ResultBase.java