View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.core.service.timezone;
17  
18  import java.util.List;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.joda.time.DateTimeZone;
22  import org.joda.time.LocalDate;
23  import org.kuali.kpme.core.api.job.Job;
24  import org.kuali.kpme.core.api.location.LocationContract;
25  import org.kuali.kpme.core.api.principal.PrincipalHRAttributes;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.core.util.HrContext;
28  import org.kuali.kpme.core.util.TKUtils;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  
31  public class TimezoneServiceImpl implements TimezoneService {
32  
33      @Override
34      public String getUserTimezone(String principalId) {
35          PrincipalHRAttributes principalCalendar = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, LocalDate.now());
36          
37          if(principalCalendar != null && principalCalendar.getTimezone() != null){
38              return principalCalendar.getTimezone();
39          }
40          List<Job> jobs = HrServiceLocator.getJobService().getJobs(principalId, LocalDate.now());
41          if (jobs.size() > 0) {
42              // Grab the location off the first job in the list
43              //LocationContract location = HrServiceLocator.getLocationService().getLocation(jobs.get(0).getLocation(), LocalDate.now());
44              LocationContract location = HrServiceLocator.getLocationService().getLocation(jobs.get(0).getGroupKey().getLocationId(), LocalDate.now());
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      @Override
77      public DateTimeZone getTargetUserTimezoneWithFallback() {
78          String tzid = getTargetUserTimezone();
79          if (StringUtils.isEmpty(tzid)) {
80              return TKUtils.getSystemDateTimeZone();
81          } else {
82              return DateTimeZone.forID(tzid);
83          }
84      }
85      
86      @Override
87  	public String getTargetUserTimezone() {
88  		String timezone = "";
89  		if (GlobalVariables.getUserSession() != null) {
90  			// Logic in getTargetPrincipalId returns target principal id if the user is targeting an employee, otherwise returns the current user's id
91  			timezone = getUserTimezone(HrContext.getTargetPrincipalId());
92  		}
93          return timezone;
94  	}
95      
96  	@Override
97  	public boolean isSameTimezone() {
98  		String userTimezone = getUserTimezone();
99  		if(StringUtils.isNotBlank(userTimezone)) {
100 			return StringUtils.equals(TKUtils.getSystemTimeZone(), userTimezone);
101 		}
102 		return true;
103 	}
104 
105 	@Override
106 	public String getApproverTimezone(String principalId) {
107 		PrincipalHRAttributes principalCalendar = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, LocalDate.now());
108 	    if(principalCalendar != null && principalCalendar.getTimezone() != null){
109 	        return principalCalendar.getTimezone();
110 	    }
111 		return TKUtils.getSystemTimeZone();
112 	}
113 
114 }