1 13 package com.tonbeller.jpivot.xmla; 14 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Comparator ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.apache.log4j.Logger; 22 23 import com.tonbeller.jpivot.core.ExtensionSupport; 24 import com.tonbeller.jpivot.olap.mdxparse.Formula; 25 import com.tonbeller.jpivot.olap.mdxparse.ParsedQuery; 26 import com.tonbeller.jpivot.olap.model.Hierarchy; 27 import com.tonbeller.jpivot.olap.model.Level; 28 import com.tonbeller.jpivot.olap.model.Member; 29 import com.tonbeller.jpivot.olap.model.OlapException; 30 import com.tonbeller.jpivot.olap.navi.MemberTree; 31 import com.tonbeller.jpivot.util.StringUtil; 32 import com.tonbeller.jpivot.olap.model.Position; 33 import com.tonbeller.jpivot.olap.model.Result; 34 import com.tonbeller.jpivot.olap.query.Quax; 35 import com.tonbeller.jpivot.olap.model.Axis; 36 37 40 public class XMLA_MemberTree extends ExtensionSupport implements MemberTree { 41 42 static Logger logger = Logger.getLogger(XMLA_MemberTree.class); 43 44 47 public XMLA_MemberTree() { 48 super.setId(MemberTree.ID); 49 } 50 51 55 public Member[] getRootMembers(Hierarchy hier) { 56 XMLA_Model model = (XMLA_Model) getModel(); 57 Level[] levels = hier.getLevels(); 58 XMLA_Level rootLevel = null; 60 for (int i = 0; i < levels.length; i++) { 61 XMLA_Level xLev = (XMLA_Level) levels[i]; 62 if (xLev.getDepth() == 0) { 63 rootLevel = xLev; 64 break; 65 } 66 } 67 if (rootLevel == null) 68 return null; 70 71 final List visibleRootMembers = new ArrayList (); 72 final List invisibleRootMembers = new ArrayList (); 73 74 Member[] rootMembers = new Member[0]; 75 try { 76 rootMembers = rootLevel.getMembers(); 77 } catch (OlapException e) { 78 logger.error(null, e); 79 } 80 ArrayList aCalcMem = new ArrayList (); 83 ParsedQuery pq = ((XMLA_QueryAdapter) model.getQueryAdapter()).getParsedQuery(); 84 Formula[] formulas = pq.getFormulas(); 85 86 for (int i = 0; i < formulas.length; i++) { 87 Formula f = formulas[i]; 88 if (!f.isMember()) 89 continue; 90 91 String dimUMember = StringUtil.bracketsAround(f.getFirstName()); 92 String dimUHier = ((XMLA_Hierarchy) hier).getUniqueName(); 93 if (!(dimUHier.equals(dimUMember))) 94 continue; 95 96 String memberName = f.getUniqeName(); 97 XMLA_Member calcMem = (XMLA_Member) model.lookupMemberByUName(memberName); 98 if (calcMem == null) { 99 100 String [] nameParts = StringUtil.splitUniqueName(memberName); 101 calcMem = new XMLA_Member(model, memberName, nameParts[1], null, true); 102 } 104 aCalcMem.add(calcMem); 105 } 106 107 Result res = model.currentResult(); 111 if (res != null) { 112 XMLA_QueryAdapter adapter = (XMLA_QueryAdapter) model.getQueryAdapter(); 115 Quax quax = adapter.findQuax(hier.getDimension()); 116 if (quax != null) { 117 int iDim = quax.dimIdx(hier.getDimension()); 118 int iAx = quax.getOrdinal(); 119 if (adapter.isSwapAxes()) 120 iAx = (iAx + 1) % 2; 121 Axis axis = res.getAxes()[iAx]; 122 List positions = axis.getPositions(); 123 124 for (Iterator iter = positions.iterator(); iter.hasNext();) { 125 Position pos = (Position) iter.next(); 126 Member[] posMembers = pos.getMembers(); 127 XMLA_Member mem = (XMLA_Member) posMembers[iDim]; 128 if (!(getParent(mem) == null)) 129 continue; if (!visibleRootMembers.contains(mem)) 131 visibleRootMembers.add(mem); 132 133 boolean containsMember = false; 135 for (int i = 0; i < rootMembers.length; i++) { 136 if (rootMembers[i].equals(mem)) { 137 containsMember = true; 138 break; 139 } 140 } 141 if (!containsMember && !aCalcMem.contains(mem) && !invisibleRootMembers.contains(mem)) { 142 invisibleRootMembers.add(mem); 143 } 144 } 145 } 146 } 147 148 Member[] members = new Member[rootMembers.length + aCalcMem.size() + invisibleRootMembers.size()]; 149 int k = rootMembers.length; 150 for (int i = 0; i < k; i++) { 151 members[i] = rootMembers[i]; 152 } 153 for (Iterator iter = aCalcMem.iterator(); iter.hasNext();) { 154 XMLA_Member calcMem = (XMLA_Member) iter.next(); 155 members[k++] = calcMem; 156 } 157 for (Iterator iter = invisibleRootMembers.iterator(); iter.hasNext();) { 158 XMLA_Member invisibleMem = (XMLA_Member) iter.next(); 159 members[k++] = invisibleMem; 160 } 161 162 if (visibleRootMembers.size() != 0) { 164 Arrays.sort(members, new Comparator () { 165 public int compare(Object arg0, Object arg1) { 166 Member m1 = (Member) arg0; 167 Member m2 = (Member) arg1; 168 int index1 = visibleRootMembers.indexOf(m1); 169 int index2 = visibleRootMembers.indexOf(m2); 170 if (index2 == -1) 171 return -1; if (index1 == -1) 173 return 1; return index1 - index2; 175 } 176 }); 177 } 178 179 return members; 180 } 181 182 185 public boolean hasChildren(Member member) { 186 187 XMLA_Member m = (XMLA_Member) member; 188 if (m.isCalculated()) 189 return false; 190 long ccard = m.getChildrenCardinality(); if (ccard >= 0) 192 return (ccard > 0); 193 XMLA_Level xLev = (XMLA_Level) member.getLevel(); 194 if (xLev == null || xLev.getChildLevel() == null) 195 return false; 196 return true; 197 } 198 199 202 public Member[] getChildren(Member member) { 203 204 XMLA_Level xLev = (XMLA_Level) member.getLevel(); 205 206 if (xLev == null || xLev.getChildLevel() == null) 207 return null; 208 209 Member[] children = new Member[0]; 210 try { 211 children = ((XMLA_Member) member).getChildren(); 212 } catch (OlapException e) { 213 logger.error("?", e); 214 return null; 215 } 216 return children; 217 } 218 219 222 public Member getParent(Member member) { 223 224 XMLA_Level xLev = (XMLA_Level) member.getLevel(); 225 226 if (xLev == null || xLev.getDepth() == 0) 227 return null; 229 XMLA_Member parent = null; 230 try { 231 parent = (XMLA_Member) ((XMLA_Member) member).getParent(); 232 } catch (OlapException e) { 233 logger.error("?", e); 234 return null; 235 } 236 237 return parent; 238 } 239 240 } 242 243 | Popular Tags |