001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.util;
017
018 import java.lang.reflect.Field;
019 import java.sql.Timestamp;
020 import java.util.List;
021
022 import org.kuali.hr.core.cache.CacheUtils;
023 import org.kuali.hr.time.HrBusinessObject;
024 import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
025 import org.kuali.rice.krad.service.KRADServiceLocator;
026 import sun.util.LocaleServiceProviderPool;
027
028 public abstract class HrBusinessObjectMaintainableImpl extends KualiMaintainableImpl {
029 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HrBusinessObjectMaintainableImpl.class);
030 /**
031 *
032 */
033 private static final long serialVersionUID = 1L;
034
035 @Override
036 public void saveBusinessObject() {
037 HrBusinessObject hrObj = (HrBusinessObject) this.getBusinessObject();
038 if(hrObj.getId()!=null){
039 HrBusinessObject oldHrObj = this.getObjectById(hrObj.getId());
040 if(oldHrObj!= null){
041 //if the effective dates are the same do not create a new row just inactivate the old one
042 if(hrObj.getEffectiveDate().equals(oldHrObj.getEffectiveDate())){
043 oldHrObj.setActive(false);
044 oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime())));
045 } else{
046 //if effective dates not the same add a new row that inactivates the old entry based on the new effective date
047 oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime())));
048 oldHrObj.setEffectiveDate(hrObj.getEffectiveDate());
049 oldHrObj.setActive(false);
050 oldHrObj.setId(null);
051 }
052 KRADServiceLocator.getBusinessObjectService().save(oldHrObj);
053 }
054 }
055 hrObj.setTimestamp(new Timestamp(System.currentTimeMillis()));
056 hrObj.setId(null);
057
058 customSaveLogic(hrObj);
059 KRADServiceLocator.getBusinessObjectService().save(hrObj);
060
061 //cache clearing?!?!
062 try {
063 List<String> cacheNames = (List<String>)hrObj.getClass().getDeclaredField("CACHE_FLUSH").get(hrObj);
064 CacheUtils.flushCaches(cacheNames);
065 } catch (NoSuchFieldException e) {
066 try {
067 String cacheName = (String)hrObj.getClass().getDeclaredField("CACHE_NAME").get(hrObj);
068 CacheUtils.flushCache(cacheName);
069 } catch (NoSuchFieldException ex) {
070 // no cache name found
071 LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
072 } catch (IllegalAccessException ex) {
073 LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
074 }
075 // no cache name found
076 //LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
077 } catch (IllegalAccessException e) {
078 LOG.warn("No caches found for object: " + hrObj.getClass().getName());
079 }
080 }
081
082 public abstract HrBusinessObject getObjectById(String id);
083 public void customSaveLogic(HrBusinessObject hrObj){};
084 }