View Javadoc
1   /**
2    * Copyright 2004-2015 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.kpme.tklm.leave.block;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.commons.lang.builder.EqualsBuilder;
21  import org.apache.commons.lang.builder.HashCodeBuilder;
22  import org.joda.time.DateTime;
23  import org.joda.time.LocalDate;
24  import org.joda.time.LocalTime;
25  import org.kuali.kpme.core.api.accrualcategory.AccrualCategory;
26  import org.kuali.kpme.core.api.accrualcategory.rule.AccrualCategoryRule;
27  import org.kuali.kpme.core.api.accrualcategory.rule.AccrualCategoryRuleContract;
28  import org.kuali.kpme.core.api.assignment.Assignable;
29  import org.kuali.kpme.core.api.assignment.Assignment;
30  import org.kuali.kpme.core.api.assignment.AssignmentDescriptionKey;
31  import org.kuali.kpme.core.api.calendar.CalendarContract;
32  import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
33  import org.kuali.kpme.core.api.earncode.EarnCodeContract;
34  import org.kuali.kpme.core.api.groupkey.HrGroupKey;
35  import org.kuali.kpme.core.api.principal.PrincipalHRAttributes;
36  import org.kuali.kpme.core.api.task.TaskContract;
37  import org.kuali.kpme.core.api.util.KpmeUtils;
38  import org.kuali.kpme.core.api.workarea.WorkArea;
39  import org.kuali.kpme.core.block.CalendarBlock;
40  import org.kuali.kpme.core.service.HrServiceLocator;
41  import org.kuali.kpme.core.util.HrConstants;
42  import org.kuali.kpme.core.util.HrContext;
43  import org.kuali.kpme.core.util.TKUtils;
44  import org.kuali.kpme.tklm.api.common.TkConstants;
45  import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
46  import org.kuali.kpme.tklm.api.leave.block.LeaveBlockContract;
47  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
48  import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
49  import org.kuali.kpme.tklm.leave.workflow.LeaveRequestDocument;
50  import org.kuali.rice.kew.api.KewApiServiceLocator;
51  import org.kuali.rice.kew.api.document.DocumentStatus;
52  import org.kuali.rice.krad.util.ObjectUtils;
53  
54  import javax.persistence.Transient;
55  import java.math.BigDecimal;
56  import java.sql.Timestamp;
57  import java.util.ArrayList;
58  import java.util.Collections;
59  import java.util.Date;
60  import java.util.List;
61  
62  public class LeaveBlockBo extends CalendarBlock implements Assignable, LeaveBlockContract {
63  	
64  	private static final long serialVersionUID = -8240826812581295376L;
65  	public static final String CACHE_NAME = TkConstants.Namespace.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 calendarId;
101 	@Transient
102 	private String planningDescription;
103 
104     @Transient
105     private AccrualCategoryRule accrualCategoryRule;
106 
107     @Transient
108     private AccrualCategory accrualCategoryObj;
109 
110     @Transient
111     private PrincipalHRAttributes principalHRAttributes;
112   
113     @Transient
114     private String affectPay;
115 
116 	private String transactionalDocId;
117 	private LeaveCalendarDocumentHeader leaveCalendarDocumentHeader;
118 
119 	public String getAccrualCategoryRuleId() {
120         AccrualCategoryRuleContract aRule = getAccrualCategoryRule();
121 		return ObjectUtils.isNull(aRule) ? null : aRule.getLmAccrualCategoryRuleId();
122 	}
123 	
124 	public static class Builder {
125 
126 		// required parameters for the constructor
127 		private final Date leaveDate;
128 		private final String principalId;
129 		private final String documentId;
130 		private final String earnCode;
131 		private final BigDecimal leaveAmount;
132 
133 		private String description = null;
134 		private String principalIdModified = null;
135 		private Timestamp timestamp = null;
136 		private Boolean accrualGenerated = Boolean.FALSE;
137 		private Long blockId = 0L;
138 		private String scheduleTimeOffId;
139 		private String accrualCategory;
140 		private String requestStatus;
141 		private Long workArea;
142 		private Long jobNumber;
143 		private Long task;
144 		private String leaveBlockType;
145 		private String userPrincipalId;
146 		private LeaveCalendarDocumentHeader leaveCalendarDocumentHeader;
147 		private String groupKeyCode;
148 		
149 		public Builder(LocalDate leaveDate, String documentId,
150 				String principalId, String earnCode, BigDecimal leaveAmount, String groupKeyCode) {
151 			this.leaveDate = leaveDate.toDate();
152 			this.documentId = documentId;
153 			this.principalId = principalId;
154 			this.earnCode = earnCode;
155 			this.leaveAmount = leaveAmount;
156 			this.groupKeyCode = groupKeyCode;
157 		}
158 
159 		// validations could be done in the builder methods below
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 		// TODO: need to hook up the objcets to get the real ids
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 LeaveBlockBo build() {
234 			return new LeaveBlockBo(this);
235 		}
236 	}
237 
238 	private LeaveBlockBo(Builder builder) {
239 		leaveDate = builder.leaveDate;
240 		description = builder.description;
241 		principalId = builder.principalId;
242 		earnCode = builder.earnCode;
243 		leaveAmount = builder.leaveAmount;
244 		documentId = builder.documentId;
245 		principalIdModified = builder.principalIdModified;
246 		timestamp = builder.timestamp;
247 		accrualGenerated = builder.accrualGenerated;
248 		blockId = builder.blockId;
249 		scheduleTimeOffId = builder.scheduleTimeOffId;
250 		accrualCategory = builder.accrualCategory;
251 		requestStatus = builder.requestStatus;
252 		workArea = builder.workArea;
253 		jobNumber = builder.jobNumber;
254 		task = builder.task;
255 		leaveBlockType = builder.leaveBlockType;
256 		userPrincipalId = builder.userPrincipalId;
257 		leaveCalendarDocumentHeader = builder.leaveCalendarDocumentHeader;
258 		groupKeyCode = builder.groupKeyCode;
259 		// TODO: need to hook up leaveCodeObj, systemScheduledTimeOffObj,
260 		// accrualCategoryObj, and ids for individual obj
261 	}
262 
263 	public LeaveBlockBo() {
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 isAccrualGenerated() {
277 		return accrualGenerated;
278 	}
279 
280     public Boolean getAccrualGenerated() {
281         return accrualGenerated;
282     }
283 
284 	public void setAccrualGenerated(Boolean accrualGenerated) {
285 		this.accrualGenerated = accrualGenerated;
286 	}
287 
288 	public boolean isSubmit() {
289 		return submit;
290 	}
291 
292 	public void setSubmit(boolean submit) {
293 		this.submit = submit;
294 	}
295 
296 	public Long getBlockId() {
297 		return blockId;
298 	}
299 
300 	public void setBlockId(Long blockId) {
301 		this.blockId = blockId;
302 	}
303 
304 	public String getDescription() {
305 		return description;
306 	}
307 
308 	public void setDescription(String description) {
309 		this.description = description;
310 	}
311 
312 	public Date getLeaveDate() {
313 		return leaveDate;
314 	}
315 
316     public DateTime getLeaveDateTime() {
317         return leaveDate != null ? new DateTime(leaveDate.getTime()) : null;
318     }
319 
320 	public void setLeaveDate(Date leaveDate) {
321 		this.leaveDate = leaveDate;
322 	}
323 	
324 	public LocalDate getLeaveLocalDate() {
325 		return leaveDate != null ? LocalDate.fromDateFields(leaveDate) : null;
326 	}
327 	
328 	public void setLeaveLocalDate(LocalDate leaveLocalDate) {
329 		this.leaveDate = leaveLocalDate != null ? leaveLocalDate.toDate() : null;
330 	}
331 
332 	public String getScheduleTimeOffId() {
333 		return scheduleTimeOffId;
334 	}
335 
336 	public void setScheduleTimeOffId(String scheduleTimeOffId) {
337 		this.scheduleTimeOffId = scheduleTimeOffId;
338 	}
339 
340 	public String getRequestStatus() {
341 		return requestStatus;
342 	}
343 
344 	public void setRequestStatus(String requestStatus) {
345 		this.requestStatus = requestStatus;
346 	}
347 
348     public String getRequestStatusString() {
349         String status = HrConstants.REQUEST_STATUS_STRINGS.get(getRequestStatus());
350         return status == null ? "usage" : status;
351     }
352 
353 	public List<LeaveBlockHistory> getLeaveBlockHistories() {
354 		return leaveBlockHistories;
355 	}
356 
357 	public void setLeaveBlockHistories(
358 			List<LeaveBlockHistory> leaveBlockHistories) {
359 		this.leaveBlockHistories = leaveBlockHistories;
360 	}
361 
362 	public String getReason() {
363 		return reason;
364 	}
365 
366 	public void setReason(String reason) {
367 		this.reason = reason;
368 	}
369 
370     public List<Assignment> getAssignments() {
371         AssignmentDescriptionKey key = AssignmentDescriptionKey.get(getAssignmentKey());
372         return Collections.singletonList(
373                 HrServiceLocator.getAssignmentService().getAssignment(getPrincipalId(), key, getLeaveLocalDate()));
374     }
375 	public String getAssignmentTitle() {
376 		StringBuilder b = new StringBuilder();
377 
378 		if (this.workArea != null) {
379 			WorkArea wa = HrServiceLocator.getWorkAreaService().getWorkArea(
380                     this.workArea, LocalDate.now());
381 			if (wa != null) {
382 				b.append(wa.getDescription());
383 			}
384 			TaskContract task = HrServiceLocator.getTaskService().getTask(
385 					this.getTask(), this.getLeaveLocalDate());
386 			if (task != null) {
387 				// do not display task description if the task is the default
388 				// one
389 				// default task is created in getTask() of TaskService
390 				if (!task.getDescription()
391 						.equals(HrConstants.TASK_DEFAULT_DESP)) {
392 					b.append("-").append(task.getDescription());
393 				}
394 			}
395 		}
396 		return b.toString();
397 	}
398 
399 	public String getCalendarId() {
400         if (StringUtils.isEmpty(calendarId)) {
401             PrincipalHRAttributes principalHRAttributes = getPrincipalHRAttributes();
402             CalendarContract pcal;
403             if(principalHRAttributes != null) {
404                 //pcal = principalHRAttributes.getCalendar() != null ? principalHRAttributes.getCalendar() : principalHRAttributes.getLeaveCalObj() ;
405                 pcal = principalHRAttributes.getLeaveCalObj() != null ? principalHRAttributes.getLeaveCalObj() : principalHRAttributes.getCalendar();
406                 if(pcal!= null) {
407                     CalendarEntry calEntries = HrServiceLocator.getCalendarEntryService().getCurrentCalendarEntryByCalendarId(pcal.getHrCalendarId(), getLeaveLocalDate().toDateTimeAtStartOfDay());
408                     if(calEntries != null) {
409                         this.calendarId = calEntries.getHrCalendarEntryId();
410                     }
411                 }
412             }
413         }
414 		return calendarId;
415 	}
416 
417 	public void setCalendarId(String calendarId) {
418 		this.calendarId = calendarId;
419 	}
420 	
421 	public String getEarnCodeDescription() {
422 		String earnCodeDescription = "";
423 		
424 		EarnCodeContract earnCodeObj = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, getLeaveLocalDate());
425 		if (earnCodeObj != null) {
426 			earnCodeDescription = earnCodeObj.getDescription();
427 		}
428 		
429 		return earnCodeDescription;
430 	}
431 
432 	public String getLeaveBlockType() {
433 		return leaveBlockType;
434 	}
435 
436 	public void setLeaveBlockType(String leaveBlockType) {
437 		this.leaveBlockType = leaveBlockType;
438 	}
439 
440 	public LeaveCalendarDocumentHeader getLeaveCalendarDocumentHeader() {
441         if (leaveCalendarDocumentHeader == null && this.getDocumentId() != null) {
442             setLeaveCalendarDocumentHeader(LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(this.getDocumentId()));
443         }
444         return leaveCalendarDocumentHeader;
445     }
446 
447     public void setLeaveCalendarDocumentHeader(
448     		LeaveCalendarDocumentHeader leaveCalendarDocumentHeader) {
449         this.leaveCalendarDocumentHeader = leaveCalendarDocumentHeader;
450     }
451 
452 
453 	
454     public boolean isEditable() {
455         return LmServiceLocator.getLMPermissionService().canEditLeaveBlock(HrContext.getPrincipalId(), this);
456     }
457 
458     public boolean isDeletable() {
459         return LmServiceLocator.getLMPermissionService().canDeleteLeaveBlock(HrContext.getPrincipalId(), this);
460     }
461     
462     public String getAssignmentKey() {
463         if (assignmentKey == null) {
464             this.setAssignmentKey(KpmeUtils.formatAssignmentKey(groupKeyCode, jobNumber, workArea, task));
465         }
466         return assignmentKey;
467     }
468     public void setAssignmentKey(String assignmentDescription) {
469         this.assignmentKey = assignmentDescription;
470     }
471 
472 	public String getDocumentStatus() {
473 		return documentStatus;
474 	}
475 
476 	public void setDocumentStatus(String documentStatus) {
477 		this.documentStatus = documentStatus;
478 	}
479 
480     public String getLeaveRequestDocumentId() {
481         return leaveRequestDocumentId;
482     }
483 
484     public void setLeaveRequestDocumentId(String leaveRequestDocumentId) {
485         this.leaveRequestDocumentId = leaveRequestDocumentId;
486     }
487 
488     @Override
489     public int hashCode() {
490         return new HashCodeBuilder()
491                 .append(this.principalId)
492                 .append(this.jobNumber)
493                 .append(this.workArea)
494                 .append(this.task)
495                 .append(this.earnCode)
496                 .append(this.leaveDate)
497                 .append(this.leaveAmount)
498                 .append(this.accrualCategory)
499                 .append(this.earnCode)
500                 .append(this.description)
501                 .append(this.leaveBlockType)
502                 .toHashCode();
503     }
504 
505     @Override
506     public boolean equals(Object obj) {
507         if (obj == null) {
508             return false;
509         }
510         if (obj == this) {
511             return true;
512         }
513         if (obj.getClass() != getClass()) {
514             return false;
515         }
516         LeaveBlockBo leaveBlock = (LeaveBlockBo) obj;
517         return new EqualsBuilder()
518                 .append(principalId, leaveBlock.principalId)
519                 .append(jobNumber, leaveBlock.jobNumber)
520                 .append(workArea, leaveBlock.workArea)
521                 .append(task, leaveBlock.task)
522                 .append(earnCode, leaveBlock.earnCode)
523                 .append(leaveDate, leaveBlock.leaveDate)
524                 .append(KpmeUtils.nullSafeCompare(leaveAmount, leaveBlock.leaveAmount), 0)
525                 .append(accrualCategory, leaveBlock.accrualCategory)
526                 .append(earnCode, leaveBlock.earnCode)
527                 .append(description, leaveBlock.description)
528                 .append(leaveBlockType, leaveBlock.leaveBlockType)
529                 .isEquals();
530     }
531     
532     public String getPlanningDescription() {
533     	if(this.getRequestStatus() != null
534                 && this.getRequestStatus().equals(HrConstants.REQUEST_STATUS.DEFERRED)) {
535     		List<LeaveRequestDocument> lrdList = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocumentsByLeaveBlockId(this.getLmLeaveBlockId());
536     		if(CollectionUtils.isNotEmpty(lrdList)) {
537     			for(LeaveRequestDocument lrd : lrdList) {    				
538     				DocumentStatus status = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(lrd.getDocumentNumber());
539     				if(status != null && DocumentStatus.CANCELED.getCode().equals(status.getCode())) {
540 	    				String requestDescription = "";
541 						if(StringUtils.isNotEmpty(this.getDescription())) {
542 							requestDescription = this.getDescription() + " <br/>";
543 						}
544 						String actionDateString = TKUtils.formatDate(lrd.getDocumentHeader().getWorkflowDocument().getDateFinalized().toLocalDate());
545 						requestDescription += "Approval deferred on " + actionDateString + ". Reason: " + lrd.getDescription();
546 			    		this.setPlanningDescription(requestDescription);
547 						return planningDescription;
548     				}
549     			}
550     		}
551     	}
552     	this.setPlanningDescription(this.getDescription());
553     	return planningDescription;
554     }
555 
556 	public void setPlanningDescription(String planningDescription) {
557 		this.planningDescription = planningDescription;
558 	}
559 
560 	public void setTransactionDocId(String documentHeaderId) {
561 		transactionalDocId = documentHeaderId;		
562 	}
563 	
564 	public String getTransactionalDocId() {
565 		return transactionalDocId;
566 	}
567 
568 	public DateTime getBeginDateTime() {
569 		return beginTimestamp != null ? new DateTime(beginTimestamp) : null;
570 	}
571 	
572 	public void setBeginDateTime(DateTime beginDateTime) {
573 		beginTimestamp = beginDateTime != null ? new Timestamp(beginDateTime.getMillis()) : null;
574 	}
575 	
576 	
577 	public Date getBeginDate() {
578 	    	Date beginDate = null;	    	
579 	    	if (beginTimestamp != null) {
580 	    		beginDate = new Date(beginTimestamp.getTime());
581 	    	}
582 	        return beginDate;
583 	}
584 
585 	public Timestamp getBeginTimestampVal() {
586 	        return new Timestamp(beginTimestamp.getTime());
587 	}
588 
589 	public Timestamp getEndTimestampVal() {
590 	        return new Timestamp(endTimestamp.getTime());
591 	}
592 
593 	public void setBeginDate(Date beginDate) {
594 	    	DateTime dateTime = new DateTime(beginTimestamp);
595 	    	LocalDate localDate = new LocalDate(beginDate);
596 	    	LocalTime localTime = new LocalTime(beginTimestamp);
597 	    	beginTimestamp = new Timestamp(localDate.toDateTime(localTime, dateTime.getZone()).getMillis());
598 	}
599 	
600 	public DateTime getEndDateTime() {
601 		return endTimestamp != null ? new DateTime(endTimestamp) : null;
602 	}
603 	
604 	public void setEndDateTime(DateTime endDateTime) {
605 		endTimestamp = endDateTime != null ? new Timestamp(endDateTime.getMillis()) : null;
606 	}
607 
608 	public String getLmLeaveBlockId() {
609 		return lmLeaveBlockId;
610 	}
611 
612 	public void setLmLeaveBlockId(String lmLeaveBlockId) {
613 		this.lmLeaveBlockId = lmLeaveBlockId;
614 	}
615 
616 	public String getPrincipalIdModified() {
617 		return principalIdModified;
618 	}
619 
620 	public void setPrincipalIdModified(String principalIdModified) {
621 		this.principalIdModified = principalIdModified;
622 	}
623 
624 	public BigDecimal getLeaveAmount() {
625 		return leaveAmount;
626 	}
627 
628 	public void setLeaveAmount(BigDecimal leaveAmount) {
629 		this.leaveAmount = leaveAmount;
630 	}
631 
632 	public Long getWorkArea() {
633 		return workArea;
634 	}
635 
636 	public void setWorkArea(Long workArea) {
637 		this.workArea = workArea;
638 	}
639 
640 	public Long getJobNumber() {
641 		return jobNumber;
642 	}
643 
644 	public void setJobNumber(Long jobNumber) {
645 		this.jobNumber = jobNumber;
646 	}
647 
648 	public Long getTask() {
649 		return task;
650 	}
651 
652 	public void setTask(Long task) {
653 		this.task = task;
654 	}
655 
656 	public String getEarnCode() {
657 		return earnCode;
658 	}
659 
660 	public void setEarnCode(String earnCode) {
661 		this.earnCode = earnCode;
662 	}
663 
664     public AccrualCategoryRule getAccrualCategoryRule() {
665         if (accrualCategoryRule == null) {
666             AccrualCategory category = getAccrualCategoryObj();
667             PrincipalHRAttributes pha = getPrincipalHRAttributes();
668             if (category != null && pha != null) {
669                 accrualCategoryRule = HrServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(category, getLeaveLocalDate(), pha.getServiceLocalDate());
670             }
671         }
672         return accrualCategoryRule;
673     }
674 
675     public AccrualCategory getAccrualCategoryObj() {
676         if (accrualCategoryObj == null) {
677             accrualCategoryObj = HrServiceLocator.getAccrualCategoryService().getAccrualCategory(accrualCategory, getLeaveLocalDate());
678         }
679         return accrualCategoryObj;
680     }
681 
682     @Override
683     public DateTime getCreateTime() {
684         return getTimestamp() == null ? null : new DateTime(getTimestamp().getTime());
685     }
686 
687     public PrincipalHRAttributes getPrincipalHRAttributes() {
688         if (principalHRAttributes == null) {
689             principalHRAttributes = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, getLeaveLocalDate());
690         }
691         return principalHRAttributes;
692     }
693 
694     public void setPrincipalHRAttributes(PrincipalHRAttributes principalHRAttributes) {
695         this.principalHRAttributes = principalHRAttributes;
696     }
697 
698     public String getUserPrincipalId() {
699 	    return userPrincipalId;
700 	}
701 
702 	public void setUserPrincipalId(String userPrincipalId) {
703 	   	this.userPrincipalId = userPrincipalId;
704 	}
705 
706 	public String getAffectPay() {
707 		return affectPay;
708 	}
709 
710 	public void setAffectPay(String affectPay) {
711 		this.affectPay = affectPay;
712 	}
713 
714     public void setAccrualCategoryRule(AccrualCategoryRule accrualCategoryRule) {
715         this.accrualCategoryRule = accrualCategoryRule;
716     }
717 
718     public void setAccrualCategoryObj(AccrualCategory accrualCategoryObj) {
719         this.accrualCategoryObj = accrualCategoryObj;
720     }
721 
722     @Override
723     public HrGroupKey getGroupKey() {
724         if (groupKey == null
725                 && getGroupKeyCode() != null) {
726             setGroupKey(HrServiceLocator.getHrGroupKeyService().getHrGroupKey(getGroupKeyCode(), getLeaveLocalDate()));
727         }
728         return groupKey;
729     }
730 
731     public static LeaveBlockBo from(LeaveBlock im) {
732         LeaveBlockBo lb = new LeaveBlockBo();
733         lb.setHrCalendarBlockId(im.getHrCalendarBlockId());
734         lb.setPrincipalId(im.getPrincipalId());
735         lb.setUserPrincipalId(im.getUserPrincipalId());
736         lb.setDocumentId(im.getDocumentId());
737         lb.setBeginTimestamp(im.getBeginDateTime() == null ? null : new Timestamp(im.getBeginDateTime().getMillis()));
738         lb.setEndTimestamp(im.getEndDateTime() == null ? null : new Timestamp((im.getEndDateTime().getMillis())));
739         lb.setTimestamp(im.getCreateTime() == null ? null : new Timestamp(im.getCreateTime().getMillis()));
740         lb.setLunchDeleted(im.isLunchDeleted());
741         lb.setHours(im.getHours());
742         lb.setAmount(im.getAmount());
743         lb.setOvertimePref(im.getOvertimePref());
744         lb.setEarnCode(im.getEarnCode());
745         lb.setWorkArea(im.getWorkArea());
746         lb.setJobNumber(im.getJobNumber());
747         lb.setTask(im.getTask());
748         lb.setConcreteBlockType(im.getConcreteBlockType());
749         lb.setConcreteBlockId(im.getConcreteBlockId());
750         lb.setLeaveAmount(im.getLeaveAmount());
751         lb.setLeaveDate(im.getLeaveDateTime() == null ? null : im.getLeaveDateTime().toDate());
752         lb.setDescription(im.getDescription());
753         lb.setScheduleTimeOffId(im.getScheduleTimeOffId());
754         lb.setAccrualCategory(im.getAccrualCategory());
755         lb.setAccrualGenerated(im.isAccrualGenerated());
756         lb.setBlockId(im.getBlockId());
757         lb.setRequestStatus(im.getRequestStatus());
758         lb.setLeaveBlockType(im.getLeaveBlockType());
759         lb.setDocumentStatus(im.getDocumentStatus());
760         lb.setPrincipalIdModified(im.getUserPrincipalId());
761         lb.setLeaveRequestDocumentId(im.getLeaveRequestDocumentId());
762 
763         lb.setLmLeaveBlockId(im.getLmLeaveBlockId());
764 
765         lb.setBeginDate(im.getBeginDateTime() == null ? null : im.getBeginDateTime().toDate());
766         lb.setSubmit(im.isSubmit());
767         lb.setReason(im.getReason());
768 
769         lb.setAssignmentKey(im.getAssignmentKey());
770 
771         lb.setCalendarId(im.getCalendarId());
772         lb.setPlanningDescription(im.getPlanningDescription());
773 
774         lb.setAccrualCategoryRule(im.getAccrualCategoryRule());
775         lb.setAccrualCategoryObj(im.getAccrualCategoryObj());
776         lb.setGroupKeyCode(im.getGroupKeyCode());
777         lb.setGroupKey(im.getGroupKey());
778         lb.setAffectPay(im.getAffectPay());
779         lb.setTransactionDocId(im.getTransactionalDocId());
780         lb.setObjectId(im.getObjectId());
781         lb.setVersionNumber(im.getVersionNumber());
782 
783         return lb;
784     }
785 
786     public static LeaveBlock to(LeaveBlockBo bo) {
787         if (bo == null) {
788             return null;
789         }
790 
791         return LeaveBlock.Builder.create(bo).build();
792     }
793 }