1 package org.openi.chart; 2 3 import org.apache.log4j.Logger; 4 5 import org.jfree.data.category.DefaultCategoryDataset; 6 import org.openi.analysis.Analysis; 7 import org.openi.analysis.Datasource; 8 import org.openi.project.ProjectContext; 9 import org.openi.xmla.DatasetAdapter; 10 import org.openi.xmla.XmlaConnector; 11 import org.xml.sax.SAXException ; 12 13 import com.tonbeller.jpivot.olap.model.OlapException; 14 import com.tonbeller.jpivot.olap.model.OlapModel; 15 16 import java.io.FileNotFoundException ; 17 import java.io.IOException ; 18 19 import javax.servlet.ServletException ; 20 import javax.servlet.http.HttpServlet ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 24 28 public class StreamChartServlet extends HttpServlet { 29 private static Logger logger = Logger.getLogger(StreamChartServlet.class); 30 31 public StreamChartServlet() { 32 super(); 33 } 34 35 private int parseInt(String value, int defaultValue){ 36 int retVal = defaultValue; 37 38 try{ 39 retVal = Integer.parseInt(value); 40 if(retVal == 0) retVal = defaultValue; }catch(Exception e){ 42 logger.debug(e); 43 } 44 45 return retVal; 46 } 47 48 53 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException { 54 long start = System.currentTimeMillis(); 55 ProjectContext projectContext = (ProjectContext)request.getSession().getAttribute("projectContext"); 56 String analysisName = request.getParameter("config"); 57 logger.debug("streaming chart for analysis: " + analysisName); 58 logger.debug("projectContext: " + projectContext.getProject().getProjectName()); 59 60 Analysis analysis = restoreAnalysis(projectContext, analysisName); 61 if(analysis == null){ 62 try{ 63 response.sendRedirect("images/spacer.gif"); 64 }catch(IOException e){ 65 logger.error(e); 66 }finally{ 67 return; 68 } 69 } 70 String datasourceName = analysis.getDataSourceName(); 71 logger.debug("using datasource name: " + datasourceName); 72 73 Datasource datasource = projectContext.getDatasource(datasourceName); 74 76 78 OlapModel olapModel = getDataset(datasource.getServer(), datasource.getCatalog(), analysis); 79 80 try { 81 int x = this.parseInt(request.getParameter("width"), 320); 82 int y = this.parseInt(request.getParameter("height"), 240); 83 EnhancedChartFactory.createChart(response.getOutputStream(), analysis, olapModel, x, y, request.getLocale()); 84 } catch (FileNotFoundException e) { 85 throw new ServletException (e); 86 } catch (IOException e) { 87 throw new ServletException (e); 88 } catch (OlapException e) { 89 throw new ServletException (e); 90 }finally{ 91 long elapsed = System.currentTimeMillis() - start; 92 logger.info("request completed in " + elapsed + "ms"); 93 } 94 } 95 96 103 private Analysis restoreAnalysis(ProjectContext projectContext, String analysisName) throws ServletException { 104 105 Analysis analysis = null; 106 107 try { 108 analysis = projectContext.restoreAnalysis(analysisName); 109 } catch (IOException e) { 110 } 112 return analysis; 113 } 114 115 123 private OlapModel getDataset(String xmlaUri, String catalog, 124 Analysis analysis) throws ServletException { 125 126 XmlaConnector connector = new XmlaConnector(); 127 String mdxQuery = analysis.getMdxQuery(); 128 129 OlapModel model = null;; 130 try { 131 model = connector.query(xmlaUri, catalog, mdxQuery); 132 } catch (IOException e) { 133 throw new ServletException (e); 134 } catch (OlapException e) { 135 throw new ServletException (e); 136 } catch (SAXException e) { 137 throw new ServletException (e); 138 } 139 return model; 140 141 142 } 143 144 145 } 146 | Popular Tags |