1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.leave.batch;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.log4j.Logger;
27 import org.joda.time.DateTime;
28 import org.joda.time.LocalDate;
29 import org.joda.time.LocalDateTime;
30 import org.kuali.kpme.core.accrualcategory.AccrualCategory;
31 import org.kuali.kpme.core.accrualcategory.service.AccrualCategoryService;
32 import org.kuali.kpme.core.assignment.Assignment;
33 import org.kuali.kpme.core.assignment.service.AssignmentService;
34 import org.kuali.kpme.core.batch.BatchJob;
35 import org.kuali.kpme.core.calendar.entry.service.CalendarEntryService;
36 import org.kuali.kpme.core.leaveplan.LeavePlan;
37 import org.kuali.kpme.core.leaveplan.service.LeavePlanService;
38 import org.kuali.kpme.core.principal.PrincipalHRAttributes;
39 import org.kuali.kpme.core.principal.service.PrincipalHRAttributesService;
40 import org.kuali.kpme.core.service.HrServiceLocator;
41 import org.kuali.kpme.core.util.HrConstants;
42 import org.kuali.kpme.tklm.common.LMConstants;
43 import org.kuali.kpme.tklm.common.TkConstants;
44 import org.kuali.kpme.tklm.leave.block.LeaveBlock;
45 import org.kuali.kpme.tklm.leave.block.service.LeaveBlockService;
46 import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
47 import org.kuali.kpme.tklm.leave.summary.LeaveSummary;
48 import org.kuali.kpme.tklm.leave.summary.LeaveSummaryRow;
49 import org.kuali.kpme.tklm.leave.summary.service.LeaveSummaryService;
50 import org.kuali.rice.core.api.config.property.ConfigContext;
51 import org.kuali.rice.kim.api.identity.principal.Principal;
52 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
53 import org.quartz.Job;
54 import org.quartz.JobDataMap;
55 import org.quartz.JobExecutionContext;
56 import org.quartz.JobExecutionException;
57
58 public class CarryOverJob extends BatchJob {
59
60 private static final Logger LOG = Logger.getLogger(CarryOverJob.class);
61
62 private AccrualCategoryService accrualCategoryService;
63 private AssignmentService assignmentService;
64 private LeavePlanService leavePlanService;
65 private PrincipalHRAttributesService principalHRAttributesService;
66 private LeaveSummaryService leaveSummaryService;
67 private CalendarEntryService calendarEntryService;
68 private LeaveBlockService leaveBlockService;
69
70 @Override
71 public void execute(JobExecutionContext context) throws JobExecutionException {
72 String batchUserPrincipalId = getBatchUserPrincipalId();
73
74 if (batchUserPrincipalId != null) {
75 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
76 String leavePlan = jobDataMap.getString("leavePlan");
77 if (leavePlan!= null) {
78
79 LocalDate asOfDate = LocalDate.now();
80 LeavePlan leavePlanObj = getLeavePlanService().getLeavePlan(leavePlan, asOfDate);
81 List<Assignment> assignments = getAssignmentService().getActiveAssignments(asOfDate);
82
83
84 Set<String> principalIds = new HashSet<String>();
85 for (Assignment assignment : assignments) {
86 String principalId = assignment.getPrincipalId();
87 if (assignment.getJob().isEligibleForLeave() && !principalIds.contains(principalId)) {
88
89 PrincipalHRAttributes principalHRAttributes = getPrincipalHRAttributesService().getPrincipalCalendar(principalId, asOfDate);
90 principalIds.add(principalId);
91
92 if (principalHRAttributes != null) {
93 LocalDate serviceDate = principalHRAttributes.getServiceLocalDate();
94 if(serviceDate != null){
95
96 if (leavePlanObj != null && leavePlanObj.getLeavePlan().equalsIgnoreCase(principalHRAttributes.getLeavePlan())) {
97
98 DateTime leavePlanStartDate = getLeavePlanService().getFirstDayOfLeavePlan(leavePlan, LocalDate.now());
99
100 DateTime lpPreviousLastDay = (new LocalDateTime(leavePlanStartDate)).toDateTime().minus(1);
101 DateTime lpPreviousFirstDay = getLeavePlanService().getFirstDayOfLeavePlan(leavePlan, lpPreviousLastDay.toLocalDate());
102
103 List<LeaveBlock> prevYearCarryOverleaveBlocks = getLeaveBlockService().getLeaveBlocksWithType(principalId, lpPreviousFirstDay.toLocalDate(), lpPreviousLastDay.toLocalDate(), LMConstants.LEAVE_BLOCK_TYPE.CARRY_OVER);
104 LeaveSummary leaveSummary = getLeaveSummaryService().getLeaveSummaryAsOfDateWithoutFuture(principalId, lpPreviousLastDay.toLocalDate());
105
106 if(CollectionUtils.isEmpty(prevYearCarryOverleaveBlocks)){
107 getLeaveBlockService().saveLeaveBlocks(createCarryOverLeaveBlocks(principalId, lpPreviousLastDay, leaveSummary));
108 } else {
109 Map<String, LeaveBlock> existingCarryOver = new HashMap<String, LeaveBlock>(prevYearCarryOverleaveBlocks.size());
110
111 for (LeaveBlock lb : prevYearCarryOverleaveBlocks) {
112 existingCarryOver.put(lb.getAccrualCategory(), lb);
113 }
114
115
116 for (Map.Entry<String, LeaveBlock> entry : existingCarryOver.entrySet()) {
117 LeaveBlock carryOverBlock = entry.getValue();
118 LeaveSummaryRow lsr = leaveSummary.getLeaveSummaryRowForAccrualCtgy(entry.getKey());
119
120
121 if(lsr.getAccruedBalance() != null) {
122 if(lsr.getMaxCarryOver() != null && lsr.getAccruedBalance().compareTo(lsr.getMaxCarryOver()) > 0 ){
123 carryOverBlock.setLeaveAmount(lsr.getMaxCarryOver());
124 } else {
125 carryOverBlock.setLeaveAmount(lsr.getAccruedBalance());
126 }
127 }
128 getLeaveBlockService().updateLeaveBlock(carryOverBlock, batchUserPrincipalId);
129
130 leaveSummary.getLeaveSummaryRows().remove(lsr);
131 }
132
133
134
135 getLeaveBlockService().saveLeaveBlocks(createCarryOverLeaveBlocks(principalId, lpPreviousLastDay, leaveSummary));
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143 } else {
144 String principalName = getBatchUserPrincipalName();
145 LOG.error("Could not run batch jobs due to missing batch user " + principalName);
146 }
147
148 }
149
150 private AccrualCategoryService getAccrualCategoryService() {
151 if (accrualCategoryService == null) {
152 accrualCategoryService = HrServiceLocator.getAccrualCategoryService();
153 }
154 return accrualCategoryService;
155 }
156
157 public void setAccrualCategoryService(AccrualCategoryService accrualCategoryService) {
158 this.accrualCategoryService = accrualCategoryService;
159 }
160
161 private AssignmentService getAssignmentService() {
162 if (assignmentService == null) {
163 assignmentService = HrServiceLocator.getAssignmentService();
164 }
165 return assignmentService;
166 }
167
168 public void setAssignmentService(AssignmentService assignmentService) {
169 this.assignmentService = assignmentService;
170 }
171
172 public LeavePlanService getLeavePlanService() {
173 if (leavePlanService == null) {
174 leavePlanService = HrServiceLocator.getLeavePlanService();
175 }
176 return leavePlanService;
177 }
178
179 public void setLeavePlanService(LeavePlanService leavePlanService) {
180 this.leavePlanService = leavePlanService;
181 }
182
183 private PrincipalHRAttributesService getPrincipalHRAttributesService() {
184 if (principalHRAttributesService == null) {
185 principalHRAttributesService = HrServiceLocator.getPrincipalHRAttributeService();
186 }
187 return principalHRAttributesService;
188 }
189
190 public void setPrincipalHRAttributesService(PrincipalHRAttributesService principalHRAttributesService) {
191 this.principalHRAttributesService = principalHRAttributesService;
192 }
193
194 private LeaveSummaryService getLeaveSummaryService() {
195 if (leaveSummaryService == null) {
196 leaveSummaryService = LmServiceLocator.getLeaveSummaryService();
197 }
198 return leaveSummaryService;
199 }
200
201 public void setLeaveSummaryService(LeaveSummaryService leaveSummaryService) {
202 this.leaveSummaryService = leaveSummaryService;
203 }
204
205 private CalendarEntryService getCalendarEntryService() {
206 if (calendarEntryService == null) {
207 calendarEntryService = HrServiceLocator.getCalendarEntryService();
208 }
209 return calendarEntryService;
210 }
211
212 public void setCalendarEntryService(CalendarEntryService calendarEntryService) {
213 this.calendarEntryService = calendarEntryService;
214 }
215
216 private LeaveBlockService getLeaveBlockService() {
217 if (leaveBlockService == null) {
218 leaveBlockService = LmServiceLocator.getLeaveBlockService();
219 }
220 return leaveBlockService;
221 }
222
223 public void setLeaveBlockService(LeaveBlockService leaveBlockService) {
224 this.leaveBlockService = leaveBlockService;
225 }
226
227 private List<LeaveBlock> createCarryOverLeaveBlocks(String principalId,
228 DateTime prevCalEndDate,
229 LeaveSummary leaveSummary) {
230
231 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
232 List<LeaveSummaryRow> leaveSummaryRows = leaveSummary.getLeaveSummaryRows();
233 if(leaveSummaryRows !=null && !leaveSummaryRows.isEmpty()){
234
235 for(LeaveSummaryRow lsr : leaveSummaryRows){
236 AccrualCategory accrualCategory = getAccrualCategoryService().getAccrualCategory(lsr.getAccrualCategoryId());
237
238 LeaveBlock leaveBlock = new LeaveBlock();
239 leaveBlock.setAccrualCategory(lsr.getAccrualCategory());
240 leaveBlock.setLeaveLocalDate(prevCalEndDate.toLocalDate());
241 leaveBlock.setLeaveBlockType(LMConstants.LEAVE_BLOCK_TYPE.CARRY_OVER);
242
243
244 if(accrualCategory != null && accrualCategory.getEarnCode() != null ){
245 leaveBlock.setEarnCode(accrualCategory.getEarnCode());
246 }
247
248 leaveBlock.setAccrualGenerated(true);
249 leaveBlock.setBlockId(0L);
250
251
252 leaveBlock.setScheduleTimeOffId(null);
253
254 if(lsr.getAccruedBalance() != null) {
255 if(lsr.getMaxCarryOver() != null && lsr.getAccruedBalance().compareTo(lsr.getMaxCarryOver()) > 0 ){
256 leaveBlock.setLeaveAmount(lsr.getMaxCarryOver());
257 } else {
258 leaveBlock.setLeaveAmount(lsr.getAccruedBalance());
259 }
260 }
261
262 leaveBlock.setPrincipalId(principalId);
263 leaveBlock.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
264
265
266 if(leaveBlock.getLeaveAmount() != null && leaveBlock.getEarnCode() != null) {
267 leaveBlocks.add(leaveBlock);
268 }
269 }
270 }
271
272
273 return leaveBlocks;
274 }
275
276
277
278 }