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