forked from OSBI/saiku-mondrian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmorph_pivot.jsp
311 lines (301 loc) · 11.9 KB
/
morph_pivot.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<%--
// This software is subject to the terms of the Eclipse Public License v1.0
// Agreement, available at the following URL:
// http://www.eclipse.org/legal/epl-v10.html.
// You must accept the terms of that agreement to use this software.
//
// Copyright (C) 2002-2005 Julian Hyde and others
// All Rights Reserved.
//
// Julian Hyde, June 20, 2002
--%>
<%@ page language="java"
import="mondrian.web.taglib.ResultCache,
mondrian.olap.*,
java.io.PrintWriter,
java.util.ArrayList,
java.util.List,
java.util.Iterator,
java.util.LinkedList,
java.io.IOException"%>
<%@ taglib uri="/WEB-INF/mdxtable.tld" prefix="mdx" %>
<%!
static final String queryName = "pivotQuery";
static final String redirect = "pivot.jsp";
String actionURL(String operation) {
return "mdxquery?query=" + queryName +
"&" + operation +
"&redirect=" + redirect;
}
String removeHierarchyUrl(Hierarchy hierarchy) {
return actionURL(
"operation=remove_hierarchy&hierarchy=" +
hierarchy.getUniqueName());
}
String moveHierarchyUpURL(Hierarchy hierarchy) {
return actionURL(
"operation=move_hierarchy_up&hierarchy=" +
hierarchy.getUniqueName());
}
String moveHierarchyDownURL(Hierarchy hierarchy) {
return actionURL(
"operation=move_hierarchy_down&hierarchy=" +
hierarchy.getUniqueName());
}
String moveHierarchyToAxisURL(Hierarchy hierarchy, int targetAxis) {
return actionURL(
"operation=move_hierarchy_to_axis&hierarchy=" +
hierarchy.getUniqueName() + "&to_axis=" + targetAxis);
}
String replaceHierarchyURL(Hierarchy hierarchy, String expression) {
return actionURL(
"operation=replace_hierarchy&hierarchy=" +
hierarchy.getUniqueName() + "&expression=" + expression);
}
String setSlicerURL(Member member) {
return actionURL(
"operation=set_slicer&member=" + member.getUniqueName());
}
String doubleQuote(String s) {
return Util.quoteForMdx(s);
}
String makeHref(String url, String text) {
if (url == null) {
return "<i>" + text + "</i>";
} else {
return "<a href=" + doubleQuote(url) + ">" + text + "</a>";
}
}
ArrayList getMembersOnAxis(Result result, int axisOffset, int offset) {
ArrayList list = new ArrayList();
Position[] positions = getAxis(result,axisOffset).positions;
for (int i = 0; i < positions.length; i++) {
Position position = positions[i];
if (!list.contains(position.members[offset])) {
list.add(position.members[offset]);
}
}
return list;
}
/**
* Returns all of a member's ancestors, including itself.
*/
Member[] getAncestors(Member member) {
LinkedList list = new LinkedList();
for (Member m = member; m != null; m = m.getParentMember()) {
list.addFirst(m);
}
return (Member[]) list.toArray(new Member[list.size()]);
}
/**
* Returns all of a member's ancestors, including itself, and their
* siblings, and its children, in prefix order.
*/
Member[] getAncestorsAndSiblings(Member member) {
LinkedList list = new LinkedList();
Member[] children = getSchemaReader(member).getMemberChildren(member);
if (children != null && children.length > 0) {
member = children[0];
}
getAncestorsAndSiblings(member,list);
return (Member[]) list.toArray(new Member[list.size()]);
}
void getAncestorsAndSiblings(Member member, List list) {
// member's parent
// older siblings
// member
// member's children
// younger siblings
Member parent = member.getParentMember();
Member[] siblings;
int parentPos;
if (parent != null) {
getAncestorsAndSiblings(parent, list);
parentPos = list.indexOf(parent);
siblings = getSchemaReader(member).getMemberChildren(parent);
} else {
parentPos = -1;
final Hierarchy hierarchy = member.getHierarchy();
final Schema schema = hierarchy.getDimension().getSchema();
siblings = schema.getSchemaReader().getHierarchyRootMembers(hierarchy);
}
for (int i = 0; i < siblings.length; i++) {
Member sibling = siblings[i];
list.add(parentPos + 1 + i, sibling);
}
}
void generateAxis(Result result, int axis, JspWriter out)
throws IOException {
Hierarchy[] hierarchies = getHierarchiesOnAxis(result, axis);
out.write("<h2>" + getAxisName(axis) + "</h2>"); // e.g. "<p>Columns:</p>"
out.write("<ul>");
for (int i = 0; i < hierarchies.length; i++) {
Hierarchy hierarchy = hierarchies[i];
out.write("<li>");
out.write(hierarchy.getName());
out.write(" [" + makeHref(removeHierarchyUrl(hierarchy), "remove") + "]");
out.write(" [" + makeHref(i == 0 ? null : moveHierarchyUpURL(hierarchy), "move up") + "]");
out.write(" [" + makeHref(i == hierarchies.length - 1 ? null : moveHierarchyDownURL(hierarchy), "move down") + "]");
for (int j = -1; j < result.getAxes().length; j++) {
if (j != axis) {
out.write(" [" + makeHref(moveHierarchyToAxisURL(hierarchy,j), "move to " + getAxisName(j)) + "]");
}
}
if (axis == SLICER_AXIS) {
// Want to generate something like the following.
// <li>Time [remove] [move to columns] [move to rows]<blockquote>
// <table border="1" cellspacing="0" id="AutoNumber12">
/// <tr>
// <td width="100%"><u>1997</u><br>
// <u>Q1</u><br>
// <u>Q2</u><br>
// Q3 (current)<br>
// <u>July</u><br>
// <u>August</u><br>
// <u>September</u><br>
// <u>Q4</u><br>
// <u>1998</u><br>
// <u>1999</u></td>
// </tr>
// </table>
// </blockquote></li>
out.write("<blockquote><table border=1 cellspacing=0><tr><td>");
Member member = result.getSlicerAxis().positions[0].members[i];
Member[] ancestors = getAncestorsAndSiblings(member);
for (int j = 0; j < ancestors.length; j++) {
if (j > 0) {
out.write("<br/>");
out.newLine();
}
Member ancestor = ancestors[j];
for (int k = 0, depth = ancestor.getLevel().getDepth(); k < depth; k++) {
out.write(" ");
}
if (ancestor == member) {
out.write(ancestor.getName() + " (current)");
} else {
out.write(makeHref(setSlicerURL(ancestor), ancestor.getName()));
}
}
out.write("</td></tr></table></blockquote>");
}
out.write("</li>");
out.newLine();
if (hierarchy.getDimension().isMeasures()) {
out.write("<ul>");
List membersList = getMembersOnAxis(result, axis, i);
for (Iterator iterator = membersList.iterator(); iterator.hasNext();) {
Member member = (Member) iterator.next();
out.write("<li>" + member.getName());
out.write(" [" + makeHref(replaceHierarchyURL(hierarchy,"{todo: [Measures].[A],[Measures].[B], [Measures].[C]}"),"move up") + "]");
out.write(" [" + makeHref(replaceHierarchyURL(hierarchy,"{todo: [Measures].[A],[Measures].[B], [Measures].[C]}"),"move down") + "]");
out.write(" [" + makeHref(replaceHierarchyURL(hierarchy,"{todo: [Measures].[A],[Measures].[B], [Measures].[C]}"),"remove") + "]");
out.write("</li>");
out.newLine();
}
final SchemaReader schemaReader = hierarchy.getDimension().getSchema().getSchemaReader();
Member[] allMeasures = schemaReader.getHierarchyRootMembers(hierarchy);
for (int j = 0; j < allMeasures.length; j++) {
Member measure = allMeasures[j];
if (membersList.contains(measure)) {
continue;
}
out.write("<li>" + measure.getName());
out.write(" [" + makeHref(replaceHierarchyURL(hierarchy,"{todo: [Measures].[A],[Measures].[B], [Measures].[C]}"),"add") + "]");
out.write("</li>");
out.newLine();
}
out.write("</ul>");
out.newLine();
}
}
out.write("</ul>");
out.newLine();
}
SchemaReader getSchemaReader(OlapElement element) {
return null;
}
Hierarchy[] getHierarchiesOnAxis(Result result, int axis) {
Axis resultAxis = getAxis(result,axis);
Position position = resultAxis.positions[0];
int count = position.members.length;
Hierarchy[] hierarchies = new Hierarchy[count];
for (int i = 0; i < hierarchies.length; i++) {
hierarchies[i] = position.members[i].getHierarchy();
}
return hierarchies;
}
boolean isHierarchyUsed(Result result, Hierarchy hierarchy) {
for (int i = -1, axisCount = result.getAxes().length;
i < axisCount; i++) {
Axis axis = getAxis(result, i);
for (int j = 0; j < axis.positions[0].members.length; j++) {
Member member = axis.positions[0].members[j];
if (member.getHierarchy() == hierarchy) {
return true;
}
}
}
return false;
}
Axis getAxis(Result result, int axis) {
if (axis < 0) {
return result.getSlicerAxis();
} else {
return result.getAxes()[axis];
}
}
private static final int SLICER_AXIS = -1;
private static final int COLUMNS_AXIS = 0;
private static final int ROWS_AXIS = 1;
String getAxisName(int axis) {
switch (axis) {
case -1: return "slicer";
case 0: return "columns";
case 1: return "rows";
default: return "axis#" + axis;
}
}
%>
<html>
<head>
<title>Morph Pivot Table</title>
</head>
<%
ResultCache rc = ResultCache.getInstance(
pageContext.getSession(), pageContext.getServletContext(), queryName);
Query query = rc.getQuery();
QueryAxis[] axes = query.axes;
Result result = rc.getResult();
%>
<body>
<h1>Organize hierarchies</h1>
<%
generateAxis(result, COLUMNS_AXIS, out);
generateAxis(result, ROWS_AXIS, out);
generateAxis(result, SLICER_AXIS, out);
%>
<h2>Hierarchies not shown:</h2>
<ul>
<%
Cube cube = query.getCube();
for (int i = 0; i < cube.getDimensions().length; i++) {
Dimension dimension = cube.getDimensions()[i];
for (int j = 0; j < dimension.getHierarchies().length; j++) {
Hierarchy hierarchy = dimension.getHierarchies()[j];
if (isHierarchyUsed(result, hierarchy)) {
continue;
}
out.write("<li>" + hierarchy.getName());
for (int k = -1; k < result.getAxes().length; k++) {
out.write(" [" + makeHref(moveHierarchyToAxisURL(hierarchy,k), "add to " + getAxisName(k)) + "]");
}
out.write("</li>");
out.newLine();
}
}
%>
</ul>
<p>[<u>OK</u>]</td>
</body>
<%-- End morph_pivot.jsp --%>