1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45 if(hrObj.getEffectiveDate().equals(oldHrObj.getEffectiveDate())){
46 oldHrObj.setActive(false);
47 oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(DateTime.now().getMillis())));
48 } else{
49
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
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
76 LOG.warn("No cache name found for object: " + hrObj.getClass().getName());
77 }
78
79
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 }