KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > test > ResultRenderer


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.test;
21
22 import com.tonbeller.jpivot.olap.model.Axis;
23 import com.tonbeller.jpivot.olap.model.Cell;
24 import com.tonbeller.jpivot.olap.model.Member;
25 import com.tonbeller.jpivot.olap.model.Position;
26 import com.tonbeller.jpivot.olap.model.Result;
27 import org.apache.log4j.Logger;
28 import java.io.BufferedWriter JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32
33
34 /**
35  * @author plucas
36  *
37  * TODO To change the template for this generated type comment go to
38  * Window - Preferences - Java - Code Style - Code Templates
39  */

40 public class ResultRenderer {
41     private static Logger logger = Logger.getLogger(ResultRenderer.class);
42
43     public static void renderHtml(Result result, String JavaDoc mdx, String JavaDoc outfile)
44         throws IOException JavaDoc {
45         long start = System.currentTimeMillis();
46
47         int i;
48
49         PrintWriter JavaDoc wout = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(
50                     new FileWriter JavaDoc(outfile)));
51
52         Axis[] axes = result.getAxes();
53
54         wout.println("<HTML>");
55         wout.println("<HEAD>");
56         wout.println(
57             "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">");
58         wout.println("<TITLE>Result from MDX Query</TITLE>");
59         wout.println("</HEAD>");
60         wout.println("<BODY>");
61
62         wout.println("<h1>MDX Query Result</h1>");
63
64         //String mdx = ((MdxOlapModel) model).getCurrentMdx();
65
if (mdx != null) {
66             wout.println("<p>");
67             wout.println(mdx);
68             wout.println("</p>");
69         }
70
71         wout.println("<table border=\"3\">");
72         wout.println("<thead><tr>");
73
74         if (axes.length == 0) {
75             // result is 0 dimensional
76
wout.println("<th>Slicer</th><th>Result</th>");
77             wout.println("</thead><tbody><tr>");
78
79             // slicer members as row header
80
renderSlicerRowHeader(result.getSlicer(), wout);
81
82             Cell cell = (Cell) result.getCells().get(0);
83             String JavaDoc value = cell.getFormattedValue();
84             wout.println("<td>" + value + "</td>");
85             wout.println("</tr>");
86         } else if (axes.length == 1) {
87             // result is 1 dimensional
88
// print position of axis 0 as column headers
89
renderColHeaders(wout, axes[0]);
90             wout.println("</tr></thead>");
91
92             wout.println("<tbody><tr>");
93             // slicer members as row header
94
renderSlicerRowHeader(result.getSlicer(), wout);
95
96             int n = 0;
97
98             for (i = 0; i < axes[0].getPositions().size(); i++) {
99                 Cell cell = (Cell) result.getCells().get(n++);
100                 String JavaDoc value = cell.getFormattedValue();
101                 wout.println("<td>" + value + "</td>");
102             }
103
104             wout.println("</tr>");
105         } else if (axes.length == 2) {
106             // assume 2 dimensional
107
// print position of axis 0 as column headers
108
renderColHeaders(wout, axes[0]);
109
110             wout.println("</tr></thead>");
111
112             // print rows, each one starting with row headers
113
wout.println("<tbody>");
114
115             Position[] positions = (Position[]) axes[1].getPositions()
116                                                        .toArray(new Position[0]);
117             int n = 0;
118
119             for (i = 0; i < positions.length; i++) {
120                 wout.println("<tr>");
121
122                 Member[] members = positions[i].getMembers();
123
124                 String JavaDoc caption = "";
125
126                 for (int j = 0; j < members.length; j++) {
127                     if (j > 0) {
128                         caption = caption + "<br>" + members[j].getLabel();
129                     } else {
130                         caption = members[j].getLabel();
131                     }
132                 }
133
134                 wout.println("<th>" + caption + "</th>");
135
136                 for (int j = 0; j < axes[0].getPositions().size(); j++) {
137                     Cell cell = (Cell) result.getCells().get(n++);
138                     String JavaDoc value = cell.getFormattedValue();
139                     wout.println("<td>" + value + "</td>");
140                 }
141
142                 wout.println("</tr>");
143             }
144         } else {
145             // cannot handle more than 2 axes
146
throw new IllegalArgumentException JavaDoc(
147                 "ResultBase.renderHtml cannot handle more than 2 axes");
148         }
149
150         wout.println("</tbody>");
151
152         wout.println("</BODY></HTML>");
153         wout.close();
154         logger.debug("render time: " + (System.currentTimeMillis() - start)
155             + "ms");
156     }
157
158     private static void renderColHeaders(PrintWriter JavaDoc wout, Axis axis) {
159         // print position of axis as column headers
160
wout.println("<th></th>");
161
162         Position[] positions = (Position[]) axis.getPositions()
163                                                 .toArray(new Position[0]);
164
165         for (int i = 0; i < positions.length; i++) {
166             Member[] members = positions[i].getMembers();
167
168             String JavaDoc caption = "";
169
170             for (int j = 0; j < members.length; j++) {
171                 if (j > 0) {
172                     caption = caption + "<br>" + members[j].getLabel();
173                 } else {
174                     caption = members[j].getLabel();
175                 }
176             }
177
178             wout.println("<th>" + caption + "</th>");
179         }
180     }
181
182     /**
183      * print row header from slicer axis
184      */

185     private static void renderSlicerRowHeader(Axis slicerax, PrintWriter JavaDoc wout) {
186         Position[] positions = (Position[]) slicerax.getPositions()
187                                                     .toArray(new Position[0]);
188         String JavaDoc caption = "";
189
190         for (int i = 0; i < positions.length; i++) {
191             Member[] members = positions[i].getMembers();
192
193             for (int j = 0; j < members.length; j++) {
194                 if ((j == 0) && (i == 0)) {
195                     caption = members[j].getLabel();
196                 } else {
197                     caption = caption + "<br>" + members[j].getLabel();
198                 }
199             }
200         }
201
202         wout.println("<th>" + caption + "</th>");
203     }
204
205     public static void main(String JavaDoc[] args) {
206     }
207 }
208
Popular Tags