/* * 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.businessobject.PurApAccountingLine; import org.kuali.kfs.sys.KFSKeyConstants; import org.kuali.kfs.sys.document.validation.GenericValidation; import org.kuali.kfs.sys.document.validation.event.AddAccountingLineEvent; 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 VendorCreditMemoAccountPercentBetween0And100Validation extends GenericValidation { private DataDictionaryService dataDictionaryService; public boolean validate(AttributedDocumentEvent event) { boolean isValid = true; PurApAccountingLine account = (PurApAccountingLine)((AddAccountingLineEvent)event).getAccountingLine(); if (validateRequiredField(account, PurapPropertyConstants.ACCOUNT_LINE_PERCENT)) { double pct = account.getAccountLinePercent().doubleValue(); if (pct <= 0 || pct > 100) { GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ACCOUNT_LINE_PERCENT, PurapKeyConstants.ERROR_CREDIT_MEMO_LINE_PERCENT); isValid = false; } } else { isValid = false; } return isValid; } /** * 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; } }