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.syslunch.service;
017    
018    import java.sql.Timestamp;
019    
020    import org.kuali.hr.core.cache.CacheUtils;
021    import org.kuali.hr.time.service.base.TkServiceLocator;
022    import org.kuali.hr.time.syslunch.rule.SystemLunchRule;
023    import org.kuali.hr.time.util.TKUtils;
024    import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
025    import org.kuali.rice.krad.service.KRADServiceLocator;
026    
027    public class SystemLunchRuleMaintainableImpl extends KualiMaintainableImpl {
028            /**
029             * 
030             */
031            private static final long serialVersionUID = 1L;
032    
033        @Override
034        public void saveBusinessObject() {
035            SystemLunchRule sysLunchRule = (SystemLunchRule)this.getBusinessObject();
036            if(sysLunchRule.getTkSystemLunchRuleId()!=null && sysLunchRule.isActive()){
037                SystemLunchRule oldSystemLunchRule = TkServiceLocator.getSystemLunchRuleService().getSystemLunchRule(sysLunchRule.getTkSystemLunchRuleId());
038                if(sysLunchRule.getEffectiveDate().equals(oldSystemLunchRule.getEffectiveDate())){
039                    sysLunchRule.setTimestamp(null);
040                } else{
041                    if(oldSystemLunchRule!=null){
042                        oldSystemLunchRule.setActive(false);
043                        //NOTE this is done to prevent the timestamp of the inactive one to be greater than the
044                        oldSystemLunchRule.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(System.currentTimeMillis())));
045                        oldSystemLunchRule.setEffectiveDate(sysLunchRule.getEffectiveDate());
046                        KRADServiceLocator.getBusinessObjectService().save(oldSystemLunchRule);
047                    }
048                    sysLunchRule.setTimestamp(new Timestamp(System.currentTimeMillis()));
049                    sysLunchRule.setTkSystemLunchRuleId(null);
050                }
051            }
052    
053            KRADServiceLocator.getBusinessObjectService().save(sysLunchRule);
054            CacheUtils.flushCache(SystemLunchRule.CACHE_NAME);
055        }
056    }