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 java.util.Map;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.hr.job.Job;
022    import org.kuali.hr.time.HrBusinessObject;
023    import org.kuali.hr.time.service.base.TkServiceLocator;
024    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
025    import org.kuali.rice.kim.api.identity.Person;
026    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
027    import org.kuali.rice.kns.document.MaintenanceDocument;
028    
029    /**
030     * Hooks in to Rice to over-ride the way we are saving our Business Objects.  We
031     * treat our business objects as immutable, the default Rice behavior is to modify
032     * existing rows in the database.
033     */
034    public class JobMaintainableImpl extends HrBusinessObjectMaintainableImpl {
035    
036            /**
037             * 
038             */
039            private static final long serialVersionUID = 1L;
040    
041            
042            public void setJobNumber(Job job) {
043                    Long jobNumber = new Long("0");
044                    Job maxJob = TkServiceLocator.getJobService().getMaxJob(job.getPrincipalId());
045                    
046                    if(maxJob != null) {
047                            // get the max of job number of the collection
048                            jobNumber = maxJob.getJobNumber() +1;
049                    }               
050                    job.setJobNumber(jobNumber);
051            }
052            /**
053             * Override to populate user information in Maintenance page
054             */
055            @SuppressWarnings("rawtypes")
056            @Override
057            public Map populateBusinessObject(Map<String, String> fieldValues,
058                            MaintenanceDocument maintenanceDocument, String methodToCall) {
059                    if (fieldValues.containsKey("principalId")
060                                    && StringUtils.isNotEmpty(fieldValues.get("principalId"))) {
061                            Person p = KimApiServiceLocator.getPersonService().getPerson(
062                                            fieldValues.get("principalId"));
063                            if (p != null) {
064                                    fieldValues.put("name", p.getName());
065                            }else{
066                                    fieldValues.put("name", "");
067                            }
068                    }
069                    if(StringUtils.equals(getMaintenanceAction(),"New")){
070                            if (!fieldValues.containsKey("jobNumber") || StringUtils.isEmpty(fieldValues.get("jobNumber"))) {
071                                    if (fieldValues.containsKey("principalId") && StringUtils.isNotEmpty(fieldValues.get("principalId"))) {
072                                            Job maxJob = TkServiceLocator.getJobService().getMaxJob(fieldValues.get("principalId"));
073                                            if(maxJob != null) {
074                                                    fieldValues.put("jobNumber", Long.toString(maxJob.getJobNumber() +1));
075                                            } else {
076                                                    fieldValues.put("jobNumber", "0");
077                                            }
078                                    }
079                            } 
080                    }
081                    
082                    return super.populateBusinessObject(fieldValues, maintenanceDocument,
083                                    methodToCall); 
084            }
085    
086            @Override
087            public HrBusinessObject getObjectById(String id) {
088                    return (HrBusinessObject)TkServiceLocator.getJobService().getJob(id);
089            }
090    
091            @Override
092            public void customSaveLogic(HrBusinessObject hrObj) {
093                    if(StringUtils.equals(getMaintenanceAction(),"New")){
094                            Job job = (Job)hrObj;
095                            this.setJobNumber(job);
096                    }
097            }
098            
099            
100    }