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(principalId, 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 private DateTimeZone getUserTimezoneWithFallback(String principalId) {
77 String tzid = getUserTimezone(principalId);
78 if (StringUtils.isEmpty(tzid)) {
79 return TKUtils.getSystemDateTimeZone();
80 } else {
81 return DateTimeZone.forID(tzid);
82 }
83 }
84
85
86
87
88
89
90
91 public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, DateTimeZone timezone){
92 for(TimeBlock tb : timeBlocks){
93
94 if(ObjectUtils.equals(timezone, TKUtils.getSystemDateTimeZone())){
95 tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp()));
96 tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp()));
97 }
98 else {
99 tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp(), timezone));
100 tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp(), timezone));
101 }
102 }
103 return timeBlocks;
104 }
105
106 public void translateForTimezone(List<TimeBlock> timeBlocks) {
107 if (timeBlocks.isEmpty()) {
108 translateForTimezone(timeBlocks, getUserTimezoneWithFallback());
109 }
110 else {
111 translateForTimezone(timeBlocks, getUserTimezoneWithFallback(timeBlocks.get(0).getPrincipalId()));
112 }
113 }
114
115 @Override
116 public boolean isSameTimezone() {
117 String userTimezone = getUserTimezone();
118 if(StringUtils.isNotBlank(userTimezone)) {
119 return StringUtils.equals(TKUtils.getSystemTimeZone(), userTimezone);
120 }
121 return true;
122 }
123
124
125 public long getTimezoneOffsetFromServerTime(DateTimeZone dtz){
126 long systemOffsetUTC = TKUtils.getSystemDateTimeZone().getOffset(null);
127 long tzOffsetUTC = dtz.getOffset(null);
128 return tzOffsetUTC - systemOffsetUTC;
129 }
130
131
132
133 }