1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34 public class CreditCardVendorRule extends MaintenanceDocumentRuleBase {
35
36 protected CreditCardVendor newCreditCardVendor;
37
38
39
40
41
42
43 public void setupConvenienceObjects() {
44
45 newCreditCardVendor = (CreditCardVendor) super.getNewBo();
46 }
47
48
49
50
51
52
53
54
55
56 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
57
58 boolean success = true;
59 setupConvenienceObjects();
60
61
62 success &= checkCreditCardVendorNumber();
63
64 return success;
65 }
66
67
68
69
70
71
72
73
74
75 protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
76
77 return processCustomRouteDocumentBusinessRules(document);
78 }
79
80
81
82
83
84
85
86
87
88 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
89
90 boolean success = true;
91
92 setupConvenienceObjects();
93
94
95 success &= checkCreditCardVendorNumber();
96
97
98 if ( StringUtils.isNotBlank( newCreditCardVendor.getIncomeAccountNumber() ) ) {
99 success &= checkExistingActiveAccount( newCreditCardVendor.getIncomeFinancialChartOfAccountsCode(), newCreditCardVendor.getIncomeAccountNumber(), "incomeAccountNumber", "Income Account Number");
100 }
101
102
103 if ( StringUtils.isNotBlank( newCreditCardVendor.getExpenseAccountNumber() ) ) {
104 success &= checkExistingActiveAccount( newCreditCardVendor.getExpenseFinancialChartOfAccountsCode(), newCreditCardVendor.getExpenseAccountNumber(), "expenseAccountNumber", "Expense Account Number");
105 }
106
107
108 if ( StringUtils.isNotBlank( newCreditCardVendor.getIncomeSubAccountNumber() ) ) {
109
110
111 if (checkRequiredSubAccount("Income")) {
112 SubAccount existenceSubAccount = checkExistenceSubAccount("Income");
113
114
115 if (existenceSubAccount == null) {
116 putFieldError("incomeSubAccountNumber", OLEKeyConstants.ERROR_CCV_INVALIDSUBACCOUNT, "Income Sub-Account Number, " + newCreditCardVendor.getIncomeSubAccountNumber());
117 }
118 else
119
120
121 if (!existenceSubAccount.isActive()) {
122 putFieldError("incomeSubAccountNumber", OLEKeyConstants.ERROR_INACTIVE, "Income Sub-Account");
123 }
124 }
125 }
126
127
128 if ( StringUtils.isNotBlank( newCreditCardVendor.getExpenseSubAccountNumber() ) ) {
129 if (checkRequiredSubAccount("Expense")) {
130
131
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
140 if (!existenceSubAccount.isActive()) {
141 putFieldError("expenseSubAccountNumber", OLEKeyConstants.ERROR_INACTIVE, "Expense Sub-Account");
142 }
143 }
144 }
145
146
147 if ( StringUtils.isNotBlank( newCreditCardVendor.getIncomeFinancialSubObjectCode() ) ) {
148 if (checkRequiredSubObjectCode("Income")) {
149
150
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
157 if (!existenceSubObj.isActive()) {
158 putFieldError("incomeFinancialSubObjectCode", OLEKeyConstants.ERROR_INACTIVE, "Income Sub-Object");
159 }
160
161 }
162 }
163
164
165 if ( StringUtils.isNotBlank( newCreditCardVendor.getExpenseFinancialSubObjectCode() ) ) {
166 if (checkRequiredSubObjectCode("Expense")) {
167
168
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
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
188
189
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
214
215
216
217
218
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
229 if (account == null) {
230 putFieldError(fieldName, OLEKeyConstants.ERROR_EXISTENCE, errorMessage);
231 return result;
232 }
233
234
235 if (account.isExpired()) {
236 putFieldError(fieldName, OLEKeyConstants.ERROR_EXPIRED, errorMessage);
237 return result;
238 }
239
240
241 if (!account.isActive()) {
242 putFieldError(fieldName, OLEKeyConstants.ERROR_CLOSED, errorMessage);
243 return result;
244 }
245
246
247 return true;
248 }
249
250
251
252
253
254
255
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
291
292
293
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
321
322
323
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
372
373
374
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 }