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.kpme.core.job.service;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.log4j.Logger;
26  import org.joda.time.LocalDate;
27  import org.kuali.kpme.core.KPMENamespace;
28  import org.kuali.kpme.core.department.Department;
29  import org.kuali.kpme.core.job.Job;
30  import org.kuali.kpme.core.job.dao.JobDao;
31  import org.kuali.kpme.core.paytype.PayType;
32  import org.kuali.kpme.core.permission.KPMEPermissionTemplate;
33  import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
34  import org.kuali.kpme.core.service.HrServiceLocator;
35  import org.kuali.rice.kim.api.KimConstants;
36  import org.kuali.rice.kim.api.identity.Person;
37  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
38  
39  /**
40   * Represents an implementation of {@link JobService}.
41   */
42  public class JobServiceImpl implements JobService {
43  
44  	private static final Logger LOG = Logger.getLogger(JobServiceImpl.class);
45      private JobDao jobDao;
46  
47      @Override
48      public void saveOrUpdate(Job job) {
49          jobDao.saveOrUpdate(job);
50      }
51  
52      @Override
53      public void saveOrUpdate(List<Job> jobList) {
54          jobDao.saveOrUpdate(jobList);
55      }
56  
57      public void setJobDao(JobDao jobDao) {
58          this.jobDao = jobDao;
59      }
60  
61      @Override
62      public List<Job> getJobs(String principalId, LocalDate asOfDate) {
63          List<Job> jobs = jobDao.getJobs(principalId, asOfDate);
64  
65          for (Job job : jobs) {
66              PayType payType = HrServiceLocator.getPayTypeService().getPayType(
67                      job.getHrPayType(), asOfDate);
68              job.setPayTypeObj(payType);
69          }
70  
71          return jobs;
72      }
73  
74      @Override
75      public Job getJob(String principalId, Long jobNumber, LocalDate asOfDate) {
76          return getJob(principalId, jobNumber, asOfDate, true);
77      }
78  
79      public Job getPrimaryJob(String principalId, LocalDate payPeriodEndDate) {
80          return jobDao.getPrimaryJob(principalId, payPeriodEndDate);
81      }
82  
83      @Override
84      public Job getJob(String principalId, Long jobNumber, LocalDate asOfDate,
85                        boolean chkDetails) {
86          Job job = jobDao.getJob(principalId, jobNumber, asOfDate);
87          if (job == null && chkDetails) {
88              return null;
89              //throw new RuntimeException("No job for principal : " + principalId
90              //        + " Job Number: " + jobNumber);
91          }
92          if (chkDetails) {
93              String hrPayType = job.getHrPayType();
94              if (StringUtils.isBlank(hrPayType)) {
95  //                throw new RuntimeException("No pay type for this job!");
96              	LOG.warn("No pay type for this job!");
97                  return null;
98              }
99              PayType payType = HrServiceLocator.getPayTypeService().getPayType(
100                     hrPayType, asOfDate);
101             if (payType == null) {
102 //                throw new RuntimeException("No paytypes defined for this job!");
103             	LOG.warn("No paytypes defined for this job!");
104             	return null;
105             } else {
106             	job.setPayTypeObj(payType);
107             }
108         }
109         return job;
110     }
111 
112     @Override
113     public List<Job> getActiveJobsForPayType(String hrPayType, LocalDate asOfDate) {
114         return jobDao.getActiveJobsForPayType(hrPayType, asOfDate);
115     }
116 
117     @Override
118     public Job getJob(String hrJobId) {
119         return jobDao.getJob(hrJobId);
120     }
121 
122     @Override
123     public Job getMaxJob(String principalId) {
124         return jobDao.getMaxJob(principalId);
125     }
126 
127     @Override
128     public List<Job> getJobs(String userPrincipalId, String principalId, String firstName, String lastName, String jobNumber,
129                              String dept, String positionNbr, String payType,
130                              LocalDate fromEffdt, LocalDate toEffdt, String active, String showHistory) {
131     	List<Job> results = new ArrayList<Job>();
132     	
133     	List<Job> jobObjs = new ArrayList<Job>();
134     	
135         if (StringUtils.isNotEmpty(firstName) || StringUtils.isNotEmpty(lastName)) {
136             Map<String, String> fields = new HashMap<String, String>();
137             fields.put("firstName", firstName);
138             fields.put("lastName", lastName);
139             List<Person> people = KimApiServiceLocator.getPersonService().findPeople(fields);
140 
141             for (Person p : people) {
142                 List<Job> jobsForPerson = jobDao.getJobs(p.getPrincipalId(), jobNumber, dept, positionNbr, payType, fromEffdt, toEffdt, active, showHistory);
143                 jobObjs.addAll(jobsForPerson);
144             }
145         } else {
146         	jobObjs.addAll(jobDao.getJobs(principalId, jobNumber, dept, positionNbr, payType, fromEffdt, toEffdt, active, showHistory));
147         }
148         
149     	for (Job jobObj : jobObjs) {
150         	String department = jobObj.getDept();
151         	Department departmentObj = HrServiceLocator.getDepartmentService().getDepartment(department, jobObj.getEffectiveLocalDate());
152         	String location = departmentObj != null ? departmentObj.getLocation() : null;
153         	
154         	Map<String, String> roleQualification = new HashMap<String, String>();
155         	roleQualification.put(KimConstants.AttributeConstants.PRINCIPAL_ID, userPrincipalId);
156         	roleQualification.put(KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(), department);
157         	roleQualification.put(KPMERoleMemberAttribute.LOCATION.getRoleMemberAttributeName(), location);
158         	
159         	if (!KimApiServiceLocator.getPermissionService().isPermissionDefinedByTemplate(KPMENamespace.KPME_WKFLW.getNamespaceCode(),
160     				KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>())
161     		  || KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(userPrincipalId, KPMENamespace.KPME_WKFLW.getNamespaceCode(),
162     				  KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>(), roleQualification)) {
163         		results.add(jobObj);
164         	}
165     	}
166     	
167     	return results;
168     }
169     
170     public int getJobCount(String principalId, Long jobNumber, String dept) {
171     	return jobDao.getJobCount(principalId, jobNumber, dept);
172     }
173     
174     @Override
175     public List<Job> getActiveLeaveJobs(String principalId, LocalDate asOfDate) {
176     	return jobDao.getActiveLeaveJobs(principalId, asOfDate);
177     }
178     
179     @Override
180     public BigDecimal getFteSumForJobs(List<Job> jobs) {
181     	BigDecimal fteSum = new BigDecimal(0);
182     	for(Job aJob : jobs) {
183     		fteSum = fteSum.add(aJob.getFte());
184     	}
185     	return fteSum;
186     	
187     }
188     
189 	@Override
190 	public BigDecimal getFteSumForAllActiveLeaveEligibleJobs(String principalId, LocalDate asOfDate) {
191 		BigDecimal fteSum = new BigDecimal(0);
192 		List<Job> lmEligibleJobs = jobDao.getActiveLeaveJobs(principalId, asOfDate);
193 		for(Job job : lmEligibleJobs) {
194 			fteSum = fteSum.add(job.getFte());
195 		}
196 		return fteSum;
197 	}
198     
199     @Override
200     public BigDecimal getStandardHoursSumForJobs(List<Job> jobs) {
201     	BigDecimal hoursSum = new BigDecimal(0);
202     	for(Job aJob : jobs) {
203     		hoursSum = hoursSum.add(aJob.getStandardHours());
204     	}
205     	return hoursSum;
206     }
207    
208     @Override
209     public List<Job> getAllActiveLeaveJobs(String principalId, LocalDate asOfDate) {
210     	return jobDao.getAllActiveLeaveJobs(principalId, asOfDate);
211     }
212     
213     public List<Job> getInactiveLeaveJobs(Long jobNumber, LocalDate endDate) {
214     	return jobDao.getInactiveLeaveJobs(jobNumber, endDate);
215     }
216     
217     @Override
218     public List<Job> getAllInActiveLeaveJobsInRange(String principalId, LocalDate endDate) {
219     	return jobDao.getAllInActiveLeaveJobsInRange(principalId, endDate);
220     }
221     
222     @Override
223     public Job getMaxTimestampJob(String principalId) {
224     	return jobDao.getMaxTimestampJob(principalId);
225     }
226     
227     @Override
228     public List<String> getPrincipalIdsInPosition(String positionNumber, LocalDate asOfDate) {
229         return jobDao.getPrincipalIdsInPosition(positionNumber, asOfDate);
230     }
231     
232 }