001/* 002 * Copyright 2008 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */ 016package org.kuali.ole.fp.document.validation.impl; 017 018import java.text.MessageFormat; 019import java.util.List; 020 021import org.kuali.ole.fp.businessobject.DisbursementVoucherNonEmployeeExpense; 022import org.kuali.ole.fp.businessobject.DisbursementVoucherNonEmployeeTravel; 023import org.kuali.ole.fp.businessobject.TravelCompanyCode; 024import org.kuali.ole.fp.document.DisbursementVoucherDocument; 025import org.kuali.ole.sys.OLEKeyConstants; 026import org.kuali.ole.sys.OLEPropertyConstants; 027import org.kuali.ole.sys.context.SpringContext; 028import org.kuali.ole.sys.document.AccountingDocument; 029import org.kuali.ole.sys.document.validation.GenericValidation; 030import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent; 031import org.kuali.rice.krad.service.BusinessObjectService; 032import org.kuali.rice.krad.util.GlobalVariables; 033import org.kuali.rice.krad.util.MessageMap; 034import org.kuali.rice.krad.util.ObjectUtils; 035 036public class DisbursementVoucherNonEmployeeTravelCompanyValidation extends GenericValidation { 037 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherNonEmployeeTravelCompanyValidation.class); 038 039 private AccountingDocument accountingDocumentForValidation; 040 041 public final static String DV_PRE_PAID_EMPLOYEE_EXPENSES_PROPERTY_PATH = OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.DV_NON_EMPLOYEE_TRAVEL + "." + OLEPropertyConstants.DV_PRE_PAID_EMPLOYEE_EXPENSES; 042 public final static String DV_NON_EMPLOYEE_EXPENSES_PROPERTY_PATH = OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.DV_NON_EMPLOYEE_TRAVEL + "." + OLEPropertyConstants.DV_NON_EMPLOYEE_EXPENSES; 043 044 /** 045 * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent) 046 */ 047 @Override 048 public boolean validate(AttributedDocumentEvent event) { 049 LOG.debug("validate start"); 050 051 boolean isValid = true; 052 DisbursementVoucherDocument disbursementVoucherDocument = (DisbursementVoucherDocument) accountingDocumentForValidation; 053 DisbursementVoucherNonEmployeeTravel nonEmployeeTravel = disbursementVoucherDocument.getDvNonEmployeeTravel(); 054 055 MessageMap errors = GlobalVariables.getMessageMap(); 056 errors.addToErrorPath(OLEPropertyConstants.DOCUMENT); 057 058 // check non employee travel company exists 059 int index = 0; 060 List<DisbursementVoucherNonEmployeeExpense> expenses = nonEmployeeTravel.getDvNonEmployeeExpenses(); 061 for (DisbursementVoucherNonEmployeeExpense expense : expenses) { 062 TravelCompanyCode travelCompanyCode = retrieveCompany(expense.getDisbVchrExpenseCode(), expense.getDisbVchrExpenseCompanyName()); 063 064 if (ObjectUtils.isNull(travelCompanyCode)) { 065 String fullPropertyName = this.buildFullPropertyName(DV_NON_EMPLOYEE_EXPENSES_PROPERTY_PATH, index, OLEPropertyConstants.DISB_VCHR_EXPENSE_COMPANY_NAME); 066 errors.putErrorWithoutFullErrorPath(fullPropertyName, OLEKeyConstants.ERROR_EXISTENCE, "Company "); 067 isValid = false; 068 } 069 070 index++; 071 } 072 073 // check prepaid expenses company exists 074 index = 0; 075 List<DisbursementVoucherNonEmployeeExpense> prePaidExpenses = nonEmployeeTravel.getDvPrePaidEmployeeExpenses(); 076 for (DisbursementVoucherNonEmployeeExpense prePaidExpense : prePaidExpenses) { 077 TravelCompanyCode travelCompanyCode = retrieveCompany(prePaidExpense.getDisbVchrExpenseCode(), prePaidExpense.getDisbVchrExpenseCompanyName()); 078 079 if (ObjectUtils.isNull(travelCompanyCode)) { 080 String fullPropertyName = this.buildFullPropertyName(DV_PRE_PAID_EMPLOYEE_EXPENSES_PROPERTY_PATH, index, OLEPropertyConstants.DISB_VCHR_EXPENSE_COMPANY_NAME); 081 errors.putErrorWithoutFullErrorPath(fullPropertyName, OLEKeyConstants.ERROR_EXISTENCE, "Company "); 082 isValid = false; 083 } 084 085 index++; 086 } 087 088 errors.removeFromErrorPath(OLEPropertyConstants.DOCUMENT); 089 090 return isValid; 091 } 092 093 // build the full name of a document property 094 protected String buildFullPropertyName(String propertyPath, int index, String propertyName) { 095 String fileNamePattern = "{0}[{1}].{2}"; 096 097 return MessageFormat.format(fileNamePattern, propertyPath, index, propertyName); 098 } 099 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}