KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > mondrian > MondrianResult


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.mondrian;
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.impl.FormatStringParser;
21 import com.tonbeller.jpivot.olap.query.ResultBase;
22 import mondrian.olap.Position;
23 import mondrian.olap.Member;
24 import mondrian.olap.MemoryLimitExceededException;
25
26 /**
27  * Result implementation for Mondrian
28  */

29 public class MondrianResult extends ResultBase {
30
31   private mondrian.olap.Result monResult = null;
32   private int[] posize;
33   private FormatStringParser formatStringParser = new FormatStringParser();
34
35   /**
36    * Constructor
37    * @param model the associated MondrianModel
38    */

39   protected MondrianResult(mondrian.olap.Result monResult, MondrianModel model)
40         throws MemoryLimitExceededException {
41     super(model);
42     this.monResult = monResult;
43
44     initData();
45   }
46
47   /**
48    * initData creates all the wrapper objects
49    */

50   private void initData() throws MemoryLimitExceededException {
51     MondrianModel mmodel = (MondrianModel) model;
52
53     mondrian.olap.Axis[] monAxes = monResult.getAxes();
54     // first step: walk through axes and add the members to the model
55
int nCells = 1;
56     posize = new int[monAxes.length];
57     for (int i = 0; i < monAxes.length; i++) {
58       List JavaDoc monPositions = monAxes[i].getPositions();
59       int size = 0;
60       Iterator JavaDoc pit = monPositions.iterator();
61       while (pit.hasNext()) {
62         Position position = (Position) pit.next();
63         Iterator JavaDoc mit = position.iterator();
64         while (mit.hasNext()) {
65           mmodel.addMember((Member) mit.next());
66         }
67         size++;
68       }
69       // check for OutOfMemory
70
mmodel.checkListener();
71
72       posize[i] = size;
73       nCells = nCells * size;
74     }
75     mondrian.olap.Axis monSlicer = monResult.getSlicerAxis();
76     List JavaDoc monPositions = monSlicer.getPositions();
77     Iterator JavaDoc pit = monPositions.iterator();
78     while (pit.hasNext()) {
79       Position position = (Position) pit.next();
80         Iterator JavaDoc mit = position.iterator();
81         while (mit.hasNext()) {
82           mmodel.addMember((Member) mit.next());
83         }
84         // check for OutOfMemory
85
mmodel.checkListener();
86     }
87
88     // second step: create the result data
89
axesList = new ArrayList JavaDoc();
90     for (int i = 0; i < monAxes.length; i++) {
91       axesList.add(new MondrianAxis(i, monAxes[i], mmodel));
92       // check for OutOfMemory
93
mmodel.checkListener();
94     }
95     slicer = new MondrianAxis(-1, monSlicer, mmodel);
96
97     int[] iar = new int[monAxes.length];
98     for (int i = 0; i < monAxes.length; i++) {
99       iar[i] = 0;
100     }
101     for (int i = 0; i < nCells; i++) {
102       mondrian.olap.Cell monCell = monResult.getCell(iar);
103       MondrianCell cell = new MondrianCell(monCell, mmodel);
104       cell.setFormattedValue(monCell.getFormattedValue(), formatStringParser);
105       aCells.add(cell);
106       if (nCells > 1) {
107         // not for 0-dimensional case
108
increment(iar);
109       }
110
111       // check for OutOfMemory every 1000 cells created
112
if (i % 1000 == 0) {
113         mmodel.checkListener();
114       }
115     }
116
117   }
118
119   /**
120    * increment int array according to size of axis positions
121    * first index changes fastest
122    * (0,0), (1,0) ... (NX-1, 0)
123    * (0,1), (1,1) ... (NX-1, 1)
124    */

125   private void increment(int[] iar) {
126     int nn = ++iar[0];
127     // done for the 1-dimensional case
128
if (iar.length > 1 && nn >= posize[0]) {
129       iar[0] = 0;
130       for (int i = 1; i < iar.length; i++) {
131         int kk = ++iar[i];
132         if (kk < posize[i])
133           break;
134         else
135           iar[i] = 0;
136       }
137     }
138   }
139
140   /**
141    * Returns the axes.
142    * @return Axis[]
143    */

144   public Axis[] getAxes() {
145     if (monResult == null)
146       return null; // todo error handling
147
return (Axis[]) axesList.toArray(new MondrianAxis[0]);
148   }
149
150 } // MondrianResult
151
Popular Tags