KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
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 /**
38  * Member Tree Implementation vor XMLA
39  */

40 public class XMLA_MemberTree extends ExtensionSupport implements MemberTree {
41
42   static Logger logger = Logger.getLogger(XMLA_MemberTree.class);
43
44   /**
45    * Constructor sets ID
46    */

47   public XMLA_MemberTree() {
48     super.setId(MemberTree.ID);
49   }
50
51   /**
52    * @return the root members of a hierarchy. This is for example
53    * the "All" member or the list of measures.
54    */

55   public Member[] getRootMembers(Hierarchy hier) {
56     XMLA_Model model = (XMLA_Model) getModel();
57     Level[] levels = hier.getLevels();
58     // get root level
59
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; // should not occur
69

70     
71     final List JavaDoc visibleRootMembers = new ArrayList JavaDoc();
72     final List JavaDoc invisibleRootMembers = new ArrayList JavaDoc();
73     
74     Member[] rootMembers = new Member[0];
75     try {
76       rootMembers = rootLevel.getMembers();
77     } catch (OlapException e) {
78       logger.error(null, e);
79     }
80     // find the calculated members for this hierarchy
81
// show them together with root level members
82
ArrayList JavaDoc aCalcMem = new ArrayList JavaDoc();
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 JavaDoc dimUMember = StringUtil.bracketsAround(f.getFirstName());
92       String JavaDoc dimUHier = ((XMLA_Hierarchy) hier).getUniqueName();
93       if (!(dimUHier.equals(dimUMember)))
94         continue;
95
96       String JavaDoc memberName = f.getUniqeName();
97       XMLA_Member calcMem = (XMLA_Member) model.lookupMemberByUName(memberName);
98       if (calcMem == null) {
99           /* Strip brackets from name */
100           String JavaDoc[] nameParts = StringUtil.splitUniqueName(memberName);
101           calcMem = new XMLA_Member(model, memberName, nameParts[1], null, true);
102           //calcMem = new XMLA_Member(model, memberName, f.getLastName(), null, true);
103
}
104       aCalcMem.add(calcMem);
105     }
106     
107     // order members according to occurrence in query result
108
// if there is no result available, do not sort
109
// If the result contains invisible members, add them to the list
110
Result res = model.currentResult();
111     if (res != null) {
112         // locate the appropriate result axis
113
// find the Quax for this hier
114
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 JavaDoc positions = axis.getPositions();
123             
124             for (Iterator JavaDoc 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; // ignore, not root
130
if (!visibleRootMembers.contains(mem))
131                 visibleRootMembers.add(mem);
132               
133               // Check if the result axis contains invisible members
134
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 JavaDoc iter = aCalcMem.iterator(); iter.hasNext();) {
154       XMLA_Member calcMem = (XMLA_Member) iter.next();
155       members[k++] = calcMem;
156     }
157     for (Iterator JavaDoc iter = invisibleRootMembers.iterator(); iter.hasNext();) {
158         XMLA_Member invisibleMem = (XMLA_Member) iter.next();
159         members[k++] = invisibleMem;
160     }
161   
162     // If there is no query result, do not sort
163
if (visibleRootMembers.size() != 0) {
164         Arrays.sort(members, new Comparator JavaDoc() {
165           public int compare(Object JavaDoc arg0, Object JavaDoc 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; // m2 is higher, unvisible to the end
172
if (index1 == -1)
173               return 1; // m1 is higher, unvisible to the end
174
return index1 - index2;
175           }
176         });
177     }
178
179     return members;
180   }
181
182   /**
183    * @return true if the member has children
184    */

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(); // -1 if not initialized
191
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   /**
200    * @return the children of the member
201    */

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   /**
220    * @return the parent of member or null, if this is a root member
221    */

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; // already top level
228

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 } // End XMLA_MemberTree
241

242          
243
Popular Tags