View Javadoc

1   /**
2    * Copyright 2004-2012 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.time.util;
17  
18  import java.lang.reflect.Field;
19  import java.sql.Timestamp;
20  
21  import org.kuali.hr.core.cache.CacheUtils;
22  import org.kuali.hr.time.HrBusinessObject;
23  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import sun.util.LocaleServiceProviderPool;
26  
27  public abstract class HrBusinessObjectMaintainableImpl extends KualiMaintainableImpl {
28      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HrBusinessObjectMaintainableImpl.class);
29  	/**
30  	 * 
31  	 */
32  	private static final long serialVersionUID = 1L;
33  
34  	@Override
35  	public void saveBusinessObject() {
36  		HrBusinessObject hrObj = (HrBusinessObject) this.getBusinessObject();
37  		if(hrObj.getId()!=null){
38  			HrBusinessObject oldHrObj = this.getObjectById(hrObj.getId());
39  			if(oldHrObj!= null){
40  				//if the effective dates are the same do not create a new row just inactivate the old one
41  				if(hrObj.getEffectiveDate().equals(oldHrObj.getEffectiveDate())){
42  					oldHrObj.setActive(false);
43  					oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime()))); 
44  				} else{
45  					//if effective dates not the same add a new row that inactivates the old entry based on the new effective date
46  					oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime())));
47  					oldHrObj.setEffectiveDate(hrObj.getEffectiveDate());
48  					oldHrObj.setActive(false);
49  					oldHrObj.setId(null);
50  				}
51  				KRADServiceLocator.getBusinessObjectService().save(oldHrObj);
52  			}
53  		}
54  		hrObj.setTimestamp(new Timestamp(System.currentTimeMillis()));
55  		hrObj.setId(null);
56  		
57  		customSaveLogic(hrObj);
58  		KRADServiceLocator.getBusinessObjectService().save(hrObj);
59  
60          //cache clearing?!?!
61          try {
62              String cacheName = (String)hrObj.getClass().getDeclaredField("CACHE_NAME").get(hrObj);
63              CacheUtils.flushCache(cacheName);
64          } catch (NoSuchFieldException e) {
65              // no cache name found
66              LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
67          } catch (IllegalAccessException e) {
68              LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
69          }
70      }
71  	
72  	public abstract HrBusinessObject getObjectById(String id);
73  	public void customSaveLogic(HrBusinessObject hrObj){};
74  }