View Javadoc
1   /*
2    * Copyright 2006 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 java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.coa.businessobject.Account;
23  import org.kuali.ole.coa.businessobject.SubAccount;
24  import org.kuali.ole.coa.businessobject.SubObjectCode;
25  import org.kuali.ole.coa.businessobject.SubObjectCodeCurrent;
26  import org.kuali.ole.fp.businessobject.CreditCardVendor;
27  import org.kuali.ole.sys.OLEKeyConstants;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
30  
31  /**
32   * This class represents business rules for the credit card vendor maintenance document
33   */
34  public class CreditCardVendorRule extends MaintenanceDocumentRuleBase {
35  
36      protected CreditCardVendor newCreditCardVendor;
37  
38      /**
39       *  Sets up a CreditCardVendor convenience objects to make sure all possible sub-objects are populated
40       * 
41       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
42       */
43      public void setupConvenienceObjects() {
44  
45          newCreditCardVendor = (CreditCardVendor) super.getNewBo();
46      }
47  
48      /**
49       * Return true if rules for processing a save for the credit card maintenance document are are valid.
50       * 
51       * @param document maintenance document
52       * @return true credit card vendor number is valid
53       * 
54       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
55       */
56      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
57          // default to success
58          boolean success = true;
59          setupConvenienceObjects();
60  
61          // check valid Credit Card Vendor Number (numeric, minimum length)
62          success &= checkCreditCardVendorNumber();
63  
64          return success;
65      }
66  
67      /**
68       * Returns value from processCustomRouteDocumentBusinessRules(document)
69       * 
70       * @param document maintenance document
71       * @return value from processCustomRouteDocumentBusinessRules(document)
72       * 
73       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
74       */
75      protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
76  
77          return processCustomRouteDocumentBusinessRules(document);
78      }
79  
80      /**
81       * Returns true credit card vendor maintenance document is routed successfully
82       * 
83       * @param document submitted credit card maintenance document
84       * @return true if credit card vendor number, income/expense account numbers, income/expense sub-account numbers, and income/expense sub-object codes are valid
85       * 
86       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
87       */
88      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
89          // default to success
90          boolean success = true;
91  
92          setupConvenienceObjects();
93  
94          // check valid Credit Card Vendor Number (numeric, minimum length)
95          success &= checkCreditCardVendorNumber();
96  
97          // check Income Account Number business rule
98          if ( StringUtils.isNotBlank( newCreditCardVendor.getIncomeAccountNumber() ) ) {
99              success &= checkExistingActiveAccount( newCreditCardVendor.getIncomeFinancialChartOfAccountsCode(), newCreditCardVendor.getIncomeAccountNumber(), "incomeAccountNumber", "Income Account Number");
100         }
101 
102         // check Expense Account Number business rule
103         if ( StringUtils.isNotBlank( newCreditCardVendor.getExpenseAccountNumber() ) ) {
104             success &= checkExistingActiveAccount( newCreditCardVendor.getExpenseFinancialChartOfAccountsCode(), newCreditCardVendor.getExpenseAccountNumber(), "expenseAccountNumber", "Expense Account Number");
105         }
106 
107         // check Income Sub-Account business rule
108         if ( StringUtils.isNotBlank( newCreditCardVendor.getIncomeSubAccountNumber() ) ) {
109 
110             // check required fields to validate Sub-Account
111             if (checkRequiredSubAccount("Income")) {
112                 SubAccount existenceSubAccount = checkExistenceSubAccount("Income");
113 
114                 // check existence of Sub-Account
115                 if (existenceSubAccount == null) {
116                     putFieldError("incomeSubAccountNumber", OLEKeyConstants.ERROR_CCV_INVALIDSUBACCOUNT, "Income Sub-Account Number, " + newCreditCardVendor.getIncomeSubAccountNumber());
117                 }
118                 else
119 
120                 // check the Sub-Account is active
121                 if (!existenceSubAccount.isActive()) {
122                     putFieldError("incomeSubAccountNumber", OLEKeyConstants.ERROR_INACTIVE, "Income Sub-Account");
123                 }
124             }
125         }
126 
127         // check Expense Sub-Account business rule
128         if ( StringUtils.isNotBlank( newCreditCardVendor.getExpenseSubAccountNumber() ) ) {
129             if (checkRequiredSubAccount("Expense")) {
130 
131                 // check existence of Sub-Account
132                 SubAccount existenceSubAccount = checkExistenceSubAccount("Expense");
133                 if (existenceSubAccount == null) {
134                     putFieldError("expenseSubAccountNumber", OLEKeyConstants.ERROR_CCV_INVALIDSUBACCOUNT, "Expense Sub-Account Number, " + newCreditCardVendor.getExpenseSubAccountNumber());
135 
136                 }
137                 else
138 
139                 // check the Sub-Account is active
140                 if (!existenceSubAccount.isActive()) {
141                     putFieldError("expenseSubAccountNumber", OLEKeyConstants.ERROR_INACTIVE, "Expense Sub-Account");
142                 }
143             }
144         }
145 
146         // check Income Sub-Object Code business rule
147         if ( StringUtils.isNotBlank( newCreditCardVendor.getIncomeFinancialSubObjectCode() ) ) {
148             if (checkRequiredSubObjectCode("Income")) {
149 
150                 // check existence of Sub-Object
151                 SubObjectCode existenceSubObj = checkExistenceSubObj("Income");
152                 if (existenceSubObj == null) {
153                     putFieldError("incomeFinancialSubObjectCode", OLEKeyConstants.ERROR_CCV_INVALIDSUBOBJECT, "Income Sub-Object Code, " + newCreditCardVendor.getIncomeFinancialSubObjectCode());
154                 }
155                 else
156                 // check the Sub-Object is active
157                 if (!existenceSubObj.isActive()) {
158                     putFieldError("incomeFinancialSubObjectCode", OLEKeyConstants.ERROR_INACTIVE, "Income Sub-Object");
159                 }
160 
161             }
162         }
163 
164         // check Expense Sub-Object Code business rule
165         if ( StringUtils.isNotBlank( newCreditCardVendor.getExpenseFinancialSubObjectCode() ) ) {
166             if (checkRequiredSubObjectCode("Expense")) {
167 
168                 // check existence of Sub-Object
169                 SubObjectCode existenceSubObj = checkExistenceSubObj("Expense");
170                 if (existenceSubObj == null) {
171                     putFieldError("expenseFinancialSubObjectCode", OLEKeyConstants.ERROR_CCV_INVALIDSUBOBJECT, "Expense Sub-Object Code, " + newCreditCardVendor.getExpenseFinancialSubObjectCode());
172                 }
173                 else
174                 // check the Sub-Object is active
175                 if (!existenceSubObj.isActive()) {
176                     putFieldError("expenseFinancialSubObjectCode", OLEKeyConstants.ERROR_INACTIVE, "Expense Sub-Object");
177                 }
178             }
179         }
180 
181 
182         return success;
183     }
184 
185 
186     /**
187      * Returns true if credit card vendor number is valid (i.e. numeric and at least 5 digits)
188      * 
189      * @return true if credit card vendor number is valid (i.e. numeric and at least 5 digits)
190      */
191     protected boolean checkCreditCardVendorNumber() {
192         String ccvNumber = newCreditCardVendor.getFinancialDocumentCreditCardVendorNumber();
193 
194         if (ccvNumber == null) {
195             return false;
196         }
197         else if (!StringUtils.isNumeric(ccvNumber)) {
198             putFieldError("financialDocumentCreditCardVendorNumber", OLEKeyConstants.ERROR_NUMERIC, "Vendor Credit Card Number");
199             return false;
200         }
201         else if (ccvNumber.length() < 5) {
202             String errorMessage[] = null;
203             errorMessage = new String[] { "Vendor Credit Card Number", "5" };
204             putFieldError("financialDocumentCreditCardVendorNumber", OLEKeyConstants.ERROR_MIN_LENGTH, errorMessage);
205             return false;
206         }
207 
208         return true;
209     }
210 
211 
212     /**
213      * Returns true if account is active (i.e. exists and is not expired or closed)
214      * 
215      * @param accountNumber account number
216      * @param fieldName field name to place error for
217      * @param errorMessage error message to display
218      * @return true if account is active (i.e. exists and is not expired or closed)
219      */
220     protected boolean checkExistingActiveAccount( String chartOfAccountsCode, String accountNumber, String fieldName, String errorMessage) {
221         boolean result = false;
222         Account account;
223         Map<String,String> pkMap = new HashMap<String,String>();
224         pkMap.put("accountNumber", accountNumber);
225         pkMap.put("chartOfAccountsCode", chartOfAccountsCode);
226         account = (Account) getBoService().findByPrimaryKey(Account.class, pkMap);
227 
228         // if the object doesnt exist, then we cant continue, so exit
229         if (account == null) {
230             putFieldError(fieldName, OLEKeyConstants.ERROR_EXISTENCE, errorMessage);
231             return result;
232         }
233 
234         // check whether expired or not
235         if (account.isExpired()) {
236             putFieldError(fieldName, OLEKeyConstants.ERROR_EXPIRED, errorMessage);
237             return result;
238         }
239 
240         // check whether closed or not
241         if (!account.isActive()) {
242             putFieldError(fieldName, OLEKeyConstants.ERROR_CLOSED, errorMessage);
243             return result;
244         }
245 
246 
247         return true;
248     }
249 
250     /**
251      * Returns true if income/expense financial chart of accounts code and account number exist. Income or expense is determined by
252      * the "Income" value or the "Expense" value passed in to the method as a string
253      * 
254      * @param string determines whether or not to check income or expense sub account information (valid values include "Income" or "Expense")
255      * @return true if corresponding sub account values exist
256      */
257     protected boolean checkRequiredSubAccount(String string) {
258         boolean returnVal = true;
259         if (string.equals("Income")) {
260             if (newCreditCardVendor.getIncomeFinancialChartOfAccountsCode() == null) {
261                 putFieldError("incomeFinancialChartOfAccountsCode", OLEKeyConstants.ERROR_CCV_INCOME_SUBACCOUNT_REQUIRED, "Income Chart");
262                 returnVal = false;
263             }
264 
265             if (newCreditCardVendor.getIncomeAccountNumber() == null) {
266                 putFieldError("incomeAccountNumber", OLEKeyConstants.ERROR_CCV_INCOME_SUBACCOUNT_REQUIRED, "Income Account Number");
267                 returnVal = false;
268             }
269         }
270 
271 
272         if (string.equals("Expense")) {
273             if (newCreditCardVendor.getExpenseFinancialChartOfAccountsCode() == null) {
274                 putFieldError("expenseFinancialChartOfAccountsCode", OLEKeyConstants.ERROR_CCV_EXPENSE_SUBACCOUNT_REQUIRED, "Expense Chart");
275                 returnVal = false;
276             }
277 
278             if (newCreditCardVendor.getExpenseAccountNumber() == null) {
279                 putFieldError("expenseAccountNumber", OLEKeyConstants.ERROR_CCV_EXPENSE_SUBACCOUNT_REQUIRED, "Expense Account Number");
280                 returnVal = false;
281             }
282         }
283 
284 
285         return returnVal;
286     }
287 
288 
289     /**
290      * Returns a SubAccount object if SubAccount object exists for "Income" or "Expense"
291      * 
292      * @param string determines whether or not to retrieve a income or expense sub account (valid values include "Income" or "Expense")
293      * @return SubAccount Income/Expense SubAccount object
294      */
295     protected SubAccount checkExistenceSubAccount(String string) {
296 
297         SubAccount subAccount = null;
298 
299         if (string.equals("Income")) {
300             Map<String,String> pkMap = new HashMap<String,String>();
301             pkMap.put("chartOfAccountsCode", newCreditCardVendor.getIncomeFinancialChartOfAccountsCode());
302             pkMap.put("accountNumber", newCreditCardVendor.getIncomeAccountNumber());
303             pkMap.put("subAccountNumber", newCreditCardVendor.getIncomeSubAccountNumber());
304             subAccount = (SubAccount) getBoService().findByPrimaryKey(SubAccount.class, pkMap);
305         }
306 
307         if (string.equals("Expense")) {
308             Map<String,String> pkMap = new HashMap<String,String>();
309             pkMap.put("chartOfAccountsCode", newCreditCardVendor.getExpenseFinancialChartOfAccountsCode());
310             pkMap.put("accountNumber", newCreditCardVendor.getExpenseAccountNumber());
311             pkMap.put("subAccountNumber", newCreditCardVendor.getExpenseSubAccountNumber());
312             subAccount = (SubAccount) getBoService().findByPrimaryKey(SubAccount.class, pkMap);
313         }
314 
315         return subAccount;
316     }
317 
318 
319     /**
320      * Returns a true sub-object code exists for "Income" or "Expense"
321      * 
322      * @param string determines whether or not to check for an income or expense sub-object code (valid values include "Income" or "Expense")
323      * @return true if income/expense chart of account code, account number, and financial object code exist
324      */
325     protected boolean checkRequiredSubObjectCode(String string) {
326 
327         boolean returnVal = true;
328         if (string.equals("Income")) {
329             if ( StringUtils.isBlank( newCreditCardVendor.getIncomeFinancialChartOfAccountsCode() ) ) {
330                 putFieldError("incomeFinancialChartOfAccountsCode", OLEKeyConstants.ERROR_CCV_INCOME_SUBOBJ_REQUIRED, "Income Chart");
331                 returnVal = false;
332             }
333 
334             if ( StringUtils.isBlank( newCreditCardVendor.getIncomeAccountNumber() ) ) {
335                 putFieldError("incomeAccountNumber", OLEKeyConstants.ERROR_CCV_INCOME_SUBOBJ_REQUIRED, "Income Account Number");
336                 returnVal = false;
337             }
338 
339             if ( StringUtils.isBlank( newCreditCardVendor.getIncomeFinancialObjectCode() ) ) {
340                 putFieldError("incomeFinancialObjectCode", OLEKeyConstants.ERROR_CCV_INCOME_SUBOBJ_REQUIRED, "Income Object Code");
341                 returnVal = false;
342             }
343 
344         }
345 
346 
347         if (string.equals("Expense")) {
348             if ( StringUtils.isBlank( newCreditCardVendor.getExpenseFinancialChartOfAccountsCode() ) ) {
349                 putFieldError("expenseFinancialChartOfAccountsCode", OLEKeyConstants.ERROR_CCV_EXPENSE_SUBOBJ_REQUIRED, "Expense Chart");
350                 returnVal = false;
351             }
352 
353             if ( StringUtils.isBlank( newCreditCardVendor.getExpenseAccountNumber() ) ) {
354                 putFieldError("expenseAccountNumber", OLEKeyConstants.ERROR_CCV_EXPENSE_SUBOBJ_REQUIRED, "Expense Account Number");
355                 returnVal = false;
356             }
357 
358             if ( StringUtils.isBlank( newCreditCardVendor.getExpenseFinancialObjectCode() ) ) {
359                 putFieldError("expenseFinancialObjectCode", OLEKeyConstants.ERROR_CCV_EXPENSE_SUBOBJ_REQUIRED, "Expense Object Code");
360                 returnVal = false;
361             }
362 
363         }
364 
365 
366         return returnVal;
367     }
368 
369 
370     /**
371      * Returns a SubObjCd object if SubObjCd object exists for "Income" or "Expense"
372      * 
373      * @param string determines whether or not to retrieve a income or expense sub object (valid values include "Income" or "Expense")
374      * @return SubAccount Income/Expense SubObjCd object
375      */
376     protected SubObjectCode checkExistenceSubObj(String string) {
377 
378         SubObjectCode subObjCd = null;
379 
380         if (string.equals("Income")) {
381             Map<String,String> pkMap = new HashMap<String,String>();
382             pkMap.put("chartOfAccountsCode", newCreditCardVendor.getIncomeFinancialChartOfAccountsCode());
383             pkMap.put("accountNumber", newCreditCardVendor.getIncomeAccountNumber());
384             pkMap.put("financialObjectCode", newCreditCardVendor.getIncomeFinancialObjectCode());
385             pkMap.put("financialSubObjectCode", newCreditCardVendor.getIncomeFinancialSubObjectCode());
386             subObjCd = (SubObjectCode) super.getBoService().findByPrimaryKey(SubObjectCodeCurrent.class, pkMap);
387         }
388 
389         if (string.equals("Expense")) {
390             Map<String,String> pkMap = new HashMap<String,String>();
391             pkMap.put("chartOfAccountsCode", newCreditCardVendor.getExpenseFinancialChartOfAccountsCode());
392             pkMap.put("accountNumber", newCreditCardVendor.getExpenseAccountNumber());
393             pkMap.put("financialObjectCode", newCreditCardVendor.getExpenseFinancialObjectCode());
394             pkMap.put("financialSubObjectCode", newCreditCardVendor.getExpenseFinancialSubObjectCode());
395             subObjCd = (SubObjectCode) super.getBoService().findByPrimaryKey(SubObjectCodeCurrent.class, pkMap);
396         }
397 
398         return subObjCd;
399     }
400 
401 }