001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.fp.document.validation.impl;
017
018 import java.util.List;
019
020 import org.apache.commons.lang.StringUtils;
021 import org.kuali.kfs.fp.document.DisbursementVoucherConstants;
022 import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
023 import org.kuali.kfs.sys.KFSConstants;
024 import org.kuali.kfs.sys.KFSKeyConstants;
025 import org.kuali.kfs.sys.KFSPropertyConstants;
026 import org.kuali.kfs.sys.context.SpringContext;
027 import org.kuali.kfs.sys.document.AccountingDocument;
028 import org.kuali.kfs.sys.document.validation.GenericValidation;
029 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
030 import org.kuali.rice.kns.bo.Note;
031 import org.kuali.rice.kns.service.DictionaryValidationService;
032 import org.kuali.rice.kns.service.NoteService;
033 import org.kuali.rice.kns.util.GlobalVariables;
034 import org.kuali.rice.kns.util.MessageMap;
035
036 public class DisbursementVoucherDocumentFieldValidation extends GenericValidation {
037 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherDocumentFieldValidation.class);
038
039 private AccountingDocument accountingDocumentForValidation;
040
041 /**
042 * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
043 */
044 public boolean validate(AttributedDocumentEvent event) {
045 LOG.debug("validate start");
046 boolean isValid = true;
047
048 DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
049
050 MessageMap errors = GlobalVariables.getMessageMap();
051
052 // validate document required fields
053 SpringContext.getBean(DictionaryValidationService.class).validateDocument(document);
054
055 // validate payee fields
056 errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
057 errors.addToErrorPath(KFSPropertyConstants.DV_PAYEE_DETAIL);
058 SpringContext.getBean(DictionaryValidationService.class).validateBusinessObject(document.getDvPayeeDetail());
059 errors.removeFromErrorPath(KFSPropertyConstants.DV_PAYEE_DETAIL);
060 errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT);
061
062 if (!errors.isEmpty()) {
063 return false;
064 }
065
066 /* special handling name & address required if special handling is indicated */
067 if (document.isDisbVchrSpecialHandlingCode()) {
068 if (StringUtils.isBlank(document.getDvPayeeDetail().getDisbVchrSpecialHandlingPersonName()) || StringUtils.isBlank(document.getDvPayeeDetail().getDisbVchrSpecialHandlingLine1Addr())) {
069 errors.putErrorWithoutFullErrorPath(KFSConstants.GENERAL_SPECHAND_TAB_ERRORS, KFSKeyConstants.ERROR_DV_SPECIAL_HANDLING);
070 isValid = false;
071 }
072 }
073
074 boolean hasNoNotes = this.hasNoNotes(document);
075
076 /* if no documentation is selected, must be a note explaining why */
077 if (DisbursementVoucherConstants.NO_DOCUMENTATION_LOCATION.equals(document.getDisbursementVoucherDocumentationLocationCode()) && hasNoNotes) {
078 errors.putError(KFSPropertyConstants.DISBURSEMENT_VOUCHER_DOCUMENTATION_LOCATION_CODE, KFSKeyConstants.ERROR_DV_NO_DOCUMENTATION_NOTE_MISSING);
079 isValid = false;
080 }
081
082 /* if special handling indicated, must be a note explaining why */
083 if (document.isDisbVchrSpecialHandlingCode() && hasNoNotes) {
084 errors.putErrorWithoutFullErrorPath(KFSConstants.GENERAL_PAYMENT_TAB_ERRORS, KFSKeyConstants.ERROR_DV_SPECIAL_HANDLING_NOTE_MISSING);
085 isValid = false;
086 }
087
088 /* if exception attached indicated, must be a note explaining why */
089 if (document.isExceptionIndicator() && hasNoNotes) {
090 errors.putErrorWithoutFullErrorPath(KFSConstants.GENERAL_PAYMENT_TAB_ERRORS, KFSKeyConstants.ERROR_DV_EXCEPTION_ATTACHED_NOTE_MISSING);
091 isValid = false;
092 }
093
094 return isValid;
095 }
096
097 /**
098 * Return true if disbursement voucher does not have any notes
099 *
100 * @param document submitted disbursement voucher document
101 * @return whether the given document has no notes
102 */
103 protected boolean hasNoNotes(DisbursementVoucherDocument document) {
104 List<Note> notes = document.getDocumentHeader().getBoNotes();
105
106 if(notes == null || notes.isEmpty()) {
107 String remoteObjectId = document.getDocumentHeader().getObjectId();
108 notes = SpringContext.getBean(NoteService.class).getByRemoteObjectId(remoteObjectId);
109 }
110
111 return (notes == null || notes.isEmpty());
112 }
113
114 /**
115 * Sets the accountingDocumentForValidation attribute value.
116 *
117 * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
118 */
119 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
120 this.accountingDocumentForValidation = accountingDocumentForValidation;
121 }
122
123 /**
124 * Gets the accountingDocumentForValidation attribute.
125 *
126 * @return Returns the accountingDocumentForValidation.
127 */
128 public AccountingDocument getAccountingDocumentForValidation() {
129 return accountingDocumentForValidation;
130 }
131 }