001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.module.financial.rules;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.kuali.core.document.MaintenanceDocument;
023: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
024: import org.kuali.core.util.ObjectUtils;
025: import org.kuali.kfs.KFSKeyConstants;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.module.chart.bo.SubAccount;
028: import org.kuali.module.chart.bo.SubObjCd;
029: import org.kuali.module.financial.bo.BankAccount;
030: import org.kuali.module.financial.service.UniversityDateService;
031:
032: /**
033: * This is the rules class that is used for the default implementation of the Bank Account maintenance document.
034: */
035: public class BankAccountRule extends MaintenanceDocumentRuleBase {
036:
037: BankAccount oldBankAccount;
038: BankAccount newBankAccount;
039:
040: /**
041: * Validates a bank account maintenance document when it is approved. Method returns true if objects are completely filled out,
042: * sub account number exists, and sub object code exists
043: *
044: * @param document submitted document
045: * @return true if objects are completed filled out, sub account number exists, and sub object code exists
046: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
047: */
048: protected boolean processCustomApproveDocumentBusinessRules(
049: MaintenanceDocument document) {
050: // default to success
051: boolean success = true;
052:
053: success &= checkPartiallyFilledOutReferences();
054: success &= checkSubAccountNumberExistence();
055: success &= checkSubObjectCodeExistence();
056:
057: return success;
058: }
059:
060: /**
061: * Validates a bank account maintenance document when it is routed. Method returns true if objects are completely filled out,
062: * sub account number exists, and sub object code exists
063: *
064: * @param document submitted document
065: * @return true if objects are completed filled out, sub account number exists, and sub object code exists
066: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
067: */
068: protected boolean processCustomRouteDocumentBusinessRules(
069: MaintenanceDocument document) {
070: // default to success
071: boolean success = true;
072:
073: success &= checkPartiallyFilledOutReferences();
074: success &= checkSubAccountNumberExistence();
075: success &= checkSubObjectCodeExistence();
076:
077: return success;
078: }
079:
080: /**
081: * Validates a bank account maintenance document when it is save. Although method checks if objects are completely filled out,
082: * sub account number exists, and sub object code exists, this method always returns TRUE.
083: *
084: * @param document submitted document
085: * @return true always
086: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
087: */
088: protected boolean processCustomSaveDocumentBusinessRules(
089: MaintenanceDocument document) {
090: checkPartiallyFilledOutReferences();
091: checkSubAccountNumberExistence();
092: checkSubObjectCodeExistence();
093:
094: return true;
095: }
096:
097: /**
098: * This method sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and
099: * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
100: * all sub-objects from the DB by their primary keys, if available.
101: *
102: * @param document the maintenanceDocument being evaluated
103: */
104: public void setupConvenienceObjects() {
105: // setup oldAccount convenience objects, make sure all possible sub-objects are populated
106: oldBankAccount = (BankAccount) super .getOldBo();
107:
108: // setup newAccount convenience objects, make sure all possible sub-objects are populated
109: newBankAccount = (BankAccount) super .getNewBo();
110: }
111:
112: /**
113: * This method checks for partially filled out objects.
114: *
115: * @param document maintenance document being evaluated
116: * @return true if there are no partially filled out references
117: */
118: private boolean checkPartiallyFilledOutReferences() {
119:
120: boolean success = true;
121:
122: success &= checkForPartiallyFilledOutReferenceForeignKeys("cashOffsetAccount");
123: return success;
124: }
125:
126: /**
127: * This method validates that the Cash Offset subAccountNumber exists, if it is entered
128: *
129: * @param document maintenance document being evaluated
130: * @return true if sub account number exists
131: */
132: private boolean checkSubAccountNumberExistence() {
133:
134: boolean success = true;
135:
136: // if all of the relevant values arent entered, don't bother
137: if (StringUtils.isBlank(newBankAccount
138: .getCashOffsetFinancialChartOfAccountCode())) {
139: return success;
140: }
141: if (StringUtils.isBlank(newBankAccount
142: .getCashOffsetAccountNumber())) {
143: return success;
144: }
145: if (StringUtils.isBlank(newBankAccount
146: .getCashOffsetSubAccountNumber())) {
147: return success;
148: }
149:
150: // setup the map to search on
151: Map pkMap = new HashMap();
152: pkMap.put("chartOfAccountsCode", newBankAccount
153: .getCashOffsetFinancialChartOfAccountCode());
154: pkMap.put("accountNumber", newBankAccount
155: .getCashOffsetAccountNumber());
156: pkMap.put("subAccountNumber", newBankAccount
157: .getCashOffsetSubAccountNumber());
158:
159: // do the search
160: SubAccount testSubAccount = (SubAccount) getBoService()
161: .findByPrimaryKey(SubAccount.class, pkMap);
162:
163: // fail if the subAccount isnt found
164: if (testSubAccount == null) {
165: putFieldError("cashOffsetSubAccountNumber",
166: KFSKeyConstants.ERROR_EXISTENCE, getDdService()
167: .getAttributeLabel(BankAccount.class,
168: "cashOffsetSubAccountNumber"));
169: success &= false;
170: }
171:
172: return success;
173: }
174:
175: /**
176: * This method validates that the Cash Offset subObjectCode exists, if it is entered
177: *
178: * @param document maintenance document being evaluated
179: * @return true if sub object code exists
180: */
181: private boolean checkSubObjectCodeExistence() {
182:
183: boolean success = true;
184:
185: // if all of the relevant values arent entered, dont bother
186: if (StringUtils.isBlank(newBankAccount
187: .getCashOffsetFinancialChartOfAccountCode())) {
188: return success;
189: }
190: if (StringUtils.isBlank(newBankAccount
191: .getCashOffsetAccountNumber())) {
192: return success;
193: }
194: if (StringUtils.isBlank(newBankAccount
195: .getCashOffsetObjectCode())) {
196: return success;
197: }
198: if (StringUtils.isBlank(newBankAccount
199: .getCashOffsetSubObjectCode())) {
200: return success;
201: }
202:
203: // setup the map to search on
204: Map pkMap = new HashMap();
205: pkMap.put("universityFiscalYear", SpringContext.getBean(
206: UniversityDateService.class).getCurrentFiscalYear());
207: pkMap.put("chartOfAccountsCode", newBankAccount
208: .getCashOffsetFinancialChartOfAccountCode());
209: pkMap.put("accountNumber", newBankAccount
210: .getCashOffsetAccountNumber());
211: pkMap.put("financialObjectCode", newBankAccount
212: .getCashOffsetObjectCode());
213: pkMap.put("financialSubObjectCode", newBankAccount
214: .getCashOffsetSubObjectCode());
215:
216: // do the search
217: SubObjCd testSubObjCd = (SubObjCd) getBoService()
218: .findByPrimaryKey(SubObjCd.class, pkMap);
219:
220: // fail if the subObjectCode isnt found
221: if (testSubObjCd == null) {
222: putFieldError("cashOffsetSubObjectCode",
223: KFSKeyConstants.ERROR_EXISTENCE, getDdService()
224: .getAttributeLabel(BankAccount.class,
225: "cashOffsetSubObjectCode"));
226: success &= false;
227: }
228:
229: return success;
230: }
231:
232: /**
233: * This method has been taken out of service to be replaced by the defaultExistenceChecks happening through the
234: * BankAccountMaintenanceDocument.xml
235: *
236: * @param document maintenance document being evaluated
237: * @return true if sub object code exists
238: */
239: private boolean checkSubObjectExistence(MaintenanceDocument document) {
240: // default to success
241: boolean success = true;
242: BankAccount newBankAcct = (BankAccount) document
243: .getNewMaintainableObject().getBusinessObject();
244: newBankAcct.refresh();
245:
246: // existence check on bank code
247:
248: if (StringUtils.isNotEmpty(newBankAcct
249: .getFinancialDocumentBankCode())) {
250: if (ObjectUtils.isNull(newBankAcct.getBank())) {
251: success &= false;
252: putFieldError(
253: "financialDocumentBankCode",
254: KFSKeyConstants.ERROR_DOCUMENT_BANKACCMAINT_INVALID_BANK);
255: }
256: }
257:
258: return success;
259: }
260: }
|