Improve call of earliest and current dates (through protected methods)
[proteocache.git] / server / compbio / controllers / BasicController.java
index 2612360..5c20236 100644 (file)
@@ -12,7 +12,9 @@ import compbio.cassandra.DateFormatter;
 import compbio.cassandra.readers.CassandraReader;
 
 /**
- * 
+ * ProteoCache Basic controller. All other controllers should inherit this
+ * controller. Currently BasicController provides user role checks and some
+ * global dates for the system (current and the earliest date of executing job)
  * 
  * @author Alexander Sherstnev
  * @version 1.0
@@ -21,10 +23,13 @@ import compbio.cassandra.readers.CassandraReader;
 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);
 
+       /**
+        * give the user (principal) name
+        * 
+        * @return the user name. If the user uses LDAP credentials LDAP prefix is
+        *         set
+        */
        protected String getPrincipalName() {
                Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                if (principal instanceof UserDetails) {
@@ -42,6 +47,12 @@ public class BasicController {
                return principal.toString();
        }
 
+       /**
+        * check whether the current user has standard user permissions (ROLE_USER
+        * or ROLE_LDAP_USER)
+        * 
+        * @return true if this is a ROLE_USER/ROLE_LDAP_USER user, false otherwise
+        */
        protected boolean isUserRole() {
                Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                if (principal instanceof UserDetails) {
@@ -57,6 +68,11 @@ public class BasicController {
                return false;
        }
 
+       /**
+        * check whether the current user has administrator permissions (ROLE_ADMIN)
+        * 
+        * @return true if this is a ROLE_ADMIN user, false otherwise
+        */
        protected boolean isAdminRole() {
                Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                if (principal instanceof UserDetails) {
@@ -72,20 +88,57 @@ public class BasicController {
                return false;
        }
 
-       protected String DateChecking(String trimmeddate1, String trimmeddate2, long longDate1, long longDate2) {
+       /**
+        * check whether provided dates are ordered properly and within permitted
+        * time range
+        * 
+        * @param date1
+        *            early limit of the time range)
+        * 
+        *            * @param date2 late limit of the time range)
+        * 
+        *            * @param longDate1 date 1 in the numerical represenation (in
+        *            milliseconds)
+        * 
+        *            * @param longDate2 date 2 in the numerical represenation (in
+        *            milliseconds)
+        * 
+        * @return true if these dates are correct, false otherwise
+        */
+       protected String checkDates(String date1, String date2, 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";
+               if (date1.equalsIgnoreCase("") || date2.equalsIgnoreCase(""))
+                       return "The date can not be empty strinfs";
+               else if (!DateFormatter.isThisDateValid(date1, formaterYYMMDD) || !DateFormatter.isThisDateValid(date2, formaterYYMMDD))
+                       return "The date format in invalid. The format yyyy/mm/dd should be used";
                else if (longDate2 < CassandraReader.earliestDate())
-                       return "The date2 is after the earlestDate " + theEaerlistDate;
+                       return "The date2 is earlier than the earliest date in the system " + getEarliestDate();
                else if (longDate1 > cal2.getTimeInMillis())
-                       return "The date1 is before the current date " + theCurrentDate;
+                       return "The date1 is later than the current date " + getCurrentDate();
                else if (longDate1 > longDate2)
-                       return "Wrong date's diaposon. The date1 is more than date2.";
-               else
-                       return null;
+                       return "Wrong date range. The date1 is later than date2.";
+
+               return null;
+       }
+
+       /**
+        * gives the current date in the form of string (yyyy/mm/dd)
+        * 
+        * @return the current date
+        */
+       protected String getCurrentDate() {
+               Calendar cal = Calendar.getInstance();
+               String date = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
+               return date;
+       }
+
+       /**
+        * gives the earliest date in the system (through a direct call of a
+        * Cassandra related class) in the form of string (yyyy/mm/dd)
+        * 
+        * @return the current date
+        */
+       protected String getEarliestDate() {
+               return DateFormatter.DateLongToString(CassandraReader.earliestDate(), formaterYYMMDD);
        }
 }