1 11 package com.tonbeller.jpivot.jboss.portlet; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.IOException ; 16 import java.io.StringWriter ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import javax.portlet.ActionResponse; 22 import javax.portlet.PortletException; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.HttpSession ; 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.dom.DOMSource ; 28 import javax.xml.transform.stream.StreamResult ; 29 30 import org.apache.fop.apps.Driver; 31 import org.apache.fop.apps.FOPException; 32 import org.apache.log4j.Logger; 33 import org.jboss.portal.server.WindowContext; 34 import org.jboss.portlet.JBossActionResponse; 35 import org.w3c.dom.Document ; 36 import org.xml.sax.InputSource ; 37 38 import com.tonbeller.jpivot.chart.ChartComponent; 39 import com.tonbeller.jpivot.print.PrintComponent; 40 import com.tonbeller.jpivot.table.TableComponent; 41 import com.tonbeller.wcf.component.RendererParameters; 42 import com.tonbeller.wcf.controller.RequestContext; 43 import com.tonbeller.wcf.controller.RequestContextFactoryFinder; 44 import com.tonbeller.wcf.utils.XmlUtils; 45 46 49 public class PrintTable { 50 private static Logger logger = Logger.getLogger(PrintTable.class); 51 public static final int XLS = 0; 52 public static final int PDF = 1; 53 54 59 60 public static void processRequest( 61 HttpServletRequest request, 62 HttpServletResponse response, 63 WindowContext windowCtx, 64 ActionResponse portletResponse) throws PortletException { 65 if (request.getParameter("cube") == null || request.getParameter("type") == null){ 66 return; 67 } 68 69 try { 70 71 RequestContext context = RequestContextFactoryFinder.createContext(request,response,true); 72 73 String xslUri = null; 74 75 int type = Integer.parseInt(request.getParameter("type")); 76 String contentType = null; 77 78 switch (type){ 79 case XLS: 80 xslUri="/WEB-INF/jpivot/table/xls_mdxtable.xsl"; 81 RendererParameters.setParameter(context.getRequest(), "mode","excel","request"); 82 contentType = "application/vnd.ms-excel"; 83 break; 85 case PDF: 86 xslUri="/WEB-INF/jpivot/table/fo_mdxtable.xsl"; 87 RendererParameters.setParameter(context.getRequest(), "mode","print","request"); 88 contentType = "application/pdf"; 89 break; 91 default: 92 return; 93 } 94 String tableRef = "table"+request.getParameter("cube"); 96 String chartRef = "chart"+request.getParameter("cube"); 97 String printRef = "print"+request.getParameter("cube"); 98 99 TableComponent table = (TableComponent) context.getModelReference(tableRef); 101 103 if (table == null) { 104 return; 105 } 106 PrintComponent printConfig = (PrintComponent) context.getModelReference(printRef); 108 109 Map parameters = new HashMap (); 110 111 if (printConfig != null) { 112 if (printConfig.isSetTableWidth()) { 113 parameters.put(printConfig.PRINT_TABLE_WIDTH, new Double (printConfig.getTableWidth())); 114 } 115 if (printConfig.getReportTitle().trim().length()!=0){ 116 parameters.put(printConfig.PRINT_TITLE, printConfig.getReportTitle().trim()); 117 } 118 parameters.put(printConfig.PRINT_PAGE_ORIENTATION, printConfig.getPageOrientation()); 119 parameters.put(printConfig.PRINT_PAPER_TYPE, printConfig.getPaperType()); 120 if (printConfig.getPaperType().equals("custom")){ 121 parameters.put(printConfig.PRINT_PAGE_WIDTH, new Double (printConfig.getPageWidth())); 122 parameters.put(printConfig.PRINT_PAGE_HEIGHT, new Double (printConfig.getPageHeight())); 123 } 124 parameters.put(printConfig.PRINT_CHART_PAGEBREAK, new Boolean (printConfig.isChartPageBreak())); 125 126 } 127 128 ChartComponent chart = (ChartComponent)request.getSession().getAttribute(chartRef); 130 if (chart != null && chart.isVisible() ) { 131 132 String host = request.getServerName(); 133 int port = request.getServerPort(); 134 135 StringBuffer sb = new StringBuffer (); 136 sb.append("http://") 137 .append(host) 138 .append(":") 139 .append(port) 140 .append(chart.getGraphURL(context)); 141 String chartURL = sb.toString(); 142 143 logger.debug("image is: " + chartURL); 144 parameters.put("chartimage", chartURL); 145 146 parameters.put("chartheight", new Integer (chart.getChartHeight())); 147 parameters.put("chartwidth", new Integer (chart.getChartWidth())); 148 } 149 150 parameters.put("context", context.getRequest().getContextPath()); 151 152 155 table.setDirty(true); 157 Document document = table.render(context); 158 table.setDirty(true); 159 160 DOMSource source = new DOMSource (document); 161 162 HttpSession session = request.getSession(); 164 Transformer transformer = XmlUtils.getTransformer(session, xslUri, true); 165 for (Iterator it = parameters.keySet().iterator(); it.hasNext();) { 166 String name = (String ) it.next(); 167 Object value = parameters.get(name); 168 transformer.setParameter(name, value); 169 } 170 StringWriter sw = new StringWriter (); 171 StreamResult result = new StreamResult (sw); 172 transformer.transform(source, result); 174 sw.flush(); 175 176 JBossActionResponse responseImpl = (JBossActionResponse) portletResponse; 178 179 if (type == XLS){ 181 logger.info("Creating XLS"); 182 responseImpl.sendBytes(contentType, sw.toString().getBytes()); 185 RendererParameters.removeParameter(context.getRequest(), "mode","excel","request"); 186 } else { 188 logger.info("Creating PDF!"); 190 ByteArrayInputStream bain = new ByteArrayInputStream (sw.toString().getBytes("UTF-8")); 191 ByteArrayOutputStream baout = new ByteArrayOutputStream (16384); 192 convertFO2PDF(bain,baout); 193 final byte[] content = baout.toByteArray(); 194 responseImpl.sendBytes(contentType, content); 197 RendererParameters.removeParameter(context.getRequest(), "mode","print","request"); 198 } 199 } catch (Exception e){ 200 throw new PortletException(e); 201 } 202 } 203 204 207 public static void convertFO2PDF(ByteArrayInputStream bain,ByteArrayOutputStream baout) throws IOException ,FOPException { 208 209 logger.debug("Construct driver"); 210 Driver driver = new Driver(); 211 212 logger.debug("Setup Renderer (output format)"); 213 driver.setRenderer(Driver.RENDER_PDF); 214 215 try { 216 driver.setOutputStream(baout); 217 logger.debug("Setup input"); 218 driver.setInputSource(new InputSource (bain)); 219 220 logger.debug("Process FO"); 221 driver.run(); 222 logger.debug("PDF file generation completed"); 223 } catch (Exception e){ 224 logger.error(e); 225 } 226 } 227 228 } 229 | Popular Tags |