001    /**
002     * Copyright 2004-2013 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.ObjectUtils;
021    import org.apache.commons.lang.StringUtils;
022    import org.joda.time.DateTime;
023    import org.joda.time.DateTimeZone;
024    import org.kuali.hr.job.Job;
025    import org.kuali.hr.location.Location;
026    import org.kuali.hr.time.principal.PrincipalHRAttributes;
027    import org.kuali.hr.time.service.base.TkServiceLocator;
028    import org.kuali.hr.time.timeblock.TimeBlock;
029    import org.kuali.hr.time.util.TKContext;
030    import org.kuali.hr.time.util.TKUtils;
031    import org.kuali.rice.krad.util.GlobalVariables;
032    
033    public class TimezoneServiceImpl implements TimezoneService {
034    
035        @Override
036        public String getUserTimezone(String principalId) {
037            PrincipalHRAttributes principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, TKUtils.getCurrentDate());
038            if(principalCalendar != null && principalCalendar.getTimezone() != null){
039                return principalCalendar.getTimezone();
040            }
041            List<Job> jobs = TkServiceLocator.getJobService().getJobs(principalId, TKUtils.getCurrentDate());
042            if (jobs.size() > 0) {
043                // Grab the location off the first job in the list
044                Location location = TkServiceLocator.getLocationService().getLocation(jobs.get(0).getLocation(), TKUtils.getCurrentDate());
045                if (location!=null){
046                    if(StringUtils.isNotBlank(location.getTimezone())){
047                        return location.getTimezone();
048                    }
049                }
050            }
051            return TKUtils.getSystemTimeZone();
052        }
053    
054        /**
055             * Used to determine if an override condition exists for a user timezone
056             */
057            @Override
058            public String getUserTimezone() {
059                    String timezone = "";
060                    if (GlobalVariables.getUserSession() != null) {
061                            timezone = getUserTimezone(GlobalVariables.getUserSession().getPrincipalId());
062                    }
063            return timezone;
064            }
065    
066        @Override
067        public DateTimeZone getUserTimezoneWithFallback() {
068            String tzid = getUserTimezone();
069            if (StringUtils.isEmpty(tzid)) {
070                return TKUtils.getSystemDateTimeZone();
071            } else {
072                return DateTimeZone.forID(tzid);
073            }
074        }
075    
076        private DateTimeZone getUserTimezoneWithFallback(String principalId) {
077            String tzid = getUserTimezone(principalId);
078            if (StringUtils.isEmpty(tzid)) {
079                return TKUtils.getSystemDateTimeZone();
080            } else {
081                return DateTimeZone.forID(tzid);
082            }
083        }
084    
085            /**
086             * Translation needed for UI Display
087             * @param timeBlocks
088             * @param timezone
089             * @return timeblock list modified with times offset for timezone
090             */
091            public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, DateTimeZone timezone){
092                    for(TimeBlock tb : timeBlocks){
093                            //No need for translation if it matches the current timezone
094                            if(ObjectUtils.equals(timezone, TKUtils.getSystemDateTimeZone())){
095                                    tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp()));
096                                    tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp()));
097                            }
098                            else {
099                                    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    }