/*
* The Kuali Financial System, a comprehensive financial management system for higher education.
*
* Copyright 2005-2014 The Kuali Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kuali.kfs.module.purap.document.validation.impl;
import org.apache.commons.lang.StringUtils;
import org.kuali.kfs.module.purap.PurapKeyConstants;
import org.kuali.kfs.module.purap.PurapPropertyConstants;
import org.kuali.kfs.module.purap.document.VendorCreditMemoDocument;
import org.kuali.kfs.module.purap.document.service.PaymentRequestService;
import org.kuali.kfs.sys.KFSKeyConstants;
import org.kuali.kfs.sys.document.validation.GenericValidation;
import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
import org.kuali.rice.kns.service.DataDictionaryService;
import org.kuali.rice.krad.bo.BusinessObject;
import org.kuali.rice.krad.util.GlobalVariables;
import org.kuali.rice.krad.util.ObjectUtils;
public class VendorCreditMemoInitTabRequiredFieldsValidation extends GenericValidation {
private DataDictionaryService dataDictionaryService;
private PaymentRequestService paymentRequestService;
/**
* Validates the necessary fields on the init tab were given and credit memo date is valid. (NOTE: formats for cm date and
* number already performed by pojo conversion)
*/
public boolean validate(AttributedDocumentEvent event) {
VendorCreditMemoDocument cmDocument = (VendorCreditMemoDocument) event.getDocument();
boolean valid = true;
valid = validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_NUMBER);
valid = valid && validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_AMOUNT);
boolean creditMemoDateExist = validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_DATE);
if (creditMemoDateExist) {
if (paymentRequestService.isInvoiceDateAfterToday(cmDocument.getCreditMemoDate())) {
String label = dataDictionaryService.getAttributeErrorLabel(VendorCreditMemoDocument.class, PurapPropertyConstants.CREDIT_MEMO_DATE);
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CREDIT_MEMO_DATE, PurapKeyConstants.ERROR_INVALID_INVOICE_DATE, label);
valid = false;
}
}
return valid;
}
/**
* Helper method to perform required field checks add error messages if the validation fails. Adds an error required to
* GlobalVariables.errorMap using the given fieldName as the error key and retrieving the error label from the data dictionary
* for the error required message param.
*
* @param businessObject - Business object to check for value
* @param fieldName - Name of the property in the business object
*/
protected boolean validateRequiredField(BusinessObject businessObject, String fieldName) {
boolean valid = true;
Object fieldValue = ObjectUtils.getPropertyValue(businessObject, fieldName);
if (fieldValue == null || (fieldValue instanceof String && StringUtils.isBlank(fieldName))) {
String label = dataDictionaryService.getAttributeErrorLabel(businessObject.getClass(), fieldName);
GlobalVariables.getMessageMap().putError(fieldName, KFSKeyConstants.ERROR_REQUIRED, label);
valid = false;
}
return valid;
}
public DataDictionaryService getDataDictionaryService() {
return dataDictionaryService;
}
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
this.dataDictionaryService = dataDictionaryService;
}
public PaymentRequestService getPaymentRequestService() {
return paymentRequestService;
}
public void setPaymentRequestService(PaymentRequestService paymentRequestService) {
this.paymentRequestService = paymentRequestService;
}
}