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.leaveblock;
17  
18  import java.math.BigDecimal;
19  import java.sql.Date;
20  import java.sql.Timestamp;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.persistence.Transient;
25  
26  import org.apache.commons.collections.CollectionUtils;
27  import org.apache.commons.lang.builder.EqualsBuilder;
28  import org.apache.commons.lang3.StringUtils;
29  import org.joda.time.DateTime;
30  import org.kuali.hr.lm.LMConstants;
31  import org.kuali.hr.lm.accrual.AccrualCategory;
32  import org.kuali.hr.lm.timeoff.SystemScheduledTimeOff;
33  import org.kuali.hr.lm.workflow.LeaveRequestDocument;
34  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
35  import org.kuali.hr.time.calendar.Calendar;
36  import org.kuali.hr.time.calendar.CalendarEntries;
37  import org.kuali.hr.time.earncode.EarnCode;
38  import org.kuali.hr.time.principal.PrincipalHRAttributes;
39  import org.kuali.hr.time.service.base.TkServiceLocator;
40  import org.kuali.hr.time.task.Task;
41  import org.kuali.hr.time.util.TKUtils;
42  import org.kuali.hr.time.util.TkConstants;
43  import org.kuali.hr.time.workarea.WorkArea;
44  import org.kuali.rice.kew.api.KewApiServiceLocator;
45  import org.kuali.rice.kew.api.document.DocumentStatus;
46  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
47  
48  public class LeaveBlock extends PersistableBusinessObjectBase {
49  
50  	private static final long serialVersionUID = -8240826812581295376L;
51  	
52  	private String lmLeaveBlockId;
53  	private Date leaveDate;
54  	private String description;
55  	private String principalId;
56  	private String earnCode;
57  	private String scheduleTimeOffId;
58  	private String accrualCategory;
59  	// private Boolean active;
60  	private BigDecimal leaveAmount = new BigDecimal("0.0");
61  	private String applyToYtdUsed;
62  	private String documentId;
63  	private String principalIdModified;
64  	private Timestamp timestamp;
65  	private Boolean accrualGenerated;
66  	private Long blockId;
67  	private String tkAssignmentId;
68  	private String requestStatus;
69  	private String leaveBlockType;
70  	private String documentStatus;
71  	
72  	private List<LeaveBlockHistory> leaveBlockHistories = new ArrayList<LeaveBlockHistory>();
73  	private SystemScheduledTimeOff systemScheduledTimeOffObj;
74  	private AccrualCategory accrualCategoryObj;
75      private String leaveRequestDocumentId;
76  
77  	@Transient
78  	private boolean submit;
79  	@Transient
80  	private String reason;
81  	@Transient
82  	private Timestamp dateAndTime;
83  
84  	private Long workArea;
85  	private Long jobNumber;
86  	private Long task;
87  	private String assignmentKey;
88  	
89  	@Transient
90  	private String assignmentTitle;
91  	@Transient
92  	private String calendarId;
93  	@Transient
94  	private String planningDescription;
95  
96  	private String transactionalDocId;
97  
98  	public static class Builder {
99  
100 		// required parameters for the constructor
101 		private final Date leaveDate;
102 		private final String principalId;
103 		private final String documentId;
104 		private final String earnCode;
105 		private final BigDecimal leaveAmount;
106 
107 		private String description = null;
108 		private String applyToYtdUsed = null;
109 		private String principalIdModified = null;
110 		private Timestamp timestamp = null;
111 		private Boolean accrualGenerated = Boolean.FALSE;
112 		private Long blockId = 0L;
113 		private String scheduleTimeOffId;
114 		private String accrualCategory;
115 		private String tkAssignmentId;
116 		private String requestStatus;
117 		private Long workArea;
118 		private Long jobNumber;
119 		private Long task;
120 		private String leaveBlockType;
121 
122 		public Builder(DateTime leaveBlockDate, String documentId,
123 				String principalId, String earnCode, BigDecimal leaveAmount) {
124 			this.leaveDate = new java.sql.Date(leaveBlockDate.toDate()
125 					.getTime());
126 			this.documentId = documentId;
127 			this.principalId = principalId;
128 			this.earnCode = earnCode;
129 			this.leaveAmount = leaveAmount;
130 		}
131 
132 		// validations could be done in the builder methods below
133 
134 		public Builder description(String val) {
135 			this.description = val;
136 			return this;
137 		}
138 
139 		public Builder applyToYtdUsed(String val) {
140 			this.applyToYtdUsed = val;
141 			return this;
142 		}
143 
144 		public Builder principalIdModified(String val) {
145 			this.principalIdModified = val;
146 			return this;
147 		}
148 
149 		public Builder timestamp(Timestamp val) {
150 			this.timestamp = val;
151 			return this;
152 		}
153 
154 		public Builder accrualGenerated(Boolean val) {
155 			this.accrualGenerated = val;
156 			return this;
157 		}
158 
159 		public Builder blockId(Long val) {
160 			this.blockId = val;
161 			return this;
162 		}
163 
164 		// TODO: need to hook up the objcets to get the real ids
165 		public Builder scheduleTimeOffId(String val) {
166 			this.scheduleTimeOffId = val;
167 			return this;
168 		}
169 
170 		public Builder accrualCategory(String val) {
171 			this.accrualCategory = val;
172 			return this;
173 		}
174 
175 		public Builder tkAssignmentId(String val) {
176 			this.tkAssignmentId = val;
177 			return this;
178 		}
179 
180 		public Builder workArea(Long val) {
181 			this.workArea = val;
182 			return this;
183 		}
184 
185 		public Builder jobNumber(Long val) {
186 			this.jobNumber = val;
187 			return this;
188 		}
189 
190 		public Builder task(Long val) {
191 			this.task = val;
192 			return this;
193 		}
194 
195 		public Builder requestStatus(String val) {
196 			this.requestStatus = val;
197 			return this;
198 		}
199 		
200 		public Builder leaveBlockType(String leaveBlockType) {
201 			this.leaveBlockType = leaveBlockType;
202 			return this;
203 		}
204 
205 		public LeaveBlock build() {
206 			return new LeaveBlock(this);
207 		}
208 
209 	}
210 
211 	private LeaveBlock(Builder builder) {
212 		leaveDate = builder.leaveDate;
213 		description = builder.description;
214 		principalId = builder.principalId;
215 		earnCode = builder.earnCode;
216 		leaveAmount = builder.leaveAmount;
217 		applyToYtdUsed = builder.applyToYtdUsed;
218 		documentId = builder.documentId;
219 		principalIdModified = builder.principalIdModified;
220 		timestamp = builder.timestamp;
221 		accrualGenerated = builder.accrualGenerated;
222 		blockId = builder.blockId;
223 		scheduleTimeOffId = builder.scheduleTimeOffId;
224 		accrualCategory = builder.accrualCategory;
225 		tkAssignmentId = builder.tkAssignmentId;
226 		requestStatus = builder.requestStatus;
227 		workArea = builder.workArea;
228 		jobNumber = builder.jobNumber;
229 		task = builder.task;
230 		leaveBlockType = builder.leaveBlockType;
231 		// TODO: need to hook up leaveCodeObj, systemScheduledTimeOffObj,
232 		// accrualCategoryObj, and ids for individual obj
233 	}
234 
235 	public LeaveBlock() {
236 
237 	}
238 
239 	public String getAccrualCategory() {
240 		return accrualCategory;
241 	}
242 
243 	public void setAccrualCategory(String accrualCategory) {
244 		this.accrualCategory = accrualCategory;
245 	}
246 
247 	public Boolean getAccrualGenerated() {
248 		return accrualGenerated;
249 	}
250 
251 	public void setAccrualGenerated(Boolean accrualGenerated) {
252 		this.accrualGenerated = accrualGenerated;
253 	}
254 
255 	// public Boolean getActive() {
256 	// return active;
257 	// }
258 	//
259 	// public void setActive(Boolean active) {
260 	// this.active = active;
261 	// }
262 
263 	public String getApplyToYtdUsed() {
264 		return applyToYtdUsed;
265 	}
266 
267 	public boolean getSubmit() {
268 		return submit;
269 	}
270 
271 	public void setSubmit(boolean submit) {
272 		this.submit = submit;
273 	}
274 
275 	public void setApplyToYtdUsed(String applyToYtdUsed) {
276 		this.applyToYtdUsed = applyToYtdUsed;
277 	}
278 
279 	public Long getBlockId() {
280 		return blockId;
281 	}
282 
283 	public void setBlockId(Long blockId) {
284 		this.blockId = blockId;
285 	}
286 
287 	public String getDescription() {
288 		return description;
289 	}
290 
291 	public void setDescription(String description) {
292 		this.description = description;
293 	}
294 
295 	public String getDocumentId() {
296 		return documentId;
297 	}
298 
299 	public void setDocumentId(String documentId) {
300 		this.documentId = documentId;
301 	}
302 
303 	public BigDecimal getLeaveAmount() {
304 		return leaveAmount;
305 	}
306 
307 	public void setLeaveAmount(BigDecimal leaveAmount) {
308 		this.leaveAmount = leaveAmount;
309 	}
310 
311 	public Date getLeaveDate() {
312 		return leaveDate;
313 	}
314 
315 	public void setLeaveDate(Date leaveDate) {
316 		this.leaveDate = leaveDate;
317 	}
318 
319 	public String getLmLeaveBlockId() {
320 		return lmLeaveBlockId;
321 	}
322 
323 	public void setLmLeaveBlockId(String lmLeaveBlockId) {
324 		this.lmLeaveBlockId = lmLeaveBlockId;
325 	}
326 
327 	public String getPrincipalIdModified() {
328 		return principalIdModified;
329 	}
330 
331 	public void setPrincipalIdModified(String principalIdModified) {
332 		this.principalIdModified = principalIdModified;
333 	}
334 
335 	public String getPrincipalId() {
336 		return principalId;
337 	}
338 
339 	public void setPrincipalId(String principalId) {
340 		this.principalId = principalId;
341 	}
342 
343 	public String getScheduleTimeOffId() {
344 		return scheduleTimeOffId;
345 	}
346 
347 	public void setScheduleTimeOffId(String scheduleTimeOffId) {
348 		this.scheduleTimeOffId = scheduleTimeOffId;
349 	}
350 
351 	public Timestamp getTimestamp() {
352 		return timestamp;
353 	}
354 
355 	public void setTimestamp(Timestamp timestamp) {
356 		this.timestamp = timestamp;
357 	}
358 
359 	public AccrualCategory getAccrualCategoryObj() {
360 		return accrualCategoryObj;
361 	}
362 
363 	public void setAccrualCategoryObj(AccrualCategory accrualCategoryObj) {
364 		this.accrualCategoryObj = accrualCategoryObj;
365 	}
366 
367 	public SystemScheduledTimeOff getSystemScheduledTimeOffObj() {
368 		return systemScheduledTimeOffObj;
369 	}
370 
371 	public void setSystemScheduledTimeOffObj(
372 			SystemScheduledTimeOff systemScheduledTimeOffObj) {
373 		this.systemScheduledTimeOffObj = systemScheduledTimeOffObj;
374 	}
375 
376 	public String getTkAssignmentId() {
377 		return tkAssignmentId;
378 	}
379 
380 	public void setTkAssignmentId(String tkAssignmentId) {
381 		this.tkAssignmentId = tkAssignmentId;
382 	}
383 
384 	public String getRequestStatus() {
385 		return requestStatus;
386 	}
387 
388 	public void setRequestStatus(String requestStatus) {
389 		this.requestStatus = requestStatus;
390 	}
391 
392     public String getRequestStatusString() {
393         String status = LMConstants.REQUEST_STATUS_STRINGS.get(getRequestStatus());
394         return status == null ? "usage" : status;
395     }
396 
397 	public List<LeaveBlockHistory> getLeaveBlockHistories() {
398 		return leaveBlockHistories;
399 	}
400 
401 	public void setLeaveBlockHistories(
402 			List<LeaveBlockHistory> leaveBlockHistories) {
403 		this.leaveBlockHistories = leaveBlockHistories;
404 	}
405 
406 	public String getReason() {
407 		return reason;
408 	}
409 
410 	public void setReason(String reason) {
411 		this.reason = reason;
412 	}
413 
414 	public Timestamp getDateAndTime() {
415 		return dateAndTime;
416 	}
417 
418 	public void setDateAndTime(Timestamp dateAndTime) {
419 		this.dateAndTime = dateAndTime;
420 	}
421 
422 	public Long getWorkArea() {
423 		return workArea;
424 	}
425 
426 	public void setWorkArea(Long workArea) {
427 		this.workArea = workArea;
428 	}
429 
430 	public Long getJobNumber() {
431 		return jobNumber;
432 	}
433 
434 	public void setJobNumber(Long jobNumber) {
435 		this.jobNumber = jobNumber;
436 	}
437 
438 	public Long getTask() {
439 		return task;
440 	}
441 
442 	public void setTask(Long task) {
443 		this.task = task;
444 	}
445 
446 	public String getAssignmentTitle() {
447 		StringBuilder b = new StringBuilder();
448 
449 		if (this.workArea != null) {
450 			WorkArea wa = TkServiceLocator.getWorkAreaService().getWorkArea(
451 					this.workArea, TKUtils.getCurrentDate());
452 			if (wa != null) {
453 				b.append(wa.getDescription());
454 			}
455 			Task task = TkServiceLocator.getTaskService().getTask(
456 					this.getTask(), this.getLeaveDate());
457 			if (task != null) {
458 				// do not display task description if the task is the default
459 				// one
460 				// default task is created in getTask() of TaskService
461 				if (!task.getDescription()
462 						.equals(TkConstants.TASK_DEFAULT_DESP)) {
463 					b.append("-" + task.getDescription());
464 				}
465 			}
466 		}
467 		return b.toString();
468 	}
469 
470 	public void setAssignmentTitle(String assignmentTitle) {
471 		this.assignmentTitle = assignmentTitle;
472 	}
473 
474 	public String getCalendarId() {
475 		PrincipalHRAttributes principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(this.principalId, TKUtils.getCurrentDate());
476 		Calendar pcal= null;
477 		if(principalHRAttributes != null) {
478 			//pcal = principalHRAttributes.getCalendar() != null ? principalHRAttributes.getCalendar() : principalHRAttributes.getLeaveCalObj() ;
479 			pcal = principalHRAttributes.getLeaveCalObj() != null ? principalHRAttributes.getLeaveCalObj() : principalHRAttributes.getCalendar();
480             if(pcal!= null) {
481 				CalendarEntries calEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(pcal.getHrCalendarId(), this.leaveDate);
482 				if(calEntries != null) {
483 					this.calendarId = calEntries.getHrCalendarEntriesId();
484 				}
485 			}
486 		}
487 		return calendarId;
488 	}
489 
490 	public void setCalendarId(String calendarId) {
491 		this.calendarId = calendarId;
492 	}
493 
494 	public String getEarnCode() {
495 		return earnCode;
496 	}
497 
498 	public void setEarnCode(String earnCode) {
499 		this.earnCode = earnCode;
500 	}
501 	
502 	public String getEarnCodeDescription() {
503 		String earnCodeDescription = "";
504 		
505 		EarnCode earnCodeObj = TkServiceLocator.getEarnCodeService().getEarnCode(earnCode, leaveDate);
506 		if (earnCodeObj != null) {
507 			earnCodeDescription = earnCodeObj.getDescription();
508 		}
509 		
510 		return earnCodeDescription;
511 	}
512 
513 	public String getLeaveBlockType() {
514 		return leaveBlockType;
515 	}
516 
517 	public void setLeaveBlockType(String leaveBlockType) {
518 		this.leaveBlockType = leaveBlockType;
519 	}
520 
521     public boolean isEditable() {
522         return TkServiceLocator.getPermissionsService().canEditLeaveBlock(this);
523     }
524 
525     public boolean isDeletable() {
526         return TkServiceLocator.getPermissionsService().canDeleteLeaveBlock(this);
527     }
528     
529     public String getAssignmentKey() {
530         if (assignmentKey == null) {
531             this.setAssignmentKey(TKUtils.formatAssignmentKey(jobNumber, workArea, task));
532         }
533         return assignmentKey;
534     }
535     public void setAssignmentKey(String assignmentDescription) {
536         this.assignmentKey = assignmentDescription;
537     }
538 
539 	public String getDocumentStatus() {
540 		return documentStatus;
541 	}
542 
543 	public void setDocumentStatus(String documentStatus) {
544 		this.documentStatus = documentStatus;
545 	}
546 
547     public String getLeaveRequestDocumentId() {
548         return leaveRequestDocumentId;
549     }
550 
551     public void setLeaveRequestDocumentId(String leaveRequestDocumentId) {
552         this.leaveRequestDocumentId = leaveRequestDocumentId;
553     }
554 
555     @Override
556     public boolean equals(Object obj) {
557         if (obj == null) {
558             return false;
559         }
560         if (obj == this) {
561             return true;
562         }
563         if (obj.getClass() != getClass()) {
564             return false;
565         }
566         LeaveBlock leaveBlock = (LeaveBlock) obj;
567         return new EqualsBuilder()
568                 .append(principalId, leaveBlock.principalId)
569                 .append(jobNumber, leaveBlock.jobNumber)
570                 .append(workArea, leaveBlock.workArea)
571                 .append(task, leaveBlock.task)
572                 .append(earnCode, leaveBlock.earnCode)
573                 .append(leaveDate, leaveBlock.leaveDate)
574                 .append(leaveAmount, leaveBlock.leaveAmount)
575                 .append(accrualCategory, leaveBlock.accrualCategory)
576                 .append(earnCode, leaveBlock.earnCode)
577                 .append(description, leaveBlock.description)
578                 .append(leaveBlockType, leaveBlock.leaveBlockType)
579                 .isEquals();
580     }
581     
582     public String getPlanningDescription() {
583     	if(this.getRequestStatus().equals(LMConstants.REQUEST_STATUS.DEFERRED)) {
584     		List<LeaveRequestDocument> lrdList = TkServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocumentsByLeaveBlockId(this.getLmLeaveBlockId());
585     		if(CollectionUtils.isNotEmpty(lrdList)) {
586     			for(LeaveRequestDocument lrd : lrdList) {    				
587     				DocumentStatus status = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(lrd.getDocumentNumber());
588     				if(status != null && DocumentStatus.CANCELED.getCode().equals(status.getCode())) {
589 	    				String requestDescription = "";
590 						if(StringUtils.isNotEmpty(this.getDescription())) {
591 							requestDescription = this.getDescription() + " <br/>";
592 						}
593 						String actionDateString = TKUtils.formatDate(new Date(lrd.getDocumentHeader().getWorkflowDocument().getDateFinalized().getMillis()));
594 						requestDescription += "Approval deferred on " + actionDateString + ". Reason: " + lrd.getDescription();
595 			    		this.setPlanningDescription(requestDescription);
596 						return planningDescription;
597     				}
598     			}
599     		}
600     	}
601     	this.setPlanningDescription(this.getDescription());
602     	return planningDescription;
603     }
604 
605 	public void setPlanningDescription(String planningDescription) {
606 		this.planningDescription = planningDescription;
607 	}
608 
609 	public void setTransactionDocId(String documentHeaderId) {
610 		transactionalDocId = documentHeaderId;		
611 	}
612 	
613 	public String getTransactionalDocId() {
614 		return transactionalDocId;
615 	}
616 
617 }