KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > model > OlapUtils


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.olap.model;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import com.tonbeller.tbutils.res.Resources;
24
25 /**
26  * Misc helpers
27  * @author av
28  */

29 public class OlapUtils {
30
31   static final Set JavaDoc infix = new HashSet JavaDoc();
32   static final Set JavaDoc prefix = new HashSet JavaDoc();
33   static {
34     String JavaDoc[] s = new String JavaDoc[] { "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 JavaDoc singleRecordLevelNames = new HashSet JavaDoc();
45   static {
46     String JavaDoc s = resources.getString("single.record.level");
47     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s);
48     while (st.hasMoreTokens())
49       singleRecordLevelNames.add(st.nextToken());
50   }
51
52   private OlapUtils() {
53   }
54
55   /**
56    * returns a matrix[rows][columns] of cells
57    * @param result - a 0, 1, or 2 dimensional Result
58    * @return a matrix containing the result cells
59    * @see #getCellList
60    */

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 JavaDoc("result must be 0,1 or 2 dimensional");
78     }
79     Iterator JavaDoc 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   /**
88    * creates a list of cells out of a matrix
89    * @see #getCellMatrix
90    */

91   public static List JavaDoc 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 JavaDoc list = new ArrayList JavaDoc(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   /**
118    * returns the number of members on each position
119    */

120   public static int countHierarchies(Axis axis) {
121     List JavaDoc 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   /**
146    * compare member array
147    * @param aMem1
148    * @param aMem2
149    * @return true if member arrays compare
150    */

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       // any null does not compare
156
if (aMem1[i] == null)
157         return false;
158       if (!aMem1[i].equals(aMem2[i]))
159         return false;
160     }
161     return true;
162   }
163
164   /**
165    * return the dimensions that are displayed on a visible axis
166    */

167   public static Set JavaDoc getVisibleDimensions(OlapModel model) throws OlapException {
168     Set JavaDoc visible = new HashSet JavaDoc();
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   /**
179    * return the dimensions that are on the slicer axis (all that are not visible)
180    */

181   public static Set JavaDoc getSlicerDimensions(OlapModel model) throws OlapException {
182     Set JavaDoc visible = getVisibleDimensions(model);
183     Set JavaDoc slicer = new HashSet JavaDoc();
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   /**
193    * check, whether a function name matches an infis function like "+" or "-"
194    * @param fuName
195    * @return
196    */

197   public static boolean isInfixFunction(String JavaDoc fuName) {
198     return infix.contains(fuName);
199   }
200
201   /**
202    * check, whether a function name matches an prefix function like "not" or "unary -"
203    * @param fuName
204    * @return
205    */

206   public static boolean isPrefixFunction(String JavaDoc fuName) {
207     return prefix.contains(fuName);
208   }
209
210   /**
211    * true if for every member of the lowest level of <code>hier</code>
212    * there is a corresponding row in the fact table. If so, the
213    * Hierarchy is handled differently in the GUI.
214    */

215   public static boolean isSingleRecord(Hierarchy hier) {
216     Level lowest = getLowestLevel(hier);
217     return singleRecordLevelNames.contains(lowest.getLabel());
218   }
219
220   /**
221    * true if for every member of the lowest level of any hierarchy in <code>dim</code>
222    * there is a corresponding row in the fact table. If so, the
223    * Hierarchy is handled differently in the GUI.
224    */

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   /**
239    * Return a list of active dimensions on the slicer
240    */

241   public static Set JavaDoc getActiveSlicerDimensions(OlapModel model) throws OlapException {
242     Set JavaDoc active = new HashSet JavaDoc();
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   /**
252    * Return a list of active dimensions on the slicer
253    */

254   public static Set JavaDoc getActiveSlicerHierarchies(OlapModel model) throws OlapException {
255     Set JavaDoc active = new HashSet JavaDoc();
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   /**
264    * return the hierarchies that are on the slicer axis
265    (all that are not visible)
266    */

267   public static Set JavaDoc getSlicerHierarchies(OlapModel model) throws OlapException {
268     Set JavaDoc visible = getVisibleDimensions(model);
269            
270     /* Get dimensions and hierarchies on the slicer */
271     
272     Axis slicer = model.getResult().getSlicer();
273     Set JavaDoc selectedSlicerDims = new HashSet JavaDoc();
274     Set JavaDoc selectedSlicerHiers = new HashSet JavaDoc();
275     
276     List JavaDoc positions = slicer.getPositions();
277
278     for (Iterator JavaDoc 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     /* Return hierarchies that are not on the rows or columns and for the selected
293      * members, return the selected hierarchy
294      */

295     Set JavaDoc slicerHiers = new HashSet JavaDoc();
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 JavaDoc 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