1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.maintenance; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException; |
20 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
21 | |
import org.kuali.rice.core.api.encryption.EncryptionService; |
22 | |
import org.kuali.rice.kim.bo.Person; |
23 | |
import org.kuali.rice.krad.bo.BusinessObject; |
24 | |
import org.kuali.rice.krad.bo.DocumentHeader; |
25 | |
import org.kuali.rice.krad.bo.Note; |
26 | |
import org.kuali.rice.krad.bo.PersistableBusinessObject; |
27 | |
import org.kuali.rice.krad.document.MaintenanceDocument; |
28 | |
import org.kuali.rice.krad.document.MaintenanceLock; |
29 | |
import org.kuali.rice.krad.exception.PessimisticLockingException; |
30 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
31 | |
import org.kuali.rice.krad.service.DataObjectAuthorizationService; |
32 | |
import org.kuali.rice.krad.service.DataObjectMetaDataService; |
33 | |
import org.kuali.rice.krad.service.DocumentDictionaryService; |
34 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
35 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
36 | |
import org.kuali.rice.krad.service.LookupService; |
37 | |
import org.kuali.rice.krad.service.MaintenanceDocumentService; |
38 | |
import org.kuali.rice.krad.uif.container.CollectionGroup; |
39 | |
import org.kuali.rice.krad.uif.container.View; |
40 | |
import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl; |
41 | |
import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; |
42 | |
import org.kuali.rice.krad.util.KRADConstants; |
43 | |
import org.kuali.rice.krad.util.ObjectUtils; |
44 | |
import org.kuali.rice.krad.web.form.MaintenanceForm; |
45 | |
|
46 | |
import java.security.GeneralSecurityException; |
47 | |
import java.util.ArrayList; |
48 | |
import java.util.Collection; |
49 | |
import java.util.Iterator; |
50 | |
import java.util.List; |
51 | |
import java.util.Map; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | public class MaintainableImpl extends ViewHelperServiceImpl implements Maintainable { |
59 | |
private static final long serialVersionUID = 9125271369161634992L; |
60 | |
|
61 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MaintainableImpl.class); |
62 | |
|
63 | |
private String documentNumber; |
64 | |
private Object dataObject; |
65 | |
private Class<?> dataObjectClass; |
66 | |
private String maintenanceAction; |
67 | |
|
68 | |
private transient LookupService lookupService; |
69 | |
private transient DataObjectAuthorizationService dataObjectAuthorizationService; |
70 | |
private transient DataObjectMetaDataService dataObjectMetaDataService; |
71 | |
private transient DocumentDictionaryService documentDictionaryService; |
72 | |
private transient EncryptionService encryptionService; |
73 | |
private transient BusinessObjectService businessObjectService; |
74 | |
private transient MaintenanceDocumentService maintenanceDocumentService; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
@Override |
80 | |
public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) { |
81 | 0 | Object dataObject = null; |
82 | |
|
83 | |
try { |
84 | 0 | dataObject = getLookupService().findObjectBySearch(getDataObjectClass(), dataObjectKeys); |
85 | 0 | } catch (ClassNotPersistenceCapableException ex) { |
86 | 0 | if (!document.getOldMaintainableObject().isExternalBusinessObject()) { |
87 | 0 | throw new RuntimeException("Data Object Class: " + getDataObjectClass() + |
88 | |
" is not persistable and is not externalizable - configuration error"); |
89 | |
} |
90 | |
|
91 | 0 | } |
92 | |
|
93 | 0 | return dataObject; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
@Override |
100 | |
public void setDocumentNumber(String documentNumber) { |
101 | 0 | this.documentNumber = documentNumber; |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
@Override |
108 | |
public String getDocumentTitle(MaintenanceDocument document) { |
109 | |
|
110 | |
|
111 | 0 | return ""; |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
@Override |
118 | |
public Object getDataObject() { |
119 | 0 | return dataObject; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
@Override |
126 | |
public void setDataObject(Object object) { |
127 | 0 | this.dataObject = object; |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
@Override |
134 | |
public Class getDataObjectClass() { |
135 | 0 | return dataObjectClass; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
@Override |
142 | |
public void setDataObjectClass(Class dataObjectClass) { |
143 | 0 | this.dataObjectClass = dataObjectClass; |
144 | 0 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
@Override |
150 | |
public String getMaintenanceAction() { |
151 | 0 | return maintenanceAction; |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
@Override |
158 | |
public void setMaintenanceAction(String maintenanceAction) { |
159 | 0 | this.maintenanceAction = maintenanceAction; |
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
@Override |
169 | |
public List<MaintenanceLock> generateMaintenanceLocks() { |
170 | 0 | List<MaintenanceLock> maintenanceLocks = new ArrayList<MaintenanceLock>(); |
171 | 0 | StringBuffer lockRepresentation = new StringBuffer(dataObjectClass.getName()); |
172 | 0 | lockRepresentation.append(KRADConstants.Maintenance.LOCK_AFTER_CLASS_DELIM); |
173 | |
|
174 | 0 | Object bo = getDataObject(); |
175 | 0 | List keyFieldNames = getDocumentDictionaryService().getLockingKeys(getDocumentTypeName()); |
176 | |
|
177 | 0 | for (Iterator i = keyFieldNames.iterator(); i.hasNext(); ) { |
178 | 0 | String fieldName = (String) i.next(); |
179 | 0 | Object fieldValue = ObjectUtils.getPropertyValue(bo, fieldName); |
180 | 0 | if (fieldValue == null) { |
181 | 0 | fieldValue = ""; |
182 | |
} |
183 | |
|
184 | |
|
185 | 0 | if (getDataObjectAuthorizationService() |
186 | |
.attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObjectClass, fieldName)) { |
187 | |
try { |
188 | 0 | fieldValue = getEncryptionService().encrypt(fieldValue); |
189 | 0 | } catch (GeneralSecurityException e) { |
190 | 0 | LOG.error("Unable to encrypt secure field for locking representation " + e.getMessage()); |
191 | 0 | throw new RuntimeException( |
192 | |
"Unable to encrypt secure field for locking representation " + e.getMessage()); |
193 | 0 | } |
194 | |
} |
195 | |
|
196 | 0 | lockRepresentation.append(fieldName); |
197 | 0 | lockRepresentation.append(KRADConstants.Maintenance.LOCK_AFTER_FIELDNAME_DELIM); |
198 | 0 | lockRepresentation.append(String.valueOf(fieldValue)); |
199 | 0 | if (i.hasNext()) { |
200 | 0 | lockRepresentation.append(KRADConstants.Maintenance.LOCK_AFTER_VALUE_DELIM); |
201 | |
} |
202 | 0 | } |
203 | |
|
204 | 0 | MaintenanceLock maintenanceLock = new MaintenanceLock(); |
205 | 0 | maintenanceLock.setDocumentNumber(documentNumber); |
206 | 0 | maintenanceLock.setLockingRepresentation(lockRepresentation.toString()); |
207 | 0 | maintenanceLocks.add(maintenanceLock); |
208 | |
|
209 | 0 | return maintenanceLocks; |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
protected String getDocumentTypeName() { |
217 | 0 | return getDocumentDictionaryService().getMaintenanceDocumentTypeName(dataObjectClass); |
218 | |
} |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
@Override |
224 | |
public void saveDataObject() { |
225 | 0 | if (dataObject instanceof PersistableBusinessObject) { |
226 | 0 | getBusinessObjectService().linkAndSave((PersistableBusinessObject) dataObject); |
227 | |
} else { |
228 | 0 | throw new RuntimeException( |
229 | |
"Cannot save object of type: " + dataObjectClass + " with business object service"); |
230 | |
} |
231 | 0 | } |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
@Override |
237 | |
public void deleteDataObject() { |
238 | 0 | if (dataObject == null) { |
239 | 0 | return; |
240 | |
} |
241 | |
|
242 | 0 | if (dataObject instanceof PersistableBusinessObject) { |
243 | 0 | getBusinessObjectService().delete((PersistableBusinessObject) dataObject); |
244 | 0 | dataObject = null; |
245 | |
} else { |
246 | 0 | throw new RuntimeException( |
247 | |
"Cannot delete object of type: " + dataObjectClass + " with business object service"); |
248 | |
} |
249 | 0 | } |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
@Override |
255 | |
public void doRouteStatusChange(DocumentHeader documentHeader) { |
256 | |
|
257 | 0 | } |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
@Override |
263 | |
public String getLockingDocumentId() { |
264 | 0 | return getMaintenanceDocumentService().getLockingDocumentId(this, documentNumber); |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
@Override |
271 | |
public List<Long> getWorkflowEngineDocumentIdsToLock() { |
272 | 0 | return null; |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
@Override |
284 | |
public boolean useCustomLockDescriptors() { |
285 | 0 | return false; |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
@Override |
296 | |
public String getCustomLockDescriptor(Person user) { |
297 | 0 | throw new PessimisticLockingException("The Maintainable for document " + documentNumber + |
298 | |
" is using pessimistic locking with custom lock descriptors, but the Maintainable has not overridden the getCustomLockDescriptor method"); |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
@Override |
305 | |
public boolean isNotesEnabled() { |
306 | 0 | return getDataObjectMetaDataService().areNotesSupported(dataObjectClass); |
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
@Override |
313 | |
public boolean isExternalBusinessObject() { |
314 | 0 | return false; |
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
@Override |
321 | |
public void prepareExternalBusinessObject(BusinessObject businessObject) { |
322 | |
|
323 | 0 | } |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
@Override |
331 | |
public boolean isOldDataObjectInDocument() { |
332 | 0 | boolean isOldDataObjectInExistence = true; |
333 | |
|
334 | 0 | if (getDataObject() == null) { |
335 | 0 | isOldDataObjectInExistence = false; |
336 | |
} else { |
337 | 0 | Map<String, ?> keyFieldValues = getDataObjectMetaDataService().getPrimaryKeyFieldValues(getDataObject()); |
338 | 0 | for (Object keyValue : keyFieldValues.values()) { |
339 | 0 | if (keyValue == null) { |
340 | 0 | isOldDataObjectInExistence = false; |
341 | 0 | } else if ((keyValue instanceof String) && StringUtils.isBlank((String) keyValue)) { |
342 | 0 | isOldDataObjectInExistence = false; |
343 | |
} |
344 | |
|
345 | 0 | if (!isOldDataObjectInExistence) { |
346 | 0 | break; |
347 | |
} |
348 | |
} |
349 | |
} |
350 | |
|
351 | 0 | return isOldDataObjectInExistence; |
352 | |
} |
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
@Override |
358 | |
public void prepareForSave() { |
359 | |
|
360 | 0 | } |
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
@Override |
366 | |
public void processAfterRetrieve() { |
367 | |
|
368 | 0 | } |
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
@Override |
374 | |
public void setupNewFromExisting(MaintenanceDocument document, Map<String, String[]> parameters) { |
375 | |
|
376 | 0 | } |
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
@Override |
382 | |
public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) { |
383 | |
|
384 | 0 | } |
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
@Override |
390 | |
public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) { |
391 | |
|
392 | 0 | } |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
@Override |
398 | |
public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) { |
399 | |
|
400 | 0 | } |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
@Override |
406 | |
public void processAfterPost(MaintenanceDocument document, Map<String, String[]> requestParameters) { |
407 | |
|
408 | 0 | } |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
@Override |
421 | |
protected void processAfterAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) { |
422 | 0 | super.processAfterAddLine(view, collectionGroup, model, addLine); |
423 | |
|
424 | |
|
425 | 0 | if (model instanceof MaintenanceForm && KRADConstants.MAINTENANCE_EDIT_ACTION.equals(((MaintenanceForm)model).getMaintenanceAction()) && !(addLine instanceof Note)) { |
426 | 0 | MaintenanceForm maintenanceForm = (MaintenanceForm) model; |
427 | 0 | MaintenanceDocument document = maintenanceForm.getDocument(); |
428 | |
|
429 | |
|
430 | 0 | Collection<Object> oldCollection = ObjectPropertyUtils |
431 | |
.getPropertyValue(document.getOldMaintainableObject().getDataObject(), |
432 | |
collectionGroup.getPropertyName()); |
433 | |
try { |
434 | 0 | Object blankLine = getDataObjectClass().newInstance(); |
435 | 0 | oldCollection.add(blankLine); |
436 | 0 | } catch (Exception e) { |
437 | 0 | throw new RuntimeException("Unable to create new line instance for old maintenance object", e); |
438 | 0 | } |
439 | |
} |
440 | 0 | } |
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
@Override |
451 | |
protected void processAfterDeleteLine(View view, CollectionGroup collectionGroup, Object model, int lineIndex) { |
452 | 0 | super.processAfterDeleteLine(view, collectionGroup, model, lineIndex); |
453 | |
|
454 | |
|
455 | 0 | if (model instanceof MaintenanceForm && KRADConstants.MAINTENANCE_EDIT_ACTION.equals(((MaintenanceForm)model).getMaintenanceAction()) |
456 | |
&& !collectionGroup.getCollectionObjectClass().getName().equals(Note.class.getName())) { |
457 | 0 | MaintenanceForm maintenanceForm = (MaintenanceForm) model; |
458 | 0 | MaintenanceDocument document = maintenanceForm.getDocument(); |
459 | |
|
460 | |
|
461 | 0 | Collection<Object> oldCollection = ObjectPropertyUtils |
462 | |
.getPropertyValue(document.getOldMaintainableObject().getDataObject(), |
463 | |
collectionGroup.getPropertyName()); |
464 | |
try { |
465 | |
|
466 | |
|
467 | 0 | oldCollection.remove(oldCollection.toArray()[lineIndex]); |
468 | 0 | } catch (Exception e) { |
469 | 0 | throw new RuntimeException("Unable to delete line instance for old maintenance object", e); |
470 | 0 | } |
471 | |
} |
472 | 0 | } |
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
protected String getDocumentNumber() { |
480 | 0 | return this.documentNumber; |
481 | |
} |
482 | |
|
483 | |
protected LookupService getLookupService() { |
484 | 0 | if (lookupService == null) { |
485 | 0 | lookupService = KRADServiceLocatorWeb.getLookupService(); |
486 | |
} |
487 | 0 | return this.lookupService; |
488 | |
} |
489 | |
|
490 | |
public void setLookupService(LookupService lookupService) { |
491 | 0 | this.lookupService = lookupService; |
492 | 0 | } |
493 | |
|
494 | |
protected DataObjectAuthorizationService getDataObjectAuthorizationService() { |
495 | 0 | if (dataObjectAuthorizationService == null) { |
496 | 0 | this.dataObjectAuthorizationService = KRADServiceLocatorWeb.getDataObjectAuthorizationService(); |
497 | |
} |
498 | 0 | return dataObjectAuthorizationService; |
499 | |
} |
500 | |
|
501 | |
public void setDataObjectAuthorizationService(DataObjectAuthorizationService dataObjectAuthorizationService) { |
502 | 0 | this.dataObjectAuthorizationService = dataObjectAuthorizationService; |
503 | 0 | } |
504 | |
|
505 | |
protected DataObjectMetaDataService getDataObjectMetaDataService() { |
506 | 0 | if (dataObjectMetaDataService == null) { |
507 | 0 | this.dataObjectMetaDataService = KRADServiceLocatorWeb.getDataObjectMetaDataService(); |
508 | |
} |
509 | 0 | return dataObjectMetaDataService; |
510 | |
} |
511 | |
|
512 | |
public void setDataObjectMetaDataService(DataObjectMetaDataService dataObjectMetaDataService) { |
513 | 0 | this.dataObjectMetaDataService = dataObjectMetaDataService; |
514 | 0 | } |
515 | |
|
516 | |
public DocumentDictionaryService getDocumentDictionaryService() { |
517 | 0 | if (documentDictionaryService == null) { |
518 | 0 | this.documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService(); |
519 | |
} |
520 | 0 | return documentDictionaryService; |
521 | |
} |
522 | |
|
523 | |
public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) { |
524 | 0 | this.documentDictionaryService = documentDictionaryService; |
525 | 0 | } |
526 | |
|
527 | |
protected EncryptionService getEncryptionService() { |
528 | 0 | if (encryptionService == null) { |
529 | 0 | encryptionService = CoreApiServiceLocator.getEncryptionService(); |
530 | |
} |
531 | 0 | return encryptionService; |
532 | |
} |
533 | |
|
534 | |
public void setEncryptionService(EncryptionService encryptionService) { |
535 | 0 | this.encryptionService = encryptionService; |
536 | 0 | } |
537 | |
|
538 | |
protected BusinessObjectService getBusinessObjectService() { |
539 | 0 | if (businessObjectService == null) { |
540 | 0 | businessObjectService = KRADServiceLocator.getBusinessObjectService(); |
541 | |
} |
542 | 0 | return businessObjectService; |
543 | |
} |
544 | |
|
545 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
546 | 0 | this.businessObjectService = businessObjectService; |
547 | 0 | } |
548 | |
|
549 | |
protected MaintenanceDocumentService getMaintenanceDocumentService() { |
550 | 0 | if (maintenanceDocumentService == null) { |
551 | 0 | maintenanceDocumentService = KRADServiceLocatorWeb.getMaintenanceDocumentService(); |
552 | |
} |
553 | 0 | return maintenanceDocumentService; |
554 | |
} |
555 | |
|
556 | |
public void setMaintenanceDocumentService(MaintenanceDocumentService maintenanceDocumentService) { |
557 | 0 | this.maintenanceDocumentService = maintenanceDocumentService; |
558 | 0 | } |
559 | |
} |