View Javadoc
1   /*
2    * Copyright 2006 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.businessobject.CreditCardDetail;
22  import org.kuali.ole.fp.document.CreditCardReceiptDocument;
23  import org.kuali.ole.sys.OLEKeyConstants;
24  import org.kuali.ole.sys.OLEKeyConstants.CashReceipt;
25  import org.kuali.ole.sys.OLEPropertyConstants;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.rice.core.api.util.type.KualiDecimal;
28  import org.kuali.rice.kns.service.DataDictionaryService;
29  import org.kuali.rice.kns.service.DictionaryValidationService;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.MessageMap;
32  
33  /**
34   * Common Credit Card Receipt Document rule utilities.
35   */
36  public class CreditCardReceiptDocumentRuleUtil {
37      /**
38       * This method method will invoke the data dictionary validation for a CreditCardDetail bo instance, in addition to checking
39       * existence of the CreditCardType and CreditCardVendor attributes that hang off of it. This method assumes that the document
40       * hierarchy for the error map path is managed outside of this call.
41       * 
42       * @param creditCardReceipt credit card detail
43       * @return true if credit card detail amount is non zero and credit card vendor and type references exist
44       */
45      public static boolean validateCreditCardReceipt(CreditCardDetail creditCardReceipt) {
46          MessageMap errorMap = GlobalVariables.getMessageMap();
47          int originalErrorCount = errorMap.getErrorCount();
48  
49          // call the DD validation which checks basic data integrity
50          SpringContext.getBean(DictionaryValidationService.class).validateBusinessObject(creditCardReceipt);
51          boolean isValid = (errorMap.getErrorCount() == originalErrorCount);
52  
53          // check that dollar amount is not zero before continuing
54          if (isValid) {
55              isValid = !creditCardReceipt.getCreditCardAdvanceDepositAmount().isZero();
56              if (!isValid) {
57                  String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(CreditCardDetail.class, OLEPropertyConstants.CREDIT_CARD_ADVANCE_DEPOSIT_AMOUNT);
58                  errorMap.putError(OLEPropertyConstants.CREDIT_CARD_ADVANCE_DEPOSIT_AMOUNT, OLEKeyConstants.ERROR_ZERO_AMOUNT, label);
59              }
60          }
61  
62          if (isValid) {
63              isValid = SpringContext.getBean(DictionaryValidationService.class).validateReferenceExists(creditCardReceipt, OLEPropertyConstants.CREDIT_CARD_TYPE);
64              if (!isValid) {
65                  String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(CreditCardDetail.class, OLEPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_TYPE_CODE);
66                  errorMap.putError(OLEPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_TYPE_CODE, OLEKeyConstants.ERROR_EXISTENCE, label);
67              }
68          }
69          if (isValid) {
70              isValid = SpringContext.getBean(DictionaryValidationService.class).validateReferenceExists(creditCardReceipt, OLEPropertyConstants.CREDIT_CARD_VENDOR);
71              if (!isValid) {
72                  String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(CreditCardDetail.class, OLEPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_VENDOR_NUMBER);
73                  errorMap.putError(OLEPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_VENDOR_NUMBER, OLEKeyConstants.ERROR_EXISTENCE, label);
74              }
75          }
76  
77          return isValid;
78      }
79  
80  
81      /**
82       * Checks whether the CashReceiptDocument's cash totals are invalid, generating global errors if so.
83       * 
84       * @param cashReceiptDocument submitted cash receipt document
85       * @return true if any of the cash totals on cash credit card receipt document are invalid
86       */
87      public static boolean areCashTotalsInvalid(CreditCardReceiptDocument ccrDocument) {
88          String documentEntryName = ccrDocument.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
89  
90          boolean isInvalid = isTotalInvalid(ccrDocument, ccrDocument.getTotalDollarAmount(), documentEntryName, OLEPropertyConstants.CREDIT_CARD_RECEIPTS_TOTAL);
91  
92          return isInvalid;
93      }
94  
95      /**
96       * Returns true if total is invalid and puts an error message in the error map for that property if the amount is negative
97       * 
98       * @param cashReceiptDocument
99       * @param totalAmount
100      * @param documentEntryName
101      * @param propertyName
102      * @return true if the totalAmount is an invalid value
103      */
104     private static boolean isTotalInvalid(CreditCardReceiptDocument ccrDocument, KualiDecimal totalAmount, String documentEntryName, String propertyName) {
105         boolean isInvalid = false;
106         String errorProperty = CREDIT_CARD_RECEIPT_PREFIX + propertyName;
107 
108         // treating null totalAmount as if it were a zero
109         DataDictionaryService dds = SpringContext.getBean(DataDictionaryService.class);
110         String errorLabel = dds.getAttributeLabel(documentEntryName, propertyName);
111         if ((totalAmount == null) || totalAmount.isZero()) {
112             GlobalVariables.getMessageMap().putError(errorProperty, CashReceipt.ERROR_ZERO_TOTAL, errorLabel);
113 
114             isInvalid = true;
115         }
116         else {
117             int precount = GlobalVariables.getMessageMap().getNumberOfPropertiesWithErrors();
118 
119             DictionaryValidationService dvs = SpringContext.getBean(DictionaryValidationService.class);
120             dvs.validateDocumentAttribute(ccrDocument, propertyName, DOCUMENT_ERROR_PREFIX);
121 
122             // replace generic error message, if any, with something more readable
123             GlobalVariables.getMessageMap().replaceError(errorProperty, OLEKeyConstants.ERROR_MAX_LENGTH, CashReceipt.ERROR_EXCESSIVE_TOTAL, errorLabel);
124 
125             int postcount = GlobalVariables.getMessageMap().getNumberOfPropertiesWithErrors();
126             isInvalid = (postcount > precount);
127         }
128 
129         return isInvalid;
130     }
131 }