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 org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.job.Job;
20  import org.kuali.hr.time.HrBusinessObject;
21  import org.kuali.hr.time.service.base.TkServiceLocator;
22  import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
23  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.kns.document.MaintenanceDocument;
26  
27  import java.util.Map;
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  			EntityNamePrincipalName p = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(fieldValues.get("principalId"));
62  			if (p != null && p.getDefaultName() != null) {
63  				fieldValues.put("name", p.getDefaultName().getCompositeName());
64  			}else{
65  				fieldValues.put("name", "");
66  			}
67  		}
68  		if(StringUtils.equals(getMaintenanceAction(),"New")){
69  			if (!fieldValues.containsKey("jobNumber") || StringUtils.isEmpty(fieldValues.get("jobNumber"))) {
70  				if (fieldValues.containsKey("principalId") && StringUtils.isNotEmpty(fieldValues.get("principalId"))) {
71  					Job maxJob = TkServiceLocator.getJobService().getMaxJob(fieldValues.get("principalId"));
72  					if(maxJob != null) {
73  						fieldValues.put("jobNumber", Long.toString(maxJob.getJobNumber() +1));
74  					} else {
75  						fieldValues.put("jobNumber", "0");
76  					}
77  				}
78  			} 
79  		}
80  		
81  		return super.populateBusinessObject(fieldValues, maintenanceDocument,
82  				methodToCall); 
83  	}
84  
85  	@Override
86  	public HrBusinessObject getObjectById(String id) {
87  		return (HrBusinessObject)TkServiceLocator.getJobService().getJob(id);
88  	}
89  
90  	@Override
91  	public void customSaveLogic(HrBusinessObject hrObj) {
92  		if(StringUtils.equals(getMaintenanceAction(),"New")){
93  			Job job = (Job)hrObj;
94  			this.setJobNumber(job);
95  		}
96  	}
97  	
98  	
99  }