KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > xmla > XMLA_Util


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.xmla;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.apache.log4j.Logger;
20
21 import com.tonbeller.jpivot.olap.mdxparse.Exp;
22 import com.tonbeller.jpivot.olap.mdxparse.FunCall;
23 import com.tonbeller.jpivot.olap.mdxparse.Literal;
24 import com.tonbeller.jpivot.olap.model.Axis;
25 import com.tonbeller.jpivot.olap.model.Hierarchy;
26 import com.tonbeller.jpivot.olap.model.Member;
27 import com.tonbeller.jpivot.olap.model.OlapException;
28 import com.tonbeller.jpivot.util.TreeNode;
29 import com.tonbeller.jpivot.util.TreeNodeCallback;
30
31 /**
32  * static XMLA Utils
33  */

34 public class XMLA_Util {
35
36   static Logger logger = Logger.getLogger(XMLA_Util.class);
37
38   /**
39    * generate Exp[] from Member[]
40    * @param members
41    * @return Exp[]
42    * @deprecated
43    */

44   public static Exp[] member2Exp(Member[] members) {
45     Exp[] memExp = new Exp[members.length];
46     for (int i = 0; i < memExp.length; i++) {
47       memExp[i] = Literal.createSymbol(((XMLA_Member) members[i]).getUniqueName());
48     }
49     return memExp;
50   }
51
52   /**
53    * generate Exp from Member
54    * @param member
55    * @return Exp
56    * @deprecated
57    */

58   public static Exp member2Exp(Member member) {
59     return Literal.createSymbol(member.getLabel());
60   }
61
62   /**
63    * determine hierarchy index on axis
64    * @param axis
65    * @param member
66    * @return int
67    */

68   public static int hierIndex(XMLA_Axis axis, XMLA_Member member) {
69     XMLA_Level lev = (XMLA_Level) member.getLevel();
70     XMLA_Hierarchy hier = (XMLA_Hierarchy) lev.getHierarchy();
71
72     return hierIndex(axis, hier);
73   }
74
75   /**
76    * determine hierarchy index on axis
77    * @param axis
78    * @param hier
79    * @return int
80    */

81   public static int hierIndex(XMLA_Axis axis, XMLA_Hierarchy hier) {
82
83     Hierarchy[] hiersOfAxis = axis.getHierarchies();
84     for (int j = 0; j < hiersOfAxis.length; j++) {
85       if (((XMLA_Hierarchy) hiersOfAxis[j]).isEqual(hier)) { return j; }
86     }
87     return -1; // should never get here
88

89   }
90
91   /**
92    * find the result axis for a member
93    */

94   public static int axisOrdinalForMember(Axis[] axes, XMLA_Member member) {
95     XMLA_Level lev = (XMLA_Level) member.getLevel();
96     XMLA_Hierarchy hier = (XMLA_Hierarchy) lev.getHierarchy();
97
98     return axisOrdinalForHier(axes, hier);
99   }
100
101   /**
102    * find the result axis for a hierarchy
103    */

104   public static int axisOrdinalForHier(Axis[] axes, XMLA_Hierarchy hier) {
105
106     AxesLoop: for (int i = 0; i < axes.length; i++) {
107       Hierarchy[] hiersOfAxis = axes[i].getHierarchies();
108       for (int j = 0; j < hiersOfAxis.length; j++) {
109         if (((XMLA_Hierarchy) hiersOfAxis[j]).isEqual(hier)) { return i; }
110       }
111     }
112     return -1; // should never get here
113

114   }
115
116   /**
117    * determine descendants of member at specific level
118    * @param scr SchemaReader
119    * @param member
120    * @param level
121    * @return descendants
122    */

123   public static XMLA_Member[] getMemberDescendants(XMLA_Member member, XMLA_Level level)
124       throws OlapException {
125     int depth = level.getDepth();
126     XMLA_Level lev = (XMLA_Level) member.getLevel();
127     if (depth <= lev.getDepth())
128       return new XMLA_Member[0];
129     //XMLA_Member[] currentMembers = new XMLA_Member[] { member };
130
List JavaDoc currentMembers = new ArrayList JavaDoc();
131     currentMembers.add(member);
132     while (depth > lev.getDepth()) {
133       lev = lev.getChildLevel();
134       List JavaDoc aMembers = new ArrayList JavaDoc();
135       for (Iterator JavaDoc iter = currentMembers.iterator(); iter.hasNext();) {
136         XMLA_Member m = (XMLA_Member) iter.next();
137         XMLA_Member[] mems = m.getChildren();
138         for (int i = 0; i < mems.length; i++) {
139           aMembers.add(mems[i]);
140         }
141       }
142       currentMembers = aMembers;
143     }
144     return (XMLA_Member[]) currentMembers.toArray(new XMLA_Member[0]);
145   }
146
147   /**
148    * @param ancestor
149    * @param descendant
150    */

151   public static boolean isDescendant(XMLA_Member ancestor, XMLA_Member descendant) {
152     // a calculated member, even if defined under "ancestor" is *not* descendant,
153
// WITM MEMBER a.b as '..'
154
// a.children does *not* include b
155
if (descendant.isCalculated())
156       return false;
157     if (ancestor.equals(descendant))
158       return false;
159     int ancestorLevelNumber = ((XMLA_Level) ancestor.getLevel()).getDepth();
160     XMLA_Member mm = descendant;
161     while (mm != null && ancestorLevelNumber < ((XMLA_Level) mm.getLevel()).getDepth()) {
162       try {
163         mm = (XMLA_Member) mm.getParent();
164       } catch (OlapException e) {
165         logger.error("?", e); // should not occur
166
break;
167       }
168     }
169
170     if (mm.equals(ancestor))
171       return true;
172     else
173       return false;
174   }
175
176   /**
177    * @return an Expression Object for the top level members of an hierarchy
178    */

179   public static Exp topLevelMembers(Hierarchy hier, boolean expandAllMember) {
180     XMLA_Level topLevel = (XMLA_Level) ((XMLA_Hierarchy) hier).getLevels()[0];
181     XMLA_Member mAll = (XMLA_Member) ((XMLA_Hierarchy) hier).getAllMember();
182     // if there is an All Member, we will have to expand it
183
// according to expandAllMember flag
184
if (mAll != null ) {
185       XMLA_Member [] memar = new XMLA_Member[] {mAll};
186       Exp mAllSet = new FunCall("{}", memar, FunCall.TypeBraces);
187       if (!expandAllMember) {
188         return mAll;
189       }
190       // must expand
191
// create Union({AllMember}, AllMember.children)
192
Exp mAllChildren = new FunCall("children", memar, FunCall.TypeProperty);
193       Exp union = new FunCall("union", new Exp[] {mAllSet, mAllChildren}, FunCall.TypeFunction);
194       return union;
195     }
196
197     XMLA_Member[] topMembers;
198     try {
199       // HHTASK ok, for a parent-child hierarchy ?
200
topMembers = topLevel.getMembers();
201     } catch (OlapException e) {
202       // should not occur
203
logger.error("?", e);
204       return null;
205     }
206     if (topMembers.length == 1)
207       return topMembers[0]; // single member
208
return new FunCall("{}", topMembers, FunCall.TypeBraces);
209   }
210
211   /**
212    *
213    * @param root
214    * @param iDim
215    * @return
216    */

217   static List JavaDoc collectMembers(TreeNode root, final int iDim) {
218     if (root == null)
219       return null;
220     final List JavaDoc memberList = new ArrayList JavaDoc();
221     root.walkChildren(new TreeNodeCallback() {
222
223       /**
224        * callback
225        * find node matching member Path exactly
226        */

227       public int handleTreeNode(TreeNode node) {
228         int iDimNode = node.getLevel() - 1;
229         if (iDimNode < iDim)
230           return TreeNodeCallback.CONTINUE; // we are below iDim, don't care
231

232         // iDimNode == iDim
233
// node Exp must contain children of member[iDim]
234
Exp exp = (Exp) node.getReference();
235         if (exp instanceof XMLA_Member) {
236           XMLA_Member m = (XMLA_Member) exp;
237           if (!memberList.contains(m))
238             memberList.add(m);
239         } else {
240           // must be FunCall
241
FunCall f = (FunCall) exp;
242           try {
243             resolveFunCallMembers(f, memberList);
244           } catch (OlapException e) {
245             logger.error("?", e);
246           }
247         }
248         return TreeNodeCallback.CONTINUE_SIBLING; // continue next sibling
249
}
250     });
251     return memberList;
252   }
253
254   /**
255    *
256    * @param f
257    * @param memberList
258    */

259   static void resolveFunCallMembers(FunCall f, List JavaDoc memberList) throws OlapException {
260     if (f.isCallTo("Children")) {
261       XMLA_Member m = (XMLA_Member) f.getArgs()[0];
262       XMLA_Member[] members = m.getChildren();
263       for (int i = 0; i < members.length; i++) {
264         if (!memberList.contains(members[i]))
265           memberList.add(members[i]);
266       }
267     } else if (f.isCallTo("Descendants")) {
268       XMLA_Member m = (XMLA_Member) f.getArgs()[0];
269       XMLA_Level level = (XMLA_Level) f.getArgs()[1];
270       XMLA_Member[] members = XMLA_Util.getMemberDescendants(m, level);
271       for (int i = 0; i < members.length; i++) {
272         if (!memberList.contains(members[i]))
273           memberList.add(members[i]);
274       }
275     } else if (f.isCallTo("Members")) {
276       XMLA_Level level = (XMLA_Level) f.getArgs()[0];
277       XMLA_Member[] members = level.getMembers();
278       for (int i = 0; i < members.length; i++) {
279         if (!memberList.contains(members[i]))
280           memberList.add(members[i]);
281       }
282     } else if (f.isCallTo("Union")) {
283       resolveFunCallMembers((FunCall) f.getArgs()[0], memberList);
284       resolveFunCallMembers((FunCall) f.getArgs()[1], memberList);
285     } else if (f.isCallTo("{}")) {
286       for (int i = 0; i < f.getArgs().length; i++) {
287         if (!memberList.contains(f.getArgs()[i]))
288           memberList.add(f.getArgs()[i]);
289       }
290     } else if (
291     // we cannot handle this properly, just return members of base set
292
f.isCallTo("TopCount") || f.isCallTo("BottomCount") || f.isCallTo("TopPercent")
293         || f.isCallTo("BottomPercent")) {
294       resolveFunCallMembers((FunCall) f.getArgs()[0], memberList);
295     } else {
296       logger.error("invalid FunCall encountered " + f.getFunction());
297     }
298   }
299
300   /**
301    * Map function names to XMLA Function type
302    * @param fuName
303    * @return Syntax type
304    */

305   public static int funCallSyntax(String JavaDoc fuName) {
306     if (fuName.equals("()"))
307       return FunCall.TypeParentheses;
308     else if (fuName.equals("{}"))
309       return FunCall.TypeBraces;
310     else if (fuName.equalsIgnoreCase("members"))
311       return FunCall.TypeProperty;
312     else if (fuName.equalsIgnoreCase("children"))
313       return FunCall.TypeProperty;
314     else
315       return FunCall.TypeFunction;
316     // HHTASK complete
317
}
318 } // End XMLA_Util
319
Popular Tags