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(TKContext.getPrincipalId(), 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            /**
077             * Translation needed for UI Display
078             * @param timeBlocks
079             * @param timezone
080             * @return timeblock list modified with times offset for timezone
081             */
082            public List<TimeBlock> translateForTimezone(List<TimeBlock> timeBlocks, DateTimeZone timezone){
083                    for(TimeBlock tb : timeBlocks){
084                            //No need for translation if it matches the current timezone
085                            if(ObjectUtils.equals(timezone, TKUtils.getSystemDateTimeZone())){
086                                    tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp()));
087                                    tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp()));
088                            }
089                            else {
090                                    tb.setBeginTimeDisplay(new DateTime(tb.getBeginTimestamp(), timezone));
091                                    tb.setEndTimeDisplay(new DateTime(tb.getEndTimestamp(), timezone));
092                            }
093                    }
094                    return timeBlocks;
095            }
096    
097        public void translateForTimezone(List<TimeBlock> timeBlocks) {
098            translateForTimezone(timeBlocks, getUserTimezoneWithFallback());
099        }
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    }