Improve call of earliest and current dates (through protected methods)
[proteocache.git] / server / compbio / controllers / BasicController.java
index 32703aa..5c20236 100644 (file)
@@ -1,13 +1,35 @@
 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;
 
+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
+ * @since Dec 2013
+ */
 public class BasicController {
+       final protected SimpleDateFormat formaterDDMMYY = DateFormatter.getFormatDDMMYY();
+       final protected SimpleDateFormat formaterYYMMDD = DateFormatter.getFormatYYMMDD();
 
+       /**
+        * 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) {
@@ -25,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) {
@@ -40,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) {
@@ -54,5 +87,58 @@ public class BasicController {
                }
                return false;
        }
-       
+
+       /**
+        * 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 (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 earlier than the earliest date in the system " + getEarliestDate();
+               else if (longDate1 > cal2.getTimeInMillis())
+                       return "The date1 is later than the current date " + getCurrentDate();
+               else if (longDate1 > longDate2)
+                       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);
+       }
 }