Change header template for a new version
[jabaws.git] / webservices / compbio / stat / servlet / Joblist.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.File;\r
21 import java.io.IOException;\r
22 import java.sql.SQLException;\r
23 import java.sql.Timestamp;\r
24 \r
25 import javax.servlet.RequestDispatcher;\r
26 import javax.servlet.ServletException;\r
27 import javax.servlet.http.HttpServlet;\r
28 import javax.servlet.http.HttpServletRequest;\r
29 import javax.servlet.http.HttpServletResponse;\r
30 import javax.servlet.http.HttpSession;\r
31 \r
32 import compbio.engine.client.PathValidator;\r
33 import compbio.stat.collector.StatDB;\r
34 import compbio.stat.collector.StatProcessor;\r
35 import compbio.util.Util;\r
36 import compbio.ws.client.Services;\r
37 \r
38 public class Joblist extends HttpServlet {\r
39 \r
40         static final String JT_FAILED = "failed";\r
41         static final String JT_ABANDONED = "abandoned";\r
42         static final String JT_CANCELLED = "cancelled";\r
43         static final String JT_ALL = "all";\r
44         static final String JT_INCOMPLETE = "incomplete";\r
45         /**\r
46          * Input:\r
47          * \r
48          * ws=${ws.key}\r
49          * \r
50          * where=everywhere cluster local\r
51          * \r
52          * type=cancelled all incomplete\r
53          * \r
54          * from=${startDate}\r
55          * \r
56          * to=${stopDate}\r
57          * \r
58          */\r
59         @Override\r
60         protected void doGet(HttpServletRequest req, HttpServletResponse resp)\r
61                         throws ServletException, IOException {\r
62                 /*\r
63                  * Values for this servlet are not user supplied, so do not bother with\r
64                  * nice error messages just throw the exception is something is wrong!\r
65                  */\r
66                 String wsname = req.getParameter("ws");\r
67                 Services wservice = Services.getService(wsname);\r
68                 if (wservice == null) {\r
69                         throw new ServletException(\r
70                                         "Webservice name 'ws' is not specified or is incorrect. Given value:"\r
71                                                         + wsname);\r
72                 }\r
73                 String executor = req.getParameter("where");\r
74                 if (Util.isEmpty(executor)) {\r
75                         throw new ServletException("'Where' is not specified!");\r
76                 }\r
77                 if (!(executor.equalsIgnoreCase("everywhere")\r
78                                 || executor.equalsIgnoreCase("local") || executor\r
79                                 .equalsIgnoreCase("cluster"))) {\r
80                         throw new ServletException("Invalid 'where' value '" + executor\r
81                                         + "' can be one of 'everywhere', 'local', 'cluster'!");\r
82                 }\r
83                 Boolean where = null;\r
84                 if (executor.equalsIgnoreCase("local")) {\r
85                         where = false;\r
86                 } else if (executor.equalsIgnoreCase("cluster")) {\r
87                         where = true;\r
88                 }\r
89 \r
90                 String jobtype = req.getParameter("type");\r
91                 if (Util.isEmpty(executor)) {\r
92                         throw new ServletException("'type' is not specified!");\r
93                 }\r
94                 if (!(jobtype.equalsIgnoreCase(JT_CANCELLED)\r
95                                 || jobtype.equalsIgnoreCase(JT_ALL)\r
96                                 || jobtype.equalsIgnoreCase(JT_INCOMPLETE)\r
97                                 || jobtype.equalsIgnoreCase(JT_ABANDONED) || jobtype\r
98                                 .equalsIgnoreCase(JT_FAILED))) {\r
99                         throw new ServletException("Invalid 'jobtype' value '" + jobtype\r
100                                         + "' can be one of 'cancelled', 'all', 'incomplete', "\r
101                                         + "'failed', 'abandoned'!");\r
102                 }\r
103                 String fromDate = req.getParameter("from");\r
104                 if (Util.isEmpty(fromDate)) {\r
105                         throw new ServletException("'fromDate' is not specified!");\r
106                 }\r
107                 String toDate = req.getParameter("to");\r
108                 if (Util.isEmpty(toDate)) {\r
109                         throw new ServletException("'toDate' is not specified!");\r
110                 }\r
111 \r
112                 // Just extract the last name from the path\r
113                 String clusterTempDir = StatisticCollector.getClusterJobDir();\r
114                 if (!Util.isEmpty(clusterTempDir)) {\r
115                         clusterTempDir = new File(clusterTempDir).getName();\r
116                 } else {\r
117                         clusterTempDir = "";\r
118                 }\r
119                 // Just extract the last name from the path\r
120                 String localTempDir = StatisticCollector.getLocalJobDir();\r
121                 if (!Util.isEmpty(localTempDir)) {\r
122                         if (PathValidator.isAbsolutePath(localTempDir)) {\r
123                                 localTempDir = new File(localTempDir).getName();\r
124                         }\r
125                 } else {\r
126                         localTempDir = "";\r
127                 }\r
128 \r
129                 Timestamp startDate = new Timestamp(Long.parseLong(fromDate));\r
130                 Timestamp stopDate = new Timestamp(Long.parseLong(toDate));\r
131                 StatDB statdb = null;\r
132                 try {\r
133                         statdb = new StatDB();\r
134                         StatProcessor stat = new StatProcessor(statdb.readData(startDate,\r
135                                         stopDate, wservice, where));\r
136 \r
137                         HttpSession session = req.getSession();\r
138                         if (jobtype.equalsIgnoreCase(JT_CANCELLED)) {\r
139                                 session.setAttribute("stat",\r
140                                                 new StatProcessor(stat.getCancelledJobs()));\r
141                         } else if (jobtype.equalsIgnoreCase(JT_INCOMPLETE)) {\r
142                                 session.setAttribute("stat",\r
143                                                 new StatProcessor(stat.getIncompleteJobs()));\r
144                         } else if (jobtype.equalsIgnoreCase(JT_ALL)) {\r
145                                 session.setAttribute("stat", stat);\r
146                         } else if (jobtype.equalsIgnoreCase(JT_FAILED)) {\r
147                                 session.setAttribute("stat",\r
148                                                 new StatProcessor(stat.getFailedJobs()));\r
149                         } else if (jobtype.equalsIgnoreCase(JT_ABANDONED)) {\r
150                                 session.setAttribute("stat",\r
151                                                 new StatProcessor(stat.getAbandonedJobs()));\r
152                         } else {\r
153                                 throw new AssertionError("Unrecognised job type: " + jobtype);\r
154                         }\r
155                         session.setAttribute("clusterTemp", clusterTempDir);\r
156                         session.setAttribute("localTemp", localTempDir);\r
157                         req.setAttribute("startDate", startDate.getTime());\r
158                         req.setAttribute("stopDate", stopDate.getTime());\r
159 \r
160                         RequestDispatcher dispatcher = req\r
161                                         .getRequestDispatcher("statpages/Joblist.jsp");\r
162                         dispatcher.forward(req, resp);\r
163 \r
164                 } catch (SQLException e) {\r
165                         e.printStackTrace();\r
166                         throw new ServletException("SQLException : "\r
167                                         + e.getLocalizedMessage(), e);\r
168                 }\r
169 \r
170         }\r
171 }\r