View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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              // Grab the location off the first job in the list
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  	 * Used to determine if an override condition exists for a user timezone
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  	 * Translation needed for UI Display
73  	 * @param timeBlocks
74  	 * @param timezone
75  	 * @return timeblock list modified with times offset for timezone
76  	 */
77  	public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, String timezone){
78  		for(TimeBlock tb : timeBlocks){
79  			//No need for translation if it matches the current timezone
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 }