KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > table > LevelAxisDecorator


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.table;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import com.tonbeller.jpivot.olap.model.Axis;
20 import com.tonbeller.jpivot.olap.model.Hierarchy;
21 import com.tonbeller.jpivot.olap.model.Member;
22 import com.tonbeller.jpivot.olap.model.Position;
23 import com.tonbeller.jpivot.olap.model.Visitor;
24 import com.tonbeller.jpivot.olap.navi.MemberTree;
25
26 /**
27  * Decorates an Axis by adding the parents of all members.
28  * Every Position will contain an equal number of members,
29  * where some of them will be the same. For example, an
30  * axis in the result contains 2 members in 1 hierarchy
31  * <p>
32  * <table>
33  * <tr><th> </th><th>Revenue</th></tr>
34  * <tr><td>USA</td><td>1000</td></tr>
35  * <tr><td>CA </td><td>100</td></tr>
36  * </table>
37  * <p>
38  * will become
39  * <p>
40  * <table>
41  * <tr><th> </th><th> </th><th>Revenue</th></tr>
42  * <tr><td>USA</td><td>USA</td><td>1000</td></tr>
43  * <tr><td>USA</td><td>CA </td><td>100</td></tr>
44  * </table>
45  *
46  * <p>
47  * If the all member is not visible on the axis, its not added as a parent, because
48  * this would not add information. Otherwise its added like other parent members too.
49  * @author av
50  */

51
52 public class LevelAxisDecorator implements Axis {
53   Axis axis;
54   MemberTree tree;
55   int[] levelCount;
56   boolean[] skipAllMember;
57   int totalLevelCount;
58
59   List JavaDoc positions;
60
61   /**
62    * Constructor for LevelAxisDecorator.
63    */

64   public LevelAxisDecorator(Axis axis, MemberTree tree) {
65     this.axis = axis;
66     this.tree = tree;
67     computeLevelCount();
68     makePositions();
69   }
70
71   /**
72    * for each hierarchy of the underlying axis compute the
73    * number of levels (maxRootDistance - minRootDistance).
74    */

75   void computeLevelCount() {
76     Hierarchy[] hiers = axis.getHierarchies();
77     int hierarchyCount = axis.getHierarchies().length;
78     levelCount = new int[hierarchyCount];
79     skipAllMember = new boolean[hierarchyCount];
80     for (int i = 0; i < hiers.length; i++) {
81       levelCount[i] = Integer.MIN_VALUE;
82       skipAllMember[i] = hiers[i].hasAll();
83     }
84
85     Iterator JavaDoc it = axis.getPositions().iterator();
86     while (it.hasNext()) {
87       Position p = (Position) it.next();
88       Member[] members = p.getMembers();
89       for (int i = 0; i < members.length; i++) {
90         int count = members[i].getRootDistance() + 1;
91         levelCount[i] = Math.max(levelCount[i], count);
92         if (members[i].isAll())
93           skipAllMember[i] = false;
94       }
95     }
96
97     // if the ALL member is not on the axis, we will not add it
98
for (int i = 0; i < hierarchyCount; i++) {
99       if (skipAllMember[i])
100         levelCount[i] -= 1;
101     }
102
103     // the number of members per position is the sum of all deltas
104
totalLevelCount = 0;
105     for (int i = 0; i < hierarchyCount; i++)
106       totalLevelCount += levelCount[i];
107   }
108
109   void makePositions() {
110     positions = new ArrayList JavaDoc();
111     Iterator JavaDoc it = axis.getPositions().iterator();
112     while (it.hasNext()) {
113       Position p = (Position) it.next();
114       positions.add(makePosition(p));
115     }
116   }
117
118   private Position makePosition(Position source) {
119     Member[] members = source.getMembers();
120     Member[] result = new Member[totalLevelCount];
121     int offset = 0;
122     for (int i = 0; i < members.length; i++) {
123       int totalCount = levelCount[i];
124       int memberCount = members[i].getRootDistance() + 1;
125       if (skipAllMember[i])
126         memberCount -= 1;
127       addParents(result, offset, totalCount, memberCount, members[i]);
128       offset += totalCount;
129     }
130     return new MyPosition(source, result);
131   }
132
133   /**
134    * adds members to result array from right to left, starting at offset
135    * @param result the array to add the members to
136    * @param offset the offset in the array
137    * @param totalCount number of positions to fill in the array
138    * @param memberCount the number of different members to add, rest will be padded
139    * @param member start member
140    */

141   private void addParents(Member[] result, int offset, int totalCount, int memberCount, Member member) {
142     int fillCount = totalCount - memberCount;
143     // fill from right to left because we want the parents to appear left
144
offset = offset + totalCount - 1;
145     for (int i = 0; i < fillCount; i++)
146       result[offset--] = member;
147
148     for (int i = 0; i < memberCount; i++) {
149       result[offset--] = member;
150       member = tree.getParent(member);
151     }
152   }
153
154   /**
155    * @see com.tonbeller.jpivot.olap.model.Axis#getPositions()
156    */

157   public List JavaDoc getPositions() {
158     return positions;
159   }
160
161   /**
162    * returns the hierarchies of the underlying axis.
163    * @see com.tonbeller.jpivot.olap.model.Axis#getHierarchies()
164    */

165   public Hierarchy[] getHierarchies() {
166     return axis.getHierarchies();
167   }
168
169   /**
170    * @see com.tonbeller.jpivot.olap.model.impl.AxisDecorator#getAxis()
171    */

172   public Axis getAxis() {
173     return axis;
174   }
175
176   public Object JavaDoc getRootDecoree() {
177     return axis.getRootDecoree();
178   }
179
180   private static class MyPosition implements Position {
181     Position position;
182     Member[] members;
183
184     MyPosition(Position position, Member[] members) {
185       this.position = position;
186       this.members = members;
187     }
188
189     /**
190      * @see com.tonbeller.jpivot.olap.model.impl.PositionDecorator#getPosition()
191      */

192     public Position getPosition() {
193       return position;
194     }
195
196     /**
197      * @see com.tonbeller.jpivot.olap.model.Position#getMembers()
198      */

199     public Member[] getMembers() {
200       return members;
201     }
202
203     public Object JavaDoc getRootDecoree() {
204       return position.getRootDecoree();
205     }
206
207     /**
208      * @see com.tonbeller.jpivot.olap.model.Visitable#accept(Visitor)
209      */

210     public void accept(Visitor visitor) {
211       visitor.visitPosition(this);
212     }
213
214   }
215
216   /**
217    * @see com.tonbeller.jpivot.olap.model.Visitable#accept(Visitor)
218    */

219   public void accept(Visitor visitor) {
220     visitor.visitAxis(this);
221   }
222
223 }
224
Popular Tags