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.sys.OLEConstants.AUXILIARY_LINE_HELPER_PROPERTY_NAME;
19  import static org.kuali.ole.sys.OLEConstants.CREDIT_AMOUNT_PROPERTY_NAME;
20  import static org.kuali.ole.sys.OLEConstants.DEBIT_AMOUNT_PROPERTY_NAME;
21  import static org.kuali.ole.sys.OLEConstants.GL_DEBIT_CODE;
22  import static org.kuali.ole.sys.OLEConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME;
23  import static org.kuali.ole.sys.OLEConstants.SQUARE_BRACKET_LEFT;
24  import static org.kuali.ole.sys.OLEConstants.SQUARE_BRACKET_RIGHT;
25  import static org.kuali.ole.sys.OLEConstants.VOUCHER_LINE_HELPER_CREDIT_PROPERTY_NAME;
26  import static org.kuali.ole.sys.OLEConstants.VOUCHER_LINE_HELPER_DEBIT_PROPERTY_NAME;
27  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_ZERO_OR_NEGATIVE_AMOUNT;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.kuali.ole.sys.businessobject.AccountingLine;
31  import org.kuali.ole.sys.document.validation.GenericValidation;
32  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
33  import org.kuali.rice.core.api.util.type.KualiDecimal;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  
36  /**
37   * The Auxiliary Voucher's customization of the accounting line amount validation.
38   */
39  public class AuxiliaryVoucherAccountingLineAmountValidation extends GenericValidation {
40      private AccountingLine accountingLineForValidation;
41  
42      /**
43       * Accounting lines for Auxiliary Vouchers can only be positive non-zero numbers
44       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
45       */
46      public boolean validate(AttributedDocumentEvent event) {
47          boolean retval = true;
48          KualiDecimal amount = accountingLineForValidation.getAmount();
49  
50          // check for negative or zero amounts
51          if (KualiDecimal.ZERO.equals(amount)) { // if 0
52              GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(buildMessageMapKeyPathForDebitCreditAmount(true), ERROR_ZERO_OR_NEGATIVE_AMOUNT, "an accounting line");
53              GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(buildMessageMapKeyPathForDebitCreditAmount(false), ERROR_ZERO_OR_NEGATIVE_AMOUNT, "an accounting line");
54  
55              retval = false;
56          }
57          else if (amount.isNegative()) { // entered a negative number
58              String debitCreditCode = accountingLineForValidation.getDebitCreditCode();
59              if (StringUtils.isNotBlank(debitCreditCode) && GL_DEBIT_CODE.equals(debitCreditCode)) {
60                  GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(buildMessageMapKeyPathForDebitCreditAmount(true), ERROR_ZERO_OR_NEGATIVE_AMOUNT, "an accounting line");
61              }
62              else {
63                  GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(buildMessageMapKeyPathForDebitCreditAmount(false), ERROR_ZERO_OR_NEGATIVE_AMOUNT, "an accounting line");
64              }
65  
66              retval = false;
67          }
68  
69          return retval;
70      }
71      
72      /**
73       * This method looks at the current full key path that exists in the MessageMap structure to determine how to build the error map
74       * for the special journal voucher credit and debit fields since they don't conform to the standard pattern of accounting lines.
75       * 
76       * @param isDebit boolean to determine whether or not value isDebit or not
77       * @return String represents error map key to use
78       */
79      protected String buildMessageMapKeyPathForDebitCreditAmount(boolean isDebit) {
80          // determine if we are looking at a new line add or an update
81          boolean isNewLineAdd = GlobalVariables.getMessageMap().getErrorPath().contains(NEW_SOURCE_ACCT_LINE_PROPERTY_NAME);
82          isNewLineAdd |= GlobalVariables.getMessageMap().getErrorPath().contains(NEW_SOURCE_ACCT_LINE_PROPERTY_NAME);
83  
84          if (isNewLineAdd) {
85              if (isDebit) {
86                  return DEBIT_AMOUNT_PROPERTY_NAME;
87              }
88              else {
89                  return CREDIT_AMOUNT_PROPERTY_NAME;
90              }
91          }
92          else {
93              String index = StringUtils.substringBetween(GlobalVariables.getMessageMap().getKeyPath("", true), SQUARE_BRACKET_LEFT, SQUARE_BRACKET_RIGHT);
94              String indexWithParams = SQUARE_BRACKET_LEFT + index + SQUARE_BRACKET_RIGHT;
95              if (isDebit) {
96                  return AUXILIARY_LINE_HELPER_PROPERTY_NAME + indexWithParams + VOUCHER_LINE_HELPER_DEBIT_PROPERTY_NAME;
97              }
98              else {
99                  return AUXILIARY_LINE_HELPER_PROPERTY_NAME + indexWithParams + VOUCHER_LINE_HELPER_CREDIT_PROPERTY_NAME;
100             }
101         }
102     }
103 
104     /**
105      * Gets the accountingLineForValidation attribute. 
106      * @return Returns the accountingLineForValidation.
107      */
108     public AccountingLine getAccountingLineForValidation() {
109         return accountingLineForValidation;
110     }
111 
112     /**
113      * Sets the accountingLineForValidation attribute value.
114      * @param accountingLineForValidation The accountingLineForValidation to set.
115      */
116     public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
117         this.accountingLineForValidation = accountingLineForValidation;
118     }
119 }