1 13 package com.tonbeller.jpivot.xmla; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 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 34 public class XMLA_Util { 35 36 static Logger logger = Logger.getLogger(XMLA_Util.class); 37 38 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 58 public static Exp member2Exp(Member member) { 59 return Literal.createSymbol(member.getLabel()); 60 } 61 62 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 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; 89 } 90 91 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 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; 114 } 115 116 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 List currentMembers = new ArrayList (); 131 currentMembers.add(member); 132 while (depth > lev.getDepth()) { 133 lev = lev.getChildLevel(); 134 List aMembers = new ArrayList (); 135 for (Iterator 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 151 public static boolean isDescendant(XMLA_Member ancestor, XMLA_Member descendant) { 152 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); break; 167 } 168 } 169 170 if (mm.equals(ancestor)) 171 return true; 172 else 173 return false; 174 } 175 176 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 (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 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 topMembers = topLevel.getMembers(); 201 } catch (OlapException e) { 202 logger.error("?", e); 204 return null; 205 } 206 if (topMembers.length == 1) 207 return topMembers[0]; return new FunCall("{}", topMembers, FunCall.TypeBraces); 209 } 210 211 217 static List collectMembers(TreeNode root, final int iDim) { 218 if (root == null) 219 return null; 220 final List memberList = new ArrayList (); 221 root.walkChildren(new TreeNodeCallback() { 222 223 227 public int handleTreeNode(TreeNode node) { 228 int iDimNode = node.getLevel() - 1; 229 if (iDimNode < iDim) 230 return TreeNodeCallback.CONTINUE; 232 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 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; } 250 }); 251 return memberList; 252 } 253 254 259 static void resolveFunCallMembers(FunCall f, List 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 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 305 public static int funCallSyntax(String 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 } 318 } | Popular Tags |