001 /** 002 * Copyright 2004-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.time.timezone.service; 017 018 import java.util.List; 019 020 import org.apache.commons.lang.StringUtils; 021 import org.joda.time.DateTime; 022 import org.joda.time.DateTimeZone; 023 import org.kuali.hr.job.Job; 024 import org.kuali.hr.location.Location; 025 import org.kuali.hr.time.principal.PrincipalHRAttributes; 026 import org.kuali.hr.time.service.base.TkServiceLocator; 027 import org.kuali.hr.time.timeblock.TimeBlock; 028 import org.kuali.hr.time.util.TKContext; 029 import org.kuali.hr.time.util.TKUtils; 030 import org.kuali.hr.time.util.TkConstants; 031 032 public class TimezoneServiceImpl implements TimezoneService { 033 034 @Override 035 public String getUserTimezone(String principalId) { 036 PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, TKUtils.getCurrentDate()); 037 if(principalCalendar != null && principalCalendar.getTimezone() != null){ 038 return principalCalendar.getTimezone(); 039 } 040 List<Job> jobs = TkServiceLocator.getJobService().getJobs(TKContext.getPrincipalId(), TKUtils.getCurrentDate()); 041 if (jobs.size() > 0) { 042 // Grab the location off the first job in the list 043 Location location = TkServiceLocator.getLocationService().getLocation(jobs.get(0).getLocation(), TKUtils.getCurrentDate()); 044 if (location!=null){ 045 if(StringUtils.isNotBlank(location.getTimezone())){ 046 return location.getTimezone(); 047 } 048 } 049 } 050 return TKUtils.getSystemTimeZone(); 051 } 052 053 /** 054 * Used to determine if an override condition exists for a user timezone 055 */ 056 @Override 057 public String getUserTimezone() { 058 return getUserTimezone(TKContext.getPrincipalId()); 059 } 060 061 @Override 062 public DateTimeZone getUserTimezoneWithFallback() { 063 String tzid = getUserTimezone(); 064 if (StringUtils.isEmpty(tzid)) { 065 return TKUtils.getSystemDateTimeZone(); 066 } else { 067 return DateTimeZone.forID(tzid); 068 } 069 } 070 071 /** 072 * Translation needed for UI Display 073 * @param timeBlocks 074 * @param timezone 075 * @return timeblock list modified with times offset for timezone 076 */ 077 public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, String timezone){ 078 for(TimeBlock tb : timeBlocks){ 079 //No need for translation if it matches the current timezone 080 if(StringUtils.equals(timezone, TKUtils.getSystemTimeZone())){ 081 tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp())); 082 tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp())); 083 } 084 else { 085 tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp(),DateTimeZone.forID(timezone))); 086 tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp(), DateTimeZone.forID(timezone))); 087 } 088 } 089 return timeBlocks; 090 } 091 092 public void translateForTimezone(List<TimeBlock> timeBlocks) { 093 translateForTimezone(timeBlocks, getUserTimezone()); 094 } 095 096 @Override 097 public boolean isSameTimezone() { 098 String userTimezone = getUserTimezone(); 099 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 }