1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.timezone.service;
17
18 import java.util.List;
19
20 import org.apache.commons.lang.ObjectUtils;
21 import org.apache.commons.lang.StringUtils;
22 import org.joda.time.DateTime;
23 import org.joda.time.DateTimeZone;
24 import org.kuali.hr.job.Job;
25 import org.kuali.hr.location.Location;
26 import org.kuali.hr.time.principal.PrincipalHRAttributes;
27 import org.kuali.hr.time.service.base.TkServiceLocator;
28 import org.kuali.hr.time.timeblock.TimeBlock;
29 import org.kuali.hr.time.util.TKContext;
30 import org.kuali.hr.time.util.TKUtils;
31 import org.kuali.rice.krad.util.GlobalVariables;
32
33 public class TimezoneServiceImpl implements TimezoneService {
34
35 @Override
36 public String getUserTimezone(String principalId) {
37 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, TKUtils.getCurrentDate());
38 if(principalCalendar != null && principalCalendar.getTimezone() != null){
39 return principalCalendar.getTimezone();
40 }
41 List<Job> jobs = TkServiceLocator.getJobService().getJobs(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
42 if (jobs.size() > 0) {
43
44 Location location = TkServiceLocator.getLocationService().getLocation(jobs.get(0).getLocation(), TKUtils.getCurrentDate());
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
77
78
79
80
81
82 public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, DateTimeZone timezone){
83 for(TimeBlock tb : timeBlocks){
84
85 if(ObjectUtils.equals(timezone, TKUtils.getSystemDateTimeZone())){
86 tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp()));
87 tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp()));
88 }
89 else {
90 tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp(), timezone));
91 tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp(), timezone));
92 }
93 }
94 return timeBlocks;
95 }
96
97 public void translateForTimezone(List<TimeBlock> timeBlocks) {
98 translateForTimezone(timeBlocks, getUserTimezoneWithFallback());
99 }
100
101 @Override
102 public boolean isSameTimezone() {
103 String userTimezone = getUserTimezone();
104 if(StringUtils.isNotBlank(userTimezone)) {
105 return StringUtils.equals(TKUtils.getSystemTimeZone(), userTimezone);
106 }
107 return true;
108 }
109
110
111 public long getTimezoneOffsetFromServerTime(DateTimeZone dtz){
112 long systemOffsetUTC = TKUtils.getSystemDateTimeZone().getOffset(null);
113 long tzOffsetUTC = dtz.getOffset(null);
114 return tzOffsetUTC - systemOffsetUTC;
115 }
116
117
118
119 }