1 28 package net.sf.jasperreports.olap.xmla; 29 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 import net.sf.jasperreports.engine.JRRuntimeException; 38 import net.sf.jasperreports.olap.result.JROlapCell; 39 import net.sf.jasperreports.olap.result.JROlapResult; 40 import net.sf.jasperreports.olap.result.JROlapResultAxis; 41 42 43 47 public class JRXmlaResult implements JROlapResult 48 { 49 50 private final static Log log = LogFactory.getLog(JRXmlaResult.class); 51 52 private List axesList = new ArrayList (); 53 private JRXmlaResultAxis[] axes; 54 private int[] cellOrdinalFactors; 55 private final List cells = new ArrayList (); 56 57 public JROlapResultAxis[] getAxes() 58 { 59 return ensureAxisArray(); 60 } 61 62 public JROlapCell getCell(int[] axisPositions) 63 { 64 int cellOrdinal = getCellOrdinal(axisPositions); 65 return (JROlapCell) cells.get(cellOrdinal); 66 } 67 68 protected int getCellOrdinal(int[] axisPositions) 69 { 70 ensureCellOrdinalFactors(); 71 72 if (axisPositions.length != axes.length) 73 { 74 throw new JRRuntimeException("Number of axis positions (" + axisPositions.length 75 + ") doesn't match the number of axes (" + axes.length + ")"); 76 } 77 78 int ordinal = 0; 79 for (int i = 0; i < axisPositions.length; ++i) 80 { 81 ordinal += cellOrdinalFactors[i] * axisPositions[i]; 82 } 83 return ordinal; 84 } 85 86 public void addAxis(JRXmlaResultAxis axis) 87 { 88 axesList.add(axis); 89 resetAxisArray(); 90 } 91 92 public JRXmlaResultAxis getAxisByName(String name) 93 { 94 JRXmlaResultAxis axis = null; 95 for (Iterator iter = axesList.iterator(); axis == null && iter.hasNext();) 96 { 97 JRXmlaResultAxis ax = (JRXmlaResultAxis) iter.next(); 98 if (ax.getAxisName().equals(name)) 99 { 100 axis = ax; 101 } 102 } 103 return axis; 104 } 105 106 protected JRXmlaResultAxis[] ensureAxisArray() 107 { 108 if (axes == null) 109 { 110 axes = new JRXmlaResultAxis[axesList.size()]; 111 axes = (JRXmlaResultAxis[]) axesList.toArray(axes); 112 } 113 return axes; 114 } 115 116 protected void ensureCellOrdinalFactors() 117 { 118 ensureAxisArray(); 119 120 if (cellOrdinalFactors == null) 121 { 122 int axisCount = axes.length; 123 cellOrdinalFactors = new int[axisCount]; 124 125 cellOrdinalFactors[0] = 1; 126 for (int i = 1; i < axisCount; ++i) 127 { 128 cellOrdinalFactors[i] = cellOrdinalFactors[i - 1] * axes[i - 1].getTupleCount(); 129 } 130 } 131 } 132 133 protected void resetAxisArray() 134 { 135 axes = null; 136 cellOrdinalFactors = null; 137 } 138 139 public void setCell(JRXmlaCell cell, int cellOrdinal) 140 { 141 int cellsCount = cells.size(); 142 if (cellOrdinal == -1 || cellOrdinal == cellsCount) 143 { 144 cells.add(cell); 145 } 146 else if (cellOrdinal > cellsCount) 147 { 148 for (int i = cellsCount; i < cellOrdinal; ++i) 149 { 150 cells.add(null); 151 } 152 cells.add(cell); 153 } 154 else 155 { 156 if (log.isWarnEnabled()) 157 { 158 log.warn("Overwriting cell at ordinal " + cellOrdinal); 159 } 160 161 cells.set(cellOrdinal, cell); 162 } 163 } 164 } 165 | Popular Tags |