1 13 package com.tonbeller.jpivot.olap.model; 14 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Set ; 21 import java.util.StringTokenizer ; 22 23 import com.tonbeller.tbutils.res.Resources; 24 25 29 public class OlapUtils { 30 31 static final Set infix = new HashSet (); 32 static final Set prefix = new HashSet (); 33 static { 34 String [] s = new String [] { "and", "or", "xor", "*", "/", "+", "-", "%", "<", ">", "<=", ">=", 35 "<>", "="}; 36 for (int i = 0; i < s.length; i++) 37 infix.add(s[i]); 38 39 prefix.add("-"); 40 prefix.add("not"); 41 }; 42 43 static Resources resources = Resources.instance(OlapUtils.class); 44 static final Set singleRecordLevelNames = new HashSet (); 45 static { 46 String s = resources.getString("single.record.level"); 47 StringTokenizer st = new StringTokenizer (s); 48 while (st.hasMoreTokens()) 49 singleRecordLevelNames.add(st.nextToken()); 50 } 51 52 private OlapUtils() { 53 } 54 55 61 public static Cell[][] getCellMatrix(Result result) { 62 int rows, cols; 63 switch (result.getAxes().length) { 64 case 0: 65 cols = 1; 66 rows = 1; 67 break; 68 case 1: 69 cols = result.getAxes()[0].getPositions().size(); 70 rows = 1; 71 break; 72 case 2: 73 cols = result.getAxes()[0].getPositions().size(); 74 rows = result.getAxes()[1].getPositions().size(); 75 break; 76 default: 77 throw new IllegalArgumentException ("result must be 0,1 or 2 dimensional"); 78 } 79 Iterator it = result.getCells().iterator(); 80 Cell[][] matrix = new Cell[rows][cols]; 81 for (int row = 0; row < rows; row++) 82 for (int col = 0; col < cols; col++) 83 matrix[row][col] = (Cell) it.next(); 84 return matrix; 85 } 86 87 91 public static List getCellList(Cell[][] matrix) { 92 if (matrix.length == 0) 93 return Collections.EMPTY_LIST; 94 int rows = matrix.length; 95 int cols = matrix[0].length; 96 List list = new ArrayList (rows * cols); 97 for (int row = 0; row < rows; row++) { 98 for (int col = 0; col < cols; col++) { 99 list.add(matrix[row][col]); 100 } 101 } 102 return list; 103 } 104 105 public static Cell[][] transposeCellMatrix(Cell[][] oldCells) { 106 int rows = oldCells.length; 107 int cols = oldCells[0].length; 108 Cell[][] newCells = new Cell[cols][rows]; 109 for (int row = 0; row < rows; row++) { 110 for (int col = 0; col < cols; col++) { 111 newCells[col][row] = oldCells[row][col]; 112 } 113 } 114 return newCells; 115 } 116 117 120 public static int countHierarchies(Axis axis) { 121 List positions = axis.getPositions(); 122 if (positions.size() == 0) 123 return 0; 124 return ((Position) positions.get(0)).getMembers().length; 125 } 126 127 public static Level getParentLevel(Level level) { 128 Hierarchy hier = level.getHierarchy(); 129 Level[] levels = hier.getLevels(); 130 for (int i = 1; i < levels.length; i++) 131 if (level.equals(levels[i])) 132 return levels[i - 1]; 133 return null; 134 } 135 136 public static Level getChildLevel(Level level) { 137 Hierarchy hier = level.getHierarchy(); 138 Level[] levels = hier.getLevels(); 139 for (int i = 0; i < levels.length - 1; i++) 140 if (level.equals(levels[i])) 141 return levels[i + 1]; 142 return null; 143 } 144 145 151 public static boolean compareMembers(Member[] aMem1, Member[] aMem2) { 152 if (aMem1.length != aMem2.length) 153 return false; 154 for (int i = 0; i < aMem1.length; i++) { 155 if (aMem1[i] == null) 157 return false; 158 if (!aMem1[i].equals(aMem2[i])) 159 return false; 160 } 161 return true; 162 } 163 164 167 public static Set getVisibleDimensions(OlapModel model) throws OlapException { 168 Set visible = new HashSet (); 169 Axis[] axes = model.getResult().getAxes(); 170 for (int i = 0; i < axes.length; i++) { 171 Hierarchy[] hiers = axes[i].getHierarchies(); 172 for (int j = 0; j < hiers.length; j++) 173 visible.add(hiers[j].getDimension()); 174 } 175 return visible; 176 } 177 178 181 public static Set getSlicerDimensions(OlapModel model) throws OlapException { 182 Set visible = getVisibleDimensions(model); 183 Set slicer = new HashSet (); 184 Dimension[] dims = model.getDimensions(); 185 for (int i = 0; i < dims.length; i++) { 186 if (!visible.contains(dims[i])) 187 slicer.add(dims[i]); 188 } 189 return slicer; 190 } 191 192 197 public static boolean isInfixFunction(String fuName) { 198 return infix.contains(fuName); 199 } 200 201 206 public static boolean isPrefixFunction(String fuName) { 207 return prefix.contains(fuName); 208 } 209 210 215 public static boolean isSingleRecord(Hierarchy hier) { 216 Level lowest = getLowestLevel(hier); 217 return singleRecordLevelNames.contains(lowest.getLabel()); 218 } 219 220 225 public static boolean isSingleRecord(Dimension dim) { 226 Hierarchy[] hiers = dim.getHierarchies(); 227 for (int i = 0; i < hiers.length; i++) 228 if (isSingleRecord(hiers[i])) 229 return true; 230 return false; 231 } 232 233 public static Level getLowestLevel(Hierarchy hier) { 234 Level[] levels = hier.getLevels(); 235 return levels[levels.length - 1]; 236 } 237 238 241 public static Set getActiveSlicerDimensions(OlapModel model) throws OlapException { 242 Set active = new HashSet (); 243 Axis slicer = model.getResult().getSlicer(); 244 Hierarchy[] hiers = slicer.getHierarchies(); 245 for (int j = 0; j < hiers.length; j++) { 246 active.add(hiers[j].getDimension()); 247 } 248 return active; 249 } 250 251 254 public static Set getActiveSlicerHierarchies(OlapModel model) throws OlapException { 255 Set active = new HashSet (); 256 Axis slicer = model.getResult().getSlicer(); 257 Hierarchy[] hiers = slicer.getHierarchies(); 258 for (int j = 0; j < hiers.length; j++) 259 active.add(hiers[j]); 260 return active; 261 } 262 263 267 public static Set getSlicerHierarchies(OlapModel model) throws OlapException { 268 Set visible = getVisibleDimensions(model); 269 270 271 272 Axis slicer = model.getResult().getSlicer(); 273 Set selectedSlicerDims = new HashSet (); 274 Set selectedSlicerHiers = new HashSet (); 275 276 List positions = slicer.getPositions(); 277 278 for (Iterator iter = positions.iterator(); iter.hasNext();) { 279 Position pos = (Position) iter.next(); 280 Member[] posMembers = pos.getMembers(); 281 for (int i = 0; i < posMembers.length; i++) { 282 Hierarchy hier = posMembers[i].getLevel().getHierarchy(); 283 Dimension dim = hier.getDimension(); 284 285 if (!selectedSlicerHiers.contains(hier)) 286 selectedSlicerHiers.add(hier); 287 288 if (!selectedSlicerDims.contains(dim)) 289 selectedSlicerDims.add(dim); 290 } 291 } 292 295 Set slicerHiers = new HashSet (); 296 Dimension[] dims = model.getDimensions(); 297 for (int i = 0; i < dims.length; i++) { 298 if (!visible.contains(dims[i])) { 299 if (!selectedSlicerDims.contains(dims[i])) { 300 slicerHiers.add(dims[i].getHierarchies()[0]); 301 } else { 302 for (Iterator it = selectedSlicerHiers.iterator(); it.hasNext();) { 303 Hierarchy hier = (Hierarchy) it.next(); 304 Dimension dim = hier.getDimension(); 305 if (dim.equals(dims[i])) { 306 slicerHiers.add(hier); 307 break; 308 } 309 } 310 } 311 312 } 313 } 314 315 return slicerHiers; 316 } 317 318 } 319 320 321 | Popular Tags |