001 /**
002 * Copyright 2004-2012 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.time.assignment.service;
017
018 import org.apache.commons.collections.CollectionUtils;
019 import org.apache.commons.lang.StringUtils;
020 import org.apache.log4j.Logger;
021 import org.kuali.hr.time.assignment.Assignment;
022 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
023 import org.kuali.hr.time.assignment.dao.AssignmentDao;
024 import org.kuali.hr.time.calendar.CalendarEntries;
025 import org.kuali.hr.time.service.base.TkServiceLocator;
026 import org.kuali.hr.time.timesheet.TimesheetDocument;
027 import org.kuali.hr.time.util.TKContext;
028 import org.kuali.hr.time.util.TKUtils;
029
030 import java.sql.Date;
031 import java.util.*;
032
033 public class AssignmentServiceImpl implements AssignmentService {
034
035 private static final Logger LOG = Logger.getLogger(AssignmentServiceImpl.class);
036 private AssignmentDao assignmentDao;
037
038 public AssignmentDao getAssignmentDao() {
039 return assignmentDao;
040 }
041
042 public void setAssignmentDao(AssignmentDao assignmentDao) {
043 this.assignmentDao = assignmentDao;
044 }
045
046
047 @Override
048 public List<Assignment> getAssignments(String principalId, Date asOfDate) {
049 List<Assignment> assignments;
050
051 if (asOfDate == null) {
052 asOfDate = TKUtils.getCurrentDate();
053 }
054
055 assignments = assignmentDao.findAssignments(principalId, asOfDate);
056
057 for (Assignment assignment : assignments) {
058 populateAssignment(assignment, asOfDate);
059 }
060
061 return assignments;
062 }
063
064 public List<Assignment> getAssignments(String principalId, Date beginDate, Date endDate) {
065 List<Assignment> assignments;
066
067 assignments = assignmentDao.findAssignmentsWithinPeriod(principalId, beginDate, endDate);
068
069 for (Assignment assignment : assignments) {
070 populateAssignment(assignment, assignment.getEffectiveDate());
071 }
072
073 return assignments;
074 }
075
076
077 @Override
078 public List<Assignment> searchAssignments(Date fromEffdt, Date toEffdt, String principalId, String jobNumber,
079 String dept, String workArea, String active, String showHistory) {
080 return assignmentDao.searchAssignments(fromEffdt, toEffdt, principalId, jobNumber, dept, workArea, active, showHistory);
081 }
082
083
084 public List<Assignment> getAssignmentsByPayEntry(String principalId, CalendarEntries payCalendarEntry) {
085 List<Assignment> beginPeriodAssign = getAssignments(principalId, payCalendarEntry.getBeginPeriodDate());
086 List<Assignment> endPeriodAssign = getAssignments(principalId, payCalendarEntry.getEndPeriodDate());
087 List<Assignment> assignsWithPeriod = getAssignments(principalId, payCalendarEntry.getBeginPeriodDate(), payCalendarEntry.getEndPeriodDate());
088
089 List<Assignment> finalAssignments = new ArrayList<Assignment>();
090 Map<String, Assignment> assignKeyToAssignmentMap = new HashMap<String, Assignment>();
091 for (Assignment assign : endPeriodAssign) {
092 assignKeyToAssignmentMap.put(TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask()), assign);
093 finalAssignments.add(assign);
094 }
095
096 //Compare the begin and end and add any assignments to the end thats are not there
097 for (Assignment assign : beginPeriodAssign) {
098 String assignKey = TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask());
099 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 }