001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.lm.leaveblock;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    import java.sql.Timestamp;
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import javax.persistence.Transient;
025    
026    import org.apache.commons.collections.CollectionUtils;
027    import org.apache.commons.lang.builder.EqualsBuilder;
028    import org.apache.commons.lang.builder.HashCodeBuilder;
029    import org.apache.commons.lang3.StringUtils;
030    import org.joda.time.DateTime;
031    import org.kuali.hr.lm.LMConstants;
032    import org.kuali.hr.lm.accrual.AccrualCategory;
033    import org.kuali.hr.lm.accrual.AccrualCategoryRule;
034    import org.kuali.hr.lm.leaveplan.LeavePlan;
035    import org.kuali.hr.lm.timeoff.SystemScheduledTimeOff;
036    import org.kuali.hr.lm.workflow.LeaveRequestDocument;
037    import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
038    import org.kuali.hr.time.calendar.Calendar;
039    import org.kuali.hr.time.calendar.CalendarEntries;
040    import org.kuali.hr.time.earncode.EarnCode;
041    import org.kuali.hr.time.principal.PrincipalHRAttributes;
042    import org.kuali.hr.time.service.base.TkServiceLocator;
043    import org.kuali.hr.time.task.Task;
044    import org.kuali.hr.time.util.TKUtils;
045    import org.kuali.hr.time.util.TkConstants;
046    import org.kuali.hr.time.workarea.WorkArea;
047    import org.kuali.rice.kew.api.KewApiServiceLocator;
048    import org.kuali.rice.kew.api.document.DocumentStatus;
049    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
050    import org.kuali.rice.krad.util.ObjectUtils;
051    
052    public class LeaveBlock extends PersistableBusinessObjectBase {
053    
054            private static final long serialVersionUID = -8240826812581295376L;
055            
056            private String lmLeaveBlockId;
057            private Date leaveDate;
058            private String description;
059            private String principalId;
060            private String earnCode;
061            private String scheduleTimeOffId;
062            private String accrualCategory;
063            // private Boolean active;
064            private BigDecimal leaveAmount = new BigDecimal("0.0");
065            private String applyToYtdUsed;
066            private String documentId;
067            private String principalIdModified;
068            private Timestamp timestamp;
069            private Boolean accrualGenerated;
070            private Long blockId;
071            private String tkAssignmentId;
072            private String requestStatus;
073            private String leaveBlockType;
074            private String documentStatus;
075            
076            private List<LeaveBlockHistory> leaveBlockHistories = new ArrayList<LeaveBlockHistory>();
077            private SystemScheduledTimeOff systemScheduledTimeOffObj;
078            private AccrualCategory accrualCategoryObj;
079        private String leaveRequestDocumentId;
080        
081        private Timestamp beginTimestamp;
082        private Timestamp endTimestamp;
083        
084            @Transient
085            private boolean submit;
086            @Transient
087            private String reason;
088            @Transient
089            private Timestamp dateAndTime;
090    
091            private Long workArea;
092            private Long jobNumber;
093            private Long task;
094            private String assignmentKey;
095            
096            @Transient
097            private String assignmentTitle;
098            @Transient
099            private String calendarId;
100            @Transient
101            private String planningDescription;
102    
103            private String transactionalDocId;
104    
105            public String getAccrualCategoryRuleId() {
106                    AccrualCategory category = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(accrualCategory, leaveDate);
107                    PrincipalHRAttributes pha = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, leaveDate);
108                    AccrualCategoryRule aRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(category, leaveDate, pha.getServiceDate());
109                    return ObjectUtils.isNull(aRule) ? null : aRule.getLmAccrualCategoryRuleId();
110            }
111            
112            public static class Builder {
113    
114                    // required parameters for the constructor
115                    private final Date leaveDate;
116                    private final String principalId;
117                    private final String documentId;
118                    private final String earnCode;
119                    private final BigDecimal leaveAmount;
120    
121                    private String description = null;
122                    private String applyToYtdUsed = null;
123                    private String principalIdModified = null;
124                    private Timestamp timestamp = null;
125                    private Boolean accrualGenerated = Boolean.FALSE;
126                    private Long blockId = 0L;
127                    private String scheduleTimeOffId;
128                    private String accrualCategory;
129                    private String tkAssignmentId;
130                    private String requestStatus;
131                    private Long workArea;
132                    private Long jobNumber;
133                    private Long task;
134                    private String leaveBlockType;
135                    
136                    public Builder(DateTime leaveBlockDate, String documentId,
137                                    String principalId, String earnCode, BigDecimal leaveAmount) {
138                            this.leaveDate = new java.sql.Date(leaveBlockDate.toDate()
139                                            .getTime());
140                            this.documentId = documentId;
141                            this.principalId = principalId;
142                            this.earnCode = earnCode;
143                            this.leaveAmount = leaveAmount;
144                    }
145    
146                    // validations could be done in the builder methods below
147    
148                    public Builder description(String val) {
149                            this.description = val;
150                            return this;
151                    }
152    
153                    public Builder applyToYtdUsed(String val) {
154                            this.applyToYtdUsed = val;
155                            return this;
156                    }
157    
158                    public Builder principalIdModified(String val) {
159                            this.principalIdModified = val;
160                            return this;
161                    }
162    
163                    public Builder timestamp(Timestamp val) {
164                            this.timestamp = val;
165                            return this;
166                    }
167    
168                    public Builder accrualGenerated(Boolean val) {
169                            this.accrualGenerated = val;
170                            return this;
171                    }
172    
173                    public Builder blockId(Long val) {
174                            this.blockId = val;
175                            return this;
176                    }
177    
178                    // TODO: need to hook up the objcets to get the real ids
179                    public Builder scheduleTimeOffId(String val) {
180                            this.scheduleTimeOffId = val;
181                            return this;
182                    }
183    
184                    public Builder accrualCategory(String val) {
185                            this.accrualCategory = val;
186                            return this;
187                    }
188    
189                    public Builder tkAssignmentId(String val) {
190                            this.tkAssignmentId = val;
191                            return this;
192                    }
193    
194                    public Builder workArea(Long val) {
195                            this.workArea = val;
196                            return this;
197                    }
198    
199                    public Builder jobNumber(Long val) {
200                            this.jobNumber = val;
201                            return this;
202                    }
203    
204                    public Builder task(Long val) {
205                            this.task = val;
206                            return this;
207                    }
208    
209                    public Builder requestStatus(String val) {
210                            this.requestStatus = val;
211                            return this;
212                    }
213                    
214                    public Builder leaveBlockType(String leaveBlockType) {
215                            this.leaveBlockType = leaveBlockType;
216                            return this;
217                    }
218    
219                    public LeaveBlock build() {
220                            return new LeaveBlock(this);
221                    }
222    
223            }
224    
225            private LeaveBlock(Builder builder) {
226                    leaveDate = builder.leaveDate;
227                    description = builder.description;
228                    principalId = builder.principalId;
229                    earnCode = builder.earnCode;
230                    leaveAmount = builder.leaveAmount;
231                    applyToYtdUsed = builder.applyToYtdUsed;
232                    documentId = builder.documentId;
233                    principalIdModified = builder.principalIdModified;
234                    timestamp = builder.timestamp;
235                    accrualGenerated = builder.accrualGenerated;
236                    blockId = builder.blockId;
237                    scheduleTimeOffId = builder.scheduleTimeOffId;
238                    accrualCategory = builder.accrualCategory;
239                    tkAssignmentId = builder.tkAssignmentId;
240                    requestStatus = builder.requestStatus;
241                    workArea = builder.workArea;
242                    jobNumber = builder.jobNumber;
243                    task = builder.task;
244                    leaveBlockType = builder.leaveBlockType;
245                    // TODO: need to hook up leaveCodeObj, systemScheduledTimeOffObj,
246                    // accrualCategoryObj, and ids for individual obj
247            }
248    
249            public LeaveBlock() {
250    
251            }
252    
253            public String getAccrualCategory() {
254                    return accrualCategory;
255            }
256    
257            public void setAccrualCategory(String accrualCategory) {
258                    this.accrualCategory = accrualCategory;
259            }
260    
261            public Boolean getAccrualGenerated() {
262                    return accrualGenerated;
263            }
264    
265            public void setAccrualGenerated(Boolean accrualGenerated) {
266                    this.accrualGenerated = accrualGenerated;
267            }
268    
269            // public Boolean getActive() {
270            // return active;
271            // }
272            //
273            // public void setActive(Boolean active) {
274            // this.active = active;
275            // }
276    
277            public String getApplyToYtdUsed() {
278                    return applyToYtdUsed;
279            }
280    
281            public boolean getSubmit() {
282                    return submit;
283            }
284    
285            public void setSubmit(boolean submit) {
286                    this.submit = submit;
287            }
288    
289            public void setApplyToYtdUsed(String applyToYtdUsed) {
290                    this.applyToYtdUsed = applyToYtdUsed;
291            }
292    
293            public Long getBlockId() {
294                    return blockId;
295            }
296    
297            public void setBlockId(Long blockId) {
298                    this.blockId = blockId;
299            }
300    
301            public String getDescription() {
302                    return description;
303            }
304    
305            public void setDescription(String description) {
306                    this.description = description;
307            }
308    
309            public String getDocumentId() {
310                    return documentId;
311            }
312    
313            public void setDocumentId(String documentId) {
314                    this.documentId = documentId;
315            }
316    
317            public BigDecimal getLeaveAmount() {
318                    return leaveAmount;
319            }
320    
321            public void setLeaveAmount(BigDecimal leaveAmount) {
322                    this.leaveAmount = leaveAmount;
323            }
324    
325            public Date getLeaveDate() {
326                    return leaveDate;
327            }
328    
329            public void setLeaveDate(Date leaveDate) {
330                    this.leaveDate = leaveDate;
331            }
332    
333            public String getLmLeaveBlockId() {
334                    return lmLeaveBlockId;
335            }
336    
337            public void setLmLeaveBlockId(String lmLeaveBlockId) {
338                    this.lmLeaveBlockId = lmLeaveBlockId;
339            }
340    
341            public String getPrincipalIdModified() {
342                    return principalIdModified;
343            }
344    
345            public void setPrincipalIdModified(String principalIdModified) {
346                    this.principalIdModified = principalIdModified;
347            }
348    
349            public String getPrincipalId() {
350                    return principalId;
351            }
352    
353            public void setPrincipalId(String principalId) {
354                    this.principalId = principalId;
355            }
356    
357            public String getScheduleTimeOffId() {
358                    return scheduleTimeOffId;
359            }
360    
361            public void setScheduleTimeOffId(String scheduleTimeOffId) {
362                    this.scheduleTimeOffId = scheduleTimeOffId;
363            }
364    
365            public Timestamp getTimestamp() {
366                    return timestamp;
367            }
368    
369            public void setTimestamp(Timestamp timestamp) {
370                    this.timestamp = timestamp;
371            }
372    
373            public AccrualCategory getAccrualCategoryObj() {
374                    return accrualCategoryObj;
375            }
376    
377            public void setAccrualCategoryObj(AccrualCategory accrualCategoryObj) {
378                    this.accrualCategoryObj = accrualCategoryObj;
379            }
380    
381            public SystemScheduledTimeOff getSystemScheduledTimeOffObj() {
382                    return systemScheduledTimeOffObj;
383            }
384    
385            public void setSystemScheduledTimeOffObj(
386                            SystemScheduledTimeOff systemScheduledTimeOffObj) {
387                    this.systemScheduledTimeOffObj = systemScheduledTimeOffObj;
388            }
389    
390            public String getTkAssignmentId() {
391                    return tkAssignmentId;
392            }
393    
394            public void setTkAssignmentId(String tkAssignmentId) {
395                    this.tkAssignmentId = tkAssignmentId;
396            }
397    
398            public String getRequestStatus() {
399                    return requestStatus;
400            }
401    
402            public void setRequestStatus(String requestStatus) {
403                    this.requestStatus = requestStatus;
404            }
405    
406        public String getRequestStatusString() {
407            String status = LMConstants.REQUEST_STATUS_STRINGS.get(getRequestStatus());
408            return status == null ? "usage" : status;
409        }
410    
411            public List<LeaveBlockHistory> getLeaveBlockHistories() {
412                    return leaveBlockHistories;
413            }
414    
415            public void setLeaveBlockHistories(
416                            List<LeaveBlockHistory> leaveBlockHistories) {
417                    this.leaveBlockHistories = leaveBlockHistories;
418            }
419    
420            public String getReason() {
421                    return reason;
422            }
423    
424            public void setReason(String reason) {
425                    this.reason = reason;
426            }
427    
428            public Timestamp getDateAndTime() {
429                    return dateAndTime;
430            }
431    
432            public void setDateAndTime(Timestamp dateAndTime) {
433                    this.dateAndTime = dateAndTime;
434            }
435    
436            public Long getWorkArea() {
437                    return workArea;
438            }
439    
440            public void setWorkArea(Long workArea) {
441                    this.workArea = workArea;
442            }
443    
444            public Long getJobNumber() {
445                    return jobNumber;
446            }
447    
448            public void setJobNumber(Long jobNumber) {
449                    this.jobNumber = jobNumber;
450            }
451    
452            public Long getTask() {
453                    return task;
454            }
455    
456            public void setTask(Long task) {
457                    this.task = task;
458            }
459    
460            public String getAssignmentTitle() {
461                    StringBuilder b = new StringBuilder();
462    
463                    if (this.workArea != null) {
464                            WorkArea wa = TkServiceLocator.getWorkAreaService().getWorkArea(
465                                            this.workArea, TKUtils.getCurrentDate());
466                            if (wa != null) {
467                                    b.append(wa.getDescription());
468                            }
469                            Task task = TkServiceLocator.getTaskService().getTask(
470                                            this.getTask(), this.getLeaveDate());
471                            if (task != null) {
472                                    // do not display task description if the task is the default
473                                    // one
474                                    // default task is created in getTask() of TaskService
475                                    if (!task.getDescription()
476                                                    .equals(TkConstants.TASK_DEFAULT_DESP)) {
477                                            b.append("-" + task.getDescription());
478                                    }
479                            }
480                    }
481                    return b.toString();
482            }
483    
484            public void setAssignmentTitle(String assignmentTitle) {
485                    this.assignmentTitle = assignmentTitle;
486            }
487    
488            public String getCalendarId() {
489                    PrincipalHRAttributes principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(this.principalId, TKUtils.getCurrentDate());
490                    Calendar pcal= null;
491                    if(principalHRAttributes != null) {
492                            //pcal = principalHRAttributes.getCalendar() != null ? principalHRAttributes.getCalendar() : principalHRAttributes.getLeaveCalObj() ;
493                            pcal = principalHRAttributes.getLeaveCalObj() != null ? principalHRAttributes.getLeaveCalObj() : principalHRAttributes.getCalendar();
494                if(pcal!= null) {
495                                    CalendarEntries calEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(pcal.getHrCalendarId(), this.leaveDate);
496                                    if(calEntries != null) {
497                                            this.calendarId = calEntries.getHrCalendarEntriesId();
498                                    }
499                            }
500                    }
501                    return calendarId;
502            }
503    
504            public void setCalendarId(String calendarId) {
505                    this.calendarId = calendarId;
506            }
507    
508            public String getEarnCode() {
509                    return earnCode;
510            }
511    
512            public void setEarnCode(String earnCode) {
513                    this.earnCode = earnCode;
514            }
515            
516            public String getEarnCodeDescription() {
517                    String earnCodeDescription = "";
518                    
519                    EarnCode earnCodeObj = TkServiceLocator.getEarnCodeService().getEarnCode(earnCode, leaveDate);
520                    if (earnCodeObj != null) {
521                            earnCodeDescription = earnCodeObj.getDescription();
522                    }
523                    
524                    return earnCodeDescription;
525            }
526    
527            public String getLeaveBlockType() {
528                    return leaveBlockType;
529            }
530    
531            public void setLeaveBlockType(String leaveBlockType) {
532                    this.leaveBlockType = leaveBlockType;
533            }
534    
535        public boolean isEditable() {
536            return TkServiceLocator.getPermissionsService().canEditLeaveBlock(this);
537        }
538    
539        public boolean isDeletable() {
540            return TkServiceLocator.getPermissionsService().canDeleteLeaveBlock(this);
541        }
542        
543        public String getAssignmentKey() {
544            if (assignmentKey == null) {
545                this.setAssignmentKey(TKUtils.formatAssignmentKey(jobNumber, workArea, task));
546            }
547            return assignmentKey;
548        }
549        public void setAssignmentKey(String assignmentDescription) {
550            this.assignmentKey = assignmentDescription;
551        }
552    
553            public String getDocumentStatus() {
554                    return documentStatus;
555            }
556    
557            public void setDocumentStatus(String documentStatus) {
558                    this.documentStatus = documentStatus;
559            }
560    
561        public String getLeaveRequestDocumentId() {
562            return leaveRequestDocumentId;
563        }
564    
565        public void setLeaveRequestDocumentId(String leaveRequestDocumentId) {
566            this.leaveRequestDocumentId = leaveRequestDocumentId;
567        }
568    
569        @Override
570        public int hashCode() {
571            return new HashCodeBuilder()
572                    .append(this.principalId)
573                    .append(this.jobNumber)
574                    .append(this.workArea)
575                    .append(this.task)
576                    .append(this.earnCode)
577                    .append(this.leaveDate)
578                    .append(this.leaveAmount)
579                    .append(this.accrualCategory)
580                    .append(this.earnCode)
581                    .append(this.description)
582                    .append(this.leaveBlockType)
583                    .toHashCode();
584        }
585    
586        @Override
587        public boolean equals(Object obj) {
588            if (obj == null) {
589                return false;
590            }
591            if (obj == this) {
592                return true;
593            }
594            if (obj.getClass() != getClass()) {
595                return false;
596            }
597            LeaveBlock leaveBlock = (LeaveBlock) obj;
598            return new EqualsBuilder()
599                    .append(principalId, leaveBlock.principalId)
600                    .append(jobNumber, leaveBlock.jobNumber)
601                    .append(workArea, leaveBlock.workArea)
602                    .append(task, leaveBlock.task)
603                    .append(earnCode, leaveBlock.earnCode)
604                    .append(leaveDate, leaveBlock.leaveDate)
605                    .append(leaveAmount, leaveBlock.leaveAmount)
606                    .append(accrualCategory, leaveBlock.accrualCategory)
607                    .append(earnCode, leaveBlock.earnCode)
608                    .append(description, leaveBlock.description)
609                    .append(leaveBlockType, leaveBlock.leaveBlockType)
610                    .isEquals();
611        }
612        
613        public String getPlanningDescription() {
614            if(this.getRequestStatus().equals(LMConstants.REQUEST_STATUS.DEFERRED)) {
615                    List<LeaveRequestDocument> lrdList = TkServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocumentsByLeaveBlockId(this.getLmLeaveBlockId());
616                    if(CollectionUtils.isNotEmpty(lrdList)) {
617                            for(LeaveRequestDocument lrd : lrdList) {                               
618                                    DocumentStatus status = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(lrd.getDocumentNumber());
619                                    if(status != null && DocumentStatus.CANCELED.getCode().equals(status.getCode())) {
620                                            String requestDescription = "";
621                                                    if(StringUtils.isNotEmpty(this.getDescription())) {
622                                                            requestDescription = this.getDescription() + " <br/>";
623                                                    }
624                                                    String actionDateString = TKUtils.formatDate(new Date(lrd.getDocumentHeader().getWorkflowDocument().getDateFinalized().getMillis()));
625                                                    requestDescription += "Approval deferred on " + actionDateString + ". Reason: " + lrd.getDescription();
626                                            this.setPlanningDescription(requestDescription);
627                                                    return planningDescription;
628                                    }
629                            }
630                    }
631            }
632            this.setPlanningDescription(this.getDescription());
633            return planningDescription;
634        }
635    
636            public void setPlanningDescription(String planningDescription) {
637                    this.planningDescription = planningDescription;
638            }
639    
640            public void setTransactionDocId(String documentHeaderId) {
641                    transactionalDocId = documentHeaderId;          
642            }
643            
644            public String getTransactionalDocId() {
645                    return transactionalDocId;
646            }
647    
648            public Timestamp getBeginTimestamp() {
649                    return beginTimestamp;
650            }
651    
652            public void setBeginTimestamp(Timestamp beginTimestamp) {
653                    this.beginTimestamp = beginTimestamp;
654            }
655    
656            public Timestamp getEndTimestamp() {
657                    return endTimestamp;
658            }
659    
660            public void setEndTimestamp(Timestamp endTimestamp) {
661                    this.endTimestamp = endTimestamp;
662            }
663    }
664    
665