KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > test > olap > TestOlapModelUtils


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.test.olap;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import com.tonbeller.jpivot.olap.model.Axis;
21 import com.tonbeller.jpivot.olap.model.Hierarchy;
22 import com.tonbeller.jpivot.olap.model.Member;
23 import com.tonbeller.jpivot.olap.model.Position;
24
25 /**
26  * Created on 02.12.2002
27  *
28  * @author av
29  */

30 public class TestOlapModelUtils {
31
32   /**
33    * private Constructor for TestOlapModelUtils.
34    */

35   private TestOlapModelUtils() {
36   }
37
38   public static List JavaDoc getVisible(TestDimension dim) {
39     TestHierarchy th = (TestHierarchy) dim.getHierarchies()[0];
40     List JavaDoc roots = Arrays.asList(th.getRootMembers());
41     ArrayList JavaDoc list = new ArrayList JavaDoc();
42     getVisible(roots, list);
43     return list;
44   }
45
46   static void getVisible(List JavaDoc members, ArrayList JavaDoc list) {
47     Iterator JavaDoc it = members.iterator();
48     while (it.hasNext()) {
49       TestMember m = (TestMember) it.next();
50       if (m.isVisible())
51         list.add(m);
52       getVisible(m.getChildMember(), list);
53     }
54   }
55
56   public static TestAxis createAxis(Member[] members) {
57     List JavaDoc list = Arrays.asList(members);
58     return createAxis(list);
59   }
60
61   public static TestAxis createAxis(List JavaDoc members) {
62     TestAxis a = new TestAxis();
63     Iterator JavaDoc it = members.iterator();
64     while (it.hasNext()) {
65       Member m = (Member) it.next();
66       TestPosition p = new TestPosition(a);
67       p.setMembers(new Member[] { m });
68       a.addPosition(p);
69     }
70     return a;
71   }
72
73   public static TestAxis crossJoin(Axis a1, Axis a2) {
74     TestAxis a = new TestAxis();
75     Iterator JavaDoc i1 = a1.getPositions().iterator();
76     while (i1.hasNext()) {
77       Member[] m1 = ((Position) i1.next()).getMembers();
78       Iterator JavaDoc i2 = a2.getPositions().iterator();
79       while (i2.hasNext()) {
80         Member[] m2 = ((Position) i2.next()).getMembers();
81
82         Member[] m = new Member[m1.length + m2.length];
83         for (int i = 0; i < m1.length; i++)
84           m[i] = m1[i];
85         for (int i = 0, j = m1.length; i < m2.length; i++, j++)
86           m[j] = m2[i];
87         TestPosition p = new TestPosition(a);
88         p.setMembers(m);
89         a.addPosition(p);
90
91       }
92     }
93     return a;
94   }
95
96   public static TestAxis createAxis(TestDimension dim) {
97     return createAxis(getVisible(dim));
98   }
99
100   public static TestAxis createAxis(TestDimension[] dims) {
101     TestAxis a = createAxis(getVisible(dims[0]));
102     for (int i = 1; i < dims.length; i++) {
103       TestAxis b = createAxis(getVisible(dims[i]));
104       a = crossJoin(a, b);
105     }
106     return a;
107   }
108
109   public static List JavaDoc findPositions(TestAxis axis, Member member) {
110     List JavaDoc list = new ArrayList JavaDoc();
111     for (Iterator JavaDoc it = axis.getPositions().iterator(); it.hasNext();) {
112       Object JavaDoc o = it.next();
113       TestPosition p = (TestPosition) o;
114       if (p.contains(member))
115         list.add(p);
116     }
117     return list;
118   }
119
120   public static TestAxis findAxis(TestOlapModel model, Member member) {
121     TestHierarchy hier = (TestHierarchy) member.getLevel().getHierarchy();
122     return findAxis(model, hier);
123   }
124
125   public static TestAxis findAxis(TestOlapModel model, Hierarchy hier) {
126     TestAxis[] axes = model.getAxes();
127     for (int i = 0; i < axes.length; i++) {
128       Hierarchy[] th = axes[i].getHierarchies();
129       for (int j = 0; j < th.length; j++)
130         if (hier.equals(th[j]))
131           return axes[i];
132     }
133     return null;
134   }
135
136
137   public static void rebuildAxis(TestOlapModel tom, TestMember m) {
138     // find hierarchies
139
TestAxis axis = TestOlapModelUtils.findAxis(tom, m);
140     rebuildAxis(tom, axis);
141   }
142
143   public static void rebuildAxis(TestOlapModel tom, TestAxis axis) {
144     Hierarchy[] dims = axis.getHierarchies();
145
146     // crossjoin a new axis
147
TestAxis newAxis = TestOlapModelUtils.createAxis((TestDimension) dims[0].getDimension());
148     for (int i = 1; i < dims.length; i++) {
149       TestAxis ax = TestOlapModelUtils.createAxis((TestDimension) dims[i].getDimension());
150       newAxis = TestOlapModelUtils.crossJoin(newAxis, ax);
151     }
152
153     // replace old axis
154
TestAxis[] axes = tom.getAxes();
155     for (int i = 0; i < axes.length; i++)
156       if (axes[i] == axis)
157         axes[i] = newAxis;
158   }
159
160   public static void hideAll(Hierarchy hier) {
161     TestMember[] members = ((TestHierarchy) hier).getRootMembers();
162     for (int i = 0; i < members.length; i++)
163       recurseHideAll(members[i]);
164   }
165
166   private static void recurseHideAll(TestMember member) {
167     member.setVisible(false);
168     if (member.hasChildren()) {
169       for (Iterator JavaDoc it = member.getChildMember().iterator(); it.hasNext();) {
170         TestMember child = (TestMember) it.next();
171         recurseHideAll(child);
172       }
173     }
174   }
175
176   public static void setVisible(List JavaDoc members) {
177     if (members.size() > 0) {
178         Member m0 = (Member) members.get(0);
179         hideAll(m0.getLevel().getHierarchy());
180         for (Iterator JavaDoc it = members.iterator(); it.hasNext();)
181            ((TestMember) it.next()).setVisible(true);
182     }
183   }
184
185   public static void setVisible(Member[] members) {
186     hideAll(members[0].getLevel().getHierarchy());
187     for (int i = 0; i < members.length; i++)
188        ((TestMember) members[i]).setVisible(true);
189   }
190
191   public static int indexOf(Object JavaDoc[] array, Object JavaDoc obj) {
192     for (int i = 0; i < array.length; i++)
193       if (array[i] == obj)
194         return i;
195     return -1;
196   }
197
198 }
199
Popular Tags