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.time.earncode.service;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.earncodesec.EarnCodeSecurity;
20  import org.kuali.hr.earncodesec.EarnCodeType;
21  import org.kuali.hr.job.Job;
22  import org.kuali.hr.time.assignment.Assignment;
23  import org.kuali.hr.time.earncode.EarnCode;
24  import org.kuali.hr.time.earncode.dao.EarnCodeDao;
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.workarea.WorkArea;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  import java.sql.Date;
33  import java.util.*;
34  
35  public class EarnCodeServiceImpl implements EarnCodeService {
36  
37  	private EarnCodeDao earnCodeDao;
38  
39  	public void setEarnCodeDao(EarnCodeDao earnCodeDao) {
40  		this.earnCodeDao = earnCodeDao;
41  	}
42  
43  
44      @Override
45      public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate) {
46          return getEarnCodesForTime(a, asOfDate);
47      }
48  
49      @Override
50      public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate, String earnTypeCode) {
51          List<EarnCode> earnCodes = new LinkedList<EarnCode>();
52  
53          // Note: https://jira.kuali.org/browse/KPME-689
54          // We are grabbing a TkUser from the current thread local context here.
55          // really, this should probably be passed in..
56  
57          TKUser user = TKContext.getUser();
58          if (user == null) {
59              // TODO: Determine how to fail if there is no TkUser
60              throw new RuntimeException("No User on context.");
61          }
62  
63          if (a == null)
64              throw new RuntimeException("Can not get earn codes for null assignment");
65          Job job = a.getJob();
66          if (job == null || job.getPayTypeObj() == null)
67              throw new RuntimeException("Null job/job paytype on assignment!");
68  
69          EarnCode regularEc = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
70          if (regularEc == null)
71              throw new RuntimeException("No regular earn code defined.");
72          earnCodes.add(regularEc);
73          List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
74          for (EarnCodeSecurity dec : decs) {
75              if (StringUtils.isBlank(earnTypeCode)
76                      || earnTypeCode.equals(dec.getEarnCodeType())
77                      ) {
78  
79                  boolean addEarnCode = false;
80                  // Check employee flag
81                  if (dec.isEmployee() &&
82                          (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
83                      addEarnCode = true;
84                  }
85                  // Check approver flag
86                  if (!addEarnCode && dec.isApprover()) {
87                      Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
88                      for (Long wa : workAreas) {
89                          WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
90                          if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
91                              addEarnCode = true;
92                              break;
93                          }
94                      }
95                  }
96                  if (addEarnCode) {
97                      EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
98                      if(ec!=null){
99                          earnCodes.add(ec);
100                     }
101                 }
102             }
103         }
104 
105         return earnCodes;
106     }
107 
108     public EarnCode getEarnCode(String earnCode, Date asOfDate) {
109         EarnCode ec = null;
110 
111         ec = earnCodeDao.getEarnCode(earnCode, asOfDate);
112 
113         return ec;
114     }
115 
116     @Override
117     public String getEarnCodeType(String earnCode, Date asOfDate) {
118         EarnCode earnCodeObj = getEarnCode(earnCode, asOfDate);
119         return earnCodeObj.getEarnCodeType();
120     }
121 
122 	@Override
123 	public EarnCode getEarnCodeById(String earnCodeId) {
124 		return earnCodeDao.getEarnCodeById(earnCodeId);
125 	}
126 	
127 	public List<EarnCode> getOvertimeEarnCodes(Date asOfDate){
128 		return earnCodeDao.getOvertimeEarnCodes(asOfDate);
129 	}
130 	
131 	public List<String> getOvertimeEarnCodesStrs(Date asOfDate){
132 		List<String> ovtEarnCodeStrs = new ArrayList<String>();
133 		List<EarnCode> ovtEarnCodes = getOvertimeEarnCodes(asOfDate);
134 		if(ovtEarnCodes != null){
135 			for(EarnCode ovtEc : ovtEarnCodes){
136 				ovtEarnCodeStrs.add(ovtEc.getEarnCode());
137 			}
138 		}
139 		return ovtEarnCodeStrs;
140 	}
141 	
142 	@Override
143 	public int getEarnCodeCount(String earnCode) {
144 		return earnCodeDao.getEarnCodeCount(earnCode);
145 	}
146 	
147 	@Override
148 	public int getNewerEarnCodeCount(String earnCode, Date effdt) {
149 		return earnCodeDao.getNewerEarnCodeCount(earnCode, effdt);
150 	}
151 
152     @Override
153     public List<EarnCode> getEarnCodes(String earnCode, String ovtEarnCode, String descr, Date fromEffdt, Date toEffdt, String active, String showHist) {
154         return earnCodeDao.getEarnCodes(earnCode, ovtEarnCode, descr, fromEffdt, toEffdt, active, showHist);
155     }
156 
157 
158     public List<EarnCode> getEarnCodesForTime(Assignment a, Date asOfDate) {
159         // code moved over from earncodeservice in kpme 1.5
160         if (a == null) throw new RuntimeException("No assignment parameter.");
161         Job job = a.getJob();
162         if (job == null || job.getPayTypeObj() == null) throw new RuntimeException("Null job or null job pay type on assignment.");
163 
164         List<EarnCode> earnCodes = new LinkedList<EarnCode>();
165         String earnTypeCode = EarnCodeType.TIME.getCode();
166 
167         boolean isClockUser = a.getTimeCollectionRule().isClockUserFl();
168         boolean isUsersTimesheet = StringUtils.equals(TKContext.getPrincipalId(),a.getPrincipalId());
169 
170         // Reg earn codes will typically not be defined in the earn code security table
171         EarnCode regularEarnCode = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
172         if (regularEarnCode == null) {
173             throw new RuntimeException("No regular earn code defined for job pay type.");
174         } else {
175             //  if you are a clock user and this is your timesheet and you are processing the reg earn code, do not add this earn code. Use the clock in/out mechanism.
176             if (isClockUser && isUsersTimesheet) {
177                 // do not add reg earn code. use clock.
178             } else {
179                 earnCodes.add(regularEarnCode);
180             }
181         }
182         List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
183         for (EarnCodeSecurity dec : decs) {
184             if (earnTypeCode.equals(dec.getEarnCodeType())
185                     || EarnCodeType.BOTH.getCode().equals(dec.getEarnCodeType())) {
186 
187                 boolean addEarnCode = addEarnCodeBasedOnEmployeeApproverSettings(dec, a, asOfDate);
188                 //  this earn code could possibly be for reg earnings. reg was processed before the earn code security table of earn codes, so it should be skipped here.
189                 if ( StringUtils.equals(job.getPayTypeObj().getRegEarnCode(),dec.getEarnCode()) ){
190                     addEarnCode = false;
191                 }
192                 if (!dec.isEmployee()){
193                     addEarnCode = false;
194                 }
195                 if (addEarnCode) {
196                     EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
197                     if(ec!=null){
198                         earnCodes.add(ec);
199                     }
200                 }
201             }
202         }
203 
204         return earnCodes;
205         //        if (a == null) throw new RuntimeException("No assignment parameter.");
206 //        Job job = a.getJob();
207 //        if (job == null || job.getPayTypeObj() == null) throw new RuntimeException("Null job or null job pay type on assignment.");
208 //
209 //        List<EarnCode> earnCodes = new LinkedList<EarnCode>();
210 //
211 //        EarnCode regularEarnCode = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
212 //        if (regularEarnCode == null) {
213 //            throw new RuntimeException("No regular earn code defined for job pay type.");
214 //        } else {
215 //            earnCodes.add(regularEarnCode);
216 //        }
217 //        List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
218 //        for (EarnCodeSecurity dec : decs) {
219 //                boolean addEarnCode = false;
220 //                // Check employee flag
221 //                if (dec.isEmployee() &&
222 //                        (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
223 //                    addEarnCode = true;
224 //                }
225 //                // Check approver flag
226 //                if (!addEarnCode && dec.isApprover()) {
227 //                    Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
228 //                    for (Long wa : workAreas) {
229 //                        WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
230 //                        if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
231 //                            addEarnCode = true;
232 //                            break;
233 //                        }
234 //                    }
235 //                }
236 //                if (addEarnCode) {
237 //                    EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
238 //                    if(ec!=null){
239 //                        earnCodes.add(ec);
240 //                    }
241 //                }
242 //        }
243         // return earnCodes;
244     }
245 
246     private boolean addEarnCodeBasedOnEmployeeApproverSettings(EarnCodeSecurity security, Assignment a, Date asOfDate) {
247         boolean addEarnCode = false;
248         if (security.isEmployee() &&
249                 (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
250             addEarnCode = true;
251         }
252         // Check approver flag
253         if (!addEarnCode && security.isApprover()) {
254             Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
255             for (Long wa : workAreas) {
256                 WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
257                 if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
258                     addEarnCode = true;
259                     break;
260                 }
261             }
262         }
263         return addEarnCode;
264     }
265 
266     /* not using yet, may not be needed
267     @Override
268     public Map<String, String> getEarnCod e s ForDisplayWithAssignment(Assignment assignment, Date asOfDate) {
269         List<EarnCode> earnCodes = this.getEarnCod e s ( assignment, asOfDate);
270 
271         Date currentDate = TKUtils.getCurrentDate();
272         boolean futureDate = asOfDate.after(currentDate);
273         List<EarnCode> copyList = new ArrayList<EarnCode>();
274         copyList.addAll(earnCodes);
275         for (EarnCode earnCode : copyList) {
276             if ( futureDate
277                     && !earnCode.getAllowScheduledLeave().equalsIgnoreCase("Y")) {
278                 earnCodes.remove(earnCode);
279             }
280         }
281         Comparator<EarnCode> earnCodeComparator = new Comparator<EarnCode>() {
282             @Override
283             public int compare(EarnCode ec1, EarnCode ec2) {
284                 return ec1.getEarnCode().compareToIgnoreCase(ec2.getEarnCode());
285             }
286         };
287         // Order by leaveCode ascending
288         Ordering<EarnCode> ordering = Ordering.from(earnCodeComparator);
289 
290         Map<String, String> earnCodesForDisplay = new LinkedHashMap<String, String>();
291         for (EarnCode earnCode : ordering.sortedCopy(earnCodes)) {
292             earnCodesForDisplay.put(earnCode.getEarnCodeKeyForDisplay(), earnCode.getEarnCodeValueForDisplay());
293         }
294         return earnCodesForDisplay;
295     }
296 
297     */
298 }