View Javadoc
1   /**
2    * Copyright 2004-2014 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.core.document.calendar;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.collections.MapUtils;
20  import org.apache.commons.lang.StringUtils;
21  import org.joda.time.LocalDate;
22  import org.kuali.kpme.core.api.assignment.Assignment;
23  import org.kuali.kpme.core.api.assignment.AssignmentDescriptionKey;
24  import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
25  import org.kuali.kpme.core.api.document.calendar.CalendarDocumentContract;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.core.util.TKUtils;
28  
29  import java.io.Serializable;
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.HashMap;
33  import java.util.HashSet;
34  import java.util.LinkedHashMap;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  
39  public abstract class CalendarDocument implements Serializable, CalendarDocumentContract{
40  	private static final long serialVersionUID = 6074564807962995821L;
41  	//private <T extends CalendarDocumentHeader> T documentHeader;
42  	private Map<LocalDate, List<Assignment>> assignments = new HashMap<LocalDate, List<Assignment>>();
43  	private CalendarEntry calendarEntry = null;
44  	private LocalDate asOfDate;
45  	private String calendarType;
46  	
47  	public abstract CalendarDocumentHeader getDocumentHeader();
48  
49  	//public abstract Map<LocalDate, List<Assignment>> getAssignmentMap();
50  
51      public List<Assignment> getAllAssignments() {
52          if (MapUtils.isEmpty(getAssignmentMap())) {
53              return Collections.emptyList();
54          }
55          Set<Assignment> allAssignments = new HashSet<Assignment>();
56          for (List<Assignment> assignList : getAssignmentMap().values()) {
57              allAssignments.addAll(assignList);
58          }
59          return new ArrayList<Assignment>(allAssignments);
60      }
61  
62      @Override
63      public CalendarEntry getCalendarEntry() {
64          return calendarEntry;
65      }
66  
67      public void setCalendarEntry(CalendarEntry calendarEntry) {
68          this.calendarEntry = calendarEntry;
69      }
70  
71      @Override
72      public LocalDate getAsOfDate(){
73          return getCalendarEntry().getBeginPeriodFullDateTime().toLocalDate();
74      }
75  
76      public LocalDate getDocEndDate(){
77          return getCalendarEntry().getEndPeriodFullDateTime().toLocalDate();
78      }
79  	
80  	public abstract String getDocumentId();
81  	
82  	public abstract String getPrincipalId();
83  
84      public String getCalendarType() {
85      	return calendarType;
86      }
87      
88      public void setCalendarType(String calendarType) {
89      	this.calendarType = calendarType;
90      }
91  
92      @Override
93      public Map<LocalDate, List<Assignment>> getAssignmentMap() {
94          return assignments;
95      }
96  
97      public void setAssignments(Map<LocalDate, List<Assignment>> assignments) {
98          this.assignments = assignments;
99      }
100 
101     public Map<String, String> getAssignmentDescriptions(LocalDate date) {
102         Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
103         List<Assignment> dayAssignments = getAssignmentMap().get(date);
104         if (CollectionUtils.isNotEmpty(dayAssignments)) {
105             for (Assignment assignment : dayAssignments) {
106                 assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
107             }
108         }
109         return assignmentDescriptions;
110     }
111     
112     public Assignment getAssignment(AssignmentDescriptionKey assignmentDescriptionKey, LocalDate date) {
113         List<Assignment> dayAssignments = getAssignmentMap().get(date);
114         if (CollectionUtils.isNotEmpty(dayAssignments)) {
115             for (Assignment assignment : dayAssignments) {
116                 if (StringUtils.equals(assignment.getGroupKeyCode(), assignmentDescriptionKey.getGroupKeyCode()) &&
117                         assignment.getJobNumber().compareTo(assignmentDescriptionKey.getJobNumber()) == 0 &&
118                         assignment.getWorkArea().compareTo(assignmentDescriptionKey.getWorkArea()) == 0 &&
119                         assignment.getTask().compareTo(assignmentDescriptionKey.getTask()) == 0) {
120                     return assignment;
121                 }
122             }
123         }
124 
125         //No assignment found so fetch the inactive ones for this payBeginDate
126         Assignment foundAssign = HrServiceLocator.getAssignmentService().getAssignmentForTargetPrincipal(assignmentDescriptionKey, date);
127         if (foundAssign != null) {
128             return foundAssign;
129         } else {
130             return null;
131         }
132     }
133 }