git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@4179 e3abac25...
[jabaws.git] / webservices / compbio / stat / servlet / Joblist.java
1 package compbio.stat.servlet;\r
2 \r
3 import java.io.IOException;\r
4 import java.sql.SQLException;\r
5 import java.sql.Timestamp;\r
6 \r
7 import javax.servlet.RequestDispatcher;\r
8 import javax.servlet.ServletException;\r
9 import javax.servlet.http.HttpServlet;\r
10 import javax.servlet.http.HttpServletRequest;\r
11 import javax.servlet.http.HttpServletResponse;\r
12 import javax.servlet.http.HttpSession;\r
13 \r
14 import compbio.engine.conf.PropertyHelperManager;\r
15 import compbio.stat.collector.StatDB;\r
16 import compbio.stat.collector.StatProcessor;\r
17 import compbio.util.PropertyHelper;\r
18 import compbio.util.Util;\r
19 import compbio.ws.client.Services;\r
20 \r
21 public class Joblist extends HttpServlet {\r
22 \r
23         static final String JT_FAILED = "failed";\r
24         static final String JT_ABANDONED = "abandoned";\r
25         static final String JT_CANCELLED = "cancelled";\r
26         static final String JT_ALL = "all";\r
27         static final String JT_INCOMPLETE = "incomplete";\r
28         /**\r
29          * Input:\r
30          * \r
31          * ws=${ws.key}\r
32          * \r
33          * where=everywhere cluster local\r
34          * \r
35          * type=cancelled all incomplete\r
36          * \r
37          * from=${startDate}\r
38          * \r
39          * to=${stopDate}\r
40          * \r
41          */\r
42         @Override\r
43         protected void doGet(HttpServletRequest req, HttpServletResponse resp)\r
44                         throws ServletException, IOException {\r
45                 /*\r
46                  * Values for this servlet are not user supplied, so do not bother with\r
47                  * nice error messages just throw the exception is something is wrong!\r
48                  */\r
49                 String wsname = req.getParameter("ws");\r
50                 Services wservice = Services.getService(wsname);\r
51                 if (wservice == null) {\r
52                         throw new ServletException(\r
53                                         "Webservice name 'ws' is not specified or is incorrect. Given value:"\r
54                                                         + wsname);\r
55                 }\r
56                 String executor = req.getParameter("where");\r
57                 if (Util.isEmpty(executor)) {\r
58                         throw new ServletException("'Where' is not specified!");\r
59                 }\r
60                 if (!(executor.equalsIgnoreCase("everywhere")\r
61                                 || executor.equalsIgnoreCase("local") || executor\r
62                                 .equalsIgnoreCase("cluster"))) {\r
63                         throw new ServletException("Invalid 'where' value '" + executor\r
64                                         + "' can be one of 'everywhere', 'local', 'cluster'!");\r
65                 }\r
66                 Boolean where = null;\r
67                 if (executor.equalsIgnoreCase("local")) {\r
68                         where = false;\r
69                 } else if (executor.equalsIgnoreCase("cluster")) {\r
70                         where = true;\r
71                 }\r
72 \r
73                 String jobtype = req.getParameter("type");\r
74                 if (Util.isEmpty(executor)) {\r
75                         throw new ServletException("'type' is not specified!");\r
76                 }\r
77                 if (!(jobtype.equalsIgnoreCase(JT_CANCELLED)\r
78                                 || jobtype.equalsIgnoreCase(JT_ALL)\r
79                                 || jobtype.equalsIgnoreCase(JT_INCOMPLETE)\r
80                                 || jobtype.equalsIgnoreCase(JT_ABANDONED) || jobtype\r
81                                 .equalsIgnoreCase(JT_FAILED))) {\r
82                         throw new ServletException("Invalid 'jobtype' value '" + jobtype\r
83                                         + "' can be one of 'cancelled', 'all', 'incomplete', "\r
84                                         + "'failed', 'abandoned'!");\r
85                 }\r
86                 String fromDate = req.getParameter("from");\r
87                 if (Util.isEmpty(fromDate)) {\r
88                         throw new ServletException("'fromDate' is not specified!");\r
89                 }\r
90                 String toDate = req.getParameter("to");\r
91                 if (Util.isEmpty(toDate)) {\r
92                         throw new ServletException("'toDate' is not specified!");\r
93                 }\r
94 \r
95                 PropertyHelper helper = PropertyHelperManager.getPropertyHelper();\r
96                 String clusterTempDir = helper.getProperty("cluster.tmp.directory")\r
97                                 .trim();\r
98                 String localTempDir = helper.getProperty("local.tmp.directory").trim();\r
99                 // TODO include the time slice\r
100                 Timestamp startDate = new Timestamp(Long.parseLong(fromDate));\r
101                 Timestamp stopDate = new Timestamp(Long.parseLong(toDate));\r
102                 StatDB statdb = null;\r
103                 try {\r
104                         statdb = new StatDB();\r
105                         StatProcessor stat = new StatProcessor(statdb.readData(startDate,\r
106                                         stopDate, wservice, where));\r
107 \r
108                         HttpSession session = req.getSession();\r
109                         if (jobtype.equalsIgnoreCase(JT_CANCELLED)) {\r
110                                 session.setAttribute("stat",\r
111                                                 new StatProcessor(stat.getCancelledJobs()));\r
112                         } else if (jobtype.equalsIgnoreCase(JT_INCOMPLETE)) {\r
113                                 session.setAttribute("stat",\r
114                                                 new StatProcessor(stat.getIncompleteJobs()));\r
115                         } else if (jobtype.equalsIgnoreCase(JT_ALL)) {\r
116                                 session.setAttribute("stat", stat);\r
117                         } else if (jobtype.equalsIgnoreCase(JT_FAILED)) {\r
118                                 session.setAttribute("stat",\r
119                                                 new StatProcessor(stat.getFailedJobs()));\r
120                         } else if (jobtype.equalsIgnoreCase(JT_ABANDONED)) {\r
121                                 session.setAttribute("stat",\r
122                                                 new StatProcessor(stat.getAbandonedJobs()));\r
123                         } else {\r
124                                 throw new AssertionError("Unrecognised job type: " + jobtype);\r
125                         }\r
126                         session.setAttribute("clusterTemp", clusterTempDir);\r
127                         session.setAttribute("localTemp", localTempDir);\r
128                         req.setAttribute("startDate", startDate.getTime());\r
129                         req.setAttribute("stopDate", stopDate.getTime());\r
130 \r
131                         RequestDispatcher dispatcher = req\r
132                                         .getRequestDispatcher("statpages/Joblist.jsp");\r
133                         dispatcher.forward(req, resp);\r
134 \r
135                 } catch (SQLException e) {\r
136                         e.printStackTrace();\r
137                         throw new ServletException("SQLException : "\r
138                                         + e.getLocalizedMessage(), e);\r
139                 }\r
140 \r
141         }\r
142 }\r