KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > servlets > StatisticsController


1
2 package de.jwi.jgallery.servlets;
3
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.text.DateFormat JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.servlet.RequestDispatcher JavaDoc;
17 import javax.servlet.ServletException JavaDoc;
18 import javax.servlet.http.HttpServlet JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.sql.DataSource JavaDoc;
22
23 import de.jwi.jgallery.db.Statistics;
24
25 /*
26  * jGallery - Java web application to display image galleries
27  *
28  * Copyright (C) 2004 Juergen Weber
29  *
30  * This file is part of jGallery.
31  *
32  * jGallery is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
33  * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
34  * version.
35  *
36  * jGallery is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
37  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
38  * details.
39  *
40  * You should have received a copy of the GNU General Public License along with jGallery; if not, write to the Free
41  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston
42  */

43
44 /**
45  * @author Jürgen Weber Source file created on 08.08.2004
46  *
47  */

48 public class StatisticsController extends HttpServlet JavaDoc
49 {
50
51     private Statistics statistics;
52
53     private static String JavaDoc version = "unknown";
54
55     public void init() throws ServletException JavaDoc
56     {
57         String JavaDoc dataSource = (String JavaDoc) getServletContext().getInitParameter(
58                 "dataSource");
59
60         Context JavaDoc context;
61         try
62         {
63             context = new InitialContext JavaDoc();
64             if (context == null)
65             {
66                 throw new ServletException JavaDoc("Boom - No Context");
67             }
68             DataSource JavaDoc ds = (DataSource JavaDoc) context.lookup(dataSource);
69
70             statistics = new Statistics(ds);
71         }
72         catch (NamingException JavaDoc e)
73         {
74             throw new ServletException JavaDoc(e.getMessage(), e);
75         }
76
77         try
78         {
79             InputStream JavaDoc is = getServletContext().getResourceAsStream(
80                     "/WEB-INF/" + Controller.VERSIONCONFIGFILE);
81
82             Properties JavaDoc versionProperties = new Properties JavaDoc();
83             versionProperties.load(is);
84
85             String JavaDoc s = versionProperties.getProperty("version");
86             if (null != s)
87             {
88                 version = s;
89             }
90         }
91         catch (Exception JavaDoc e)
92         {
93             e.printStackTrace(System.err); // TODO
94
}
95
96     }
97
98     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
99             throws IOException JavaDoc, ServletException JavaDoc
100     {
101
102         String JavaDoc self = null;
103         String JavaDoc contextPath = null;
104
105         contextPath = request.getContextPath();
106         String JavaDoc servletPath = request.getServletPath();
107
108         String JavaDoc pathInfo = request.getPathInfo();
109
110         self = contextPath + servletPath;
111
112         
113         if (pathInfo != null
114                 && (pathInfo.startsWith("/to/") || pathInfo.startsWith("/to/")))
115         {
116             doGetDetail(request,response, self);
117             return;
118         }
119
120
121         try
122         {
123             List JavaDoc l = statistics.getStatistics(request, self);
124             request.setAttribute("statistics", l);
125         }
126         catch (SQLException JavaDoc e)
127         {
128             throw new ServletException JavaDoc(e.getMessage(), e);
129         }
130
131
132         request.setAttribute("self", self);
133         
134         request.setAttribute("context", contextPath);
135
136         Date JavaDoc d = new Date JavaDoc();
137         request.setAttribute("date", DateFormat
138                 .getDateInstance(DateFormat.FULL).format(d));
139         request.setAttribute("time", DateFormat.getTimeInstance().format(d));
140
141         request.setAttribute("version", version);
142
143         request.setAttribute("serverInfo", getServletContext().getServerInfo());
144
145         String JavaDoc forward = "/WEB-INF/statistics.jsp";
146
147         RequestDispatcher JavaDoc requestDispatcher = getServletContext()
148                 .getRequestDispatcher(forward);
149
150         requestDispatcher.forward(request, response);
151
152     }
153
154     public void doGetDetail(HttpServletRequest JavaDoc request,
155             HttpServletResponse JavaDoc response,String JavaDoc self) throws IOException JavaDoc, ServletException JavaDoc
156     {
157         boolean withThumbs = false;
158         String JavaDoc pathInfo = request.getPathInfo();
159         withThumbs = pathInfo.startsWith("/tn/");
160         
161         String JavaDoc id = pathInfo.substring(4);
162         
163         try
164         {
165             Map JavaDoc m = statistics.getFolderStatistics(id, request, self);
166             request.setAttribute("statistics", m.get("images"));
167             request.setAttribute("folder", m.get("folder"));
168         }
169         catch (SQLException JavaDoc e)
170         {
171             throw new ServletException JavaDoc(e.getMessage(), e);
172         }
173
174         Date JavaDoc d = new Date JavaDoc();
175         request.setAttribute("date", DateFormat
176                 .getDateInstance(DateFormat.FULL).format(d));
177         request.setAttribute("time", DateFormat.getTimeInstance().format(d));
178
179         request.setAttribute("self", self);
180         
181         request.setAttribute("context", request.getContextPath());
182         
183         String JavaDoc forward = "/WEB-INF/statisticsdet.jsp";
184
185         RequestDispatcher JavaDoc requestDispatcher = getServletContext()
186                 .getRequestDispatcher(forward);
187
188         requestDispatcher.forward(request, response);
189         
190     }
191 }
Popular Tags