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.sys.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.sys.OLEKeyConstants;
20  import org.kuali.ole.sys.OLEPropertyConstants;
21  import org.kuali.ole.sys.businessobject.Bank;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.ole.sys.service.BankService;
24  import org.kuali.rice.kns.service.DataDictionaryService;
25  import org.kuali.rice.krad.document.Document;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.util.ObjectUtils;
28  
29  /**
30   * Performs bank code validation.
31   */
32  public class BankCodeValidation {
33      protected static volatile DataDictionaryService dataDictionaryService;
34      protected static volatile BankService bankService;
35  
36      /**
37       * Performs required, exists, and active validation of bank code. Also validates bank for deposit or disbursement indicator if
38       * requested. .
39       * 
40       * @param bankCode value to validate
41       * @param bankCodeProperty property to associate errors with
42       * @param requireDeposit true if the bank code should support deposits
43       * @param requireDisbursement true if the bank code should support disbursements
44       * @return true if bank code passes all validations, false if any fail
45       */
46      public static boolean validate(String bankCode, String bankCodeProperty, boolean requireDeposit, boolean requireDisbursement) {
47          String bankCodeLabel = getDataDictionaryService().getAttributeLabel(Bank.class, OLEPropertyConstants.BANK_CODE);
48  
49          // if bank specification is not enabled, no need to validate bank code
50          if (!getBankService().isBankSpecificationEnabled()) {
51              return true;
52          }
53  
54          // required check
55          if (StringUtils.isBlank(bankCode)) {
56              GlobalVariables.getMessageMap().putError(bankCodeProperty, OLEKeyConstants.ERROR_REQUIRED, bankCodeLabel);
57  
58              return false;
59          }
60  
61          Bank bank = getBankService().getByPrimaryId(bankCode);
62          
63          if (ObjectUtils.isNull(bank)) {
64              GlobalVariables.getMessageMap().putError(bankCodeProperty, OLEKeyConstants.ERROR_DOCUMENT_BANKACCMAINT_INVALID_BANK);
65              return false;
66          }
67          
68          // validate deposit
69          if (requireDeposit && !bank.isBankDepositIndicator()) {
70              GlobalVariables.getMessageMap().putError(bankCodeProperty, OLEKeyConstants.Bank.ERROR_DEPOSIT_NOT_SUPPORTED);
71  
72              return false;
73          }
74  
75          // validate disbursement
76          if (requireDisbursement && !bank.isBankDisbursementIndicator()) {
77              GlobalVariables.getMessageMap().putError(bankCodeProperty, OLEKeyConstants.Bank.ERROR_DISBURSEMENT_NOT_SUPPORTED);
78  
79              return false;
80          }
81  
82          return true;
83      }
84  
85      /**
86       * Performs required, exists, and active validation of bank code. Also validates bank for deposit or disbursement indicator if
87       * requested.
88       *
89       * @param document the document that is being validated
90       * @param bankCode value to validate
91       * @param bankCodeProperty property to associate errors with
92       * @param requireDeposit true if the bank code should support deposits
93       * @param requireDisbursement true if the bank code should support disbursements
94       * @return true if bank code passes all validations, false if any fail
95       */
96      public static boolean validate(Document document, String bankCode, String bankCodeProperty, boolean requireDeposit, boolean requireDisbursement) {
97          if (document != null && !getBankService().isBankSpecificationEnabledForDocument(document.getClass())) {
98              return true;
99          }
100         return BankCodeValidation.validate(bankCode, bankCodeProperty, requireDeposit, requireDisbursement);
101     }
102 
103     /**
104      * @return the default implementatino of the DataDictionaryService
105      */
106     protected static DataDictionaryService getDataDictionaryService() {
107         if (dataDictionaryService == null) {
108             dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
109         }
110         return dataDictionaryService;
111     }
112 
113     /**
114      * @return the default implementation of the BankService
115      */
116     protected static BankService getBankService() {
117         if (bankService == null) {
118             bankService = SpringContext.getBean(BankService.class);
119         }
120         return bankService;
121     }
122 }