001: /*
002: * StatisticsServlet.java
003: *
004: * Version: $Revision: 1737 $
005: *
006: * Date: $Date: 2007-03-12 08:47:23 -0500 (Mon, 12 Mar 2007) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.webui.servlet;
042:
043: import java.io.BufferedReader;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.io.InputStreamReader;
048: import java.sql.SQLException;
049: import java.text.ParseException;
050: import java.text.SimpleDateFormat;
051: import java.util.ArrayList;
052: import java.util.Arrays;
053: import java.util.Date;
054: import java.util.List;
055: import java.util.regex.Matcher;
056: import java.util.regex.Pattern;
057:
058: import javax.servlet.ServletException;
059: import javax.servlet.http.HttpServletRequest;
060: import javax.servlet.http.HttpServletResponse;
061:
062: import org.apache.log4j.Logger;
063: import org.dspace.app.webui.util.JSPManager;
064: import org.dspace.authorize.AuthorizeException;
065: import org.dspace.core.ConfigurationManager;
066: import org.dspace.core.Context;
067: import org.dspace.eperson.Group;
068:
069: /**
070: * This servlet provides an interface to the statistics reporting for a DSpace
071: * repository
072: *
073: * @author Richard Jones
074: * @version $Revision: 1737 $
075: */
076: public class StatisticsServlet extends
077: org.dspace.app.webui.servlet.DSpaceServlet {
078:
079: /** log4j category */
080: private static Logger log = Logger
081: .getLogger(StatisticsServlet.class);
082:
083: protected void doDSGet(Context c, HttpServletRequest request,
084: HttpServletResponse response) throws ServletException,
085: IOException, SQLException, AuthorizeException {
086: // forward all requests to the post handler
087: doDSPost(c, request, response);
088: }
089:
090: protected void doDSPost(Context c, HttpServletRequest request,
091: HttpServletResponse response) throws ServletException,
092: IOException, SQLException, AuthorizeException {
093: // check to see if the statistics are restricted to administrators
094: boolean publicise = ConfigurationManager
095: .getBooleanProperty("report.public");
096:
097: // determine the navigation bar to be displayed
098: String navbar = (publicise == false ? "admin" : "default");
099: request.setAttribute("navbar", navbar);
100:
101: // is the user a member of the Administrator (1) group
102: boolean admin = Group.isMember(c, 1);
103:
104: if (publicise || admin) {
105: showStatistics(c, request, response);
106: } else {
107: throw new AuthorizeException();
108: }
109: }
110:
111: /**
112: * show the default statistics page
113: *
114: * @param context current DSpace context
115: * @param request current servlet request object
116: * @param response current servlet response object
117: */
118: private void showStatistics(Context context,
119: HttpServletRequest request, HttpServletResponse response)
120: throws ServletException, IOException, SQLException,
121: AuthorizeException {
122: String date = (String) request.getParameter("date");
123: request.setAttribute("date", date);
124:
125: request.setAttribute("general", new Boolean(false));
126:
127: File reportDir = new File(ConfigurationManager
128: .getProperty("report.dir"));
129:
130: File[] reports = reportDir.listFiles();
131: File reportFile = null;
132:
133: FileInputStream fir = null;
134: InputStreamReader ir = null;
135: BufferedReader br = null;
136:
137: List monthsList = new ArrayList();
138:
139: Pattern monthly = Pattern
140: .compile("report-([0-9][0-9][0-9][0-9]-[0-9]+)\\.html");
141: Pattern general = Pattern
142: .compile("report-general-([0-9]+-[0-9]+-[0-9]+)\\.html");
143:
144: // FIXME: this whole thing is horribly inflexible and needs serious
145: // work; but as a basic proof of concept will suffice
146:
147: // if no date is passed then we want to get the most recent general
148: // report
149: if (date == null) {
150: request.setAttribute("general", new Boolean(true));
151:
152: SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'M'-'dd");
153: Date mostRecentDate = null;
154:
155: for (int i = 0; i < reports.length; i++) {
156: Matcher matchGeneral = general.matcher(reports[i]
157: .getName());
158: if (matchGeneral.matches()) {
159: Date parsedDate = null;
160:
161: try {
162: parsedDate = sdf.parse(matchGeneral.group(1)
163: .trim());
164: } catch (ParseException e) {
165: // FIXME: currently no error handling
166: }
167:
168: if (mostRecentDate == null) {
169: mostRecentDate = parsedDate;
170: reportFile = reports[i];
171: }
172:
173: if (parsedDate.compareTo(mostRecentDate) > 0) {
174: mostRecentDate = parsedDate;
175: reportFile = reports[i];
176: }
177: }
178: }
179: }
180:
181: // if a date is passed then we want to get the file for that month
182: if (date != null) {
183: String desiredReport = "report-" + date + ".html";
184:
185: for (int i = 0; i < reports.length; i++) {
186: if (reports[i].getName().equals(desiredReport)) {
187: reportFile = reports[i];
188: }
189: }
190: }
191:
192: if (reportFile == null) {
193: JSPManager.showJSP(request, response,
194: "statistics/no-report.jsp");
195: }
196:
197: // finally, build the list of report dates
198: SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'M");
199: for (int i = 0; i < reports.length; i++) {
200: Matcher matchReport = monthly.matcher(reports[i].getName());
201: if (matchReport.matches()) {
202: Date parsedDate = null;
203:
204: try {
205: parsedDate = sdf.parse(matchReport.group(1).trim());
206: } catch (ParseException e) {
207: // FIXME: currently no error handling
208: }
209:
210: monthsList.add(parsedDate);
211: }
212: }
213:
214: Date[] months = new Date[monthsList.size()];
215: months = (Date[]) monthsList.toArray(months);
216:
217: Arrays.sort(months);
218:
219: request.setAttribute("months", months);
220:
221: try {
222: fir = new FileInputStream(reportFile.getPath());
223: ir = new InputStreamReader(fir, "UTF-8");
224: br = new BufferedReader(ir);
225: } catch (IOException e) {
226: // FIXME: no error handing yet
227: throw new RuntimeException(e.getMessage(), e);
228: }
229:
230: // FIXME: there's got to be a better way of doing this
231: StringBuffer report = new StringBuffer();
232: String line = null;
233: while ((line = br.readLine()) != null) {
234: report.append(line);
235: }
236:
237: // set the report to be displayed
238: request.setAttribute("report", report.toString());
239:
240: JSPManager.showJSP(request, response, "statistics/report.jsp");
241: }
242: }
|