View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.validation.impl;
17  
18  import static org.kuali.ole.fp.document.validation.impl.CreditCardReceiptDocumentRuleConstants.CREDIT_CARD_RECEIPT_PREFIX;
19  import static org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
20  
21  import org.kuali.ole.fp.document.CreditCardReceiptDocument;
22  import org.kuali.ole.sys.OLEKeyConstants;
23  import org.kuali.ole.sys.OLEKeyConstants.CashReceipt;
24  import org.kuali.ole.sys.OLEPropertyConstants;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.ole.sys.document.validation.GenericValidation;
27  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
28  import org.kuali.rice.core.api.util.type.KualiDecimal;
29  import org.kuali.rice.kns.service.DataDictionaryService;
30  import org.kuali.rice.kns.service.DictionaryValidationService;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  /**
34   * This class...
35   */
36  public class CreditCardReceiptCashTotalsValidation extends GenericValidation {
37      private CreditCardReceiptDocument accountingDocumentForValidation;
38      /**
39       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
40       */
41      public boolean validate(AttributedDocumentEvent event) {
42          CreditCardReceiptDocument ccrDocument = getAccountingDocumentForValidation();
43          KualiDecimal totalAmount = ccrDocument.getTotalDollarAmount();
44          String propertyName = OLEPropertyConstants.CREDIT_CARD_RECEIPTS_TOTAL;
45          String documentEntryName = ccrDocument.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
46          
47          boolean isValid = true;
48          String errorProperty = CREDIT_CARD_RECEIPT_PREFIX + propertyName;
49  
50          // treating null totalAmount as if it were a zero
51          DataDictionaryService dds = SpringContext.getBean(DataDictionaryService.class);
52          String errorLabel = dds.getAttributeLabel(documentEntryName, propertyName);
53          if ((totalAmount == null) || totalAmount.isZero()) {
54              GlobalVariables.getMessageMap().putError(errorProperty, CashReceipt.ERROR_ZERO_TOTAL, errorLabel);
55  
56              isValid = false;
57          }
58          else {
59              int precount = GlobalVariables.getMessageMap().getNumberOfPropertiesWithErrors();
60  
61              DictionaryValidationService dvs = SpringContext.getBean(DictionaryValidationService.class);
62              dvs.validateDocumentAttribute(ccrDocument, propertyName, DOCUMENT_ERROR_PREFIX);
63  
64              // replace generic error message, if any, with something more readable
65              GlobalVariables.getMessageMap().replaceError(errorProperty, OLEKeyConstants.ERROR_MAX_LENGTH, CashReceipt.ERROR_EXCESSIVE_TOTAL, errorLabel);
66  
67              int postcount = GlobalVariables.getMessageMap().getNumberOfPropertiesWithErrors();
68              isValid = (postcount == precount);
69          }
70  
71          return isValid;
72      }
73      /**
74       * Gets the documentForValidation attribute. 
75       * @return Returns the documentForValidation.
76       */
77      public CreditCardReceiptDocument getAccountingDocumentForValidation() {
78          return accountingDocumentForValidation;
79      }
80      /**
81       * Sets the documentForValidation attribute value.
82       * @param documentForValidation The documentForValidation to set.
83       */
84      public void setAccountingDocumentForValidation(CreditCardReceiptDocument accountingDocumentForValidation) {
85          this.accountingDocumentForValidation = accountingDocumentForValidation;
86      }
87  
88  }