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