1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.maintenance;
17
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.kuali.rice.kns.bo.GlobalBusinessObject;
23 import org.kuali.rice.kns.bo.GlobalBusinessObjectDetail;
24 import org.kuali.rice.kns.bo.PersistableBusinessObject;
25 import org.kuali.rice.kns.document.MaintenanceLock;
26 import org.kuali.rice.kns.service.BusinessObjectService;
27 import org.kuali.rice.kns.service.KNSServiceLocator;
28 import org.kuali.rice.kns.util.KNSPropertyConstants;
29 import org.kuali.rice.kns.util.ObjectUtils;
30
31 public abstract class KualiGlobalMaintainableImpl extends KualiMaintainableImpl {
32 private static final long serialVersionUID = 4814145799502207182L;
33
34 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiGlobalMaintainableImpl.class);
35
36
37
38
39 @Override
40 public void prepareForSave() {
41 if (businessObject != null) {
42 prepareGlobalsForSave();
43 }
44 }
45
46
47
48
49 @Override
50 public void processAfterRetrieve() {
51 if (businessObject != null) {
52 processGlobalsAfterRetrieve();
53 }
54 }
55
56
57
58
59
60 protected void processGlobalsAfterRetrieve() {
61
62
63
64
65
66
67
68 GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
69 Class gboClass = businessObject.getClass();
70 String finDocNumber = gbo.getDocumentNumber();
71
72
73
74
75
76
77 boolean assumptionIsWrong = false;
78
79
80 List primaryKeys = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(gboClass);
81 if (primaryKeys == null) {
82 assumptionIsWrong = true;
83 }
84 else if (primaryKeys.isEmpty()) {
85 assumptionIsWrong = true;
86 }
87 else if (primaryKeys.size() != 1) {
88 assumptionIsWrong = true;
89 }
90 else if (!primaryKeys.get(0).getClass().equals(String.class)) {
91 assumptionIsWrong = true;
92 }
93 else if (!KNSPropertyConstants.DOCUMENT_NUMBER.equalsIgnoreCase((String) primaryKeys.get(0))) {
94 assumptionIsWrong = true;
95 }
96 if (assumptionIsWrong) {
97 throw new RuntimeException("An assertion about the nature of the primary keys for this GBO has " + "failed, and processing cannot continue.");
98 }
99
100
101
102
103
104
105 Map pkMap = new HashMap();
106 pkMap.put(KNSPropertyConstants.DOCUMENT_NUMBER, finDocNumber);
107 PersistableBusinessObject newBo = null;
108 newBo = (PersistableBusinessObject) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(gboClass, pkMap);
109 if (newBo == null) {
110 throw new RuntimeException("The Global Business Object could not be retrieved from the DB. " + "This should never happen under normal circumstances. If this is a legitimate case " + "Then this exception should be removed.");
111 }
112
113
114 try {
115 ObjectUtils.setObjectPropertyDeep(newBo, KNSPropertyConstants.NEW_COLLECTION_RECORD, boolean.class, true, 2);
116 }
117 catch (Exception e) {
118 LOG.error("unable to set newCollectionRecord property: " + e.getMessage());
119 throw new RuntimeException("unable to set newCollectionRecord property: " + e.getMessage());
120 }
121
122
123 businessObject = newBo;
124 }
125
126
127
128
129 protected void prepareGlobalsForSave() {
130 GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
131
132
133 gbo.setDocumentNumber(documentNumber);
134
135 List<? extends GlobalBusinessObjectDetail> details = gbo.getAllDetailObjects();
136 for ( GlobalBusinessObjectDetail detail : details ) {
137 detail.setDocumentNumber(documentNumber);
138 }
139 }
140
141
142
143
144
145
146 @Override
147 public abstract List<MaintenanceLock> generateMaintenanceLocks();
148
149
150
151
152 @Override
153 public void saveBusinessObject() {
154 BusinessObjectService boService = KNSServiceLocator.getBusinessObjectService();
155 GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
156
157
158 List<PersistableBusinessObject> bosToDeactivate = gbo.generateDeactivationsToPersist();
159 if (bosToDeactivate != null) {
160 if (!bosToDeactivate.isEmpty()) {
161 boService.save(bosToDeactivate);
162 }
163 }
164
165
166 List<PersistableBusinessObject> bosToPersist = gbo.generateGlobalChangesToPersist();
167 if (bosToPersist != null) {
168 if (!bosToPersist.isEmpty()) {
169 boService.save(bosToPersist);
170 }
171 }
172
173 }
174
175 public abstract Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass();
176 }