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