1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.service.timezone;
17
18 import java.util.List;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.joda.time.DateTimeZone;
22 import org.joda.time.LocalDate;
23 import org.kuali.kpme.core.job.Job;
24 import org.kuali.kpme.core.location.Location;
25 import org.kuali.kpme.core.principal.PrincipalHRAttributes;
26 import org.kuali.kpme.core.service.HrServiceLocator;
27 import org.kuali.kpme.core.util.HrContext;
28 import org.kuali.kpme.core.util.TKUtils;
29 import org.kuali.rice.krad.util.GlobalVariables;
30
31 public class TimezoneServiceImpl implements TimezoneService {
32
33 @Override
34 public String getUserTimezone(String principalId) {
35 PrincipalHRAttributes principalCalendar = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, LocalDate.now());
36 if(principalCalendar != null && principalCalendar.getTimezone() != null){
37 return principalCalendar.getTimezone();
38 }
39 List<Job> jobs = HrServiceLocator.getJobService().getJobs(principalId, LocalDate.now());
40 if (jobs.size() > 0) {
41
42 Location location = HrServiceLocator.getLocationService().getLocation(jobs.get(0).getLocation(), LocalDate.now());
43 if (location!=null){
44 if(StringUtils.isNotBlank(location.getTimezone())){
45 return location.getTimezone();
46 }
47 }
48 }
49 return TKUtils.getSystemTimeZone();
50 }
51
52
53
54
55 @Override
56 public String getUserTimezone() {
57 String timezone = "";
58 if (GlobalVariables.getUserSession() != null) {
59 timezone = getUserTimezone(GlobalVariables.getUserSession().getPrincipalId());
60 }
61 return timezone;
62 }
63
64 @Override
65 public DateTimeZone getUserTimezoneWithFallback() {
66 String tzid = getUserTimezone();
67 if (StringUtils.isEmpty(tzid)) {
68 return TKUtils.getSystemDateTimeZone();
69 } else {
70 return DateTimeZone.forID(tzid);
71 }
72 }
73
74 @Override
75 public DateTimeZone getTargetUserTimezoneWithFallback() {
76 String tzid = getTargetUserTimezone();
77 if (StringUtils.isEmpty(tzid)) {
78 return TKUtils.getSystemDateTimeZone();
79 } else {
80 return DateTimeZone.forID(tzid);
81 }
82 }
83
84 @Override
85 public String getTargetUserTimezone() {
86 String timezone = "";
87 if (GlobalVariables.getUserSession() != null) {
88
89 timezone = getUserTimezone(HrContext.getTargetPrincipalId());
90 }
91 return timezone;
92 }
93
94 @Override
95 public boolean isSameTimezone() {
96 String userTimezone = getUserTimezone();
97 if(StringUtils.isNotBlank(userTimezone)) {
98 return StringUtils.equals(TKUtils.getSystemTimeZone(), userTimezone);
99 }
100 return true;
101 }
102
103 @Override
104 public String getApproverTimezone(String principalId) {
105 PrincipalHRAttributes principalCalendar = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, LocalDate.now());
106 if(principalCalendar != null && principalCalendar.getTimezone() != null){
107 return principalCalendar.getTimezone();
108 }
109 return TKUtils.getSystemTimeZone();
110 }
111
112 }