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