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