View Javadoc
1   /*
2    * Copyright 2008 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  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.fp.document.DisbursementVoucherConstants;
22  import org.kuali.ole.fp.document.DisbursementVoucherDocument;
23  import org.kuali.ole.sys.OLEConstants;
24  import org.kuali.ole.sys.OLEKeyConstants;
25  import org.kuali.ole.sys.OLEPropertyConstants;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.document.AccountingDocument;
28  import org.kuali.ole.sys.document.validation.GenericValidation;
29  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30  import org.kuali.rice.kns.service.DictionaryValidationService;
31  import org.kuali.rice.krad.bo.Note;
32  import org.kuali.rice.krad.service.NoteService;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.MessageMap;
35  
36  public class DisbursementVoucherDocumentFieldValidation extends GenericValidation {
37      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherDocumentFieldValidation.class);
38  
39      private AccountingDocument accountingDocumentForValidation;
40  
41      /**
42       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
43       */
44      public boolean validate(AttributedDocumentEvent event) {
45          LOG.debug("validate start");
46          boolean isValid = true;
47  
48          DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
49  
50          MessageMap errors = GlobalVariables.getMessageMap();
51  
52          // validate document required fields
53          SpringContext.getBean(DictionaryValidationService.class).validateDocument(document);
54  
55          // validate payee fields
56          errors.addToErrorPath(OLEPropertyConstants.DOCUMENT);
57          errors.addToErrorPath(OLEPropertyConstants.DV_PAYEE_DETAIL);
58          SpringContext.getBean(DictionaryValidationService.class).validateBusinessObject(document.getDvPayeeDetail());
59          errors.removeFromErrorPath(OLEPropertyConstants.DV_PAYEE_DETAIL);
60          errors.removeFromErrorPath(OLEPropertyConstants.DOCUMENT);
61  
62          //hasErrors() returns true if it not empty else false.  
63          if (errors.hasErrors()) {
64              return false;
65          }
66  
67          /* special handling name & address required if special handling is indicated */
68          if (document.isDisbVchrSpecialHandlingCode()) {
69              if (StringUtils.isBlank(document.getDvPayeeDetail().getDisbVchrSpecialHandlingPersonName()) || StringUtils.isBlank(document.getDvPayeeDetail().getDisbVchrSpecialHandlingLine1Addr())) {
70                  errors.putErrorWithoutFullErrorPath(OLEConstants.GENERAL_SPECHAND_TAB_ERRORS, OLEKeyConstants.ERROR_DV_SPECIAL_HANDLING);
71                  isValid = false;
72              }
73          }
74  
75          boolean hasNoNotes = this.hasNoNotes(document);
76  
77          /* if no documentation is selected, must be a note explaining why */
78          if (DisbursementVoucherConstants.NO_DOCUMENTATION_LOCATION.equals(document.getDisbursementVoucherDocumentationLocationCode()) && hasNoNotes) {
79              errors.putError(OLEPropertyConstants.DISBURSEMENT_VOUCHER_DOCUMENTATION_LOCATION_CODE, OLEKeyConstants.ERROR_DV_NO_DOCUMENTATION_NOTE_MISSING);
80              isValid = false;
81          }
82  
83          /* if special handling indicated, must be a note explaining why */
84          if (document.isDisbVchrSpecialHandlingCode() && hasNoNotes) {
85              errors.putErrorWithoutFullErrorPath(OLEConstants.GENERAL_PAYMENT_TAB_ERRORS, OLEKeyConstants.ERROR_DV_SPECIAL_HANDLING_NOTE_MISSING);
86              isValid = false;
87          }
88  
89          /* if exception attached indicated, must be a note explaining why */
90          if (document.isExceptionIndicator() && hasNoNotes) {
91              errors.putErrorWithoutFullErrorPath(OLEConstants.GENERAL_PAYMENT_TAB_ERRORS, OLEKeyConstants.ERROR_DV_EXCEPTION_ATTACHED_NOTE_MISSING);
92              isValid = false;
93          }
94  
95          return isValid;
96      }
97  
98      /**
99       * Return true if disbursement voucher does not have any notes
100      * 
101      * @param document submitted disbursement voucher document
102      * @return whether the given document has no notes
103      */
104     protected boolean hasNoNotes(DisbursementVoucherDocument document) {
105         List<Note> notes = document.getNotes();
106 
107         return (notes == null || notes.isEmpty());
108     }
109 
110     /**
111      * Sets the accountingDocumentForValidation attribute value.
112      * 
113      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
114      */
115     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
116         this.accountingDocumentForValidation = accountingDocumentForValidation;
117     }
118 
119     /**
120      * Gets the accountingDocumentForValidation attribute.
121      * 
122      * @return Returns the accountingDocumentForValidation.
123      */
124     public AccountingDocument getAccountingDocumentForValidation() {
125         return accountingDocumentForValidation;
126     }
127 }