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 static org.kuali.kfs.fp.document.validation.impl.CreditCardReceiptDocumentRuleConstants.CREDIT_CARD_RECEIPT_PREFIX;
019 import static org.kuali.kfs.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
020
021 import org.kuali.kfs.fp.document.CreditCardReceiptDocument;
022 import org.kuali.kfs.sys.KFSKeyConstants;
023 import org.kuali.kfs.sys.KFSPropertyConstants;
024 import org.kuali.kfs.sys.KFSKeyConstants.CashReceipt;
025 import org.kuali.kfs.sys.context.SpringContext;
026 import org.kuali.kfs.sys.document.validation.GenericValidation;
027 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
028 import org.kuali.rice.kns.service.DataDictionaryService;
029 import org.kuali.rice.kns.service.DictionaryValidationService;
030 import org.kuali.rice.kns.util.GlobalVariables;
031 import org.kuali.rice.kns.util.KualiDecimal;
032
033 /**
034 * This class...
035 */
036 public class CreditCardReceiptCashTotalsValidation extends GenericValidation {
037 private CreditCardReceiptDocument accountingDocumentForValidation;
038 /**
039 * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
040 */
041 public boolean validate(AttributedDocumentEvent event) {
042 CreditCardReceiptDocument ccrDocument = getAccountingDocumentForValidation();
043 KualiDecimal totalAmount = ccrDocument.getTotalDollarAmount();
044 String propertyName = KFSPropertyConstants.CREDIT_CARD_RECEIPTS_TOTAL;
045 String documentEntryName = ccrDocument.getDocumentHeader().getWorkflowDocument().getDocumentType();
046
047 boolean isValid = true;
048 String errorProperty = CREDIT_CARD_RECEIPT_PREFIX + propertyName;
049
050 // treating null totalAmount as if it were a zero
051 DataDictionaryService dds = SpringContext.getBean(DataDictionaryService.class);
052 String errorLabel = dds.getAttributeLabel(documentEntryName, propertyName);
053 if ((totalAmount == null) || totalAmount.isZero()) {
054 GlobalVariables.getMessageMap().putError(errorProperty, CashReceipt.ERROR_ZERO_TOTAL, errorLabel);
055
056 isValid = false;
057 }
058 else {
059 int precount = GlobalVariables.getMessageMap().size();
060
061 DictionaryValidationService dvs = SpringContext.getBean(DictionaryValidationService.class);
062 dvs.validateDocumentAttribute(ccrDocument, propertyName, DOCUMENT_ERROR_PREFIX);
063
064 // replace generic error message, if any, with something more readable
065 GlobalVariables.getMessageMap().replaceError(errorProperty, KFSKeyConstants.ERROR_MAX_LENGTH, CashReceipt.ERROR_EXCESSIVE_TOTAL, errorLabel);
066
067 int postcount = GlobalVariables.getMessageMap().size();
068 isValid = (postcount == precount);
069 }
070
071 return isValid;
072 }
073 /**
074 * Gets the documentForValidation attribute.
075 * @return Returns the documentForValidation.
076 */
077 public CreditCardReceiptDocument getAccountingDocumentForValidation() {
078 return accountingDocumentForValidation;
079 }
080 /**
081 * Sets the documentForValidation attribute value.
082 * @param documentForValidation The documentForValidation to set.
083 */
084 public void setAccountingDocumentForValidation(CreditCardReceiptDocument accountingDocumentForValidation) {
085 this.accountingDocumentForValidation = accountingDocumentForValidation;
086 }
087
088 }