1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.rules;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.CoreApiServiceLocator;
20 import org.kuali.rice.core.api.config.property.ConfigurationService;
21 import org.kuali.rice.core.api.datetime.DateTimeService;
22 import org.kuali.rice.core.api.mo.common.active.MutableInactivatable;
23 import org.kuali.rice.core.api.util.RiceKeyConstants;
24 import org.kuali.rice.core.web.format.Formatter;
25 import org.kuali.rice.kew.api.WorkflowDocument;
26 import org.kuali.rice.kim.api.identity.PersonService;
27 import org.kuali.rice.kim.api.role.RoleService;
28 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29 import org.kuali.rice.krad.bo.GlobalBusinessObject;
30 import org.kuali.rice.krad.bo.PersistableBusinessObject;
31 import org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata;
32 import org.kuali.rice.krad.document.Document;
33 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
34 import org.kuali.rice.krad.exception.ValidationException;
35 import org.kuali.rice.krad.maintenance.Maintainable;
36 import org.kuali.rice.krad.maintenance.MaintenanceDocumentAuthorizer;
37 import org.kuali.rice.krad.rules.rule.event.ApproveDocumentEvent;
38 import org.kuali.rice.krad.service.BusinessObjectService;
39 import org.kuali.rice.krad.service.DataDictionaryService;
40 import org.kuali.rice.krad.service.DataObjectAuthorizationService;
41 import org.kuali.rice.krad.service.DataObjectMetaDataService;
42 import org.kuali.rice.krad.service.DictionaryValidationService;
43 import org.kuali.rice.krad.service.InactivationBlockingDetectionService;
44 import org.kuali.rice.krad.service.KRADServiceLocator;
45 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
46 import org.kuali.rice.krad.service.PersistenceStructureService;
47 import org.kuali.rice.krad.util.ErrorMessage;
48 import org.kuali.rice.krad.util.ForeignKeyFieldsPopulationState;
49 import org.kuali.rice.krad.util.GlobalVariables;
50 import org.kuali.rice.krad.util.KRADConstants;
51 import org.kuali.rice.krad.util.KRADPropertyConstants;
52 import org.kuali.rice.krad.util.ObjectUtils;
53 import org.kuali.rice.krad.util.UrlFactory;
54 import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
55 import org.springframework.util.AutoPopulatingList;
56
57 import java.security.GeneralSecurityException;
58 import java.util.ArrayList;
59 import java.util.Iterator;
60 import java.util.List;
61 import java.util.Map;
62 import java.util.Properties;
63 import java.util.Set;
64
65
66
67
68
69
70 public class MaintenanceDocumentRuleBase extends DocumentRuleBase implements MaintenanceDocumentRule {
71 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MaintenanceDocumentRuleBase.class);
72
73
74
75 public static final String MAINTAINABLE_ERROR_PREFIX = KRADConstants.MAINTENANCE_NEW_MAINTAINABLE;
76 public static final String DOCUMENT_ERROR_PREFIX = "document.";
77 public static final String MAINTAINABLE_ERROR_PATH = DOCUMENT_ERROR_PREFIX + "newMaintainableObject";
78
79 private PersistenceStructureService persistenceStructureService;
80 private DataDictionaryService ddService;
81 private BusinessObjectService boService;
82 private DictionaryValidationService dictionaryValidationService;
83 private ConfigurationService configService;
84 private WorkflowDocumentService workflowDocumentService;
85 private PersonService personService;
86 private RoleService roleService;
87 private DataObjectMetaDataService dataObjectMetaDataService;
88 private DataObjectAuthorizationService dataObjectAuthorizationService;
89
90 private Object oldDataObject;
91 private Object newDataObject;
92 private Class dataObjectClass;
93
94 protected List priorErrorPath;
95
96
97
98
99 public MaintenanceDocumentRuleBase() {
100 priorErrorPath = new ArrayList();
101 }
102
103
104
105
106 @Override
107 public boolean processSaveDocument(Document document) {
108 MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
109
110
111
112 clearErrorPath();
113
114
115 setupBaseConvenienceObjects(maintenanceDocument);
116
117
118
119
120
121 if (!isDocumentValidForSave(maintenanceDocument)) {
122 resumeErrorPath();
123 return false;
124 }
125
126
127
128
129 processCustomSaveDocumentBusinessRules(maintenanceDocument);
130
131
132 resumeErrorPath();
133
134
135
136 return true;
137 }
138
139
140
141
142 @Override
143 public boolean processRouteDocument(Document document) {
144 LOG.info("processRouteDocument called");
145
146 MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
147
148
149 MaintenanceDocumentAuthorizer documentAuthorizer =
150 (MaintenanceDocumentAuthorizer) getDocumentDictionaryService().getDocumentAuthorizer(document);
151
152
153
154 clearErrorPath();
155
156
157 setupBaseConvenienceObjects(maintenanceDocument);
158
159
160 processGlobalSaveDocumentBusinessRules(maintenanceDocument);
161
162
163
164 boolean success = true;
165
166 WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
167 if (workflowDocument.isInitiated() || workflowDocument.isSaved()){
168 success &= documentAuthorizer.canCreateOrMaintain((MaintenanceDocument)document, GlobalVariables.getUserSession().getPerson());
169 if (success == false) {
170 GlobalVariables.getMessageMap()
171 .putError(KRADConstants.DOCUMENT_ERRORS, RiceKeyConstants.AUTHORIZATION_ERROR_DOCUMENT,
172 new String[]{GlobalVariables.getUserSession().getPerson().getPrincipalName(),
173 "Create/Maintain", getDocumentDictionaryService()
174 .getMaintenanceDocumentTypeName(newDataObject.getClass())});
175 }
176 }
177
178 success &= processGlobalRouteDocumentBusinessRules(maintenanceDocument);
179
180
181
182
183 success &= processCustomRouteDocumentBusinessRules(maintenanceDocument);
184
185 success &= processInactivationBlockChecking(maintenanceDocument);
186
187
188
189 resumeErrorPath();
190
191 return success;
192 }
193
194
195
196
197
198
199
200 protected boolean isDocumentInactivatingBusinessObject(MaintenanceDocument maintenanceDocument) {
201 if (maintenanceDocument.isEdit()) {
202 Class dataObjectClass = maintenanceDocument.getNewMaintainableObject().getDataObjectClass();
203
204 if (dataObjectClass != null && MutableInactivatable.class.isAssignableFrom(dataObjectClass)) {
205 MutableInactivatable oldInactivateableBO = (MutableInactivatable) oldDataObject;
206 MutableInactivatable newInactivateableBO = (MutableInactivatable) newDataObject;
207
208 return oldInactivateableBO.isActive() && !newInactivateableBO.isActive();
209 }
210 }
211 return false;
212 }
213
214
215
216
217
218
219
220 protected boolean processInactivationBlockChecking(MaintenanceDocument maintenanceDocument) {
221 if (isDocumentInactivatingBusinessObject(maintenanceDocument)) {
222 Class dataObjectClass = maintenanceDocument.getNewMaintainableObject().getDataObjectClass();
223 Set<InactivationBlockingMetadata> inactivationBlockingMetadatas =
224 getDataDictionaryService().getAllInactivationBlockingDefinitions(dataObjectClass);
225
226 if (inactivationBlockingMetadatas != null) {
227 for (InactivationBlockingMetadata inactivationBlockingMetadata : inactivationBlockingMetadatas) {
228
229
230
231 if (!processInactivationBlockChecking(maintenanceDocument, inactivationBlockingMetadata)) {
232 return false;
233 }
234 }
235 }
236 }
237 return true;
238 }
239
240
241
242
243
244
245
246
247
248
249 protected boolean processInactivationBlockChecking(MaintenanceDocument maintenanceDocument,
250 InactivationBlockingMetadata inactivationBlockingMetadata) {
251 if (newDataObject instanceof PersistableBusinessObject) {
252 String inactivationBlockingDetectionServiceBeanName =
253 inactivationBlockingMetadata.getInactivationBlockingDetectionServiceBeanName();
254 if (StringUtils.isBlank(inactivationBlockingDetectionServiceBeanName)) {
255 inactivationBlockingDetectionServiceBeanName =
256 KRADServiceLocatorWeb.DEFAULT_INACTIVATION_BLOCKING_DETECTION_SERVICE;
257 }
258 InactivationBlockingDetectionService inactivationBlockingDetectionService = KRADServiceLocatorWeb
259 .getInactivationBlockingDetectionService(inactivationBlockingDetectionServiceBeanName);
260
261 boolean foundBlockingRecord = inactivationBlockingDetectionService
262 .hasABlockingRecord((PersistableBusinessObject) newDataObject, inactivationBlockingMetadata);
263
264 if (foundBlockingRecord) {
265 putInactivationBlockingErrorOnPage(maintenanceDocument, inactivationBlockingMetadata);
266 }
267
268 return !foundBlockingRecord;
269 }
270
271 return true;
272 }
273
274
275
276
277
278
279
280
281 protected void putInactivationBlockingErrorOnPage(MaintenanceDocument document,
282 InactivationBlockingMetadata inactivationBlockingMetadata) {
283 if (!getPersistenceStructureService().hasPrimaryKeyFieldValues(newDataObject)) {
284 throw new RuntimeException("Maintenance document did not have all primary key values filled in.");
285 }
286 Properties parameters = new Properties();
287 parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE,
288 inactivationBlockingMetadata.getBlockedBusinessObjectClass().getName());
289 parameters
290 .put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.METHOD_DISPLAY_ALL_INACTIVATION_BLOCKERS);
291
292 List keys = new ArrayList();
293 if (getPersistenceStructureService().isPersistable(newDataObject.getClass())) {
294 keys = getPersistenceStructureService().listPrimaryKeyFieldNames(newDataObject.getClass());
295 }
296
297
298 String keyName = null;
299 for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
300 keyName = (String) iter.next();
301
302 Object keyValue = null;
303 if (keyName != null) {
304 keyValue = ObjectUtils.getPropertyValue(newDataObject, keyName);
305 }
306
307 if (keyValue == null) {
308 keyValue = "";
309 } else if (keyValue instanceof java.sql.Date) {
310 if (Formatter.findFormatter(keyValue.getClass()) != null) {
311 Formatter formatter = Formatter.getFormatter(keyValue.getClass());
312 keyValue = (String) formatter.format(keyValue);
313 }
314 } else {
315 keyValue = keyValue.toString();
316 }
317
318
319 if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(
320 inactivationBlockingMetadata.getBlockedBusinessObjectClass(), keyName)){
321 try {
322 keyValue = CoreApiServiceLocator.getEncryptionService().encrypt(keyValue);
323 } catch (GeneralSecurityException e) {
324 LOG.error("Exception while trying to encrypted value for inquiry framework.", e);
325 throw new RuntimeException(e);
326 }
327 }
328
329 parameters.put(keyName, keyValue);
330 }
331
332 String blockingUrl =
333 UrlFactory.parameterizeUrl(KRADConstants.DISPLAY_ALL_INACTIVATION_BLOCKERS_ACTION, parameters);
334
335
336 GlobalVariables.getMessageMap()
337 .putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_INACTIVATION_BLOCKED, blockingUrl);
338 }
339
340
341
342
343 @Override
344 public boolean processApproveDocument(ApproveDocumentEvent approveEvent) {
345 MaintenanceDocument maintenanceDocument = (MaintenanceDocument) approveEvent.getDocument();
346
347
348
349 clearErrorPath();
350
351
352 setupBaseConvenienceObjects(maintenanceDocument);
353
354
355 processGlobalSaveDocumentBusinessRules(maintenanceDocument);
356
357
358
359 boolean success = true;
360
361
362 success &= processGlobalApproveDocumentBusinessRules(maintenanceDocument);
363
364
365
366
367 success &= processCustomApproveDocumentBusinessRules(maintenanceDocument);
368
369
370
371 resumeErrorPath();
372
373 return success;
374 }
375
376
377
378
379
380
381
382
383 protected void putGlobalError(String errorConstant) {
384 if (!errorAlreadyExists(KRADConstants.DOCUMENT_ERRORS, errorConstant)) {
385 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.DOCUMENT_ERRORS, errorConstant);
386 }
387 }
388
389
390
391
392
393
394
395
396
397 protected void putGlobalError(String errorConstant, String parameter) {
398 if (!errorAlreadyExists(KRADConstants.DOCUMENT_ERRORS, errorConstant)) {
399 GlobalVariables.getMessageMap()
400 .putErrorWithoutFullErrorPath(KRADConstants.DOCUMENT_ERRORS, errorConstant, parameter);
401 }
402 }
403
404
405
406
407
408
409
410
411
412 protected void putGlobalError(String errorConstant, String[] parameters) {
413 if (!errorAlreadyExists(KRADConstants.DOCUMENT_ERRORS, errorConstant)) {
414 GlobalVariables.getMessageMap()
415 .putErrorWithoutFullErrorPath(KRADConstants.DOCUMENT_ERRORS, errorConstant, parameters);
416 }
417 }
418
419
420
421
422
423
424
425
426
427
428
429
430 protected void putFieldError(String propertyName, String errorConstant) {
431 if (!errorAlreadyExists(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant)) {
432 GlobalVariables.getMessageMap()
433 .putErrorWithoutFullErrorPath(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant);
434 }
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 protected void putFieldError(String propertyName, String errorConstant, String parameter) {
453 if (!errorAlreadyExists(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant)) {
454 GlobalVariables.getMessageMap()
455 .putErrorWithoutFullErrorPath(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant, parameter);
456 }
457 }
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473 protected void putFieldError(String propertyName, String errorConstant, String[] parameters) {
474 if (!errorAlreadyExists(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant)) {
475 GlobalVariables.getMessageMap()
476 .putErrorWithoutFullErrorPath(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant, parameters);
477 }
478 }
479
480
481
482
483
484
485
486
487
488 protected void putFieldErrorWithShortLabel(String propertyName, String errorConstant) {
489 String shortLabel = getDataDictionaryService().getAttributeShortLabel(dataObjectClass, propertyName);
490 putFieldError(propertyName, errorConstant, shortLabel);
491 }
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508 protected void putDocumentError(String propertyName, String errorConstant, String parameter) {
509 if (!errorAlreadyExists(DOCUMENT_ERROR_PREFIX + propertyName, errorConstant)) {
510 GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + propertyName, errorConstant, parameter);
511 }
512 }
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528 protected void putDocumentError(String propertyName, String errorConstant, String[] parameters) {
529 GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + propertyName, errorConstant, parameters);
530 }
531
532
533
534
535
536
537
538
539
540
541 protected boolean errorAlreadyExists(String propertyName, String errorConstant) {
542 if (GlobalVariables.getMessageMap().fieldHasMessage(propertyName, errorConstant)) {
543 return true;
544 } else {
545 return false;
546 }
547 }
548
549
550
551
552
553
554
555
556
557 protected void putGlobalsError(String propertyName, String errorConstant) {
558 if (!errorAlreadyExists(propertyName, errorConstant)) {
559 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(propertyName, errorConstant);
560 }
561 }
562
563
564
565
566
567
568
569
570
571
572 protected void putGlobalsError(String propertyName, String errorConstant, String parameter) {
573 if (!errorAlreadyExists(propertyName, errorConstant)) {
574 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(propertyName, errorConstant, parameter);
575 }
576 }
577
578
579
580
581
582
583
584
585
586
587
588
589
590 protected void clearErrorPath() {
591
592 priorErrorPath.addAll(GlobalVariables.getMessageMap().getErrorPath());
593
594
595 GlobalVariables.getMessageMap().getErrorPath().clear();
596 }
597
598
599
600
601
602
603
604
605
606
607
608 protected void resumeErrorPath() {
609
610
611 GlobalVariables.getMessageMap().getErrorPath().addAll(priorErrorPath);
612 }
613
614
615
616
617
618
619
620 protected boolean dataDictionaryValidate(MaintenanceDocument document) {
621 LOG.debug("MaintenanceDocument validation beginning");
622
623
624
625 GlobalVariables.getMessageMap().addToErrorPath("document.newMaintainableObject");
626
627
628 Maintainable newMaintainable = document.getNewMaintainableObject();
629 if (newMaintainable == null) {
630 GlobalVariables.getMessageMap().removeFromErrorPath("document.newMaintainableObject");
631 throw new ValidationException(
632 "Maintainable object from Maintenance Document '" + document.getDocumentTitle() +
633 "' is null, unable to proceed.");
634 }
635
636
637 Object dataObject = newMaintainable.getDataObject();
638 if (dataObject == null) {
639 GlobalVariables.getMessageMap().removeFromErrorPath("document.newMaintainableObject.");
640 throw new ValidationException("Maintainable's component business object is null.");
641 }
642
643 GlobalVariables.getMessageMap().addToErrorPath("dataObject");
644
645 getDictionaryValidationService().validate(newDataObject);
646
647 GlobalVariables.getMessageMap().removeFromErrorPath("dataObject");
648
649
650 GlobalVariables.getMessageMap().removeFromErrorPath("document.newMaintainableObject");
651
652 LOG.debug("MaintenanceDocument validation ending");
653 return true;
654 }
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672 protected boolean primaryKeyCheck(MaintenanceDocument document) {
673
674 boolean success = true;
675 Class<?> dataObjectClass = document.getNewMaintainableObject().getDataObjectClass();
676
677 Object oldBo = document.getOldMaintainableObject().getDataObject();
678 Object newDataObject = document.getNewMaintainableObject().getDataObject();
679
680
681
682
683
684
685 if (newDataObject instanceof GlobalBusinessObject) {
686 return success;
687 }
688
689
690
691 if (document.isEdit()) {
692 if (!getDataObjectMetaDataService().equalsByPrimaryKeys(oldBo, newDataObject)) {
693
694 putDocumentError(KRADConstants.DOCUMENT_ERRORS,
695 RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_PRIMARY_KEYS_CHANGED_ON_EDIT,
696 getHumanReadablePrimaryKeyFieldNames(dataObjectClass));
697 success &= false;
698 }
699 }
700
701
702
703 else if (document.isNew()) {
704
705
706 if (newDataObject instanceof PersistableBusinessObject) {
707
708
709 Map<String, ?> newPkFields = getDataObjectMetaDataService().getPrimaryKeyFieldValues(newDataObject);
710
711
712
713
714
715
716
717 PersistableBusinessObject testBo = getBoService()
718 .findByPrimaryKey(dataObjectClass.asSubclass(PersistableBusinessObject.class), newPkFields);
719
720
721
722 if (testBo != null) {
723 putDocumentError(KRADConstants.DOCUMENT_ERRORS,
724 RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_KEYS_ALREADY_EXIST_ON_CREATE_NEW,
725 getHumanReadablePrimaryKeyFieldNames(dataObjectClass));
726 success &= false;
727 }
728 }
729 }
730
731 return success;
732 }
733
734
735
736
737
738
739
740
741 protected String getHumanReadablePrimaryKeyFieldNames(Class<?> dataObjectClass) {
742 String delim = "";
743 StringBuffer pkFieldNames = new StringBuffer();
744
745
746 List<String> pkFields = getDataObjectMetaDataService().listPrimaryKeyFieldNames(dataObjectClass);
747 for (Iterator<String> iter = pkFields.iterator(); iter.hasNext(); ) {
748 String pkFieldName = (String) iter.next();
749
750
751
752 String humanReadableFieldName = getDataDictionaryService().getAttributeLabel(dataObjectClass, pkFieldName);
753
754
755 pkFieldNames.append(delim + humanReadableFieldName);
756
757
758 if (delim.equalsIgnoreCase("")) {
759 delim = ", ";
760 }
761 }
762
763 return pkFieldNames.toString();
764 }
765
766
767
768
769
770
771
772
773
774
775
776
777
778 protected boolean processGlobalApproveDocumentBusinessRules(MaintenanceDocument document) {
779 return true;
780 }
781
782
783
784
785
786
787
788
789
790
791
792
793
794 protected boolean processGlobalRouteDocumentBusinessRules(MaintenanceDocument document) {
795 boolean success = true;
796
797
798 success &= checkEmptyDocumentField(
799 KRADPropertyConstants.DOCUMENT_HEADER + "." + KRADPropertyConstants.DOCUMENT_DESCRIPTION,
800 document.getDocumentHeader().getDocumentDescription(), "Description");
801
802 return success;
803 }
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823 protected boolean processGlobalSaveDocumentBusinessRules(MaintenanceDocument document) {
824
825 boolean success = true;
826
827
828 primaryKeyCheck(document);
829
830
831
832 this.dataDictionaryValidate(document);
833
834 return success;
835 }
836
837
838
839
840
841
842
843 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
844 return true;
845 }
846
847
848
849
850
851
852
853 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
854 return true;
855 }
856
857
858
859
860
861
862
863 protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
864 return true;
865 }
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880 protected boolean isDocumentValidForSave(MaintenanceDocument maintenanceDocument) {
881
882 boolean success = true;
883
884 success &= super.isDocumentOverviewValid(maintenanceDocument);
885 success &= validateDocumentStructure((Document) maintenanceDocument);
886 success &= validateMaintenanceDocument(maintenanceDocument);
887 success &= validateGlobalBusinessObjectPersistable(maintenanceDocument);
888 return success;
889 }
890
891
892
893
894
895
896
897
898
899
900
901 protected boolean validateDocumentStructure(Document document) {
902 boolean success = true;
903
904
905 String documentHeaderId = document.getDocumentNumber();
906 if (documentHeaderId == null || StringUtils.isEmpty(documentHeaderId)) {
907 throw new ValidationException("Document has no document number, unable to proceed.");
908 }
909
910 return success;
911 }
912
913
914
915
916
917
918
919
920
921
922
923
924 protected boolean validateMaintenanceDocument(MaintenanceDocument maintenanceDocument) {
925 boolean success = true;
926 Maintainable newMaintainable = maintenanceDocument.getNewMaintainableObject();
927
928
929 if (newMaintainable == null) {
930 throw new ValidationException(
931 "Maintainable object from Maintenance Document '" + maintenanceDocument.getDocumentTitle() +
932 "' is null, unable to proceed.");
933 }
934
935
936 if (newMaintainable.getDataObject() == null) {
937 throw new ValidationException("Maintainable's component data object is null.");
938 }
939
940 return success;
941 }
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956 protected boolean validateGlobalBusinessObjectPersistable(MaintenanceDocument document) {
957 boolean success = true;
958
959 if (document.getNewMaintainableObject() == null) {
960 return success;
961 }
962 if (document.getNewMaintainableObject().getDataObject() == null) {
963 return success;
964 }
965 if (!(document.getNewMaintainableObject().getDataObject() instanceof GlobalBusinessObject)) {
966 return success;
967 }
968
969 PersistableBusinessObject bo = (PersistableBusinessObject) document.getNewMaintainableObject().getDataObject();
970 GlobalBusinessObject gbo = (GlobalBusinessObject) bo;
971 return gbo.isPersistable();
972 }
973
974
975
976
977
978
979
980
981
982
983 protected boolean isCorrectMaintenanceClass(MaintenanceDocument document, Class clazz) {
984
985 if (document == null || clazz == null) {
986 throw new IllegalArgumentException("Null arguments were passed in.");
987 }
988
989
990 if (clazz.toString().equals(document.getNewMaintainableObject().getDataObjectClass().toString())) {
991 return true;
992 } else {
993 return false;
994 }
995 }
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008 protected boolean checkEmptyBOField(String propertyName, Object valueToTest, String parameter) {
1009 boolean success = true;
1010
1011 success = checkEmptyValue(valueToTest);
1012
1013
1014 if (!success) {
1015 putFieldError(propertyName, RiceKeyConstants.ERROR_REQUIRED, parameter);
1016 }
1017
1018 return success;
1019 }
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 protected boolean checkEmptyDocumentField(String propertyName, Object valueToTest, String parameter) {
1034 boolean success = true;
1035 success = checkEmptyValue(valueToTest);
1036 if (!success) {
1037 putDocumentError(propertyName, RiceKeyConstants.ERROR_REQUIRED, parameter);
1038 }
1039 return success;
1040 }
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 protected boolean checkEmptyValue(Object valueToTest) {
1053 boolean success = true;
1054
1055
1056 if (valueToTest == null) {
1057 success = false;
1058 } else {
1059
1060 if (valueToTest instanceof String) {
1061 if (StringUtils.isBlank((String) valueToTest)) {
1062 success = false;
1063 }
1064 }
1065 }
1066
1067 return success;
1068 }
1069
1070
1071
1072
1073
1074
1075 protected void showErrorMap() {
1076 if (GlobalVariables.getMessageMap().hasNoErrors()) {
1077 return;
1078 }
1079
1080 for (Iterator i = GlobalVariables.getMessageMap().getAllPropertiesAndErrors().iterator(); i.hasNext(); ) {
1081 Map.Entry e = (Map.Entry) i.next();
1082
1083 AutoPopulatingList errorList = (AutoPopulatingList) e.getValue();
1084 for (Iterator j = errorList.iterator(); j.hasNext(); ) {
1085 ErrorMessage em = (ErrorMessage) j.next();
1086
1087 if (em.getMessageParameters() == null) {
1088 LOG.error(e.getKey().toString() + " = " + em.getErrorKey());
1089 } else {
1090 LOG.error(e.getKey().toString() + " = " + em.getErrorKey() + " : " +
1091 em.getMessageParameters().toString());
1092 }
1093 }
1094 }
1095 }
1096
1097
1098
1099
1100 public void setupBaseConvenienceObjects(MaintenanceDocument document) {
1101
1102 oldDataObject = document.getOldMaintainableObject().getDataObject();
1103 if (oldDataObject != null && oldDataObject instanceof PersistableBusinessObject) {
1104 ((PersistableBusinessObject) oldDataObject).refreshNonUpdateableReferences();
1105 }
1106
1107
1108 newDataObject = document.getNewMaintainableObject().getDataObject();
1109 if (newDataObject instanceof PersistableBusinessObject) {
1110 ((PersistableBusinessObject) newDataObject).refreshNonUpdateableReferences();
1111 }
1112
1113 dataObjectClass = document.getNewMaintainableObject().getDataObjectClass();
1114
1115
1116 setupConvenienceObjects();
1117 }
1118
1119 public void setupConvenienceObjects() {
1120
1121 }
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133 protected boolean checkForPartiallyFilledOutReferenceForeignKeys(String referenceName) {
1134 boolean success = true;
1135
1136 if (newDataObject instanceof PersistableBusinessObject) {
1137 ForeignKeyFieldsPopulationState fkFieldsState;
1138 fkFieldsState = getPersistenceStructureService()
1139 .getForeignKeyFieldsPopulationState((PersistableBusinessObject) newDataObject, referenceName);
1140
1141
1142 if (fkFieldsState.isAnyFieldsPopulated() && !fkFieldsState.isAllFieldsPopulated()) {
1143 success = false;
1144
1145
1146
1147
1148 List fKeys = new ArrayList(getPersistenceStructureService().getForeignKeysForReference(
1149 newDataObject.getClass().asSubclass(PersistableBusinessObject.class), referenceName).keySet());
1150 String fKeysReadable = consolidateFieldNames(fKeys, ", ").toString();
1151
1152
1153 for (Iterator iter = fkFieldsState.getUnpopulatedFieldNames().iterator(); iter.hasNext(); ) {
1154 String fieldName = (String) iter.next();
1155
1156
1157 String fieldNameReadable = getDataDictionaryService().getAttributeLabel(newDataObject.getClass(), fieldName);
1158
1159
1160 putFieldError(fieldName, RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_PARTIALLY_FILLED_OUT_REF_FKEYS,
1161 new String[]{fieldNameReadable, fKeysReadable});
1162 }
1163 }
1164 }
1165
1166 return success;
1167 }
1168
1169
1170
1171
1172
1173
1174
1175 protected StringBuffer consolidateFieldNames(List fieldNames, String delimiter) {
1176 StringBuffer sb = new StringBuffer();
1177
1178
1179 boolean firstPass = true;
1180 String delim = "";
1181
1182
1183 for (Iterator iter = fieldNames.iterator(); iter.hasNext(); ) {
1184 String fieldName = (String) iter.next();
1185
1186
1187
1188 sb.append(delim + getDataDictionaryService().getAttributeLabel(newDataObject.getClass(), fieldName));
1189
1190
1191 if (firstPass) {
1192 delim = delimiter;
1193 firstPass = false;
1194 }
1195 }
1196
1197 return sb;
1198 }
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208 protected String getFieldLabel(String fieldName) {
1209 return getDataDictionaryService().getAttributeLabel(newDataObject.getClass(), fieldName) + "(" +
1210 getDataDictionaryService().getAttributeShortLabel(newDataObject.getClass(), fieldName) + ")";
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222 protected String getFieldLabel(Class dataObjectClass, String fieldName) {
1223 return getDataDictionaryService().getAttributeLabel(dataObjectClass, fieldName) + "(" +
1224 getDataDictionaryService().getAttributeShortLabel(dataObjectClass, fieldName) + ")";
1225 }
1226
1227
1228
1229
1230
1231
1232 protected final Object getNewDataObject() {
1233 return newDataObject;
1234 }
1235
1236 protected void setNewDataObject(Object newDataObject) {
1237 this.newDataObject = newDataObject;
1238 }
1239
1240
1241
1242
1243
1244
1245 protected final Object getOldDataObject() {
1246 return oldDataObject;
1247 }
1248
1249 public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName,
1250 PersistableBusinessObject line) {
1251 return true;
1252 }
1253
1254 protected final BusinessObjectService getBoService() {
1255 if (boService == null) {
1256 this.boService = KRADServiceLocator.getBusinessObjectService();
1257 }
1258 return boService;
1259 }
1260
1261 public final void setBoService(BusinessObjectService boService) {
1262 this.boService = boService;
1263 }
1264
1265 protected final ConfigurationService getConfigService() {
1266 if (configService == null) {
1267 this.configService = KRADServiceLocator.getKualiConfigurationService();
1268 }
1269 return configService;
1270 }
1271
1272 public final void setConfigService(ConfigurationService configService) {
1273 this.configService = configService;
1274 }
1275
1276 protected final DataDictionaryService getDdService() {
1277 if (ddService == null) {
1278 this.ddService = KRADServiceLocatorWeb.getDataDictionaryService();
1279 }
1280 return ddService;
1281 }
1282
1283 public final void setDdService(DataDictionaryService ddService) {
1284 this.ddService = ddService;
1285 }
1286
1287 protected final DictionaryValidationService getDictionaryValidationService() {
1288 if (dictionaryValidationService == null) {
1289 this.dictionaryValidationService = KRADServiceLocatorWeb.getDictionaryValidationService();
1290 }
1291 return dictionaryValidationService;
1292 }
1293
1294 public final void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
1295 this.dictionaryValidationService = dictionaryValidationService;
1296 }
1297 public PersonService getPersonService() {
1298 if (personService == null) {
1299 this.personService = KimApiServiceLocator.getPersonService();
1300 }
1301 return personService;
1302 }
1303
1304 public void setPersonService(PersonService personService) {
1305 this.personService = personService;
1306 }
1307
1308 public DateTimeService getDateTimeService() {
1309 return CoreApiServiceLocator.getDateTimeService();
1310 }
1311
1312 protected RoleService getRoleService() {
1313 if (this.roleService == null) {
1314 this.roleService = KimApiServiceLocator.getRoleService();
1315 }
1316 return this.roleService;
1317 }
1318
1319 protected DataObjectMetaDataService getDataObjectMetaDataService() {
1320 if (dataObjectMetaDataService == null) {
1321 this.dataObjectMetaDataService = KRADServiceLocatorWeb.getDataObjectMetaDataService();
1322 }
1323 return dataObjectMetaDataService;
1324 }
1325
1326 public void setDataObjectMetaDataService(DataObjectMetaDataService dataObjectMetaDataService) {
1327 this.dataObjectMetaDataService = dataObjectMetaDataService;
1328 }
1329
1330 protected final PersistenceStructureService getPersistenceStructureService() {
1331 if (persistenceStructureService == null) {
1332 this.persistenceStructureService = KRADServiceLocator.getPersistenceStructureService();
1333 }
1334 return persistenceStructureService;
1335 }
1336
1337 public final void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
1338 this.persistenceStructureService = persistenceStructureService;
1339 }
1340
1341 public WorkflowDocumentService getWorkflowDocumentService() {
1342 if (workflowDocumentService == null) {
1343 this.workflowDocumentService = KRADServiceLocatorWeb.getWorkflowDocumentService();
1344 }
1345 return workflowDocumentService;
1346 }
1347
1348 public void setWorkflowDocumentService(WorkflowDocumentService workflowDocumentService) {
1349 this.workflowDocumentService = workflowDocumentService;
1350 }
1351
1352 public DataObjectAuthorizationService getDataObjectAuthorizationService() {
1353 if (dataObjectAuthorizationService == null) {
1354 this.dataObjectAuthorizationService = KRADServiceLocatorWeb.getDataObjectAuthorizationService();
1355 }
1356 return dataObjectAuthorizationService;
1357 }
1358
1359 public void setDataObjectAuthorizationService(DataObjectAuthorizationService dataObjectAuthorizationService) {
1360 this.dataObjectAuthorizationService = dataObjectAuthorizationService;
1361 }
1362
1363 }
1364