KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > ResultBase


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/ResultBase.java#11 $
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) 2001-2002 Kana Software, Inc.
7 // Copyright (C) 2001-2007 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 //
11 // jhyde, 10 August, 2001
12 */

13
14 package mondrian.olap;
15
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import org.apache.log4j.Logger;
19 import java.io.PrintWriter JavaDoc;
20
21 /**
22  * Skeleton implementation of {@link Result}.
23  *
24  * @author jhyde
25  * @since 10 August, 2001
26  * @version $Id: //open/mondrian/src/main/mondrian/olap/ResultBase.java#11 $
27  */

28 public abstract class ResultBase implements Result {
29     protected final Query query;
30     protected final Axis[] axes;
31     protected Axis slicerAxis;
32
33     protected ResultBase(Query query, Axis[] axes) {
34         this.query = query;
35         this.axes = axes;
36     }
37
38     protected abstract Logger getLogger();
39
40     public Query getQuery() {
41         return query;
42     }
43
44     // implement Result
45
public Axis[] getAxes() {
46         return axes;
47     }
48     // implement Result
49
public Axis getSlicerAxis() {
50         return slicerAxis;
51     }
52     // implement Result
53
public void print(PrintWriter JavaDoc pw) {
54         for (int i = -1; i < axes.length; i++) {
55             pw.println("Axis #" + (i + 1) + ":");
56             printAxis(pw, i < 0 ? slicerAxis : axes[i]);
57         }
58         // Usually there are 3 axes: {slicer, columns, rows}. Position is a
59
// {column, row} pair. We call printRows with axis=2. When it recurses
60
// to axis=-1, it prints.
61
int[] pos = new int[axes.length];
62         printRows(pw, axes.length - 1, pos);
63     }
64     private void printRows(PrintWriter JavaDoc pw, int axis, int[] pos) {
65         Axis _axis = axis < 0 ? slicerAxis : axes[axis];
66         List JavaDoc<Position> positions = _axis.getPositions();
67         int i = 0;
68         for (Position position: positions) {
69             if (axis < 0) {
70                 if (i > 0) {
71                     pw.print(", ");
72                 }
73                 printCell(pw, pos);
74             } else {
75                 pos[axis] = i;
76                 if (axis == 0) {
77                     int row = axis + 1 < pos.length ? pos[axis + 1] : 0;
78                     pw.print("Row #" + row + ": ");
79                 }
80                 printRows(pw, axis - 1, pos);
81                 if (axis == 0) {
82                     pw.println();
83                 }
84             }
85             i++;
86         }
87 /*
88         for (int i = 0, count = positions.size(); i < count; i++) {
89             if (axis < 0) {
90                 if (i > 0) {
91                     pw.print(", ");
92                 }
93                 printCell(pw, pos);
94             } else {
95                 pos[axis] = i;
96                 if (axis == 0) {
97                     int row = axis + 1 < pos.length ? pos[axis + 1] : 0;
98                     pw.print("Row #" + row + ": ");
99                 }
100                 printRows(pw, axis - 1, pos);
101                 if (axis == 0) {
102                     pw.println();
103                 }
104             }
105         }
106 */

107     }
108     private void printAxis(PrintWriter JavaDoc pw, Axis axis) {
109         List JavaDoc<Position> positions = axis.getPositions();
110         for (Position position: positions) {
111             boolean firstTime = true;
112             pw.print("{");
113             for (Member member: position) {
114                 if (! firstTime) {
115                     pw.print(", ");
116                 }
117                 pw.print(member.getUniqueName());
118                 firstTime = false;
119             }
120             pw.println("}");
121         }
122     }
123     private void printCell(PrintWriter JavaDoc pw, int[] pos) {
124         Cell cell = getCell(pos);
125         pw.print(cell.getFormattedValue());
126     }
127
128     /**
129      * Returns the current member of a given dimension at a given location.
130      */

131     public Member getMember(int[] pos, Dimension dimension) {
132         for (int i = -1; i < axes.length; i++) {
133             Axis axis = slicerAxis;
134             int index = 0;
135             if (i >= 0) {
136                 axis = axes[i];
137                 index = pos[i];
138             }
139             List JavaDoc<Position> positions = axis.getPositions();
140             Position position = positions.get(index);
141             for (Member member: position) {
142                 if (member.getDimension() == dimension) {
143                     return member;
144                 }
145             }
146         }
147         return dimension.getHierarchy().getDefaultMember();
148     }
149
150     public void close() {
151     }
152 }
153
154
155 // End ResultBase.java
156
Popular Tags