View Javadoc

1   /**
2    * Copyright 2004-2012 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.syslunch.service;
17  
18  import java.sql.Timestamp;
19  
20  import org.kuali.hr.core.cache.CacheUtils;
21  import org.kuali.hr.time.service.base.TkServiceLocator;
22  import org.kuali.hr.time.syslunch.rule.SystemLunchRule;
23  import org.kuali.hr.time.util.TKUtils;
24  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
25  import org.kuali.rice.krad.service.KRADServiceLocator;
26  
27  public class SystemLunchRuleMaintainableImpl extends KualiMaintainableImpl {
28  	/**
29  	 * 
30  	 */
31  	private static final long serialVersionUID = 1L;
32  
33      @Override
34      public void saveBusinessObject() {
35          SystemLunchRule sysLunchRule = (SystemLunchRule)this.getBusinessObject();
36          if(sysLunchRule.getTkSystemLunchRuleId()!=null && sysLunchRule.isActive()){
37              SystemLunchRule oldSystemLunchRule = TkServiceLocator.getSystemLunchRuleService().getSystemLunchRule(sysLunchRule.getTkSystemLunchRuleId());
38              if(sysLunchRule.getEffectiveDate().equals(oldSystemLunchRule.getEffectiveDate())){
39                  sysLunchRule.setTimestamp(null);
40              } else{
41                  if(oldSystemLunchRule!=null){
42                      oldSystemLunchRule.setActive(false);
43                      //NOTE this is done to prevent the timestamp of the inactive one to be greater than the
44                      oldSystemLunchRule.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(System.currentTimeMillis())));
45                      oldSystemLunchRule.setEffectiveDate(sysLunchRule.getEffectiveDate());
46                      KRADServiceLocator.getBusinessObjectService().save(oldSystemLunchRule);
47                  }
48                  sysLunchRule.setTimestamp(new Timestamp(System.currentTimeMillis()));
49                  sysLunchRule.setTkSystemLunchRuleId(null);
50              }
51          }
52  
53          KRADServiceLocator.getBusinessObjectService().save(sysLunchRule);
54          CacheUtils.flushCache(SystemLunchRule.CACHE_NAME);
55      }
56  }