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.hr.job.service;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.hr.job.Job;
22  import org.kuali.hr.time.HrBusinessObject;
23  import org.kuali.hr.time.service.base.TkServiceLocator;
24  import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
25  import org.kuali.rice.kim.api.identity.Person;
26  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
27  import org.kuali.rice.kns.document.MaintenanceDocument;
28  
29  /**
30   * Hooks in to Rice to over-ride the way we are saving our Business Objects.  We
31   * treat our business objects as immutable, the default Rice behavior is to modify
32   * existing rows in the database.
33   */
34  public class JobMaintainableImpl extends HrBusinessObjectMaintainableImpl {
35  
36  	/**
37  	 * 
38  	 */
39  	private static final long serialVersionUID = 1L;
40  
41  	
42  	public void setJobNumber(Job job) {
43  		Long jobNumber = new Long("0");
44  		Job maxJob = TkServiceLocator.getJobService().getMaxJob(job.getPrincipalId());
45  		
46  		if(maxJob != null) {
47  			// get the max of job number of the collection
48  			jobNumber = maxJob.getJobNumber() +1;
49  		}		
50  		job.setJobNumber(jobNumber);
51  	}
52  	/**
53  	 * Override to populate user information in Maintenance page
54  	 */
55  	@SuppressWarnings("rawtypes")
56  	@Override
57  	public Map populateBusinessObject(Map<String, String> fieldValues,
58  			MaintenanceDocument maintenanceDocument, String methodToCall) {
59  		if (fieldValues.containsKey("principalId")
60  				&& StringUtils.isNotEmpty(fieldValues.get("principalId"))) {
61  			Person p = KimApiServiceLocator.getPersonService().getPerson(
62  					fieldValues.get("principalId"));
63  			if (p != null) {
64  				fieldValues.put("name", p.getName());
65  			}else{
66  				fieldValues.put("name", "");
67  			}
68  		}
69  		if(StringUtils.equals(getMaintenanceAction(),"New")){
70  			if (!fieldValues.containsKey("jobNumber") || StringUtils.isEmpty(fieldValues.get("jobNumber"))) {
71  				if (fieldValues.containsKey("principalId") && StringUtils.isNotEmpty(fieldValues.get("principalId"))) {
72  					Job maxJob = TkServiceLocator.getJobService().getMaxJob(fieldValues.get("principalId"));
73  					if(maxJob != null) {
74  						fieldValues.put("jobNumber", Long.toString(maxJob.getJobNumber() +1));
75  					} else {
76  						fieldValues.put("jobNumber", "0");
77  					}
78  				}
79  			} 
80  		}
81  		
82  		return super.populateBusinessObject(fieldValues, maintenanceDocument,
83  				methodToCall); 
84  	}
85  
86  	@Override
87  	public HrBusinessObject getObjectById(String id) {
88  		return (HrBusinessObject)TkServiceLocator.getJobService().getJob(id);
89  	}
90  
91  	@Override
92  	public void customSaveLogic(HrBusinessObject hrObj) {
93  		if(StringUtils.equals(getMaintenanceAction(),"New")){
94  			Job job = (Job)hrObj;
95  			this.setJobNumber(job);
96  		}
97  	}
98  	
99  	
100 }