View Javadoc
1   /*
2    * Copyright 2006-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 java.util.Collection;
19  import java.util.Collections;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.sys.OLEKeyConstants;
23  import org.kuali.ole.sys.OLEPropertyConstants;
24  import org.kuali.ole.sys.businessobject.Bank;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.ole.sys.service.BankService;
27  import org.kuali.rice.kns.document.MaintenanceDocument;
28  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
29  
30  /**
31   * Evaluates business rules for editing or creation of a new bank record.
32   */
33  public class BankRule extends MaintenanceDocumentRuleBase {
34      protected Bank oldBank;
35      protected Bank newBank;
36  
37      /**
38       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
39       */
40      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
41          // default to success
42          boolean valid = true;
43  
44          valid &= checkPartiallyFilledOutReferences();
45          valid &= validateFieldsForBankOffsetEntries();
46          valid &= validateBankAccountNumber();
47  
48          return valid;
49      }
50  
51      /**
52       * Sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and old objects
53       * contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load all
54       * sub-objects from the DB by their primary keys, if available.
55       */
56      public void setupConvenienceObjects() {
57          oldBank = (Bank) super.getOldBo();
58          newBank = (Bank) super.getNewBo();
59      }
60  
61      /**
62       * Checks for partially filled out objects.
63       * 
64       * @return true if there are no partially filled out references
65       */
66      protected boolean checkPartiallyFilledOutReferences() {
67          boolean valid = true;
68  
69          valid &= checkForPartiallyFilledOutReferenceForeignKeys(OLEPropertyConstants.CASH_OFFSET_ACCOUNT);
70          valid &= checkForPartiallyFilledOutReferenceForeignKeys(OLEPropertyConstants.CASH_OFFSET_OBJECT);
71  
72          return valid;
73      }
74  
75      /**
76       * Checks system parameter to determine if the bank code functionality is enabled. If so verifies the cash offset fields needed
77       * to create the additional bank entries were given.
78       * 
79       * @return true if all cash offset fields needed have value
80       */
81      protected boolean validateFieldsForBankOffsetEntries() {
82          boolean valid = true;
83  
84          if (SpringContext.getBean(BankService.class).isBankSpecificationEnabled()) {
85  
86              if (StringUtils.isBlank(newBank.getCashOffsetAccountNumber())) {
87                  putFieldError(OLEPropertyConstants.CASH_OFFSET_ACCOUNT_NUMBER, OLEKeyConstants.Bank.ERROR_MISSING_CASH_ACCOUNT_NUMBER);
88                  valid = false;
89              }
90      
91              if (StringUtils.isBlank(newBank.getCashOffsetObjectCode())) {
92                  putFieldError(OLEPropertyConstants.CASH_OFFSET_OBJECT_CODE, OLEKeyConstants.Bank.ERROR_MISSING_CASH_OBJECT_CODE);
93                  valid = false;
94              }
95          }
96  
97          return valid;
98      }
99      
100     /**
101      * Bank account number must be unique.
102      * 
103      * @return
104      */
105     protected boolean validateBankAccountNumber() {
106         // if the new bank is not blank *AND* has been changed
107         // (I.e, never fire this edit if the account has not been changed)
108         if ( StringUtils.isNotBlank(newBank.getBankAccountNumber() )
109                 && (oldBank == null ||
110                          !StringUtils.equals(oldBank.getBankAccountNumber(), newBank.getBankAccountNumber())) ) {
111             @SuppressWarnings("rawtypes")
112             Collection existingBanks = getBoService().findMatching(Bank.class, Collections.singletonMap(OLEPropertyConstants.BANK_ACCOUNT_NUMBER, newBank.getBankAccountNumber()));
113             if ( existingBanks != null && !existingBanks.isEmpty() ) {
114                 putFieldError(OLEPropertyConstants.BANK_ACCOUNT_NUMBER, OLEKeyConstants.Bank.ERROR_ACCOUNT_NUMBER_NOT_UNIQUE);
115                 return false;
116             }
117         }
118         return true;        
119     }
120 }