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.text.MessageFormat;
19 import java.util.List;
20
21 import org.kuali.ole.fp.businessobject.DisbursementVoucherNonEmployeeExpense;
22 import org.kuali.ole.fp.businessobject.DisbursementVoucherNonEmployeeTravel;
23 import org.kuali.ole.fp.businessobject.TravelCompanyCode;
24 import org.kuali.ole.fp.document.DisbursementVoucherDocument;
25 import org.kuali.ole.sys.OLEKeyConstants;
26 import org.kuali.ole.sys.OLEPropertyConstants;
27 import org.kuali.ole.sys.context.SpringContext;
28 import org.kuali.ole.sys.document.AccountingDocument;
29 import org.kuali.ole.sys.document.validation.GenericValidation;
30 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
31 import org.kuali.rice.krad.service.BusinessObjectService;
32 import org.kuali.rice.krad.util.GlobalVariables;
33 import org.kuali.rice.krad.util.MessageMap;
34 import org.kuali.rice.krad.util.ObjectUtils;
35
36 public class DisbursementVoucherNonEmployeeTravelCompanyValidation extends GenericValidation {
37 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherNonEmployeeTravelCompanyValidation.class);
38
39 private AccountingDocument accountingDocumentForValidation;
40
41 public final static String DV_PRE_PAID_EMPLOYEE_EXPENSES_PROPERTY_PATH = OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.DV_NON_EMPLOYEE_TRAVEL + "." + OLEPropertyConstants.DV_PRE_PAID_EMPLOYEE_EXPENSES;
42 public final static String DV_NON_EMPLOYEE_EXPENSES_PROPERTY_PATH = OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.DV_NON_EMPLOYEE_TRAVEL + "." + OLEPropertyConstants.DV_NON_EMPLOYEE_EXPENSES;
43
44
45
46
47 @Override
48 public boolean validate(AttributedDocumentEvent event) {
49 LOG.debug("validate start");
50
51 boolean isValid = true;
52 DisbursementVoucherDocument disbursementVoucherDocument = (DisbursementVoucherDocument) accountingDocumentForValidation;
53 DisbursementVoucherNonEmployeeTravel nonEmployeeTravel = disbursementVoucherDocument.getDvNonEmployeeTravel();
54
55 MessageMap errors = GlobalVariables.getMessageMap();
56 errors.addToErrorPath(OLEPropertyConstants.DOCUMENT);
57
58
59 int index = 0;
60 List<DisbursementVoucherNonEmployeeExpense> expenses = nonEmployeeTravel.getDvNonEmployeeExpenses();
61 for (DisbursementVoucherNonEmployeeExpense expense : expenses) {
62 TravelCompanyCode travelCompanyCode = retrieveCompany(expense.getDisbVchrExpenseCode(), expense.getDisbVchrExpenseCompanyName());
63
64 if (ObjectUtils.isNull(travelCompanyCode)) {
65 String fullPropertyName = this.buildFullPropertyName(DV_NON_EMPLOYEE_EXPENSES_PROPERTY_PATH, index, OLEPropertyConstants.DISB_VCHR_EXPENSE_COMPANY_NAME);
66 errors.putErrorWithoutFullErrorPath(fullPropertyName, OLEKeyConstants.ERROR_EXISTENCE, "Company ");
67 isValid = false;
68 }
69
70 index++;
71 }
72
73
74 index = 0;
75 List<DisbursementVoucherNonEmployeeExpense> prePaidExpenses = nonEmployeeTravel.getDvPrePaidEmployeeExpenses();
76 for (DisbursementVoucherNonEmployeeExpense prePaidExpense : prePaidExpenses) {
77 TravelCompanyCode travelCompanyCode = retrieveCompany(prePaidExpense.getDisbVchrExpenseCode(), prePaidExpense.getDisbVchrExpenseCompanyName());
78
79 if (ObjectUtils.isNull(travelCompanyCode)) {
80 String fullPropertyName = this.buildFullPropertyName(DV_PRE_PAID_EMPLOYEE_EXPENSES_PROPERTY_PATH, index, OLEPropertyConstants.DISB_VCHR_EXPENSE_COMPANY_NAME);
81 errors.putErrorWithoutFullErrorPath(fullPropertyName, OLEKeyConstants.ERROR_EXISTENCE, "Company ");
82 isValid = false;
83 }
84
85 index++;
86 }
87
88 errors.removeFromErrorPath(OLEPropertyConstants.DOCUMENT);
89
90 return isValid;
91 }
92
93
94 protected String buildFullPropertyName(String propertyPath, int index, String propertyName) {
95 String fileNamePattern = "{0}[{1}].{2}";
96
97 return MessageFormat.format(fileNamePattern, propertyPath, index, propertyName);
98 }
99
100
101 protected TravelCompanyCode retrieveCompany(String companyCode, String companyName) {
102 TravelCompanyCode travelCompanyCode = new TravelCompanyCode();
103 travelCompanyCode.setCode(companyCode);
104 travelCompanyCode.setName(companyName);
105 return (TravelCompanyCode) SpringContext.getBean(BusinessObjectService.class).retrieve(travelCompanyCode);
106 }
107
108
109
110
111
112
113 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
114 this.accountingDocumentForValidation = accountingDocumentForValidation;
115 }
116
117
118
119
120
121 public AccountingDocument getAccountingDocumentForValidation() {
122 return accountingDocumentForValidation;
123 }
124 }