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