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