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.time.HrBusinessObject;
021    import org.kuali.hr.time.service.base.TkServiceLocator;
022    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
023    import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
024    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
025    import org.kuali.rice.kns.document.MaintenanceDocument;
026    
027    import java.util.Map;
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                            EntityNamePrincipalName p = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(fieldValues.get("principalId"));
062                            if (p != null && p.getDefaultName() != null) {
063                                    fieldValues.put("name", p.getDefaultName().getCompositeName());
064                            }else{
065                                    fieldValues.put("name", "");
066                            }
067                    }
068                    if(StringUtils.equals(getMaintenanceAction(),"New")){
069                            if (!fieldValues.containsKey("jobNumber") || StringUtils.isEmpty(fieldValues.get("jobNumber"))) {
070                                    if (fieldValues.containsKey("principalId") && StringUtils.isNotEmpty(fieldValues.get("principalId"))) {
071                                            Job maxJob = TkServiceLocator.getJobService().getMaxJob(fieldValues.get("principalId"));
072                                            if(maxJob != null) {
073                                                    fieldValues.put("jobNumber", Long.toString(maxJob.getJobNumber() +1));
074                                            } else {
075                                                    fieldValues.put("jobNumber", "0");
076                                            }
077                                    }
078                            } 
079                    }
080                    
081                    return super.populateBusinessObject(fieldValues, maintenanceDocument,
082                                    methodToCall); 
083            }
084    
085            @Override
086            public HrBusinessObject getObjectById(String id) {
087                    return (HrBusinessObject)TkServiceLocator.getJobService().getJob(id);
088            }
089    
090            @Override
091            public void customSaveLogic(HrBusinessObject hrObj) {
092                    if(StringUtils.equals(getMaintenanceAction(),"New")){
093                            Job job = (Job)hrObj;
094                            this.setJobNumber(job);
095                    }
096            }
097            
098            
099    }