Change header template for a new version
[jabaws.git] / webservices / compbio / stat / servlet / AnnualStat.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.stat.servlet;\r
19 \r
20 import java.io.IOException;\r
21 import java.io.PrintWriter;\r
22 import java.sql.SQLException;\r
23 import java.util.Date;\r
24 import java.util.Map;\r
25 \r
26 import javax.servlet.RequestDispatcher;\r
27 import javax.servlet.ServletException;\r
28 import javax.servlet.http.HttpServlet;\r
29 import javax.servlet.http.HttpServletRequest;\r
30 import javax.servlet.http.HttpServletResponse;\r
31 \r
32 import compbio.stat.collector.StatDB;\r
33 import compbio.stat.servlet.util.StatCollection;\r
34 import compbio.stat.servlet.util.Totals;\r
35 \r
36 public class AnnualStat extends HttpServlet {\r
37 \r
38         @Override\r
39         protected void doGet(HttpServletRequest req, HttpServletResponse resp)\r
40                         throws ServletException, IOException {\r
41 \r
42                 try {\r
43                         StatDB db = new StatDB();\r
44                         Date earliestRec = db.getEarliestRecord();\r
45                         if (earliestRec == null) {\r
46                                 PrintWriter writer = resp.getWriter();\r
47                                 writer.println("No statistics found in the database. Please allow "\r
48                                                 + "at least one hour after a server start for the statistics "\r
49                                                 + "collector to collect the data. ");\r
50                                 writer.close();\r
51                                 return;\r
52                         }\r
53                         Map<Date, Totals> monthlyTotals = StatCollection\r
54                                         .getStats(earliestRec);\r
55                         req.setAttribute("stat", monthlyTotals);\r
56                         req.setAttribute("total", Totals.sumOfTotals(monthlyTotals));\r
57 \r
58                         RequestDispatcher dispatcher = req\r
59                                         .getRequestDispatcher("statpages/MonthlySummary.jsp");\r
60 \r
61                         req.setAttribute("isAdmin", isAdmin(req));\r
62                         dispatcher.forward(req, resp);\r
63 \r
64                 } catch (SQLException e) {\r
65                         e.printStackTrace();\r
66                         throw new ServletException(e);\r
67                 }\r
68         }\r
69 \r
70         static boolean isAdmin(final HttpServletRequest request) {\r
71                 return request.isUserInRole("admin");\r
72         }\r
73 \r
74 }\r