1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.fp.document.validation.impl;
17
18 import java.util.List;
19 import java.util.Set;
20
21 import org.kuali.ole.fp.document.DisbursementVoucherConstants;
22 import org.kuali.ole.fp.document.DisbursementVoucherDocument;
23 import org.kuali.ole.sys.OLEKeyConstants;
24 import org.kuali.ole.sys.context.SpringContext;
25 import org.kuali.ole.sys.document.validation.GenericValidation;
26 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
27 import org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants;
28 import org.kuali.rice.kew.api.action.ActionRequest;
29 import org.kuali.rice.kew.api.document.WorkflowDocumentService;
30 import org.kuali.rice.kew.api.exception.WorkflowException;
31 import org.kuali.rice.krad.bo.Note;
32 import org.kuali.rice.krad.service.DocumentService;
33 import org.kuali.rice.krad.util.GlobalVariables;
34
35
36
37
38 public class DisbursementVoucherCampusSpecialHandlingValidation extends GenericValidation {
39 protected DisbursementVoucherDocument disbursementVoucherDocumentForValidation;
40 protected DocumentService documentService;
41
42 public static final String DOCUMENT_EDITOR_ROLE_NAME = "Document Editor";
43
44
45
46
47
48 public boolean validate(AttributedDocumentEvent event) {
49 boolean result = true;
50
51 if (isAtNodeToCheck()) {
52 final DisbursementVoucherDocument persistedDocument = getPersistedDisbursementVoucherDocument();
53 if (isSpecialHandlingChanged(persistedDocument) && !isNoteAdded()) {
54 result = false;
55 GlobalVariables.getMessageMap().putError(AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX+"disbVchrSpecialHandlingCode", OLEKeyConstants.ERROR_DV_CAMPUS_TURNED_OFF_SPECIAL_HANDLING_WITHOUT_EXPLANATORY_NOTE, new String[] {});
56 }
57 }
58
59 return result;
60 }
61
62
63
64
65
66 protected boolean isAtNodeToCheck() {
67 Set<String> currentNodes = getDisbursementVoucherDocumentForValidation().getDocumentHeader().getWorkflowDocument().getCurrentNodeNames();
68 return (!currentNodes.contains(DisbursementVoucherConstants.RouteLevelNames.PURCHASING));
69 }
70
71
72
73
74
75
76 protected DisbursementVoucherDocument getPersistedDisbursementVoucherDocument() {
77 try {
78 return (DisbursementVoucherDocument)getDocumentService().getByDocumentHeaderId(getDisbursementVoucherDocumentForValidation().getDocumentNumber());
79 }
80 catch (WorkflowException we) {
81 throw new RuntimeException("Could not retrieve persisted version of document "+getDisbursementVoucherDocumentForValidation().getDocumentNumber()+" for Special Handling validation", we);
82 }
83 }
84
85
86
87
88
89
90 protected boolean isSpecialHandlingChanged(DisbursementVoucherDocument persistedDocument) {
91 return persistedDocument.isDisbVchrSpecialHandlingCode() != getDisbursementVoucherDocumentForValidation().isDisbVchrSpecialHandlingCode();
92 }
93
94
95
96
97
98
99 protected boolean isNoteAdded() {
100 boolean foundNoteByCurrentApprover = false;
101 int count = 0;
102 final int noteCount = getDisbursementVoucherDocumentForValidation().getNotes().size();
103 while (!foundNoteByCurrentApprover && count < noteCount) {
104 foundNoteByCurrentApprover |= noteAddedByApproverForCurrentNode(getDisbursementVoucherDocumentForValidation().getNote(count));
105 count += 1;
106 }
107 return foundNoteByCurrentApprover;
108 }
109
110
111
112
113
114
115 protected boolean noteAddedByApproverForCurrentNode(Note note) {
116 List<ActionRequest> actionRequests = null;
117 try {
118 actionRequests = SpringContext.getBean(WorkflowDocumentService.class).getActionRequestsForPrincipalAtNode(getDisbursementVoucherDocumentForValidation().getDocumentNumber(), getDisbursementVoucherDocumentForValidation().getDocumentHeader().getWorkflowDocument().getCurrentNodeNames().iterator().next(), note.getAuthorUniversalIdentifier());
119 } catch (NumberFormatException nfe) {
120 throw new RuntimeException("Could not convert Disbursement Voucher document number "+getDisbursementVoucherDocumentForValidation().getDocumentNumber()+" to long", nfe);
121 }
122 return actionRequests != null && !actionRequests.isEmpty();
123 }
124
125
126
127
128
129
130 protected int getNoteCount(DisbursementVoucherDocument dvDoc) {
131 return dvDoc.getNotes().size();
132 }
133
134
135
136
137
138 public DisbursementVoucherDocument getDisbursementVoucherDocumentForValidation() {
139 return disbursementVoucherDocumentForValidation;
140 }
141
142
143
144
145
146 public void setDisbursementVoucherDocumentForValidation(DisbursementVoucherDocument disbursementVoucherDocumentForValidation) {
147 this.disbursementVoucherDocumentForValidation = disbursementVoucherDocumentForValidation;
148 }
149
150
151
152
153
154 public void setDocumentService(DocumentService documentService) {
155 this.documentService = documentService;
156 }
157
158 public DocumentService getDocumentService() {
159 return documentService;
160 }
161 }