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.api.job.Job;
24 import org.kuali.kpme.core.api.location.LocationContract;
25 import org.kuali.kpme.core.api.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
37 if(principalCalendar != null && principalCalendar.getTimezone() != null){
38 return principalCalendar.getTimezone();
39 }
40 List<Job> jobs = HrServiceLocator.getJobService().getJobs(principalId, LocalDate.now());
41 if (jobs.size() > 0) {
42
43
44 LocationContract location = HrServiceLocator.getLocationService().getLocation(jobs.get(0).getGroupKey().getLocationId(), LocalDate.now());
45 if (location!=null){
46 if(StringUtils.isNotBlank(location.getTimezone())){
47 return location.getTimezone();
48 }
49 }
50 }
51 return TKUtils.getSystemTimeZone();
52 }
53
54
55
56
57 @Override
58 public String getUserTimezone() {
59 String timezone = "";
60 if (GlobalVariables.getUserSession() != null) {
61 timezone = getUserTimezone(GlobalVariables.getUserSession().getPrincipalId());
62 }
63 return timezone;
64 }
65
66 @Override
67 public DateTimeZone getUserTimezoneWithFallback() {
68 String tzid = getUserTimezone();
69 if (StringUtils.isEmpty(tzid)) {
70 return TKUtils.getSystemDateTimeZone();
71 } else {
72 return DateTimeZone.forID(tzid);
73 }
74 }
75
76 @Override
77 public DateTimeZone getTargetUserTimezoneWithFallback() {
78 String tzid = getTargetUserTimezone();
79 if (StringUtils.isEmpty(tzid)) {
80 return TKUtils.getSystemDateTimeZone();
81 } else {
82 return DateTimeZone.forID(tzid);
83 }
84 }
85
86 @Override
87 public String getTargetUserTimezone() {
88 String timezone = "";
89 if (GlobalVariables.getUserSession() != null) {
90
91 timezone = getUserTimezone(HrContext.getTargetPrincipalId());
92 }
93 return timezone;
94 }
95
96 @Override
97 public boolean isSameTimezone() {
98 String userTimezone = getUserTimezone();
99 if(StringUtils.isNotBlank(userTimezone)) {
100 return StringUtils.equals(TKUtils.getSystemTimeZone(), userTimezone);
101 }
102 return true;
103 }
104
105 @Override
106 public String getApproverTimezone(String principalId) {
107 PrincipalHRAttributes principalCalendar = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, LocalDate.now());
108 if(principalCalendar != null && principalCalendar.getTimezone() != null){
109 return principalCalendar.getTimezone();
110 }
111 return TKUtils.getSystemTimeZone();
112 }
113
114 }