View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.fp.document.validation.impl;
20  
21  import java.util.List;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.kfs.fp.document.DisbursementVoucherConstants;
25  import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
26  import org.kuali.kfs.sys.KFSConstants;
27  import org.kuali.kfs.sys.KFSKeyConstants;
28  import org.kuali.kfs.sys.KFSPropertyConstants;
29  import org.kuali.kfs.sys.context.SpringContext;
30  import org.kuali.kfs.sys.document.AccountingDocument;
31  import org.kuali.kfs.sys.document.validation.GenericValidation;
32  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
33  import org.kuali.rice.kns.service.DictionaryValidationService;
34  import org.kuali.rice.krad.bo.Note;
35  import org.kuali.rice.krad.service.NoteService;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  import org.kuali.rice.krad.util.MessageMap;
38  
39  public class DisbursementVoucherDocumentFieldValidation extends GenericValidation {
40      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherDocumentFieldValidation.class);
41  
42      private AccountingDocument accountingDocumentForValidation;
43  
44      /**
45       * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
46       */
47      public boolean validate(AttributedDocumentEvent event) {
48          LOG.debug("validate start");
49          boolean isValid = true;
50  
51          DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
52  
53          MessageMap errors = GlobalVariables.getMessageMap();
54  
55          // validate document required fields
56          SpringContext.getBean(DictionaryValidationService.class).validateDocument(document);
57  
58          // validate payee fields
59          errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
60          errors.addToErrorPath(KFSPropertyConstants.DV_PAYEE_DETAIL);
61          SpringContext.getBean(DictionaryValidationService.class).validateBusinessObject(document.getDvPayeeDetail());
62          errors.removeFromErrorPath(KFSPropertyConstants.DV_PAYEE_DETAIL);
63          errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT);
64  
65          //hasErrors() returns true if it not empty else false.  
66          if (errors.hasErrors()) {
67              return false;
68          }
69  
70          /* special handling name & address required if special handling is indicated */
71          if (document.isDisbVchrSpecialHandlingCode()) {
72              if (StringUtils.isBlank(document.getDvPayeeDetail().getDisbVchrSpecialHandlingPersonName()) || StringUtils.isBlank(document.getDvPayeeDetail().getDisbVchrSpecialHandlingLine1Addr())) {
73                  errors.putErrorWithoutFullErrorPath(KFSConstants.GENERAL_SPECHAND_TAB_ERRORS, KFSKeyConstants.ERROR_SPECIAL_HANDLING);
74                  isValid = false;
75              }
76          }
77  
78          boolean hasNoNotes = this.hasNoNotes(document);
79  
80          /* if no documentation is selected, must be a note explaining why */
81          if (DisbursementVoucherConstants.NO_DOCUMENTATION_LOCATION.equals(document.getDisbursementVoucherDocumentationLocationCode()) && hasNoNotes) {
82              errors.putError(KFSPropertyConstants.DISBURSEMENT_VOUCHER_DOCUMENTATION_LOCATION_CODE, KFSKeyConstants.ERROR_DV_NO_DOCUMENTATION_NOTE_MISSING);
83              isValid = false;
84          }
85  
86          /* if special handling indicated, must be a note explaining why */
87          if (document.isDisbVchrSpecialHandlingCode() && hasNoNotes) {
88              errors.putErrorWithoutFullErrorPath(KFSConstants.GENERAL_PAYMENT_TAB_ERRORS, KFSKeyConstants.ERROR_SPECIAL_HANDLING_NOTE_MISSING);
89              isValid = false;
90          }
91  
92          /* if exception attached indicated, must be a note explaining why */
93          if (document.isExceptionIndicator() && hasNoNotes) {
94              errors.putErrorWithoutFullErrorPath(KFSConstants.GENERAL_PAYMENT_TAB_ERRORS, KFSKeyConstants.ERROR_EXCEPTION_ATTACHED_NOTE_MISSING);
95              isValid = false;
96          }
97  
98          return isValid;
99      }
100 
101     /**
102      * Return true if disbursement voucher does not have any notes
103      * 
104      * @param document submitted disbursement voucher document
105      * @return whether the given document has no notes
106      */
107     protected boolean hasNoNotes(DisbursementVoucherDocument document) {
108         List<Note> notes = document.getNotes();
109 
110         return (notes == null || notes.isEmpty());
111     }
112 
113     /**
114      * Sets the accountingDocumentForValidation attribute value.
115      * 
116      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
117      */
118     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
119         this.accountingDocumentForValidation = accountingDocumentForValidation;
120     }
121 
122     /**
123      * Gets the accountingDocumentForValidation attribute.
124      * 
125      * @return Returns the accountingDocumentForValidation.
126      */
127     public AccountingDocument getAccountingDocumentForValidation() {
128         return accountingDocumentForValidation;
129     }
130 }