001    /**
002     * Copyright 2004-2012 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    
021    import org.kuali.hr.core.cache.CacheUtils;
022    import org.kuali.hr.time.HrBusinessObject;
023    import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
024    import org.kuali.rice.krad.service.KRADServiceLocator;
025    import sun.util.LocaleServiceProviderPool;
026    
027    public abstract class HrBusinessObjectMaintainableImpl extends KualiMaintainableImpl {
028        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HrBusinessObjectMaintainableImpl.class);
029            /**
030             * 
031             */
032            private static final long serialVersionUID = 1L;
033    
034            @Override
035            public void saveBusinessObject() {
036                    HrBusinessObject hrObj = (HrBusinessObject) this.getBusinessObject();
037                    if(hrObj.getId()!=null){
038                            HrBusinessObject oldHrObj = this.getObjectById(hrObj.getId());
039                            if(oldHrObj!= null){
040                                    //if the effective dates are the same do not create a new row just inactivate the old one
041                                    if(hrObj.getEffectiveDate().equals(oldHrObj.getEffectiveDate())){
042                                            oldHrObj.setActive(false);
043                                            oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime()))); 
044                                    } else{
045                                            //if effective dates not the same add a new row that inactivates the old entry based on the new effective date
046                                            oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime())));
047                                            oldHrObj.setEffectiveDate(hrObj.getEffectiveDate());
048                                            oldHrObj.setActive(false);
049                                            oldHrObj.setId(null);
050                                    }
051                                    KRADServiceLocator.getBusinessObjectService().save(oldHrObj);
052                            }
053                    }
054                    hrObj.setTimestamp(new Timestamp(System.currentTimeMillis()));
055                    hrObj.setId(null);
056                    
057                    customSaveLogic(hrObj);
058                    KRADServiceLocator.getBusinessObjectService().save(hrObj);
059    
060            //cache clearing?!?!
061            try {
062                String cacheName = (String)hrObj.getClass().getDeclaredField("CACHE_NAME").get(hrObj);
063                CacheUtils.flushCache(cacheName);
064            } catch (NoSuchFieldException e) {
065                // no cache name found
066                LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
067            } catch (IllegalAccessException e) {
068                LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
069            }
070        }
071            
072            public abstract HrBusinessObject getObjectById(String id);
073            public void customSaveLogic(HrBusinessObject hrObj){};
074    }