View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.core.bo;
17  
18  import java.sql.Timestamp;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.joda.time.DateTime;
23  import org.joda.time.LocalDate;
24  import org.kuali.kpme.core.cache.CacheUtils;
25  import org.kuali.kpme.core.util.TKUtils;
26  import org.kuali.rice.kns.document.MaintenanceDocument;
27  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
28  import org.kuali.rice.kns.service.KNSServiceLocator;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  public abstract class HrBusinessObjectMaintainableImpl extends KualiMaintainableImpl {
34      protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HrBusinessObjectMaintainableImpl.class);
35  	/**
36  	 * 
37  	 */
38  	private static final long serialVersionUID = 1L;
39  
40  	@Override
41  	public void saveBusinessObject() {
42  		HrBusinessObject hrObj = (HrBusinessObject) this.getBusinessObject();
43  		if(hrObj.getId()!=null){
44  			HrBusinessObject oldHrObj = this.getObjectById(hrObj.getId());
45  			if(oldHrObj!= null){
46  				//if the effective dates are the same do not create a new row just inactivate the old one
47  				if(hrObj.getEffectiveDate().equals(oldHrObj.getEffectiveDate())){
48  					oldHrObj.setActive(false);
49  					oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(DateTime.now().getMillis())));
50  				} else{
51  					//if effective dates not the same add a new row that inactivates the old entry based on the new effective date
52  					oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(DateTime.now().getMillis())));
53  					oldHrObj.setEffectiveDate(hrObj.getEffectiveDate());
54  					oldHrObj.setActive(false);
55  					oldHrObj.setId(null);
56                      customInactiveSaveLogicNewEffective(oldHrObj);
57  				}
58                  KRADServiceLocatorWeb.getLegacyDataAdapter().save(oldHrObj);
59  			}
60  		}
61  		hrObj.setTimestamp(new Timestamp(System.currentTimeMillis()));
62  
63  		hrObj.setId(null);
64  		
65  		customSaveLogic(hrObj);
66  		KRADServiceLocatorWeb.getLegacyDataAdapter().save(hrObj);
67  
68          //cache clearing?!?!
69          try {
70              List<String> cacheNames = (List<String>)hrObj.getClass().getDeclaredField("CACHE_FLUSH").get(hrObj);
71              CacheUtils.flushCaches(cacheNames);
72          } catch (NoSuchFieldException e) {
73              try {
74                  String cacheName = (String)hrObj.getClass().getDeclaredField("CACHE_NAME").get(hrObj);
75                  CacheUtils.flushCache(cacheName);
76              } catch (NoSuchFieldException | IllegalAccessException ex) {
77                  // no cache name found
78                  LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
79              }
80              // no cache name found
81              //LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
82          } catch (IllegalAccessException e) {
83              LOG.warn("No caches found for object: " + hrObj.getClass().getName());
84          }
85      }
86  	
87  	public abstract HrBusinessObject getObjectById(String id);
88  	public void customSaveLogic(HrBusinessObject hrObj){}
89      public void customInactiveSaveLogicNewEffective(HrBusinessObject oldHrObj) {}
90  
91      @Override
92      public void prepareForSave() {
93      HrBusinessObject hrObj = (HrBusinessObject) this.getBusinessObject();
94      hrObj.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
95      }
96  }