1 40 41 package org.dspace.app.webui.servlet; 42 43 import java.io.BufferedReader ; 44 import java.io.File ; 45 import java.io.FileInputStream ; 46 import java.io.IOException ; 47 import java.io.InputStreamReader ; 48 import java.sql.SQLException ; 49 import java.text.ParseException ; 50 import java.text.SimpleDateFormat ; 51 import java.util.ArrayList ; 52 import java.util.Arrays ; 53 import java.util.Date ; 54 import java.util.List ; 55 import java.util.regex.Matcher ; 56 import java.util.regex.Pattern ; 57 58 import javax.servlet.ServletException ; 59 import javax.servlet.http.HttpServletRequest ; 60 import javax.servlet.http.HttpServletResponse ; 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 76 public class StatisticsServlet extends org.dspace.app.webui.servlet.DSpaceServlet 77 { 78 79 80 private static Logger log = Logger.getLogger(StatisticsServlet.class); 81 82 protected void doDSGet(Context c, 83 HttpServletRequest request, HttpServletResponse response) 84 throws ServletException , IOException , SQLException , AuthorizeException 85 { 86 doDSPost(c, request, response); 88 } 89 90 protected void doDSPost(Context c, 91 HttpServletRequest request, HttpServletResponse response) 92 throws ServletException , IOException , SQLException , AuthorizeException 93 { 94 boolean publicise = ConfigurationManager.getBooleanProperty("report.public"); 96 97 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 117 private void showStatistics(Context context, 118 HttpServletRequest request, HttpServletResponse response) 119 throws ServletException , IOException , SQLException , AuthorizeException 120 { 121 String date = (String ) request.getParameter("date"); 122 request.setAttribute("date", date); 123 124 request.setAttribute("general", new Boolean (false)); 125 126 File reportDir = new File (ConfigurationManager.getProperty("report.dir")); 127 128 File [] reports = reportDir.listFiles(); 129 File reportFile = null; 130 131 FileInputStream fir = null; 132 InputStreamReader ir = null; 133 BufferedReader br = null; 134 135 List monthsList = new ArrayList (); 136 137 Pattern monthly = Pattern.compile("report-([0-9][0-9][0-9][0-9]-[0-9]+)\\.html"); 138 Pattern general = Pattern.compile("report-general-([0-9]+-[0-9]+-[0-9]+)\\.html"); 139 140 143 if (date == null) 146 { 147 request.setAttribute("general", new Boolean (true)); 148 149 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy'-'M'-'dd"); 150 Date mostRecentDate = null; 151 152 for (int i = 0; i < reports.length; i++) 153 { 154 Matcher matchGeneral = general.matcher(reports[i].getName()); 155 if (matchGeneral.matches()) 156 { 157 Date parsedDate = null; 158 159 try 160 { 161 parsedDate = sdf.parse(matchGeneral.group(1).trim()); 162 } 163 catch (ParseException e) 164 { 165 } 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 (date != null) 185 { 186 String 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 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy'-'M"); 204 for (int i = 0; i < reports.length; i++) 205 { 206 Matcher matchReport = monthly.matcher(reports[i].getName()); 207 if (matchReport.matches()) 208 { 209 Date parsedDate = null; 210 211 try 212 { 213 parsedDate = sdf.parse(matchReport.group(1).trim()); 214 } 215 catch (ParseException e) 216 { 217 } 219 220 monthsList.add(parsedDate); 221 } 222 } 223 224 Date [] months = new Date [monthsList.size()]; 225 months = (Date []) monthsList.toArray(months); 226 227 Arrays.sort(months); 228 229 request.setAttribute("months", months); 230 231 try 232 { 233 fir = new FileInputStream (reportFile.getPath()); 234 ir = new InputStreamReader (fir, "UTF-8"); 235 br = new BufferedReader (ir); 236 } 237 catch (IOException e) 238 { 239 throw new RuntimeException (e.getMessage(),e); 241 } 242 243 StringBuffer report = new StringBuffer (); 245 String line = null; 246 while ((line = br.readLine()) != null) 247 { 248 report.append(line); 249 } 250 251 request.setAttribute("report", report.toString()); 253 254 JSPManager.showJSP(request, response, "statistics/report.jsp"); 255 } 256 } 257 | Popular Tags |