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.job.service;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.hr.job.Job;
020    import org.kuali.hr.job.dao.JobDao;
021    import org.kuali.hr.time.paytype.PayType;
022    import org.kuali.hr.time.service.base.TkServiceLocator;
023    import org.kuali.rice.kim.api.identity.Person;
024    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
025    
026    import java.math.BigDecimal;
027    import java.util.*;
028    
029    /**
030     * Represents an implementation of {@link JobService}.
031     */
032    public class JobServiceImpl implements JobService {
033    
034        private JobDao jobDao;
035    
036        @Override
037        public void saveOrUpdate(Job job) {
038            jobDao.saveOrUpdate(job);
039        }
040    
041        @Override
042        public void saveOrUpdate(List<Job> jobList) {
043            jobDao.saveOrUpdate(jobList);
044        }
045    
046        public void setJobDao(JobDao jobDao) {
047            this.jobDao = jobDao;
048        }
049    
050        @Override
051        public List<Job> getJobs(String principalId, Date asOfDate) {
052            List<Job> jobs = jobDao.getJobs(principalId, asOfDate);
053    
054            for (Job job : jobs) {
055                PayType payType = TkServiceLocator.getPayTypeService().getPayType(
056                        job.getHrPayType(), asOfDate);
057                job.setPayTypeObj(payType);
058            }
059    
060            return jobs;
061        }
062    
063        @Override
064        public Job getJob(String principalId, Long jobNumber, Date asOfDate) {
065            return getJob(principalId, jobNumber, asOfDate, true);
066        }
067    
068        public Job getPrimaryJob(String principalId, Date payPeriodEndDate) {
069            return jobDao.getPrimaryJob(principalId, payPeriodEndDate);
070        }
071    
072        @Override
073        public Job getJob(String principalId, Long jobNumber, Date asOfDate,
074                          boolean chkDetails) {
075            Job job = jobDao.getJob(principalId, jobNumber, asOfDate);
076            if (job == null && chkDetails) {
077                return null;
078                //throw new RuntimeException("No job for principal : " + principalId
079                //        + " Job Number: " + jobNumber);
080            }
081            if (chkDetails) {
082                String hrPayType = job.getHrPayType();
083                if (StringUtils.isBlank(hrPayType)) {
084                    throw new RuntimeException("No pay type for this job!");
085                }
086                PayType payType = TkServiceLocator.getPayTypeService().getPayType(
087                        hrPayType, asOfDate);
088                if (payType == null)
089                    throw new RuntimeException("No paytypes defined for this job!");
090                job.setPayTypeObj(payType);
091            }
092            return job;
093        }
094    
095        @Override
096        public List<Job> getActiveJobsForPosition(String positionNbr, Date asOfDate) {
097            return jobDao.getActiveJobsForPosition(positionNbr, asOfDate);
098        }
099    
100        @Override
101        public List<Job> getActiveJobsForPayType(String hrPayType, Date asOfDate) {
102            return jobDao.getActiveJobsForPayType(hrPayType, asOfDate);
103        }
104    
105        @Override
106        public Job getJob(String hrJobId) {
107            return jobDao.getJob(hrJobId);
108        }
109    
110        @Override
111        public Job getMaxJob(String principalId) {
112            return jobDao.getMaxJob(principalId);
113        }
114    
115        @Override
116        public List<Job> getJobs(String principalId, String firstName, String lastName, String jobNumber,
117                                 String dept, String positionNbr, String payType,
118                                 java.sql.Date fromEffdt, java.sql.Date toEffdt, String active, String showHistory) {
119    
120            if (StringUtils.isNotEmpty(firstName) || StringUtils.isNotEmpty(lastName)) {
121                Map<String, String> fields = new HashMap<String, String>();
122                fields.put("firstName", firstName);
123                fields.put("lastName", lastName);
124                List<Person> people = KimApiServiceLocator.getPersonService().findPeople(fields);
125    
126                List<Job> jobs = new ArrayList<Job>();
127                for (Person p : people) {
128                    List<Job> jobsForPerson = jobDao.getJobs(p.getPrincipalId(), jobNumber, dept, positionNbr, payType, fromEffdt, toEffdt, active, showHistory);
129                    jobs.addAll(jobsForPerson);
130                }
131    
132                return jobs;
133            }
134    
135            return jobDao.getJobs(principalId, jobNumber, dept, positionNbr, payType, fromEffdt, toEffdt, active, showHistory);
136        }
137        
138        public int getJobCount(String principalId, Long jobNumber, String dept) {
139            return jobDao.getJobCount(principalId, jobNumber, dept);
140        }
141        
142        @Override
143        public BigDecimal getStandardHoursSumForJobs(List<Job> jobs) {
144            BigDecimal hoursSum = new BigDecimal(0);
145            for(Job aJob : jobs) {
146                    hoursSum = hoursSum.add(aJob.getStandardHours());
147            }
148            return hoursSum;
149        }
150    
151    }