check the date in controller, new class dateFormatter
[proteocache.git] / server / compbio / controllers / DailyStatisticsController.java
index d6a0c1e..ef399f5 100644 (file)
@@ -12,10 +12,12 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
 import compbio.engine.JobStatus;
+import compbio.cassandra.CassandraReader;
 import compbio.cassandra.DateBean;
+import compbio.cassandra.DateFormatter;
 import compbio.cassandra.TotalJobsStatisticBean;
 import compbio.statistic.CassandraRequester;
-import compbio.statistic.StatisticsProt;
+
 
 /**
  * @author Alexander Sherstnev
@@ -23,18 +25,20 @@ import compbio.statistic.StatisticsProt;
  */
 @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);
+       
        @RequestMapping(value = "/stat/jobs/query", method = RequestMethod.GET)
        public String initFindForm(Map<String, Object> model) {
                model.put("username", getPrincipalName());
-               Calendar cal = Calendar.getInstance();
                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);
-
                model.put("date1", date1);
                model.put("date2", date2);
-
                return "query/JobStatistics";
        }
 
@@ -42,14 +46,34 @@ public class DailyStatisticsController extends BasicController {
        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();
-
+               final long startTime = System.currentTimeMillis();              
                CassandraRequester cr = new CassandraRequester();
                if (option.equals("AllDates,off")) {
-                       Calendar cal = Calendar.getInstance();
-                       date1 = StatisticsProt.DateFormatYYMMDD(cr.earliestDate());
-                       date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
+                       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 longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
+               String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2);
+               if (error != null) {
+                       model.put("error", error);
+                       model.put("date1", date1);
+                       model.put("date2", date2);
+                       return "query/JobStatistics";
                }
+
+               if (longDate1 < CassandraReader.earliestDate())
+                       longDate1 = CassandraReader.earliestDate();
+               if (longDate2 > cal.getTimeInMillis())
+                       longDate2 =  cal.getTimeInMillis();     
+               
+               date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
+               date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
                model.put("date1", date1);
                model.put("date2", date2);
                TotalJobsStatisticBean res = cr.countJobs(date1, date2);
@@ -68,8 +92,6 @@ public class DailyStatisticsController extends BasicController {
                final long startTime = System.currentTimeMillis();
 
                String realdate;
-               final SimpleDateFormat formaterDDMMYY = new SimpleDateFormat("dd/MM/yyyy");
-               final SimpleDateFormat formaterYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
                try {
                        long thetime = formaterYYMMDD.parse(date).getTime();
                        if (thetime < 0) {
@@ -97,4 +119,20 @@ 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;
+       }
 }