View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.core.job.web;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.kpme.core.api.job.JobContract;
22  import org.kuali.kpme.core.bo.HrBusinessObject;
23  import org.kuali.kpme.core.bo.HrBusinessObjectMaintainableImpl;
24  import org.kuali.kpme.core.job.JobBo;
25  import org.kuali.kpme.core.service.HrServiceLocator;
26  import org.kuali.rice.kim.api.identity.name.EntityName;
27  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
30  
31  /**
32   * Hooks in to Rice to over-ride the way we are saving our Business Objects.  We
33   * treat our business objects as immutable, the default Rice behavior is to modify
34   * existing rows in the database.
35   */
36  public class JobMaintainableImpl extends HrBusinessObjectMaintainableImpl {
37  
38  	/**
39  	 * 
40  	 */
41  	private static final long serialVersionUID = 1L;
42  
43  	@Override
44  	public void processAfterCopy(MaintenanceDocument document,
45  			Map<String, String[]> parameters) {
46  		super.processAfterCopy(document, parameters);
47  		JobBo job = (JobBo) document.getNewMaintainableObject().getDataObject();
48  		job.setPrincipalId(null);
49  		job.setJobNumber(null);
50  	}	
51  
52  	@Override
53  	public HrBusinessObject getObjectById(String id) {
54  		return JobBo.from(HrServiceLocator.getJobService().getJob(id));
55  	}
56  
57      //attribute query, populates name when principalID is selected
58      public EntityName getName(String principalId) {
59          return KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(principalId).getDefaultName();
60      }
61  
62      //attribute query, populates name and job number when principalID is selected, for new jobs
63      public JobBo getNameAndJob(String principalId) {
64          JobBo aJob = new JobBo();
65  
66          EntityNamePrincipalName p = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(principalId);
67          if (p != null && p.getDefaultName() != null) {
68              aJob.setPrincipalName(p.getDefaultName().getCompositeName());
69          }else{
70              aJob.setPrincipalName("");
71          }
72  
73          JobContract maxJob = HrServiceLocator.getJobService().getMaxJob(principalId);
74          if(maxJob != null) {
75              aJob.setJobNumber(maxJob.getJobNumber() +1);
76          } else {
77              aJob.setJobNumber(0L);
78          }
79  
80          return aJob;
81      }
82  
83      @Override
84      public void prepareForSave() {
85          JobBo aJob = (JobBo) this.getDataObject();
86  
87          //KPME-3238 - fill in job number if it is still null when its time to save.
88          if ((StringUtils.equals(getMaintenanceAction(), "New") || StringUtils.equals(getMaintenanceAction(), "Copy")) && aJob.getJobNumber()==null) {
89  
90              JobContract maxJob = HrServiceLocator.getJobService().getMaxJob(aJob.getPrincipalId());
91  
92              if(maxJob != null) {
93                  aJob.setJobNumber(maxJob.getJobNumber() +1);
94              } else {
95                  aJob.setJobNumber(0L);
96              }
97  
98          }
99  
100         super.prepareForSave();
101     }
102 
103 }