001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.fp.document.validation.impl;
017
018import static org.kuali.ole.fp.document.validation.impl.CreditCardReceiptDocumentRuleConstants.CREDIT_CARD_RECEIPT_PREFIX;
019import static org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
020
021import org.kuali.ole.fp.document.CreditCardReceiptDocument;
022import org.kuali.ole.sys.OLEKeyConstants;
023import org.kuali.ole.sys.OLEKeyConstants.CashReceipt;
024import org.kuali.ole.sys.OLEPropertyConstants;
025import org.kuali.ole.sys.context.SpringContext;
026import org.kuali.ole.sys.document.validation.GenericValidation;
027import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
028import org.kuali.rice.core.api.util.type.KualiDecimal;
029import org.kuali.rice.kns.service.DataDictionaryService;
030import org.kuali.rice.kns.service.DictionaryValidationService;
031import org.kuali.rice.krad.util.GlobalVariables;
032
033/**
034 * This class...
035 */
036public class CreditCardReceiptCashTotalsValidation extends GenericValidation {
037    private CreditCardReceiptDocument accountingDocumentForValidation;
038    /**
039     * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
040     */
041    public boolean validate(AttributedDocumentEvent event) {
042        CreditCardReceiptDocument ccrDocument = getAccountingDocumentForValidation();
043        KualiDecimal totalAmount = ccrDocument.getTotalDollarAmount();
044        String propertyName = OLEPropertyConstants.CREDIT_CARD_RECEIPTS_TOTAL;
045        String documentEntryName = ccrDocument.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
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().getNumberOfPropertiesWithErrors();
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, OLEKeyConstants.ERROR_MAX_LENGTH, CashReceipt.ERROR_EXCESSIVE_TOTAL, errorLabel);
066
067            int postcount = GlobalVariables.getMessageMap().getNumberOfPropertiesWithErrors();
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}