001/* 002 * Copyright 2006-2008 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.ole.sys.document.validation.impl; 017 018import java.util.Collection; 019import java.util.Collections; 020 021import org.apache.commons.lang.StringUtils; 022import org.kuali.ole.sys.OLEKeyConstants; 023import org.kuali.ole.sys.OLEPropertyConstants; 024import org.kuali.ole.sys.businessobject.Bank; 025import org.kuali.ole.sys.context.SpringContext; 026import org.kuali.ole.sys.service.BankService; 027import org.kuali.rice.kns.document.MaintenanceDocument; 028import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase; 029import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 030 031/** 032 * Evaluates business rules for editing or creation of a new bank record. 033 */ 034public class BankRule extends MaintenanceDocumentRuleBase { 035 protected Bank oldBank; 036 protected Bank newBank; 037 038 /** 039 * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument) 040 */ 041 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) { 042 // default to success 043 boolean valid = true; 044 045 valid &= checkPartiallyFilledOutReferences(); 046 valid &= validateFieldsForBankOffsetEntries(); 047 valid &= validateBankAccountNumber(); 048 049 return valid; 050 } 051 052 /** 053 * Sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and old objects 054 * contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load all 055 * sub-objects from the DB by their primary keys, if available. 056 */ 057 public void setupConvenienceObjects() { 058 oldBank = (Bank) super.getOldDataObject(); 059 newBank = (Bank) super.getNewDataObject(); 060 } 061 062 /** 063 * Checks for partially filled out objects. 064 * 065 * @return true if there are no partially filled out references 066 */ 067 protected boolean checkPartiallyFilledOutReferences() { 068 boolean valid = true; 069 070 valid &= checkForPartiallyFilledOutReferenceForeignKeys(OLEPropertyConstants.CASH_OFFSET_ACCOUNT); 071 valid &= checkForPartiallyFilledOutReferenceForeignKeys(OLEPropertyConstants.CASH_OFFSET_OBJECT); 072 073 return valid; 074 } 075 076 /** 077 * Checks system parameter to determine if the bank code functionality is enabled. If so verifies the cash offset fields needed 078 * to create the additional bank entries were given. 079 * 080 * @return true if all cash offset fields needed have value 081 */ 082 protected boolean validateFieldsForBankOffsetEntries() { 083 boolean valid = true; 084 085 if (SpringContext.getBean(BankService.class).isBankSpecificationEnabled()) { 086 087 if (StringUtils.isBlank(newBank.getCashOffsetAccountNumber())) { 088 putFieldError(OLEPropertyConstants.CASH_OFFSET_ACCOUNT_NUMBER, OLEKeyConstants.Bank.ERROR_MISSING_CASH_ACCOUNT_NUMBER); 089 valid = false; 090 } 091 092 if (StringUtils.isBlank(newBank.getCashOffsetObjectCode())) { 093 putFieldError(OLEPropertyConstants.CASH_OFFSET_OBJECT_CODE, OLEKeyConstants.Bank.ERROR_MISSING_CASH_OBJECT_CODE); 094 valid = false; 095 } 096 } 097 098 return valid; 099 } 100 101 /** 102 * Bank account number must be unique. 103 * 104 * @return 105 */ 106 protected boolean validateBankAccountNumber() { 107 // if the new bank is not blank *AND* has been changed 108 // (I.e, never fire this edit if the account has not been changed) 109 if ( StringUtils.isNotBlank(newBank.getBankAccountNumber() ) 110 && (oldBank == null || 111 !StringUtils.equals(oldBank.getBankAccountNumber(), newBank.getBankAccountNumber())) ) { 112 @SuppressWarnings("rawtypes") 113 Collection existingBanks = KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(Bank.class, Collections.singletonMap(OLEPropertyConstants.BANK_ACCOUNT_NUMBER, newBank.getBankAccountNumber())); 114 if ( existingBanks != null && !existingBanks.isEmpty() ) { 115 putFieldError(OLEPropertyConstants.BANK_ACCOUNT_NUMBER, OLEKeyConstants.Bank.ERROR_ACCOUNT_NUMBER_NOT_UNIQUE); 116 return false; 117 } 118 } 119 return true; 120 } 121}