View Javadoc
1   /**
2    * Copyright 2004-2015 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.job.Job;
24  import org.kuali.kpme.core.location.Location;
25  import org.kuali.kpme.core.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          if(principalCalendar != null && principalCalendar.getTimezone() != null){
37              return principalCalendar.getTimezone();
38          }
39          List<Job> jobs = HrServiceLocator.getJobService().getJobs(principalId, LocalDate.now());
40          if (jobs.size() > 0) {
41              // Grab the location off the first job in the list
42              Location location = HrServiceLocator.getLocationService().getLocation(jobs.get(0).getLocation(), LocalDate.now());
43              if (location!=null){
44                  if(StringUtils.isNotBlank(location.getTimezone())){
45                      return location.getTimezone();
46                  }
47              }
48          }
49          return TKUtils.getSystemTimeZone();
50      }
51  
52      /**
53  	 * Used to determine if an override condition exists for a user timezone
54  	 */
55  	@Override
56  	public String getUserTimezone() {
57  		String timezone = "";
58  		if (GlobalVariables.getUserSession() != null) {
59  			timezone = getUserTimezone(GlobalVariables.getUserSession().getPrincipalId());
60  		}
61          return timezone;
62  	}
63  
64      @Override
65      public DateTimeZone getUserTimezoneWithFallback() {
66          String tzid = getUserTimezone();
67          if (StringUtils.isEmpty(tzid)) {
68              return TKUtils.getSystemDateTimeZone();
69          } else {
70              return DateTimeZone.forID(tzid);
71          }
72      }
73      
74      @Override
75      public DateTimeZone getTargetUserTimezoneWithFallback() {
76          String tzid = getTargetUserTimezone();
77          if (StringUtils.isEmpty(tzid)) {
78              return TKUtils.getSystemDateTimeZone();
79          } else {
80              return DateTimeZone.forID(tzid);
81          }
82      }
83      
84      @Override
85  	public String getTargetUserTimezone() {
86  		String timezone = "";
87  		if (GlobalVariables.getUserSession() != null) {
88  			// Logic in getTargetPrincipalId returns target principal id if the user is targeting an employee, otherwise returns the current user's id
89  			timezone = getUserTimezone(HrContext.getTargetPrincipalId());
90  		}
91          return timezone;
92  	}
93      
94  	@Override
95  	public boolean isSameTimezone() {
96  		String userTimezone = getUserTimezone();
97  		if(StringUtils.isNotBlank(userTimezone)) {
98  			return StringUtils.equals(TKUtils.getSystemTimeZone(), userTimezone);
99  		}
100 		return true;
101 	}
102 
103 	@Override
104 	public String getApproverTimezone(String principalId) {
105 		PrincipalHRAttributes principalCalendar = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, LocalDate.now());
106 	    if(principalCalendar != null && principalCalendar.getTimezone() != null){
107 	        return principalCalendar.getTimezone();
108 	    }
109 		return TKUtils.getSystemTimeZone();
110 	}
111 
112 }