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.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) {
UserDetails details = (UserDetails) principal;
- String ldapprefix = "";
+ String rolefix = "";
String role = details.getUsername();
Collection<? extends GrantedAuthority> au = details.getAuthorities();
for (GrantedAuthority ga : au) {
- System.out.println("role -> " + ga.getAuthority());
if (ga.getAuthority().equals("ROLE_LDAP_USER")) {
- ldapprefix = "LDAP:";
+ rolefix = "LDAP:";
}
}
- return ldapprefix + role;
+ return rolefix + role;
}
return principal.toString();
}
return false;
}
+ protected boolean isAdminRole() {
+ Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+ if (principal instanceof UserDetails) {
+ UserDetails details = (UserDetails) principal;
+ Collection<? extends GrantedAuthority> au = details.getAuthorities();
+ for (GrantedAuthority ga : au) {
+ if (ga.getAuthority().equals("ROLE_ADMIN")) {
+ return true;
+ }
+ }
+ return false;
+ }
+ 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;
+ }
}