new readers for the queries
[proteocache.git] / server / compbio / controllers / BasicController.java
1 package compbio.controllers;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Calendar;
5 import java.util.Collection;
6
7 import org.springframework.security.core.GrantedAuthority;
8 import org.springframework.security.core.context.SecurityContextHolder;
9 import org.springframework.security.core.userdetails.UserDetails;
10
11 import compbio.cassandra.DateFormatter;
12 import compbio.cassandra.readers.CassandraReader;
13
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);
20         
21         protected String getPrincipalName() {
22                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
23                 if (principal instanceof UserDetails) {
24                         UserDetails details = (UserDetails) principal;
25                         String rolefix = "";
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")) {
30                                         rolefix = "LDAP:";
31                                 }
32                         }
33                         return rolefix + role;
34                 }
35                 return principal.toString();
36         }
37
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")) {
45                                         return true;
46                                 }
47                         }
48                         return false;
49                 }
50                 return false;
51         }
52
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")) {
60                                         return true;
61                                 }
62                         }
63                         return false;
64                 }
65                 return false;
66         }
67         
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) || !DateFormatter.isThisDateValid(trimmeddate2, formaterYYMMDD)) 
73                         return "The date format in invalid. Try format yyyy/mm/dd";
74                 else if (longDate2 < CassandraReader.earliestDate()) 
75                         return "The date2 is after the earlestDate " + theEaerlistDate;
76                 else if (longDate1 > cal2.getTimeInMillis())
77                         return "The date1 is before the current date " + theCurrentDate;
78                 else if (longDate1 > longDate2)         
79                         return "Wrong date's diaposon. The date1 is more than date2.";
80                 else                    
81                         return null;
82         }
83 }