View Javadoc

1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Validates that if a disbursement voucher had special handling turned off at the campus node, an extra note explaining that change has been added.
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       * Carries out the validation
46       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
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       * Determines if the DisbursementVoucherDocumentForValidation is at the Campus route node 
64       * @return true if the document is at the campus route node, false otherwise
65       */
66      protected boolean isAtNodeToCheck() {
67          Set<String> currentNodes = getDisbursementVoucherDocumentForValidation().getDocumentHeader().getWorkflowDocument().getCurrentNodeNames();
68          return (!currentNodes.contains(DisbursementVoucherConstants.RouteLevelNames.PURCHASING));
69      }
70      
71      /**
72       * Retrieves from the persistence store the persisted version of the given document
73       * @param document the document to find the persisted version of
74       * @return the persisted version of that document
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       * Determines if special handling was turned off from the DisbursementVoucherDocumentForValidation
87       * @param persistedDocument the persisted version of the document
88       * @return true if special handling was turned off, false otherwise
89       */
90      protected boolean isSpecialHandlingChanged(DisbursementVoucherDocument persistedDocument) {
91          return persistedDocument.isDisbVchrSpecialHandlingCode() != getDisbursementVoucherDocumentForValidation().isDisbVchrSpecialHandlingCode();
92      }
93      
94      /**
95       * Determines if another note was added from the time the DisbursementVoucherDocumentForValidation was persisted
96       * @param persistedDocument the persisted version of the document
97       * @return true if an extra note was added, false otherwise
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      * Determines if the given note was added by the current approver
112      * @param note the note to see added
113      * @return true if the note was added by the current approver, false otherwise
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      * Determines the count of notes on the given document
127      * @param dvDoc a document to find the count of notes on
128      * @return the count of notes on the document
129      */
130     protected int getNoteCount(DisbursementVoucherDocument dvDoc) {
131         return dvDoc.getNotes().size();
132     }
133 
134     /**
135      * Gets the disbursementVoucherDocumentForValidation attribute. 
136      * @return Returns the disbursementVoucherDocumentForValidation.
137      */
138     public DisbursementVoucherDocument getDisbursementVoucherDocumentForValidation() {
139         return disbursementVoucherDocumentForValidation;
140     }
141 
142     /**
143      * Sets the disbursementVoucherDocumentForValidation attribute value.
144      * @param disbursementVoucherDocumentForValidation The disbursementVoucherDocumentForValidation to set.
145      */
146     public void setDisbursementVoucherDocumentForValidation(DisbursementVoucherDocument disbursementVoucherDocumentForValidation) {
147         this.disbursementVoucherDocumentForValidation = disbursementVoucherDocumentForValidation;
148     }
149 
150     /**
151      * Sets the documentService attribute value.
152      * @param documentService The documentService to set.
153      */
154     public void setDocumentService(DocumentService documentService) {
155         this.documentService = documentService;
156     }
157  
158     public DocumentService getDocumentService() {
159         return documentService;
160     }
161 }