Centralize initial examples of job id and IP
[proteocache.git] / server / compbio / controllers / JobController.java
index 94045f9..4819e75 100644 (file)
@@ -1,26 +1,43 @@
 package compbio.controllers;
 
-import java.security.Principal;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.log4j.Logger;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
 import compbio.statistic.CassandraRequester;
+import compbio.cassandra.CassandraNativeConnector;
 import compbio.cassandra.DataBase;
-import compbio.statistic.StatisticsProt;
+import compbio.engine.archive.ArchivedJob;
 
 /**
  * @author Alexander Sherstnev
  * @author Natasha Sherstneva
+ * @version 1.0 Dec 2013
  */
 @Controller
 public class JobController extends BasicController {
+       private static Logger log = Logger.getLogger(JobController.class);
 
+       /**
+        * form a query page for job execution time statistics. The servlet should
+        * be available for users and admins only. By defaults the report time range
+        * is from the earliest day with jobs to today ("Query for all dates" is
+        * ticked). If the user removes the tick the time range is from today - 3
+        * days to today. Currently, the input model is empty.
+        * 
+        * @param model
+        *            MVC model
+        * @return link to the JSP query page
+        */
        @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
        public String initFormExecTime(Map<String, Object> model) {
                model.put("username", getPrincipalName());
@@ -34,13 +51,29 @@ public class JobController extends BasicController {
                return "query/JobTimeExecution";
        }
 
+       /**
+        * form a query page for a job. The servlet should no be visible to users at
+        * all.
+        * 
+        * @param model
+        *            MVC model
+        * @return link to the JSP query page
+        */
        @RequestMapping(value = "/job/query", method = RequestMethod.GET)
        public String initFindForm(Map<String, Object> model) {
                model.put("username", getPrincipalName());
-               model.put("value", "jp_NzBOJKo");
+               CassandraRequester cr = new CassandraRequester();
+               model.put("value", cr.getExample("jobid"));
                return "query/JobLog";
        }
 
+       /**
+        * form a report page for a job execution time statistics.
+        * 
+        * @param model
+        *            MVC model
+        * @return link to the JSP query page
+        */
        @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
        public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
                        @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
@@ -50,7 +83,7 @@ public class JobController extends BasicController {
                CassandraRequester sp = new CassandraRequester();
                if (option.equals("AllDates,off")) {
                        Calendar cal = Calendar.getInstance();
-                       date1 = StatisticsProt.DateFormatYYMMDD(sp.earliestDate());
+                       date1 = DateFormatYYMMDD(sp.earliestDate());
                        date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
                }
                model.put("date1", date1);
@@ -64,6 +97,14 @@ public class JobController extends BasicController {
                return "/reportTimeExecution";
        }
 
+       /**
+        * form a query page for a job. The servlet should no be visible to users at
+        * all.
+        * 
+        * @param model
+        *            MVC model
+        * @return link to the JSP query page
+        */
        @RequestMapping(value = "/job/results", method = RequestMethod.GET)
        public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) {
                model.put("username", getPrincipalName());
@@ -73,7 +114,30 @@ public class JobController extends BasicController {
                final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
                model.put("IdJob", jobid);
+
+               ArchivedJob aj = new ArchivedJob(jobid);
+               try {
+                       model.put("jobarchive", aj.prepareJobArchiveToWeb());
+               } catch (IOException e) {
+                       log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
+                       log.error(e.getLocalizedMessage(), e.getCause());
+               }
                return "reportJobLog";
        }
-       
+
+       /**
+        * convert date from the standard long representation (milliseconds from 1
+        * Jan 1970) to yyyy/mm/dd
+        * 
+        * @param indate
+        *            date in milliseconds from 1 Jan 1970
+        * @return date 
+        *            in the form of yyyy/mm/dd
+        */
+       private String DateFormatYYMMDD(long indate) {
+               SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
+               String dateString = datformat.format(new Date(indate));
+               return dateString;
+       }
+
 }