84b0193988846d013c25af89be964be106f95d02
[jabaws.git] / webservices / compbio / stat / servlet / Joblist.java
1 package compbio.stat.servlet;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.sql.SQLException;\r
6 import java.sql.Timestamp;\r
7 \r
8 import javax.servlet.RequestDispatcher;\r
9 import javax.servlet.ServletException;\r
10 import javax.servlet.http.HttpServlet;\r
11 import javax.servlet.http.HttpServletRequest;\r
12 import javax.servlet.http.HttpServletResponse;\r
13 import javax.servlet.http.HttpSession;\r
14 \r
15 import compbio.engine.client.PathValidator;\r
16 import compbio.stat.collector.StatDB;\r
17 import compbio.stat.collector.StatProcessor;\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                 // Just extract the last name from the path\r
96                 String clusterTempDir = StatisticCollector.getClusterJobDir();\r
97                 if (!Util.isEmpty(clusterTempDir)) {\r
98                         clusterTempDir = new File(clusterTempDir).getName();\r
99                 } else {\r
100                         clusterTempDir = "";\r
101                 }\r
102                 // Just extract the last name from the path\r
103                 String localTempDir = StatisticCollector.getLocalJobDir();\r
104                 if (!Util.isEmpty(localTempDir)) {\r
105                         if (PathValidator.isAbsolutePath(localTempDir)) {\r
106                                 localTempDir = new File(localTempDir).getName();\r
107                         }\r
108                 } else {\r
109                         localTempDir = "";\r
110                 }\r
111 \r
112                 Timestamp startDate = new Timestamp(Long.parseLong(fromDate));\r
113                 Timestamp stopDate = new Timestamp(Long.parseLong(toDate));\r
114                 StatDB statdb = null;\r
115                 try {\r
116                         statdb = new StatDB();\r
117                         StatProcessor stat = new StatProcessor(statdb.readData(startDate,\r
118                                         stopDate, wservice, where));\r
119 \r
120                         HttpSession session = req.getSession();\r
121                         if (jobtype.equalsIgnoreCase(JT_CANCELLED)) {\r
122                                 session.setAttribute("stat",\r
123                                                 new StatProcessor(stat.getCancelledJobs()));\r
124                         } else if (jobtype.equalsIgnoreCase(JT_INCOMPLETE)) {\r
125                                 session.setAttribute("stat",\r
126                                                 new StatProcessor(stat.getIncompleteJobs()));\r
127                         } else if (jobtype.equalsIgnoreCase(JT_ALL)) {\r
128                                 session.setAttribute("stat", stat);\r
129                         } else if (jobtype.equalsIgnoreCase(JT_FAILED)) {\r
130                                 session.setAttribute("stat",\r
131                                                 new StatProcessor(stat.getFailedJobs()));\r
132                         } else if (jobtype.equalsIgnoreCase(JT_ABANDONED)) {\r
133                                 session.setAttribute("stat",\r
134                                                 new StatProcessor(stat.getAbandonedJobs()));\r
135                         } else {\r
136                                 throw new AssertionError("Unrecognised job type: " + jobtype);\r
137                         }\r
138                         session.setAttribute("clusterTemp", clusterTempDir);\r
139                         session.setAttribute("localTemp", localTempDir);\r
140                         req.setAttribute("startDate", startDate.getTime());\r
141                         req.setAttribute("stopDate", stopDate.getTime());\r
142 \r
143                         RequestDispatcher dispatcher = req\r
144                                         .getRequestDispatcher("statpages/Joblist.jsp");\r
145                         dispatcher.forward(req, resp);\r
146 \r
147                 } catch (SQLException e) {\r
148                         e.printStackTrace();\r
149                         throw new ServletException("SQLException : "\r
150                                         + e.getLocalizedMessage(), e);\r
151                 }\r
152 \r
153         }\r
154 }\r