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.time.assignment.service;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.hr.time.assignment.Assignment;
22  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
23  import org.kuali.hr.time.assignment.dao.AssignmentDao;
24  import org.kuali.hr.time.calendar.CalendarEntries;
25  import org.kuali.hr.time.service.base.TkServiceLocator;
26  import org.kuali.hr.time.timesheet.TimesheetDocument;
27  import org.kuali.hr.time.util.TKContext;
28  import org.kuali.hr.time.util.TKUtils;
29  
30  import java.sql.Date;
31  import java.util.*;
32  
33  public class AssignmentServiceImpl implements AssignmentService {
34  
35      private static final Logger LOG = Logger.getLogger(AssignmentServiceImpl.class);
36      private AssignmentDao assignmentDao;
37  
38      public AssignmentDao getAssignmentDao() {
39          return assignmentDao;
40      }
41  
42      public void setAssignmentDao(AssignmentDao assignmentDao) {
43          this.assignmentDao = assignmentDao;
44      }
45  
46  
47      @Override
48      public List<Assignment> getAssignments(String principalId, Date asOfDate) {
49          List<Assignment> assignments;
50  
51          if (asOfDate == null) {
52              asOfDate = TKUtils.getCurrentDate();
53          }
54  
55          assignments = assignmentDao.findAssignments(principalId, asOfDate);
56  
57          for (Assignment assignment : assignments) {
58              populateAssignment(assignment, asOfDate);
59          }
60  
61          return assignments;
62      }
63  
64      public List<Assignment> getAssignments(String principalId, Date beginDate, Date endDate) {
65          List<Assignment> assignments;
66  
67          assignments = assignmentDao.findAssignmentsWithinPeriod(principalId, beginDate, endDate);
68  
69          for (Assignment assignment : assignments) {
70              populateAssignment(assignment, assignment.getEffectiveDate());
71          }
72  
73          return assignments;
74      }
75  
76  
77      @Override
78      public List<Assignment> searchAssignments(Date fromEffdt, Date toEffdt, String principalId, String jobNumber,
79                                             String dept, String workArea, String active, String showHistory) {
80          return assignmentDao.searchAssignments(fromEffdt, toEffdt, principalId, jobNumber, dept, workArea, active, showHistory);
81      }
82  
83  
84      public List<Assignment> getAssignmentsByPayEntry(String principalId, CalendarEntries payCalendarEntry) {
85          List<Assignment> beginPeriodAssign = getAssignments(principalId, payCalendarEntry.getBeginPeriodDate());
86          List<Assignment> endPeriodAssign = getAssignments(principalId, payCalendarEntry.getEndPeriodDate());
87          List<Assignment> assignsWithPeriod = getAssignments(principalId, payCalendarEntry.getBeginPeriodDate(), payCalendarEntry.getEndPeriodDate());
88  
89          List<Assignment> finalAssignments = new ArrayList<Assignment>();
90          Map<String, Assignment> assignKeyToAssignmentMap = new HashMap<String, Assignment>();
91          for (Assignment assign : endPeriodAssign) {
92              assignKeyToAssignmentMap.put(TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask()), assign);
93              finalAssignments.add(assign);
94          }
95  
96          //Compare the begin and end and add any assignments to the end thats are not there
97          for (Assignment assign : beginPeriodAssign) {
98              String assignKey = TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask());
99              if (!assignKeyToAssignmentMap.containsKey(assignKey)) {
100                 finalAssignments.add(assign);
101             }
102         }
103 
104         // Add the assignments within the pay period
105         for (Assignment assign : assignsWithPeriod) {
106             String assignKey = TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask());
107             if (!assignKeyToAssignmentMap.containsKey(assignKey)) {
108                 finalAssignments.add(assign);
109             }
110         }
111 
112         return finalAssignments;
113 
114     }
115 
116     @Override
117     public AssignmentDescriptionKey getAssignmentDescriptionKey(String assignmentKey) {
118         return new AssignmentDescriptionKey(assignmentKey);
119     }
120 
121     @Override
122     public Map<String, String> getAssignmentDescriptions(TimesheetDocument td, boolean clockOnlyAssignments) {
123         if (td == null) {
124             throw new RuntimeException("timesheet document is null.");
125         }
126         List<Assignment> assignments = td.getAssignments();
127 //		if(assignments.size() < 1) {
128 //			throw new RuntimeException("No assignment on the timesheet document.");
129 //		}
130 
131         Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
132         for (Assignment assignment : assignments) {
133             //if the user is not the same as the timesheet and does not have approver access for the assignment
134             //do not add to the display
135             if (!StringUtils.equals(TKContext.getTargetPrincipalId(), TKContext.getPrincipalId())) {
136                 if (!TKContext.getUser().isSystemAdmin() && !TKContext.getUser().getReportingWorkAreas().contains(assignment.getWorkArea())) {
137                     continue;
138                 }
139             }
140 
141             //only add to the assignment list if they are synchronous assignments
142             //or clock only assignments is false
143             if (!clockOnlyAssignments || assignment.isSynchronous()) {
144                 assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
145             }
146         }
147 
148         return assignmentDescriptions;
149     }
150 
151     @Override
152     public Map<String, String> getAssignmentDescriptions(Assignment assignment) {
153         if (assignment == null) {
154             throw new RuntimeException("Assignment is null");
155         }
156 
157         Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
158         assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
159 
160         return assignmentDescriptions;
161 
162     }
163 
164     @Override
165     public Assignment getAssignment(TimesheetDocument timesheetDocument, String assignmentKey) {
166         List<Assignment> assignments = timesheetDocument.getAssignments();
167         AssignmentDescriptionKey desc = getAssignmentDescriptionKey(assignmentKey);
168 
169         for (Assignment assignment : assignments) {
170             if (assignment.getJobNumber().compareTo(desc.getJobNumber()) == 0 &&
171                     assignment.getWorkArea().compareTo(desc.getWorkArea()) == 0 &&
172                     assignment.getTask().compareTo(desc.getTask()) == 0) {
173                 return assignment;
174             }
175         }
176 
177         //No assignment found so fetch the inactive ones for this payBeginDate
178         Assignment assign = TkServiceLocator.getAssignmentService().getAssignment(desc, timesheetDocument.getPayCalendarEntry().getBeginPeriodDate());
179         if (assign != null) {
180             return assign;
181         }
182 
183 
184         LOG.warn("no matched assignment found");
185         return new Assignment();
186     }
187 
188     @Override
189     public Assignment getAssignment(String tkAssignmentId) {
190         return getAssignmentDao().getAssignment(tkAssignmentId);
191     }
192 
193 
194     @Override
195     public List<Assignment> getActiveAssignmentsForWorkArea(Long workArea, Date asOfDate) {
196         List<Assignment> assignments = assignmentDao.getActiveAssignmentsInWorkArea(workArea, asOfDate);
197         for (Assignment assignment : assignments) {
198             populateAssignment(assignment, asOfDate);
199         }
200         return assignments;
201     }
202 
203     @Override
204     public List<Assignment> getActiveAssignments(Date asOfDate) {
205         return assignmentDao.getActiveAssignments(asOfDate);
206     }
207 
208     private void populateAssignment(Assignment assignment, Date asOfDate) {
209         assignment.setJob(TkServiceLocator.getJobService().getJob(assignment.getPrincipalId(), assignment.getJobNumber(), asOfDate));
210         assignment.setTimeCollectionRule(TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(assignment.getJob().getDept(), assignment.getWorkArea(), assignment.getJob().getHrPayType(),asOfDate));
211         assignment.setWorkAreaObj(TkServiceLocator.getWorkAreaService().getWorkArea(assignment.getWorkArea(), asOfDate));
212         assignment.setDeptLunchRule(TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule(assignment.getJob().getDept(),
213                 assignment.getWorkArea(), assignment.getPrincipalId(), assignment.getJobNumber(), asOfDate));
214     }
215 
216     public Assignment getAssignment(String principalId, AssignmentDescriptionKey key, Date asOfDate) {
217         Assignment a = null;
218 
219         if (key != null) {
220             a = assignmentDao.getAssignment(principalId, key.getJobNumber(), key.getWorkArea(), key.getTask(), asOfDate);
221         }
222 
223         return a;
224     }
225 
226     @Override
227     public Assignment getAssignment(AssignmentDescriptionKey key, Date asOfDate) {
228         Assignment a = null;
229 
230         if (key != null) {
231             a = assignmentDao.getAssignment(key.getJobNumber(), key.getWorkArea(), key.getTask(), asOfDate);
232         }
233 
234         return a;
235     }
236 
237     /**
238      * KPME-1129 Kagata
239      * Get a list of active assignments based on principalId and jobNumber as of a particular date
240      */
241     @Override
242     public List<Assignment> getActiveAssignmentsForJob(String principalId, Long jobNumber, Date asOfDate) {
243         List<Assignment> assignments = assignmentDao.getActiveAssignmentsForJob(principalId, jobNumber, asOfDate);
244 
245         return assignments;
246     }
247 
248     @Override
249     public Assignment getMaxTimestampAssignment(String principalId) {
250     	return assignmentDao.getMaxTimestampAssignment(principalId);
251     }
252 
253 }