From: Sasha Sherstnev Date: Mon, 27 Jan 2014 14:48:18 +0000 (+0000) Subject: Improve call of earliest and current dates (through protected methods) X-Git-Url: http://source.jalview.org/gitweb/?p=proteocache.git;a=commitdiff_plain;h=5637d461a2a42327d160f4de7b29ca6e6e1f32ab Improve call of earliest and current dates (through protected methods) --- diff --git a/server/compbio/controllers/BasicController.java b/server/compbio/controllers/BasicController.java index 2612360..5c20236 100644 --- a/server/compbio/controllers/BasicController.java +++ b/server/compbio/controllers/BasicController.java @@ -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); } } diff --git a/server/compbio/controllers/DailyStatisticsController.java b/server/compbio/controllers/DailyStatisticsController.java index 5d183f9..1453523 100644 --- a/server/compbio/controllers/DailyStatisticsController.java +++ b/server/compbio/controllers/DailyStatisticsController.java @@ -20,9 +20,13 @@ import compbio.cassandra.readers.CassandraReader; import compbio.cassandra.readers.DailyStatisticsReader; /** + * MVC controller for collecting and showing job statistics + * * @author Alexander Sherstnev * @author Natasha Sherstneva - * @version 1.0 Dec 2013 + * + * @version 1.0 + * @since Dec 2013 */ @Controller public class DailyStatisticsController extends BasicController { @@ -76,8 +80,8 @@ public class DailyStatisticsController extends BasicController { final long startTime = System.currentTimeMillis(); DailyStatisticsReader reader = new DailyStatisticsReader(); if (option.equals("AllDates,off")) { - date1 = theEaerlistDate; - date2 = theCurrentDate; + date1 = getEarliestDate(); + date2 = getCurrentDate(); } // dates in string format @@ -86,7 +90,7 @@ public class DailyStatisticsController extends BasicController { // dates in long format long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD); long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD); - String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2); + String error = checkDates(trimmeddate1, trimmeddate2, longDate1, longDate2); if (error != null) { model.put("error", error); model.put("date1", date1); @@ -151,10 +155,9 @@ public class DailyStatisticsController extends BasicController { thetime = formaterDDMMYY.parse(realdate).getTime(); } - if (null == JobStatus.getJobStatus(status)) + if (null == JobStatus.getJobStatus(status)) return "support/Notimplemented"; - DailyStatisticsReader reader = new DailyStatisticsReader(); // IMPORTANT: input should be suppied in the format: DD/MM/YYYY DateBean r = reader.readJobByDay(thetime, realdate, JobStatus.getJobStatus(status)); diff --git a/server/compbio/controllers/JobController.java b/server/compbio/controllers/JobController.java index 0dd39f8..314631b 100644 --- a/server/compbio/controllers/JobController.java +++ b/server/compbio/controllers/JobController.java @@ -99,8 +99,8 @@ public class JobController extends BasicController { Calendar loccal = Calendar.getInstance(); ExecutionTimeReader reader = new ExecutionTimeReader(); if (alldates.equals("AllDates,off")) { - date1 = theEaerlistDate; - date2 = theCurrentDate; + date1 = getEarliestDate(); + date2 = getCurrentDate(); } // dates in string format @@ -109,7 +109,7 @@ public class JobController extends BasicController { // dates in long format long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD); long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD); - String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2); + String error = checkDates(trimmeddate1, trimmeddate2, longDate1, longDate2); if (error != null) { model.put("error", error); model.put("date1", date1);