Move one method and som internal data form DailyStatisticsController.java to BasicCo...
authorSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Fri, 20 Dec 2013 08:29:49 +0000 (08:29 +0000)
committerSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Fri, 20 Dec 2013 08:29:49 +0000 (08:29 +0000)
server/compbio/controllers/BasicController.java
server/compbio/controllers/DailyStatisticsController.java

index 32703aa..a714c4d 100644 (file)
@@ -1,13 +1,23 @@
 package compbio.controllers;
 
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Collection;
 
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.core.userdetails.UserDetails;
 
-public class BasicController {
+import compbio.cassandra.CassandraReader;
+import compbio.cassandra.DateFormatter;
 
+public class BasicController {
+       final protected SimpleDateFormat formaterDDMMYY = DateFormatter.getFormatDDMMYY();
+       final protected SimpleDateFormat formaterYYMMDD = DateFormatter.getFormatYYMMDD();
+       protected Calendar cal = Calendar.getInstance();
+       protected String theEaerlistDate = DateFormatter.DateLongToString(CassandraReader.earliestDate(), formaterYYMMDD);
+       protected String theCurrentDate = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
+       
        protected String getPrincipalName() {
                Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                if (principal instanceof UserDetails) {
@@ -55,4 +65,19 @@ public class BasicController {
                return false;
        }
        
+       protected String DateChecking(String trimmeddate1, String trimmeddate2, long longDate1, long longDate2) {
+               Calendar cal2 = Calendar.getInstance();
+               if (trimmeddate1.equalsIgnoreCase("") || trimmeddate2.equalsIgnoreCase("")) 
+                       return "The date cann't be empty";
+               else if (!DateFormatter.isThisDateValid(trimmeddate1, formaterYYMMDD) || !DateFormatter.isThisDateValid(trimmeddate2, formaterYYMMDD)) 
+                       return "The date format in invalid. Try format yyyy/mm/dd";
+               else if (longDate2 < CassandraReader.earliestDate()) 
+                       return "The date2 is after the earlestDate " + theEaerlistDate;
+               else if (longDate1 > cal2.getTimeInMillis())
+                       return "The date1 is before the current date " + theCurrentDate;
+               else if (longDate1 > longDate2)         
+                       return "Wrong date's diaposon. The date1 is more than date2.";
+               else                    
+                       return null;
+       }
 }
index ef399f5..72ece7e 100644 (file)
@@ -1,7 +1,6 @@
 package compbio.controllers;
 
 import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Map;
@@ -18,46 +17,70 @@ import compbio.cassandra.DateFormatter;
 import compbio.cassandra.TotalJobsStatisticBean;
 import compbio.statistic.CassandraRequester;
 
-
 /**
  * @author Alexander Sherstnev
  * @author Natasha Sherstneva
+ * @version 1.0 Dec 2013
  */
 @Controller
 public class DailyStatisticsController extends BasicController {
-       Calendar cal = Calendar.getInstance();
-       final SimpleDateFormat formaterDDMMYY = DateFormatter.getFormatDDMMYY();
-       final SimpleDateFormat formaterYYMMDD = DateFormatter.getFormatYYMMDD();
-       String theEaerlistDate = DateFormatter.DateLongToString(CassandraReader.earliestDate(), formaterYYMMDD);
-       String theCurrentDate = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
-       
+
+       /**
+        * form a query page for daily job 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/jobs/query", method = RequestMethod.GET)
        public String initFindForm(Map<String, Object> model) {
                model.put("username", getPrincipalName());
-               String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
-               cal.add(Calendar.DATE, -3);
-               String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
+               Calendar loccal = Calendar.getInstance();
+               String date2 = loccal.get(Calendar.YEAR) + "/" + (loccal.get(Calendar.MONTH) + 1) + "/" + loccal.get(Calendar.DATE);
+               loccal.add(Calendar.DATE, -3);
+               String date1 = loccal.get(Calendar.YEAR) + "/" + (loccal.get(Calendar.MONTH) + 1) + "/" + loccal.get(Calendar.DATE);
                model.put("date1", date1);
                model.put("date2", date2);
                return "query/JobStatistics";
        }
 
+       /**
+        * form a report page for daily job statistics.
+        * 
+        * @param model
+        *            MVC model object
+        * @param date1
+        *            initial date for the report (if option is set, date2 = the
+        *            earliest date with jobs in DB)
+        * @param date2
+        *            the final date for the report (if option is set, date2 =
+        *            today)
+        * @param option
+        *            defined whether the whole time range of jobs is reported
+        *            (null means date1 and date2 are used)
+        * @return link to the report JSP page
+        */
        @RequestMapping(value = "/stat/jobsdaily/results", method = RequestMethod.GET)
        public String findJobsInPeriod(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
                        @RequestParam("option") String option, Map<String, Object> model) {
                model.put("username", getPrincipalName());
-               final long startTime = System.currentTimeMillis();              
+               Calendar loccal = Calendar.getInstance();
+               final long startTime = System.currentTimeMillis();
                CassandraRequester cr = new CassandraRequester();
                if (option.equals("AllDates,off")) {
                        date1 = theEaerlistDate;
                        date2 = theCurrentDate;
                }
-       
+
                // dates in string format
                String trimmeddate1 = date1.replaceAll("\\s", "");
                String trimmeddate2 = date2.replaceAll("\\s", "");
                // dates in long format
-               long longDate1 = DateFormatter.DateParsing(date1,formaterYYMMDD);
+               long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
                long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
                String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2);
                if (error != null) {
@@ -69,9 +92,9 @@ public class DailyStatisticsController extends BasicController {
 
                if (longDate1 < CassandraReader.earliestDate())
                        longDate1 = CassandraReader.earliestDate();
-               if (longDate2 > cal.getTimeInMillis())
-                       longDate2 =  cal.getTimeInMillis();     
-               
+               if (longDate2 > loccal.getTimeInMillis())
+                       longDate2 = loccal.getTimeInMillis();
+
                date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
                date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
                model.put("date1", date1);
@@ -85,6 +108,17 @@ public class DailyStatisticsController extends BasicController {
                return "/reportJobStatistics";
        }
 
+       /**
+        * form a report page for job statistics for one day only.
+        * 
+        * @param model
+        *            MVC model object
+        * @param date
+        *            date for the report
+        * @param status
+        * 
+        * @return link to the report JSP page
+        */
        @RequestMapping(value = "/stat/jobsoneday/results", method = RequestMethod.GET)
        public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("status") String status, Map<String, Object> model)
                        throws ParseException {
@@ -119,20 +153,4 @@ public class DailyStatisticsController extends BasicController {
                model.put("timeExecution", (endTime - startTime));
                return "/reportJobStatisticsOneDay";
        }
-       
-       private String DateChecking(String trimmeddate1, String trimmeddate2, long longDate1, long longDate2) {
-               
-               if (trimmeddate1.equalsIgnoreCase("") || trimmeddate2.equalsIgnoreCase("")) 
-                       return "The date cann't be empty";
-               else if (!DateFormatter.isThisDateValid(trimmeddate1, formaterYYMMDD) || !DateFormatter.isThisDateValid(trimmeddate2, formaterYYMMDD)) 
-                       return "The date format in invalid. Try format yyyy/mm/dd";
-               else if (longDate2 < CassandraReader.earliestDate()) 
-                       return "The date2 is after the earlestDate " + theEaerlistDate;
-               else if (longDate1 > cal.getTimeInMillis())
-                       return "The date1 is before the current date " + theCurrentDate;                        
-               else if (longDate1 > longDate2)         
-                       return "Wrong date's diaposon. The date1 is more than date2.";
-               else                    
-                       return null;
-       }
 }