View Javadoc

1   /**
2    * Copyright 2004-2012 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.time.earncode.service;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.earncodesec.EarnCodeSecurity;
20  import org.kuali.hr.job.Job;
21  import org.kuali.hr.time.assignment.Assignment;
22  import org.kuali.hr.time.earncode.EarnCode;
23  import org.kuali.hr.time.earncode.dao.EarnCodeDao;
24  import org.kuali.hr.time.roles.TkUserRoles;
25  import org.kuali.hr.time.service.base.TkServiceLocator;
26  import org.kuali.hr.time.util.TKContext;
27  import org.kuali.hr.time.util.TKUser;
28  import org.kuali.hr.time.workarea.WorkArea;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  
31  import java.sql.Date;
32  import java.util.*;
33  
34  public class EarnCodeServiceImpl implements EarnCodeService {
35  
36  	private EarnCodeDao earnCodeDao;
37  
38  	public void setEarnCodeDao(EarnCodeDao earnCodeDao) {
39  		this.earnCodeDao = earnCodeDao;
40  	}
41  
42  
43      @Override
44      public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate) {
45          return getEarnCodes(a, asOfDate, null);
46      }
47  
48      @Override
49      public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate, String earnTypeCode) {
50          List<EarnCode> earnCodes = new LinkedList<EarnCode>();
51  
52          // Note: https://jira.kuali.org/browse/KPME-689
53          // We are grabbing a TkUser from the current thread local context here.
54          // really, this should probably be passed in..
55  
56          TKUser user = TKContext.getUser();
57          if (user == null) {
58              // TODO: Determine how to fail if there is no TkUser
59              throw new RuntimeException("No User on context.");
60          }
61  
62          if (a == null)
63              throw new RuntimeException("Can not get earn codes for null assignment");
64          Job job = a.getJob();
65          if (job == null || job.getPayTypeObj() == null)
66              throw new RuntimeException("Null job/job paytype on assignment!");
67  
68          EarnCode regularEc = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
69          if (regularEc == null)
70              throw new RuntimeException("No regular earn code defined.");
71          earnCodes.add(regularEc);
72          List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
73          for (EarnCodeSecurity dec : decs) {
74              if (StringUtils.isBlank(earnTypeCode)
75                      || earnTypeCode.equals(dec.getEarnCodeType())
76                      ) {
77  
78                  boolean addEarnCode = false;
79                  // Check employee flag
80                  if (dec.isEmployee() &&
81                          (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
82                      addEarnCode = true;
83                  }
84                  // Check approver flag
85                  if (!addEarnCode && dec.isApprover()) {
86                      Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
87                      for (Long wa : workAreas) {
88                          WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
89                          if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
90                              addEarnCode = true;
91                              break;
92                          }
93                      }
94                  }
95                  if (addEarnCode) {
96                      EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
97                      if(ec!=null){
98                          earnCodes.add(ec);
99                      }
100                 }
101             }
102         }
103 
104         return earnCodes;
105     }
106 
107     public EarnCode getEarnCode(String earnCode, Date asOfDate) {
108         EarnCode ec = null;
109 
110         ec = earnCodeDao.getEarnCode(earnCode, asOfDate);
111 
112         return ec;
113     }
114 
115     @Override
116     public String getEarnCodeType(String earnCode, Date asOfDate) {
117         EarnCode earnCodeObj = getEarnCode(earnCode, asOfDate);
118         return earnCodeObj.getEarnCodeType();
119     }
120 
121 	@Override
122 	public EarnCode getEarnCodeById(String earnCodeId) {
123 		return earnCodeDao.getEarnCodeById(earnCodeId);
124 	}
125 	
126 	public List<EarnCode> getOvertimeEarnCodes(Date asOfDate){
127 		return earnCodeDao.getOvertimeEarnCodes(asOfDate);
128 	}
129 	
130 	public List<String> getOvertimeEarnCodesStrs(Date asOfDate){
131 		List<String> ovtEarnCodeStrs = new ArrayList<String>();
132 		List<EarnCode> ovtEarnCodes = getOvertimeEarnCodes(asOfDate);
133 		if(ovtEarnCodes != null){
134 			for(EarnCode ovtEc : ovtEarnCodes){
135 				ovtEarnCodeStrs.add(ovtEc.getEarnCode());
136 			}
137 		}
138 		return ovtEarnCodeStrs;
139 	}
140 	
141 	@Override
142 	public int getEarnCodeCount(String earnCode) {
143 		return earnCodeDao.getEarnCodeCount(earnCode);
144 	}
145 	
146 	@Override
147 	public int getNewerEarnCodeCount(String earnCode, Date effdt) {
148 		return earnCodeDao.getNewerEarnCodeCount(earnCode, effdt);
149 	}
150 
151 
152     public List<EarnCode> getEarnCodesForTime(Assignment a, Date asOfDate) {
153         if (a == null) throw new RuntimeException("No assignment parameter.");
154         Job job = a.getJob();
155         if (job == null || job.getPayTypeObj() == null) throw new RuntimeException("Null job or null job pay type on assignment.");
156 
157         List<EarnCode> earnCodes = new LinkedList<EarnCode>();
158 
159         EarnCode regularEarnCode = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
160         if (regularEarnCode == null) {
161             throw new RuntimeException("No regular earn code defined for job pay type.");
162         } else {
163             earnCodes.add(regularEarnCode);
164         }
165         List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
166         for (EarnCodeSecurity dec : decs) {
167                 boolean addEarnCode = false;
168                 // Check employee flag
169                 if (dec.isEmployee() &&
170                         (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
171                     addEarnCode = true;
172                 }
173                 // Check approver flag
174                 if (!addEarnCode && dec.isApprover()) {
175                     Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
176                     for (Long wa : workAreas) {
177                         WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
178                         if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
179                             addEarnCode = true;
180                             break;
181                         }
182                     }
183                 }
184                 if (addEarnCode) {
185                     EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
186                     if(ec!=null){
187                         earnCodes.add(ec);
188                     }
189                 }
190         }
191 
192 
193         return earnCodes;
194     }
195 
196 
197     /* not using yet, may not be needed
198     @Override
199     public Map<String, String> getEarnCod e s ForDisplayWithAssignment(Assignment assignment, Date asOfDate) {
200         List<EarnCode> earnCodes = this.getEarnCod e s ( assignment, asOfDate);
201 
202         Date currentDate = TKUtils.getCurrentDate();
203         boolean futureDate = asOfDate.after(currentDate);
204         List<EarnCode> copyList = new ArrayList<EarnCode>();
205         copyList.addAll(earnCodes);
206         for (EarnCode earnCode : copyList) {
207             if ( futureDate
208                     && !earnCode.getAllowScheduledLeave().equalsIgnoreCase("Y")) {
209                 earnCodes.remove(earnCode);
210             }
211         }
212         Comparator<EarnCode> earnCodeComparator = new Comparator<EarnCode>() {
213             @Override
214             public int compare(EarnCode ec1, EarnCode ec2) {
215                 return ec1.getEarnCode().compareToIgnoreCase(ec2.getEarnCode());
216             }
217         };
218         // Order by leaveCode ascending
219         Ordering<EarnCode> ordering = Ordering.from(earnCodeComparator);
220 
221         Map<String, String> earnCodesForDisplay = new LinkedHashMap<String, String>();
222         for (EarnCode earnCode : ordering.sortedCopy(earnCodes)) {
223             earnCodesForDisplay.put(earnCode.getEarnCodeKeyForDisplay(), earnCode.getEarnCodeValueForDisplay());
224         }
225         return earnCodesForDisplay;
226     }
227 
228     */
229 }