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.module.purap.document.validation.impl;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.kfs.module.purap.PurapKeyConstants;
020 import org.kuali.kfs.module.purap.PurapPropertyConstants;
021 import org.kuali.kfs.module.purap.businessobject.PurApAccountingLine;
022 import org.kuali.kfs.sys.KFSKeyConstants;
023 import org.kuali.kfs.sys.context.SpringContext;
024 import org.kuali.kfs.sys.document.validation.GenericValidation;
025 import org.kuali.kfs.sys.document.validation.event.AddAccountingLineEvent;
026 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027 import org.kuali.rice.kns.bo.BusinessObject;
028 import org.kuali.rice.kns.service.DataDictionaryService;
029 import org.kuali.rice.kns.util.GlobalVariables;
030 import org.kuali.rice.kns.util.ObjectUtils;
031
032 public class VendorCreditMemoAccountPercentBetween0And100Validation extends GenericValidation {
033
034 private DataDictionaryService dataDictionaryService;
035
036 public boolean validate(AttributedDocumentEvent event) {
037 boolean isValid = true;
038 PurApAccountingLine account = (PurApAccountingLine)((AddAccountingLineEvent)event).getAccountingLine();
039
040 if (validateRequiredField(account, PurapPropertyConstants.ACCOUNT_LINE_PERCENT)) {
041 double pct = account.getAccountLinePercent().doubleValue();
042 if (pct <= 0 || pct > 100) {
043 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ACCOUNT_LINE_PERCENT, PurapKeyConstants.ERROR_CREDIT_MEMO_LINE_PERCENT);
044 isValid = false;
045 }
046 }
047 else {
048 isValid = false;
049 }
050
051 return isValid;
052 }
053
054 /**
055 * Helper method to perform required field checks add error messages if the validation fails. Adds an error required to
056 * GlobalVariables.errorMap using the given fieldName as the error key and retrieving the error label from the data dictionary
057 * for the error required message param.
058 *
059 * @param businessObject - Business object to check for value
060 * @param fieldName - Name of the property in the business object
061 */
062 protected boolean validateRequiredField(BusinessObject businessObject, String fieldName) {
063 boolean valid = true;
064
065 Object fieldValue = ObjectUtils.getPropertyValue(businessObject, fieldName);
066 if (fieldValue == null || (fieldValue instanceof String && StringUtils.isBlank(fieldName))) {
067 String label = dataDictionaryService.getAttributeErrorLabel(businessObject.getClass(), fieldName);
068 GlobalVariables.getMessageMap().putError(fieldName, KFSKeyConstants.ERROR_REQUIRED, label);
069 valid = false;
070 }
071
072 return valid;
073 }
074
075 public DataDictionaryService getDataDictionaryService() {
076 return dataDictionaryService;
077 }
078
079 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
080 this.dataDictionaryService = dataDictionaryService;
081 }
082
083 }