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(principalId, 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      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  	 * Translation needed for UI Display
87  	 * @param timeBlocks
88  	 * @param timezone
89  	 * @return timeblock list modified with times offset for timezone
90  	 */
91  	public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, DateTimeZone timezone){
92  		for(TimeBlock tb : timeBlocks){
93  			//No need for translation if it matches the current timezone
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 }