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