KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > rolap > RolapCell


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/rolap/RolapCell.java#23 $
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) 2005-2007 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.rolap;
11
12 import mondrian.olap.*;
13 import mondrian.rolap.agg.AggregationManager;
14 import mondrian.rolap.agg.CellRequest;
15
16 import java.sql.*;
17
18 /**
19  * <code>RolapCell</code> implements {@link mondrian.olap.Cell} within a
20  * {@link RolapResult}.
21  */

22 class RolapCell implements Cell {
23     private final RolapResult result;
24     protected final int[] pos;
25     protected RolapResult.CellInfo ci;
26
27     RolapCell(RolapResult result, int[] pos, RolapResult.CellInfo ci) {
28         this.result = result;
29         this.pos = pos;
30         this.ci = ci;
31     }
32
33     public Object JavaDoc getValue() {
34         return ci.value;
35     }
36
37     public String JavaDoc getCachedFormatString() {
38         return ci.formatString;
39     }
40
41     public String JavaDoc getFormattedValue() {
42         return ci.getFormatValue();
43     }
44
45     public boolean isNull() {
46         return (ci.value == Util.nullValue);
47     }
48
49     public boolean isError() {
50         return (ci.value instanceof Throwable JavaDoc);
51     }
52
53     /**
54      * Create an sql query that, when executed, will return the drill through
55      * data for this cell. If the parameter extendedContext is true, then the
56      * query will include all the levels (i.e. columns) of non-constraining
57      * members (i.e. members which are at the "All" level).
58      * If the parameter extendedContext is false, the query will exclude
59      * the levels (coulmns) of non-constraining members.
60      */

61     public String JavaDoc getDrillThroughSQL(boolean extendedContext) {
62         RolapAggregationManager aggMan = AggregationManager.instance();
63         final Member[] currentMembers = getMembers();
64         CellRequest cellRequest = RolapAggregationManager.makeRequest(
65                 currentMembers, extendedContext, true);
66         return (cellRequest == null)
67             ? null
68             : aggMan.getDrillThroughSql(cellRequest, false);
69     }
70
71
72     public int getDrillThroughCount() {
73         RolapAggregationManager aggMan = AggregationManager.instance();
74         final Member[] currentMembers = getMembers();
75         CellRequest cellRequest = RolapAggregationManager.makeRequest(
76             currentMembers, false, true);
77         if (cellRequest == null) {
78             return -1;
79         }
80         RolapConnection connection =
81             (RolapConnection) result.getQuery().getConnection();
82         final String JavaDoc sql = aggMan.getDrillThroughSql(cellRequest, true);
83         final SqlStatement stmt =
84             RolapUtil.executeQuery(
85                 connection.getDataSource(),
86                 sql,
87                 "RolapCell.getDrillThroughCount",
88                 "Error while counting drill-through");
89         try {
90             ResultSet rs = stmt.getResultSet();
91             rs.next();
92             ++stmt.rowCount;
93             return rs.getInt(1);
94         } catch (SQLException e) {
95             throw stmt.handle(e);
96         } finally {
97             stmt.close();
98         }
99     }
100
101     /**
102      * Returns whether it is possible to drill through this cell.
103      * Drill-through is possible if the measure is a stored measure
104      * and not possible for calculated measures.
105      *
106      * @return true if can drill through
107      */

108     public boolean canDrillThrough() {
109         // get current members
110
final Member[] currentMembers = getMembers();
111         // First member is the measure, test if it is stored measure, return
112
// true if it is, false if not.
113
return (currentMembers[0] instanceof RolapStoredMeasure);
114     }
115
116     private RolapEvaluator getEvaluator() {
117         return result.getCellEvaluator(pos);
118     }
119     private Member[] getMembers() {
120         return result.getCellMembers(pos);
121     }
122
123     public Object JavaDoc getPropertyValue(String JavaDoc propertyName) {
124         Property property = Property.lookup(propertyName, true);
125         Object JavaDoc defaultValue = null;
126         if (property != null) {
127             switch (property.ordinal) {
128             case Property.CELL_ORDINAL_ORDINAL:
129                 return result.getCellOrdinal(pos);
130             case Property.VALUE_ORDINAL:
131                 return getValue();
132             case Property.FORMAT_STRING_ORDINAL:
133                 if (ci.formatString == null) {
134                     ci.formatString = getEvaluator().getFormatString();
135                 }
136                 return ci.formatString;
137             case Property.FORMATTED_VALUE_ORDINAL:
138                 return getFormattedValue();
139             case Property.FONT_FLAGS_ORDINAL:
140                 defaultValue = 0;
141                 break;
142             case Property.SOLVE_ORDER_ORDINAL:
143                 defaultValue = 0;
144                 break;
145             default:
146                 // fall through
147
}
148         }
149         return getEvaluator().getProperty(propertyName, defaultValue);
150     }
151
152     public Member getContextMember(Dimension dimension) {
153         return result.getMember(pos, dimension);
154     }
155 }
156
157 // End RolapCell.java
158
Popular Tags