View Javadoc
1   /*
2    * Copyright 2008 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.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       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
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          // check non employee travel company exists
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          // check prepaid expenses company exists
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      // build the full name of a document property
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     // Retrieves the Company object from the company name
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      * Sets the accountingDocumentForValidation attribute value.
110      *
111      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
112      */
113     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
114         this.accountingDocumentForValidation = accountingDocumentForValidation;
115     }
116 
117     /**
118      * Gets the accountingDocumentForValidation attribute.
119      * @return Returns the accountingDocumentForValidation.
120      */
121     public AccountingDocument getAccountingDocumentForValidation() {
122         return accountingDocumentForValidation;
123     }
124 }