Improve call of earliest and current dates (through protected methods)
[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  * ProteoCache Basic controller. All other controllers should inherit this
16  * controller. Currently BasicController provides user role checks and some
17  * global dates for the system (current and the earliest date of executing job)
18  * 
19  * @author Alexander Sherstnev
20  * @version 1.0
21  * @since Dec 2013
22  */
23 public class BasicController {
24         final protected SimpleDateFormat formaterDDMMYY = DateFormatter.getFormatDDMMYY();
25         final protected SimpleDateFormat formaterYYMMDD = DateFormatter.getFormatYYMMDD();
26
27         /**
28          * give the user (principal) name
29          * 
30          * @return the user name. If the user uses LDAP credentials LDAP prefix is
31          *         set
32          */
33         protected String getPrincipalName() {
34                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
35                 if (principal instanceof UserDetails) {
36                         UserDetails details = (UserDetails) principal;
37                         String rolefix = "";
38                         String role = details.getUsername();
39                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
40                         for (GrantedAuthority ga : au) {
41                                 if (ga.getAuthority().equals("ROLE_LDAP_USER")) {
42                                         rolefix = "LDAP:";
43                                 }
44                         }
45                         return rolefix + role;
46                 }
47                 return principal.toString();
48         }
49
50         /**
51          * check whether the current user has standard user permissions (ROLE_USER
52          * or ROLE_LDAP_USER)
53          * 
54          * @return true if this is a ROLE_USER/ROLE_LDAP_USER user, false otherwise
55          */
56         protected boolean isUserRole() {
57                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
58                 if (principal instanceof UserDetails) {
59                         UserDetails details = (UserDetails) principal;
60                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
61                         for (GrantedAuthority ga : au) {
62                                 if (ga.getAuthority().equals("ROLE_USER") || ga.getAuthority().equals("ROLE_LDAP_USER")) {
63                                         return true;
64                                 }
65                         }
66                         return false;
67                 }
68                 return false;
69         }
70
71         /**
72          * check whether the current user has administrator permissions (ROLE_ADMIN)
73          * 
74          * @return true if this is a ROLE_ADMIN user, false otherwise
75          */
76         protected boolean isAdminRole() {
77                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
78                 if (principal instanceof UserDetails) {
79                         UserDetails details = (UserDetails) principal;
80                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
81                         for (GrantedAuthority ga : au) {
82                                 if (ga.getAuthority().equals("ROLE_ADMIN")) {
83                                         return true;
84                                 }
85                         }
86                         return false;
87                 }
88                 return false;
89         }
90
91         /**
92          * check whether provided dates are ordered properly and within permitted
93          * time range
94          * 
95          * @param date1
96          *            early limit of the time range)
97          * 
98          *            * @param date2 late limit of the time range)
99          * 
100          *            * @param longDate1 date 1 in the numerical represenation (in
101          *            milliseconds)
102          * 
103          *            * @param longDate2 date 2 in the numerical represenation (in
104          *            milliseconds)
105          * 
106          * @return true if these dates are correct, false otherwise
107          */
108         protected String checkDates(String date1, String date2, long longDate1, long longDate2) {
109                 Calendar cal2 = Calendar.getInstance();
110                 if (date1.equalsIgnoreCase("") || date2.equalsIgnoreCase(""))
111                         return "The date can not be empty strinfs";
112                 else if (!DateFormatter.isThisDateValid(date1, formaterYYMMDD) || !DateFormatter.isThisDateValid(date2, formaterYYMMDD))
113                         return "The date format in invalid. The format yyyy/mm/dd should be used";
114                 else if (longDate2 < CassandraReader.earliestDate())
115                         return "The date2 is earlier than the earliest date in the system " + getEarliestDate();
116                 else if (longDate1 > cal2.getTimeInMillis())
117                         return "The date1 is later than the current date " + getCurrentDate();
118                 else if (longDate1 > longDate2)
119                         return "Wrong date range. The date1 is later than date2.";
120
121                 return null;
122         }
123
124         /**
125          * gives the current date in the form of string (yyyy/mm/dd)
126          * 
127          * @return the current date
128          */
129         protected String getCurrentDate() {
130                 Calendar cal = Calendar.getInstance();
131                 String date = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
132                 return date;
133         }
134
135         /**
136          * gives the earliest date in the system (through a direct call of a
137          * Cassandra related class) in the form of string (yyyy/mm/dd)
138          * 
139          * @return the current date
140          */
141         protected String getEarliestDate() {
142                 return DateFormatter.DateLongToString(CassandraReader.earliestDate(), formaterYYMMDD);
143         }
144 }