View Javadoc

1   /**
2    * Copyright 2004-2013 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.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              // Grab the location off the first job in the list
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  	 * Used to determine if an override condition exists for a user timezone
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  	 * Translation needed for UI Display
78  	 * @param timeBlocks
79  	 * @param timezone
80  	 * @return timeblock list modified with times offset for timezone
81  	 */
82  	public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, DateTimeZone timezone){
83  		for(TimeBlock tb : timeBlocks){
84  			//No need for translation if it matches the current timezone
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 }