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.StringUtils;
21 import org.joda.time.DateTime;
22 import org.joda.time.DateTimeZone;
23 import org.kuali.hr.job.Job;
24 import org.kuali.hr.location.Location;
25 import org.kuali.hr.time.principal.PrincipalHRAttributes;
26 import org.kuali.hr.time.service.base.TkServiceLocator;
27 import org.kuali.hr.time.timeblock.TimeBlock;
28 import org.kuali.hr.time.util.TKContext;
29 import org.kuali.hr.time.util.TKUtils;
30 import org.kuali.hr.time.util.TkConstants;
31
32 public class TimezoneServiceImpl implements TimezoneService {
33
34 @Override
35 public String getUserTimezone(String principalId) {
36 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, TKUtils.getCurrentDate());
37 if(principalCalendar != null && principalCalendar.getTimezone() != null){
38 return principalCalendar.getTimezone();
39 }
40 List<Job> jobs = TkServiceLocator.getJobService().getJobs(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
41 if (jobs.size() > 0) {
42
43 Location location = TkServiceLocator.getLocationService().getLocation(jobs.get(0).getLocation(), TKUtils.getCurrentDate());
44 if (location!=null){
45 if(StringUtils.isNotBlank(location.getTimezone())){
46 return location.getTimezone();
47 }
48 }
49 }
50 return TKUtils.getSystemTimeZone();
51 }
52
53
54
55
56 @Override
57 public String getUserTimezone() {
58 return getUserTimezone(TKContext.getPrincipalId());
59 }
60
61 @Override
62 public DateTimeZone getUserTimezoneWithFallback() {
63 String tzid = getUserTimezone();
64 if (StringUtils.isEmpty(tzid)) {
65 return TKUtils.getSystemDateTimeZone();
66 } else {
67 return DateTimeZone.forID(tzid);
68 }
69 }
70
71
72
73
74
75
76
77 public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, String timezone){
78 for(TimeBlock tb : timeBlocks){
79
80 if(StringUtils.equals(timezone, TKUtils.getSystemTimeZone())){
81 tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp()));
82 tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp()));
83 }
84 else {
85 tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp(),DateTimeZone.forID(timezone)));
86 tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp(), DateTimeZone.forID(timezone)));
87 }
88 }
89 return timeBlocks;
90 }
91
92 public void translateForTimezone(List<TimeBlock> timeBlocks) {
93 translateForTimezone(timeBlocks, getUserTimezone());
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
106 public long getTimezoneOffsetFromServerTime(DateTimeZone dtz){
107 long systemOffsetUTC = TKUtils.getSystemDateTimeZone().getOffset(null);
108 long tzOffsetUTC = dtz.getOffset(null);
109 return tzOffsetUTC - systemOffsetUTC;
110 }
111
112
113
114 }