KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > StatisticsServlet


1 /*
2  * StatisticsServlet.java
3  *
4  * Version: $Revision: 1.6 $
5  *
6  * Date: $Date: 2006/11/24 04:56:53 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40
41 package org.dspace.app.webui.servlet;
42
43 import java.io.BufferedReader JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.FileInputStream JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.InputStreamReader JavaDoc;
48 import java.sql.SQLException JavaDoc;
49 import java.text.ParseException JavaDoc;
50 import java.text.SimpleDateFormat JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.Arrays JavaDoc;
53 import java.util.Date JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.regex.Matcher JavaDoc;
56 import java.util.regex.Pattern JavaDoc;
57
58 import javax.servlet.ServletException JavaDoc;
59 import javax.servlet.http.HttpServletRequest JavaDoc;
60 import javax.servlet.http.HttpServletResponse JavaDoc;
61
62 import org.apache.log4j.Logger;
63 import org.dspace.app.webui.util.JSPManager;
64 import org.dspace.authorize.AuthorizeException;
65 import org.dspace.core.ConfigurationManager;
66 import org.dspace.core.Context;
67 import org.dspace.eperson.Group;
68
69 /**
70  * This servlet provides an interface to the statistics reporting for a DSpace
71  * repository
72  *
73  * @author Richard Jones
74  * @version $Revision: 1.6 $
75  */

76 public class StatisticsServlet extends org.dspace.app.webui.servlet.DSpaceServlet
77 {
78
79     /** log4j category */
80     private static Logger log = Logger.getLogger(StatisticsServlet.class);
81     
82     protected void doDSGet(Context c,
83         HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
84         throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc, AuthorizeException
85     {
86         // forward all requests to the post handler
87
doDSPost(c, request, response);
88     }
89     
90     protected void doDSPost(Context c,
91         HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
92         throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc, AuthorizeException
93     {
94         // check to see if the statistics are restricted to administrators
95
boolean publicise = ConfigurationManager.getBooleanProperty("report.public");
96         
97         // is the user a member of the Administrator (1) group
98
boolean admin = Group.isMember(c, 1);
99         
100         if (publicise || admin)
101         {
102             showStatistics(c, request, response);
103         }
104         else
105         {
106             throw new AuthorizeException();
107         }
108     }
109     
110     /**
111      * show the default statistics page
112      *
113      * @param context current DSpace context
114      * @param request current servlet request object
115      * @param response current servlet response object
116      */

117     private void showStatistics(Context context,
118         HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
119         throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc, AuthorizeException
120     {
121         String JavaDoc date = (String JavaDoc) request.getParameter("date");
122         request.setAttribute("date", date);
123         
124         request.setAttribute("general", new Boolean JavaDoc(false));
125         
126         File JavaDoc reportDir = new File JavaDoc(ConfigurationManager.getProperty("report.dir"));
127         
128         File JavaDoc[] reports = reportDir.listFiles();
129         File JavaDoc reportFile = null;
130         
131         FileInputStream JavaDoc fir = null;
132         InputStreamReader JavaDoc ir = null;
133         BufferedReader JavaDoc br = null;
134         
135         List JavaDoc monthsList = new ArrayList JavaDoc();
136         
137         Pattern JavaDoc monthly = Pattern.compile("report-([0-9][0-9][0-9][0-9]-[0-9]+)\\.html");
138         Pattern JavaDoc general = Pattern.compile("report-general-([0-9]+-[0-9]+-[0-9]+)\\.html");
139         
140         // FIXME: this whole thing is horribly inflexible and needs serious
141
// work; but as a basic proof of concept will suffice
142

143         // if no date is passed then we want to get the most recent general
144
// report
145
if (date == null)
146         {
147             request.setAttribute("general", new Boolean JavaDoc(true));
148             
149             SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("yyyy'-'M'-'dd");
150             Date JavaDoc mostRecentDate = null;
151             
152             for (int i = 0; i < reports.length; i++)
153             {
154                 Matcher JavaDoc matchGeneral = general.matcher(reports[i].getName());
155                 if (matchGeneral.matches())
156                 {
157                     Date JavaDoc parsedDate = null;
158
159                     try
160                     {
161                          parsedDate = sdf.parse(matchGeneral.group(1).trim());
162                     }
163                     catch (ParseException JavaDoc e)
164                     {
165                         // FIXME: currently no error handling
166
}
167                     
168                     if (mostRecentDate == null)
169                     {
170                         mostRecentDate = parsedDate;
171                         reportFile = reports[i];
172                     }
173                     
174                     if (parsedDate.compareTo(mostRecentDate) > 0)
175                     {
176                         mostRecentDate = parsedDate;
177                         reportFile = reports[i];
178                     }
179                 }
180             }
181         }
182         
183         // if a date is passed then we want to get the file for that month
184
if (date != null)
185         {
186             String JavaDoc desiredReport = "report-" + date + ".html";
187         
188             for (int i = 0; i < reports.length; i++)
189             {
190                 if (reports[i].getName().equals(desiredReport))
191                 {
192                     reportFile = reports[i];
193                 }
194             }
195         }
196         
197         if (reportFile == null)
198         {
199             JSPManager.showJSP(request, response, "statistics/no-report.jsp");
200         }
201         
202         // finally, build the list of report dates
203
SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("yyyy'-'M");
204         for (int i = 0; i < reports.length; i++)
205         {
206             Matcher JavaDoc matchReport = monthly.matcher(reports[i].getName());
207             if (matchReport.matches())
208             {
209                 Date JavaDoc parsedDate = null;
210                 
211                 try
212                 {
213                      parsedDate = sdf.parse(matchReport.group(1).trim());
214                 }
215                 catch (ParseException JavaDoc e)
216                 {
217                     // FIXME: currently no error handling
218
}
219                 
220                 monthsList.add(parsedDate);
221             }
222         }
223         
224         Date JavaDoc[] months = new Date JavaDoc[monthsList.size()];
225         months = (Date JavaDoc[]) monthsList.toArray(months);
226         
227         Arrays.sort(months);
228         
229         request.setAttribute("months", months);
230         
231         try
232         {
233             fir = new FileInputStream JavaDoc(reportFile.getPath());
234             ir = new InputStreamReader JavaDoc(fir, "UTF-8");
235             br = new BufferedReader JavaDoc(ir);
236         }
237         catch (IOException JavaDoc e)
238         {
239             // FIXME: no error handing yet
240
throw new RuntimeException JavaDoc(e.getMessage(),e);
241         }
242         
243         // FIXME: there's got to be a better way of doing this
244
StringBuffer JavaDoc report = new StringBuffer JavaDoc();
245         String JavaDoc line = null;
246         while ((line = br.readLine()) != null)
247         {
248             report.append(line);
249         }
250         
251         // set the report to be displayed
252
request.setAttribute("report", report.toString());
253         
254         JSPManager.showJSP(request, response, "statistics/report.jsp");
255     }
256 }
257
Popular Tags