1 package compbio.controllers;
3 import java.text.SimpleDateFormat;
4 import java.util.Calendar;
5 import java.util.Collection;
7 import org.springframework.security.core.GrantedAuthority;
8 import org.springframework.security.core.context.SecurityContextHolder;
9 import org.springframework.security.core.userdetails.UserDetails;
11 import compbio.cassandra.DateFormatter;
12 import compbio.cassandra.readers.CassandraReader;
14 public class BasicController {
15 final protected SimpleDateFormat formaterDDMMYY = DateFormatter.getFormatDDMMYY();
16 final protected SimpleDateFormat formaterYYMMDD = DateFormatter.getFormatYYMMDD();
17 protected Calendar cal = Calendar.getInstance();
18 protected String theEaerlistDate = DateFormatter.DateLongToString(CassandraReader.earliestDate(), formaterYYMMDD);
19 protected String theCurrentDate = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
21 protected String getPrincipalName() {
22 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
23 if (principal instanceof UserDetails) {
24 UserDetails details = (UserDetails) principal;
26 String role = details.getUsername();
27 Collection<? extends GrantedAuthority> au = details.getAuthorities();
28 for (GrantedAuthority ga : au) {
29 if (ga.getAuthority().equals("ROLE_LDAP_USER")) {
33 return rolefix + role;
35 return principal.toString();
38 protected boolean isUserRole() {
39 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
40 if (principal instanceof UserDetails) {
41 UserDetails details = (UserDetails) principal;
42 Collection<? extends GrantedAuthority> au = details.getAuthorities();
43 for (GrantedAuthority ga : au) {
44 if (ga.getAuthority().equals("ROLE_USER") || ga.getAuthority().equals("ROLE_LDAP_USER")) {
53 protected boolean isAdminRole() {
54 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
55 if (principal instanceof UserDetails) {
56 UserDetails details = (UserDetails) principal;
57 Collection<? extends GrantedAuthority> au = details.getAuthorities();
58 for (GrantedAuthority ga : au) {
59 if (ga.getAuthority().equals("ROLE_ADMIN")) {
68 protected String DateChecking(String trimmeddate1, String trimmeddate2, long longDate1, long longDate2) {
69 Calendar cal2 = Calendar.getInstance();
70 if (trimmeddate1.equalsIgnoreCase("") || trimmeddate2.equalsIgnoreCase(""))
71 return "The date cann't be empty";
72 else if (!DateFormatter.isThisDateValid(trimmeddate1, formaterYYMMDD)
73 || !DateFormatter.isThisDateValid(trimmeddate2, formaterYYMMDD))
74 return "The date format in invalid. Try format yyyy/mm/dd";
75 else if (longDate2 < CassandraReader.earliestDate())
76 return "The date2 is after the earlestDate " + theEaerlistDate;
77 else if (longDate1 > cal2.getTimeInMillis())
78 return "The date1 is before the current date " + theCurrentDate;
79 else if (longDate1 > longDate2)
80 return "Wrong date's diaposon. The date1 is more than date2.";