1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.leave.block;
17
18 import java.math.BigDecimal;
19 import java.sql.Timestamp;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.List;
24
25 import javax.persistence.Transient;
26
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.commons.lang.builder.EqualsBuilder;
30 import org.apache.commons.lang.builder.HashCodeBuilder;
31 import org.joda.time.DateTime;
32 import org.joda.time.LocalDate;
33 import org.joda.time.LocalTime;
34 import org.kuali.kpme.core.accrualcategory.AccrualCategory;
35 import org.kuali.kpme.core.accrualcategory.rule.AccrualCategoryRule;
36 import org.kuali.kpme.core.api.assignment.Assignable;
37 import org.kuali.kpme.core.assignment.Assignment;
38 import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
39 import org.kuali.kpme.core.block.CalendarBlock;
40 import org.kuali.kpme.core.calendar.Calendar;
41 import org.kuali.kpme.core.calendar.entry.CalendarEntry;
42 import org.kuali.kpme.core.earncode.EarnCode;
43 import org.kuali.kpme.core.principal.PrincipalHRAttributes;
44 import org.kuali.kpme.core.service.HrServiceLocator;
45 import org.kuali.kpme.core.task.Task;
46 import org.kuali.kpme.core.util.HrConstants;
47 import org.kuali.kpme.core.util.HrContext;
48 import org.kuali.kpme.core.util.TKUtils;
49 import org.kuali.kpme.core.workarea.WorkArea;
50 import org.kuali.kpme.tklm.api.leave.block.LeaveBlockContract;
51 import org.kuali.kpme.tklm.common.TkConstants;
52 import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
53 import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
54 import org.kuali.kpme.tklm.leave.workflow.LeaveRequestDocument;
55 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
56 import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
57 import org.kuali.rice.kew.api.KewApiServiceLocator;
58 import org.kuali.rice.kew.api.document.DocumentStatus;
59 import org.kuali.rice.krad.util.ObjectUtils;
60 import org.springframework.expression.spel.ast.Assign;
61
62 public class LeaveBlock extends CalendarBlock implements Assignable, LeaveBlockContract {
63
64 private static final long serialVersionUID = -8240826812581295376L;
65 public static final String CACHE_NAME = TkConstants.CacheNamespace.NAMESPACE_PREFIX + "LeaveBlock";
66
67 protected String earnCode;
68 protected Long workArea;
69 protected Long jobNumber;
70 protected Long task;
71 protected BigDecimal leaveAmount = new BigDecimal("0.0");
72
73 private Date leaveDate;
74 private String description;
75 private String scheduleTimeOffId;
76 private String accrualCategory;
77 private Boolean accrualGenerated;
78 private Long blockId;
79 private String requestStatus;
80 private String leaveBlockType;
81 private String documentStatus;
82 private String principalIdModified;
83
84 private List<LeaveBlockHistory> leaveBlockHistories = new ArrayList<LeaveBlockHistory>();
85 private String leaveRequestDocumentId;
86
87 protected String lmLeaveBlockId;
88 private String userPrincipalId;
89
90 @Transient
91 private Date beginDate;
92 @Transient
93 private boolean submit;
94 @Transient
95 private String reason;
96
97 private String assignmentKey;
98
99 @Transient
100 private String assignmentTitle;
101 @Transient
102 private String calendarId;
103 @Transient
104 private String planningDescription;
105
106 @Transient
107 private AccrualCategoryRule accrualCategoryRule;
108
109 @Transient
110 private AccrualCategory accrualCategoryObj;
111
112 @Transient
113 private PrincipalHRAttributes principalHRAttributes;
114
115 @Transient
116 private String affectPay;
117
118 private String transactionalDocId;
119 private LeaveCalendarDocumentHeader leaveCalendarDocumentHeader;
120
121 public String getAccrualCategoryRuleId() {
122 AccrualCategoryRule aRule = getAccrualCategoryRule();
123 return ObjectUtils.isNull(aRule) ? null : aRule.getLmAccrualCategoryRuleId();
124 }
125
126 public static class Builder {
127
128
129 private final Date leaveDate;
130 private final String principalId;
131 private final String documentId;
132 private final String earnCode;
133 private final BigDecimal leaveAmount;
134
135 private String description = null;
136 private String principalIdModified = null;
137 private Timestamp timestamp = null;
138 private Boolean accrualGenerated = Boolean.FALSE;
139 private Long blockId = 0L;
140 private String scheduleTimeOffId;
141 private String accrualCategory;
142 private String requestStatus;
143 private Long workArea;
144 private Long jobNumber;
145 private Long task;
146 private String leaveBlockType;
147 private String userPrincipalId;
148 private LeaveCalendarDocumentHeader leaveCalendarDocumentHeader;
149
150 public Builder(LocalDate leaveDate, String documentId,
151 String principalId, String earnCode, BigDecimal leaveAmount) {
152 this.leaveDate = leaveDate.toDate();
153 this.documentId = documentId;
154 this.principalId = principalId;
155 this.earnCode = earnCode;
156 this.leaveAmount = leaveAmount;
157 }
158
159
160
161 public Builder description(String val) {
162 this.description = val;
163 return this;
164 }
165
166
167 public Builder principalIdModified(String val) {
168 this.principalIdModified = val;
169 return this;
170 }
171
172 public Builder timestamp(Timestamp val) {
173 this.timestamp = val;
174 return this;
175 }
176
177 public Builder accrualGenerated(Boolean val) {
178 this.accrualGenerated = val;
179 return this;
180 }
181
182 public Builder blockId(Long val) {
183 this.blockId = val;
184 return this;
185 }
186
187
188 public Builder scheduleTimeOffId(String val) {
189 this.scheduleTimeOffId = val;
190 return this;
191 }
192
193 public Builder accrualCategory(String val) {
194 this.accrualCategory = val;
195 return this;
196 }
197
198 public Builder workArea(Long val) {
199 this.workArea = val;
200 return this;
201 }
202
203 public Builder jobNumber(Long val) {
204 this.jobNumber = val;
205 return this;
206 }
207
208 public Builder task(Long val) {
209 this.task = val;
210 return this;
211 }
212
213 public Builder requestStatus(String val) {
214 this.requestStatus = val;
215 return this;
216 }
217
218 public Builder leaveBlockType(String leaveBlockType) {
219 this.leaveBlockType = leaveBlockType;
220 return this;
221 }
222
223 public Builder userPrincipalId(String userPrincipalId) {
224 this.userPrincipalId = userPrincipalId;
225 return this;
226 }
227
228 public Builder LeaveCalendarDocumentHeader(LeaveCalendarDocumentHeader leaveCalendarDocumentHeader) {
229 this.leaveCalendarDocumentHeader = leaveCalendarDocumentHeader;
230 return this;
231 }
232
233 public LeaveBlock build() {
234 return new LeaveBlock(this);
235 }
236
237 }
238
239 private LeaveBlock(Builder builder) {
240 leaveDate = builder.leaveDate;
241 description = builder.description;
242 principalId = builder.principalId;
243 earnCode = builder.earnCode;
244 leaveAmount = builder.leaveAmount;
245 documentId = builder.documentId;
246 principalIdModified = builder.principalIdModified;
247 timestamp = builder.timestamp;
248 accrualGenerated = builder.accrualGenerated;
249 blockId = builder.blockId;
250 scheduleTimeOffId = builder.scheduleTimeOffId;
251 accrualCategory = builder.accrualCategory;
252 requestStatus = builder.requestStatus;
253 workArea = builder.workArea;
254 jobNumber = builder.jobNumber;
255 task = builder.task;
256 leaveBlockType = builder.leaveBlockType;
257 userPrincipalId = builder.userPrincipalId;
258 leaveCalendarDocumentHeader = builder.leaveCalendarDocumentHeader;
259
260
261 }
262
263 public LeaveBlock() {
264
265 }
266
267
268 public String getAccrualCategory() {
269 return accrualCategory;
270 }
271
272 public void setAccrualCategory(String accrualCategory) {
273 this.accrualCategory = accrualCategory;
274 }
275
276 public Boolean getAccrualGenerated() {
277 return accrualGenerated;
278 }
279
280 public void setAccrualGenerated(Boolean accrualGenerated) {
281 this.accrualGenerated = accrualGenerated;
282 }
283
284 public boolean isSubmit() {
285 return submit;
286 }
287
288 public void setSubmit(boolean submit) {
289 this.submit = submit;
290 }
291
292 public Long getBlockId() {
293 return blockId;
294 }
295
296 public void setBlockId(Long blockId) {
297 this.blockId = blockId;
298 }
299
300 public String getDescription() {
301 return description;
302 }
303
304 public void setDescription(String description) {
305 this.description = description;
306 }
307
308 public Date getLeaveDate() {
309 return leaveDate;
310 }
311
312 public void setLeaveDate(Date leaveDate) {
313 this.leaveDate = leaveDate;
314 }
315
316 public LocalDate getLeaveLocalDate() {
317 return leaveDate != null ? LocalDate.fromDateFields(leaveDate) : null;
318 }
319
320 public void setLeaveLocalDate(LocalDate leaveLocalDate) {
321 this.leaveDate = leaveLocalDate != null ? leaveLocalDate.toDate() : null;
322 }
323
324 public String getScheduleTimeOffId() {
325 return scheduleTimeOffId;
326 }
327
328 public void setScheduleTimeOffId(String scheduleTimeOffId) {
329 this.scheduleTimeOffId = scheduleTimeOffId;
330 }
331
332 public String getRequestStatus() {
333 return requestStatus;
334 }
335
336 public void setRequestStatus(String requestStatus) {
337 this.requestStatus = requestStatus;
338 }
339
340 public String getRequestStatusString() {
341 String status = HrConstants.REQUEST_STATUS_STRINGS.get(getRequestStatus());
342 return status == null ? "usage" : status;
343 }
344
345 public List<LeaveBlockHistory> getLeaveBlockHistories() {
346 return leaveBlockHistories;
347 }
348
349 public void setLeaveBlockHistories(
350 List<LeaveBlockHistory> leaveBlockHistories) {
351 this.leaveBlockHistories = leaveBlockHistories;
352 }
353
354 public String getReason() {
355 return reason;
356 }
357
358 public void setReason(String reason) {
359 this.reason = reason;
360 }
361
362 public List<Assignment> getAssignments() {
363 AssignmentDescriptionKey key = AssignmentDescriptionKey.get(getAssignmentKey());
364 return Collections.singletonList(
365 HrServiceLocator.getAssignmentService().getAssignment(getPrincipalId(), key, getLeaveLocalDate()));
366 }
367 public String getAssignmentTitle() {
368 StringBuilder b = new StringBuilder();
369
370 if (this.workArea != null) {
371 WorkArea wa = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(
372 this.workArea, LocalDate.now());
373 if (wa != null) {
374 b.append(wa.getDescription());
375 }
376 Task task = HrServiceLocator.getTaskService().getTask(
377 this.getTask(), this.getLeaveLocalDate());
378 if (task != null) {
379
380
381
382 if (!task.getDescription()
383 .equals(HrConstants.TASK_DEFAULT_DESP)) {
384 b.append("-" + task.getDescription());
385 }
386 }
387 }
388 return b.toString();
389 }
390
391 public void setAssignmentTitle(String assignmentTitle) {
392 this.assignmentTitle = assignmentTitle;
393 }
394
395 public String getCalendarId() {
396 if (StringUtils.isEmpty(calendarId)) {
397 PrincipalHRAttributes principalHRAttributes = getPrincipalHRAttributes();
398 Calendar pcal= null;
399 if(principalHRAttributes != null) {
400
401 pcal = principalHRAttributes.getLeaveCalObj() != null ? principalHRAttributes.getLeaveCalObj() : principalHRAttributes.getCalendar();
402 if(pcal!= null) {
403 CalendarEntry calEntries = HrServiceLocator.getCalendarEntryService().getCurrentCalendarEntryByCalendarId(pcal.getHrCalendarId(), getLeaveLocalDate().toDateTimeAtStartOfDay());
404 if(calEntries != null) {
405 this.calendarId = calEntries.getHrCalendarEntryId();
406 }
407 }
408 }
409 }
410 return calendarId;
411 }
412
413 public void setCalendarId(String calendarId) {
414 this.calendarId = calendarId;
415 }
416
417 public String getEarnCodeDescription() {
418 String earnCodeDescription = "";
419
420 EarnCode earnCodeObj = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, getLeaveLocalDate());
421 if (earnCodeObj != null) {
422 earnCodeDescription = earnCodeObj.getDescription();
423 }
424
425 return earnCodeDescription;
426 }
427
428 public String getLeaveBlockType() {
429 return leaveBlockType;
430 }
431
432 public void setLeaveBlockType(String leaveBlockType) {
433 this.leaveBlockType = leaveBlockType;
434 }
435
436 public LeaveCalendarDocumentHeader getLeaveCalendarDocumentHeader() {
437 if (leaveCalendarDocumentHeader == null && this.getDocumentId() != null) {
438 setLeaveCalendarDocumentHeader(LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(this.getDocumentId()));
439 }
440 return leaveCalendarDocumentHeader;
441 }
442
443 public void setLeaveCalendarDocumentHeader(
444 LeaveCalendarDocumentHeader leaveCalendarDocumentHeader) {
445 this.leaveCalendarDocumentHeader = leaveCalendarDocumentHeader;
446 }
447
448
449
450 public boolean isEditable() {
451 return LmServiceLocator.getLMPermissionService().canEditLeaveBlock(HrContext.getPrincipalId(), this);
452 }
453
454 public boolean isDeletable() {
455 return LmServiceLocator.getLMPermissionService().canDeleteLeaveBlock(HrContext.getPrincipalId(), this);
456 }
457
458 public String getAssignmentKey() {
459 if (assignmentKey == null) {
460 this.setAssignmentKey(TKUtils.formatAssignmentKey(jobNumber, workArea, task));
461 }
462 return assignmentKey;
463 }
464 public void setAssignmentKey(String assignmentDescription) {
465 this.assignmentKey = assignmentDescription;
466 }
467
468 public String getDocumentStatus() {
469 return documentStatus;
470 }
471
472 public void setDocumentStatus(String documentStatus) {
473 this.documentStatus = documentStatus;
474 }
475
476 public String getLeaveRequestDocumentId() {
477 return leaveRequestDocumentId;
478 }
479
480 public void setLeaveRequestDocumentId(String leaveRequestDocumentId) {
481 this.leaveRequestDocumentId = leaveRequestDocumentId;
482 }
483
484 @Override
485 public int hashCode() {
486 return new HashCodeBuilder()
487 .append(this.principalId)
488 .append(this.jobNumber)
489 .append(this.workArea)
490 .append(this.task)
491 .append(this.earnCode)
492 .append(this.leaveDate)
493 .append(this.leaveAmount)
494 .append(this.accrualCategory)
495 .append(this.earnCode)
496 .append(this.description)
497 .append(this.leaveBlockType)
498 .toHashCode();
499 }
500
501 @Override
502 public boolean equals(Object obj) {
503 if (obj == null) {
504 return false;
505 }
506 if (obj == this) {
507 return true;
508 }
509 if (obj.getClass() != getClass()) {
510 return false;
511 }
512 LeaveBlock leaveBlock = (LeaveBlock) obj;
513 return new EqualsBuilder()
514 .append(principalId, leaveBlock.principalId)
515 .append(jobNumber, leaveBlock.jobNumber)
516 .append(workArea, leaveBlock.workArea)
517 .append(task, leaveBlock.task)
518 .append(earnCode, leaveBlock.earnCode)
519 .append(leaveDate, leaveBlock.leaveDate)
520 .append(leaveAmount, leaveBlock.leaveAmount)
521 .append(accrualCategory, leaveBlock.accrualCategory)
522 .append(earnCode, leaveBlock.earnCode)
523 .append(description, leaveBlock.description)
524 .append(leaveBlockType, leaveBlock.leaveBlockType)
525 .isEquals();
526 }
527
528 public String getPlanningDescription() {
529 if(this.getRequestStatus().equals(HrConstants.REQUEST_STATUS.DEFERRED)) {
530 List<LeaveRequestDocument> lrdList = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocumentsByLeaveBlockId(this.getLmLeaveBlockId());
531 if(CollectionUtils.isNotEmpty(lrdList)) {
532 for(LeaveRequestDocument lrd : lrdList) {
533 DocumentStatus status = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(lrd.getDocumentNumber());
534 if(status != null && DocumentStatus.CANCELED.getCode().equals(status.getCode())) {
535 String requestDescription = "";
536 if(StringUtils.isNotEmpty(this.getDescription())) {
537 requestDescription = this.getDescription() + " <br/>";
538 }
539 String actionDateString = TKUtils.formatDate(lrd.getDocumentHeader().getWorkflowDocument().getDateFinalized().toLocalDate());
540 requestDescription += "Approval deferred on " + actionDateString + ". Reason: " + lrd.getDescription();
541 this.setPlanningDescription(requestDescription);
542 return planningDescription;
543 }
544 }
545 }
546 }
547 this.setPlanningDescription(this.getDescription());
548 return planningDescription;
549 }
550
551 public void setPlanningDescription(String planningDescription) {
552 this.planningDescription = planningDescription;
553 }
554
555 public void setTransactionDocId(String documentHeaderId) {
556 transactionalDocId = documentHeaderId;
557 }
558
559 public String getTransactionalDocId() {
560 return transactionalDocId;
561 }
562
563 public DateTime getBeginDateTime() {
564 return beginTimestamp != null ? new DateTime(beginTimestamp) : null;
565 }
566
567 public void setBeginDateTime(DateTime beginDateTime) {
568 beginTimestamp = beginDateTime != null ? new Timestamp(beginDateTime.getMillis()) : null;
569 }
570
571
572 public Date getBeginDate() {
573 Date beginDate = null;
574 if (beginTimestamp != null) {
575 beginDate = new Date(beginTimestamp.getTime());
576 }
577 return beginDate;
578 }
579
580 public Timestamp getBeginTimestampVal() {
581 return new Timestamp(beginTimestamp.getTime());
582 }
583
584 public Timestamp getEndTimestampVal() {
585 return new Timestamp(endTimestamp.getTime());
586 }
587
588 public void setBeginDate(Date beginDate) {
589 DateTime dateTime = new DateTime(beginTimestamp);
590 LocalDate localDate = new LocalDate(beginDate);
591 LocalTime localTime = new LocalTime(beginTimestamp);
592 beginTimestamp = new Timestamp(localDate.toDateTime(localTime, dateTime.getZone()).getMillis());
593 }
594
595 public DateTime getEndDateTime() {
596 return endTimestamp != null ? new DateTime(endTimestamp) : null;
597 }
598
599 public void setEndDateTime(DateTime endDateTime) {
600 endTimestamp = endDateTime != null ? new Timestamp(endDateTime.getMillis()) : null;
601 }
602
603 public String getLmLeaveBlockId() {
604 return lmLeaveBlockId;
605 }
606
607 public void setLmLeaveBlockId(String lmLeaveBlockId) {
608 this.lmLeaveBlockId = lmLeaveBlockId;
609 }
610
611 public String getPrincipalIdModified() {
612 return principalIdModified;
613 }
614
615 public void setPrincipalIdModified(String principalIdModified) {
616 this.principalIdModified = principalIdModified;
617 }
618
619 public BigDecimal getLeaveAmount() {
620 return leaveAmount;
621 }
622
623 public void setLeaveAmount(BigDecimal leaveAmount) {
624 this.leaveAmount = leaveAmount;
625 }
626
627 public Long getWorkArea() {
628 return workArea;
629 }
630
631 public void setWorkArea(Long workArea) {
632 this.workArea = workArea;
633 }
634
635 public Long getJobNumber() {
636 return jobNumber;
637 }
638
639 public void setJobNumber(Long jobNumber) {
640 this.jobNumber = jobNumber;
641 }
642
643 public Long getTask() {
644 return task;
645 }
646
647 public void setTask(Long task) {
648 this.task = task;
649 }
650
651 public String getEarnCode() {
652 return earnCode;
653 }
654
655 public void setEarnCode(String earnCode) {
656 this.earnCode = earnCode;
657 }
658
659 public AccrualCategoryRule getAccrualCategoryRule() {
660 if (accrualCategoryRule == null) {
661 AccrualCategory category = getAccrualCategoryObj();
662 PrincipalHRAttributes pha = getPrincipalHRAttributes();
663 accrualCategoryRule = HrServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(category, getLeaveLocalDate(), pha.getServiceLocalDate());
664 }
665 return accrualCategoryRule;
666 }
667
668 public AccrualCategory getAccrualCategoryObj() {
669 if (accrualCategoryObj == null) {
670 accrualCategoryObj = HrServiceLocator.getAccrualCategoryService().getAccrualCategory(accrualCategory, getLeaveLocalDate());
671 }
672 return accrualCategoryObj;
673 }
674
675 public PrincipalHRAttributes getPrincipalHRAttributes() {
676 if (principalHRAttributes == null) {
677 principalHRAttributes = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, getLeaveLocalDate());
678 }
679 return principalHRAttributes;
680 }
681
682 public void setPrincipalHRAttributes(PrincipalHRAttributes principalHRAttributes) {
683 this.principalHRAttributes = principalHRAttributes;
684 }
685
686 public String getUserPrincipalId() {
687 return userPrincipalId;
688 }
689
690 public void setUserPrincipalId(String userPrincipalId) {
691 this.userPrincipalId = userPrincipalId;
692 }
693
694 public String getAffectPay() {
695 return affectPay;
696 }
697
698 public void setAffectPay(String affectPay) {
699 this.affectPay = affectPay;
700 }
701
702 }