1 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 ; 29 import java.io.FileWriter ; 30 import java.io.IOException ; 31 import java.io.PrintWriter ; 32 33 34 40 public class ResultRenderer { 41 private static Logger logger = Logger.getLogger(ResultRenderer.class); 42 43 public static void renderHtml(Result result, String mdx, String outfile) 44 throws IOException { 45 long start = System.currentTimeMillis(); 46 47 int i; 48 49 PrintWriter wout = new PrintWriter (new BufferedWriter ( 50 new FileWriter (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 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 wout.println("<th>Slicer</th><th>Result</th>"); 77 wout.println("</thead><tbody><tr>"); 78 79 renderSlicerRowHeader(result.getSlicer(), wout); 81 82 Cell cell = (Cell) result.getCells().get(0); 83 String value = cell.getFormattedValue(); 84 wout.println("<td>" + value + "</td>"); 85 wout.println("</tr>"); 86 } else if (axes.length == 1) { 87 renderColHeaders(wout, axes[0]); 90 wout.println("</tr></thead>"); 91 92 wout.println("<tbody><tr>"); 93 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 value = cell.getFormattedValue(); 101 wout.println("<td>" + value + "</td>"); 102 } 103 104 wout.println("</tr>"); 105 } else if (axes.length == 2) { 106 renderColHeaders(wout, axes[0]); 109 110 wout.println("</tr></thead>"); 111 112 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 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 value = cell.getFormattedValue(); 139 wout.println("<td>" + value + "</td>"); 140 } 141 142 wout.println("</tr>"); 143 } 144 } else { 145 throw new IllegalArgumentException ( 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 wout, Axis axis) { 159 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 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 185 private static void renderSlicerRowHeader(Axis slicerax, PrintWriter wout) { 186 Position[] positions = (Position[]) slicerax.getPositions() 187 .toArray(new Position[0]); 188 String 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 [] args) { 206 } 207 } 208 | Popular Tags |