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