KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > chart > StreamChartServlet


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 JavaDoc;
12
13 import com.tonbeller.jpivot.olap.model.OlapException;
14 import com.tonbeller.jpivot.olap.model.OlapModel;
15
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.IOException JavaDoc;
18
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServlet JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 /**
25  * should this be a spring controller?
26  *
27  */

28 public class StreamChartServlet extends HttpServlet JavaDoc {
29     private static Logger logger = Logger.getLogger(StreamChartServlet.class);
30
31     public StreamChartServlet() {
32         super();
33     }
34     
35     private int parseInt(String JavaDoc value, int defaultValue){
36         int retVal = defaultValue;
37         
38         try{
39             retVal = Integer.parseInt(value);
40             if(retVal == 0) retVal = defaultValue;//if zero, also use default
41
}catch(Exception JavaDoc e){
42             logger.debug(e);
43         }
44         
45         return retVal;
46     }
47
48     /**
49      * requires:
50      * config parameter in request
51      * valid projectContext in session
52      */

53     public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
54         long start = System.currentTimeMillis();
55         ProjectContext projectContext = (ProjectContext)request.getSession().getAttribute("projectContext");
56         String JavaDoc 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 JavaDoc e){
65                 logger.error(e);
66             }finally{
67                 return;
68             }
69         }
70         String JavaDoc datasourceName = analysis.getDataSourceName();
71         logger.debug("using datasource name: " + datasourceName);
72         
73         Datasource datasource = projectContext.getDatasource(datasourceName);
74         // logger.debug("using datasource: " + datasource.getServer());
75

76         // DefaultCategoryDataset dataset = getDataset(datasource.getServer(), datasource.getCatalog(), analysis);
77

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 JavaDoc e) {
85             throw new ServletException JavaDoc(e);
86         } catch (IOException JavaDoc e) {
87             throw new ServletException JavaDoc(e);
88         } catch (OlapException e) {
89             throw new ServletException JavaDoc(e);
90         }finally{
91             long elapsed = System.currentTimeMillis() - start;
92             logger.info("request completed in " + elapsed + "ms");
93         }
94     }
95     
96     /**
97      * trap all exception?
98      * @param projectContext
99      * @param analysisName
100      * @return
101      * @throws ServletException
102      */

103     private Analysis restoreAnalysis(ProjectContext projectContext, String JavaDoc analysisName) throws ServletException JavaDoc{
104         
105         Analysis analysis = null;
106         
107         try {
108             analysis = projectContext.restoreAnalysis(analysisName);
109         } catch (IOException JavaDoc e) {
110             // throw new ServletException(e);
111
}
112         return analysis;
113     }
114     
115     /**
116      * might want to trap all exceptions?
117      * @param xmlaUri
118      * @param catalog
119      * @param analysis
120      * @return
121      * @throws ServletException
122      */

123     private OlapModel getDataset(String JavaDoc xmlaUri, String JavaDoc catalog,
124             Analysis analysis) throws ServletException JavaDoc{
125
126             XmlaConnector connector = new XmlaConnector();
127             String JavaDoc mdxQuery = analysis.getMdxQuery();
128
129             OlapModel model = null;;
130             try {
131                 model = connector.query(xmlaUri, catalog, mdxQuery);
132             } catch (IOException JavaDoc e) {
133                 throw new ServletException JavaDoc(e);
134             } catch (OlapException e) {
135                 throw new ServletException JavaDoc(e);
136             } catch (SAXException JavaDoc e) {
137                 throw new ServletException JavaDoc(e);
138             }
139             return model;
140
141         
142     }
143     
144     
145 }
146
Popular Tags