JWS-121 & JWS-111 Adding ‘timestamp’ (string formatted date + time) to the HttpServ...
[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 import java.util.Calendar;\r
26 import java.text.SimpleDateFormat;\r
27 \r
28 import javax.servlet.RequestDispatcher;\r
29 import javax.servlet.ServletException;\r
30 import javax.servlet.http.HttpServlet;\r
31 import javax.servlet.http.HttpServletRequest;\r
32 import javax.servlet.http.HttpServletResponse;\r
33 \r
34 import compbio.stat.collector.StatDB;\r
35 import compbio.stat.servlet.util.StatCollection;\r
36 import compbio.stat.servlet.util.Totals;\r
37 \r
38 public class AnnualStat extends HttpServlet {\r
39 \r
40         @Override\r
41         protected void doGet(HttpServletRequest req, HttpServletResponse resp)\r
42                         throws ServletException, IOException {\r
43 \r
44                 try {\r
45                         long startTime = System.nanoTime();\r
46                         StatDB db = new StatDB();\r
47                         Date earliestRec = db.getEarliestRecord();\r
48                         if (earliestRec == null) {\r
49                                 PrintWriter writer = resp.getWriter();\r
50                                 writer.println("No statistics found in the database. Please allow "\r
51                                                 + "at least one hour after a server start for the statistics "\r
52                                                 + "collector to collect the data. ");\r
53                                 writer.close();\r
54                                 return;\r
55                         }\r
56                         Map<Date, Totals> monthlyTotals = StatCollection.getStats(earliestRec);\r
57                         long endTime = System.nanoTime();\r
58                         req.setAttribute("stat", monthlyTotals);\r
59                         req.setAttribute("total", Totals.sumOfTotals(monthlyTotals));\r
60 //                      req.setAttribute("timeexec", (endTime - startTime) / 1000000);\r
61                         req.setAttribute("timeexec", (endTime - startTime) / 100000000);\r
62                         String timeStamp = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(\r
63                                 Calendar.getInstance().getTime());\r
64                         req.setAttribute("timestamp", timeStamp);\r
65 \r
66                         RequestDispatcher dispatcher = req.getRequestDispatcher("statpages/MonthlySummary.jsp");\r
67 \r
68                         req.setAttribute("isAdmin", isAdmin(req));\r
69                         dispatcher.forward(req, resp);\r
70 \r
71                 } catch (SQLException e) {\r
72                         e.printStackTrace();\r
73                         throw new ServletException(e);\r
74                 }\r
75         }\r
76 \r
77         static boolean isAdmin(final HttpServletRequest request) {\r
78                 return request.isUserInRole("admin");\r
79         }\r
80 \r
81 }\r