1 13 package com.tonbeller.jpivot.table; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 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 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 positions; 60 61 64 public LevelAxisDecorator(Axis axis, MemberTree tree) { 65 this.axis = axis; 66 this.tree = tree; 67 computeLevelCount(); 68 makePositions(); 69 } 70 71 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 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 for (int i = 0; i < hierarchyCount; i++) { 99 if (skipAllMember[i]) 100 levelCount[i] -= 1; 101 } 102 103 totalLevelCount = 0; 105 for (int i = 0; i < hierarchyCount; i++) 106 totalLevelCount += levelCount[i]; 107 } 108 109 void makePositions() { 110 positions = new ArrayList (); 111 Iterator 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 141 private void addParents(Member[] result, int offset, int totalCount, int memberCount, Member member) { 142 int fillCount = totalCount - memberCount; 143 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 157 public List getPositions() { 158 return positions; 159 } 160 161 165 public Hierarchy[] getHierarchies() { 166 return axis.getHierarchies(); 167 } 168 169 172 public Axis getAxis() { 173 return axis; 174 } 175 176 public Object 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 192 public Position getPosition() { 193 return position; 194 } 195 196 199 public Member[] getMembers() { 200 return members; 201 } 202 203 public Object getRootDecoree() { 204 return position.getRootDecoree(); 205 } 206 207 210 public void accept(Visitor visitor) { 211 visitor.visitPosition(this); 212 } 213 214 } 215 216 219 public void accept(Visitor visitor) { 220 visitor.visitAxis(this); 221 } 222 223 } 224 | Popular Tags |