View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.lm.leavecode.service;
17  
18  import com.google.common.collect.Ordering;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.hr.lm.LMConstants;
22  import org.kuali.hr.lm.leavecode.LeaveCode;
23  import org.kuali.hr.lm.leavecode.dao.LeaveCodeDao;
24  import org.kuali.hr.time.principal.PrincipalHRAttributes;
25  import org.kuali.hr.time.roles.TkUserRoles;
26  import org.kuali.hr.time.service.base.TkServiceLocator;
27  import org.kuali.hr.time.util.TKContext;
28  import org.kuali.hr.time.util.TKUser;
29  import org.kuali.hr.time.util.TKUtils;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  import java.math.BigDecimal;
33  import java.sql.Date;
34  import java.util.*;
35  
36  public class LeaveCodeServiceImpl implements LeaveCodeService {
37  
38  	private static final Logger LOG = Logger
39  			.getLogger(LeaveCodeServiceImpl.class);
40  	private LeaveCodeDao leaveCodeDao;
41  
42  	public LeaveCodeDao getLeaveCodeDao() {
43  		return leaveCodeDao;
44  	}
45  
46  	public void setLeaveCodeDao(LeaveCodeDao leaveCodeDao) {
47  		this.leaveCodeDao = leaveCodeDao;
48  	}
49  
50  	@Override
51  	public LeaveCode getLeaveCode(String lmLeaveCodeId) {
52  		return getLeaveCodeDao().getLeaveCode(lmLeaveCodeId);
53  	}
54  
55  	@Override
56      public List<LeaveCode> getLeaveCodes(String principalId, Date asOfDate) {
57      	String leavePlan = null;
58          List<LeaveCode> leaveCodes = new ArrayList<LeaveCode>();
59          PrincipalHRAttributes hrAttribute = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, asOfDate);
60          if(hrAttribute != null) {
61          	leavePlan = hrAttribute.getLeavePlan();
62          	if (StringUtils.isBlank(leavePlan)) {
63          		throw new RuntimeException("No leave plan defined for " + principalId + " in principal hr attributes");
64          	}
65          	
66              List<LeaveCode> unfilteredLeaveCodes = leaveCodeDao.getLeaveCodes(leavePlan, asOfDate);
67              TKUser user = TKContext.getUser();
68  
69              for (LeaveCode leaveCode : unfilteredLeaveCodes) {
70                  //if employee add this leave code
71                  //TODO how do we know this is an approver for them
72                  if ((leaveCode.getEmployee() && TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isActiveEmployee()) ||
73                          (leaveCode.getApprover() && user.isApprover())) {
74                      leaveCodes.add(leaveCode);
75                  }
76              }
77  
78          }
79  
80          return leaveCodes;
81      }
82  
83  	@Override
84  	public Map<String, String> getLeaveCodesForDisplay(String principalId) {
85  		List<LeaveCode> leaveCodes = getLeaveCodes(principalId,
86  				TKUtils.getCurrentDate());
87  		
88  		// KPME1449, chen, only display leaveCodes which are 'allow_scheduled_leave flag = Y'
89  		for (LeaveCode leaveCode : leaveCodes) {
90  			if ( !leaveCode.getAllowScheduledLeave().equalsIgnoreCase("Y")) {
91  				leaveCodes.remove(leaveCode);
92  			}
93  		} // kpme1449
94  
95  		Comparator<LeaveCode> leaveCodeComparator = new Comparator<LeaveCode>() {
96  			@Override
97  			public int compare(LeaveCode leaveCode, LeaveCode leaveCode1) {
98  				return leaveCode.getLeaveCode().compareToIgnoreCase(
99  						leaveCode1.getLeaveCode());
100 			}
101 		};
102 		// Order by leaveCode ascending
103 		Ordering<LeaveCode> ordering = Ordering.from(leaveCodeComparator);
104 
105 		Map<String, String> leaveCodesForDisplay = new LinkedHashMap<String, String>();
106 		for (LeaveCode leaveCode : ordering.sortedCopy(leaveCodes)) {
107 			leaveCodesForDisplay.put(leaveCode.getLeaveCodeKeyForDisplay(),
108 					leaveCode.getLeaveCodeValueForDisplay());
109 		}
110 
111 		return leaveCodesForDisplay;
112 	}
113 
114 	@Override
115 	public LeaveCode getLeaveCode(String leaveCode, Date effectiveDate) {
116 		return leaveCodeDao.getLeaveCode(leaveCode, effectiveDate);
117 	}
118 	
119 	@Override
120 	public BigDecimal roundHrsWithLeaveCode(BigDecimal hours, LeaveCode leaveCode) {
121 		String roundOption = LMConstants.ROUND_OPTION_MAP.get(leaveCode.getRoundingOption());
122 		BigDecimal fractScale = new BigDecimal(leaveCode.getFractionalTimeAllowed());
123 		if(roundOption == null) {
124 			throw new RuntimeException("Rounding option of Leave Code " + leaveCode.getLeaveCode() + " is not recognized.");
125 		}
126 		BigDecimal roundedHours = hours;
127 		if(roundOption.equals("Traditional")) {
128 			roundedHours = hours.setScale(fractScale.scale(), BigDecimal.ROUND_HALF_EVEN);
129 		} else if(roundOption.equals("Truncate")) {
130 			roundedHours = hours.setScale(fractScale.scale(), BigDecimal.ROUND_DOWN);
131 		}
132 		return roundedHours;
133 	}
134 }