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 | 0 | public abstract class KualiGlobalMaintainableImpl extends KualiMaintainableImpl { |
32 | |
private static final long serialVersionUID = 4814145799502207182L; |
33 | |
|
34 | 0 | 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 | 0 | if (businessObject != null) { |
42 | 0 | prepareGlobalsForSave(); |
43 | |
} |
44 | 0 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
@Override |
50 | |
public void processAfterRetrieve() { |
51 | 0 | if (businessObject != null) { |
52 | 0 | processGlobalsAfterRetrieve(); |
53 | |
} |
54 | 0 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
protected void processGlobalsAfterRetrieve() { |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject; |
69 | 0 | Class gboClass = businessObject.getClass(); |
70 | 0 | String finDocNumber = gbo.getDocumentNumber(); |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 0 | boolean assumptionIsWrong = false; |
78 | |
|
79 | |
|
80 | 0 | List primaryKeys = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(gboClass); |
81 | 0 | if (primaryKeys == null) { |
82 | 0 | assumptionIsWrong = true; |
83 | |
} |
84 | 0 | else if (primaryKeys.isEmpty()) { |
85 | 0 | assumptionIsWrong = true; |
86 | |
} |
87 | 0 | else if (primaryKeys.size() != 1) { |
88 | 0 | assumptionIsWrong = true; |
89 | |
} |
90 | 0 | else if (!primaryKeys.get(0).getClass().equals(String.class)) { |
91 | 0 | assumptionIsWrong = true; |
92 | |
} |
93 | 0 | else if (!KNSPropertyConstants.DOCUMENT_NUMBER.equalsIgnoreCase((String) primaryKeys.get(0))) { |
94 | 0 | assumptionIsWrong = true; |
95 | |
} |
96 | 0 | if (assumptionIsWrong) { |
97 | 0 | 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 | 0 | Map pkMap = new HashMap(); |
106 | 0 | pkMap.put(KNSPropertyConstants.DOCUMENT_NUMBER, finDocNumber); |
107 | 0 | PersistableBusinessObject newBo = null; |
108 | 0 | newBo = (PersistableBusinessObject) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(gboClass, pkMap); |
109 | 0 | if (newBo == null) { |
110 | 0 | 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 | 0 | ObjectUtils.setObjectPropertyDeep(newBo, KNSPropertyConstants.NEW_COLLECTION_RECORD, boolean.class, true, 2); |
116 | |
} |
117 | 0 | catch (Exception e) { |
118 | 0 | LOG.error("unable to set newCollectionRecord property: " + e.getMessage()); |
119 | 0 | throw new RuntimeException("unable to set newCollectionRecord property: " + e.getMessage()); |
120 | 0 | } |
121 | |
|
122 | |
|
123 | 0 | businessObject = newBo; |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected void prepareGlobalsForSave() { |
130 | 0 | GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject; |
131 | |
|
132 | |
|
133 | 0 | gbo.setDocumentNumber(documentNumber); |
134 | |
|
135 | 0 | List<? extends GlobalBusinessObjectDetail> details = gbo.getAllDetailObjects(); |
136 | 0 | for ( GlobalBusinessObjectDetail detail : details ) { |
137 | 0 | detail.setDocumentNumber(documentNumber); |
138 | |
} |
139 | 0 | } |
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 | 0 | BusinessObjectService boService = KNSServiceLocator.getBusinessObjectService(); |
155 | 0 | GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject; |
156 | |
|
157 | |
|
158 | 0 | List<PersistableBusinessObject> bosToDeactivate = gbo.generateDeactivationsToPersist(); |
159 | 0 | if (bosToDeactivate != null) { |
160 | 0 | if (!bosToDeactivate.isEmpty()) { |
161 | 0 | boService.save(bosToDeactivate); |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
|
166 | 0 | List<PersistableBusinessObject> bosToPersist = gbo.generateGlobalChangesToPersist(); |
167 | 0 | if (bosToPersist != null) { |
168 | 0 | if (!bosToPersist.isEmpty()) { |
169 | 0 | boService.save(bosToPersist); |
170 | |
} |
171 | |
} |
172 | |
|
173 | 0 | } |
174 | |
|
175 | |
public abstract Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass(); |
176 | |
} |