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