26123600da9467cc36d03af439654f3adcbd2cdd
[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 /**
15  * 
16  * 
17  * @author Alexander Sherstnev
18  * @version 1.0
19  * @since Dec 2013
20  */
21 public class BasicController {
22         final protected SimpleDateFormat formaterDDMMYY = DateFormatter.getFormatDDMMYY();
23         final protected SimpleDateFormat formaterYYMMDD = DateFormatter.getFormatYYMMDD();
24         protected Calendar cal = Calendar.getInstance();
25         protected String theEaerlistDate = DateFormatter.DateLongToString(CassandraReader.earliestDate(), formaterYYMMDD);
26         protected String theCurrentDate = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
27
28         protected String getPrincipalName() {
29                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
30                 if (principal instanceof UserDetails) {
31                         UserDetails details = (UserDetails) principal;
32                         String rolefix = "";
33                         String role = details.getUsername();
34                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
35                         for (GrantedAuthority ga : au) {
36                                 if (ga.getAuthority().equals("ROLE_LDAP_USER")) {
37                                         rolefix = "LDAP:";
38                                 }
39                         }
40                         return rolefix + role;
41                 }
42                 return principal.toString();
43         }
44
45         protected boolean isUserRole() {
46                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
47                 if (principal instanceof UserDetails) {
48                         UserDetails details = (UserDetails) principal;
49                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
50                         for (GrantedAuthority ga : au) {
51                                 if (ga.getAuthority().equals("ROLE_USER") || ga.getAuthority().equals("ROLE_LDAP_USER")) {
52                                         return true;
53                                 }
54                         }
55                         return false;
56                 }
57                 return false;
58         }
59
60         protected boolean isAdminRole() {
61                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
62                 if (principal instanceof UserDetails) {
63                         UserDetails details = (UserDetails) principal;
64                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
65                         for (GrantedAuthority ga : au) {
66                                 if (ga.getAuthority().equals("ROLE_ADMIN")) {
67                                         return true;
68                                 }
69                         }
70                         return false;
71                 }
72                 return false;
73         }
74
75         protected String DateChecking(String trimmeddate1, String trimmeddate2, long longDate1, long longDate2) {
76                 Calendar cal2 = Calendar.getInstance();
77                 if (trimmeddate1.equalsIgnoreCase("") || trimmeddate2.equalsIgnoreCase(""))
78                         return "The date cann't be empty";
79                 else if (!DateFormatter.isThisDateValid(trimmeddate1, formaterYYMMDD)
80                                 || !DateFormatter.isThisDateValid(trimmeddate2, formaterYYMMDD))
81                         return "The date format in invalid. Try format yyyy/mm/dd";
82                 else if (longDate2 < CassandraReader.earliestDate())
83                         return "The date2 is after the earlestDate " + theEaerlistDate;
84                 else if (longDate1 > cal2.getTimeInMillis())
85                         return "The date1 is before the current date " + theCurrentDate;
86                 else if (longDate1 > longDate2)
87                         return "Wrong date's diaposon. The date1 is more than date2.";
88                 else
89                         return null;
90         }
91 }