001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.fp.document.validation.impl;
017    
018    import java.text.MessageFormat;
019    import java.util.List;
020    
021    import org.kuali.kfs.fp.businessobject.DisbursementVoucherNonEmployeeExpense;
022    import org.kuali.kfs.fp.businessobject.DisbursementVoucherNonEmployeeTravel;
023    import org.kuali.kfs.fp.businessobject.TravelCompanyCode;
024    import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
025    import org.kuali.kfs.sys.KFSKeyConstants;
026    import org.kuali.kfs.sys.KFSPropertyConstants;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.kfs.sys.document.AccountingDocument;
029    import org.kuali.kfs.sys.document.validation.GenericValidation;
030    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
031    import org.kuali.rice.kns.service.BusinessObjectService;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    import org.kuali.rice.kns.util.MessageMap;
034    import org.kuali.rice.kns.util.ObjectUtils;
035    
036    public class DisbursementVoucherNonEmployeeTravelComanyValidation extends GenericValidation {
037        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherNonEmployeeTravelComanyValidation.class);
038    
039        private AccountingDocument accountingDocumentForValidation;
040    
041        public final static String DV_PRE_PAID_EMPLOYEE_EXPENSES_PROPERTY_PATH = KFSPropertyConstants.DOCUMENT + "." + KFSPropertyConstants.DV_NON_EMPLOYEE_TRAVEL + "." + KFSPropertyConstants.DV_PRE_PAID_EMPLOYEE_EXPENSES;
042        public final static String DV_NON_EMPLOYEE_EXPENSES_PROPERTY_PATH = KFSPropertyConstants.DOCUMENT + "." + KFSPropertyConstants.DV_NON_EMPLOYEE_TRAVEL + "." + KFSPropertyConstants.DV_NON_EMPLOYEE_EXPENSES;
043    
044        /**
045         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
046         */
047        public boolean validate(AttributedDocumentEvent event) {
048            LOG.debug("validate start");
049            
050            boolean isValid = true;
051            DisbursementVoucherDocument disbursementVoucherDocument = (DisbursementVoucherDocument) accountingDocumentForValidation;
052            DisbursementVoucherNonEmployeeTravel nonEmployeeTravel = disbursementVoucherDocument.getDvNonEmployeeTravel();
053            
054            MessageMap errors = GlobalVariables.getMessageMap();
055            errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
056    
057            // check non employee travel company exists
058            int index = 0;
059            List<DisbursementVoucherNonEmployeeExpense> expenses = nonEmployeeTravel.getDvNonEmployeeExpenses();
060            for (DisbursementVoucherNonEmployeeExpense expense : expenses) {
061                TravelCompanyCode travelCompanyCode = retrieveCompany(expense.getDisbVchrExpenseCode(), expense.getDisbVchrExpenseCompanyName());
062    
063                if (ObjectUtils.isNull(travelCompanyCode)) {
064                    String fullPropertyName = this.buildFullPropertyName(DV_NON_EMPLOYEE_EXPENSES_PROPERTY_PATH, index, KFSPropertyConstants.DISB_VCHR_EXPENSE_COMPANY_NAME);
065                    errors.putErrorWithoutFullErrorPath(fullPropertyName, KFSKeyConstants.ERROR_EXISTENCE, "Company ");
066                    isValid = false;
067                }
068    
069                index++;
070            }
071    
072            // check prepaid expenses company exists
073            index = 0;
074            List<DisbursementVoucherNonEmployeeExpense> prePaidExpenses = nonEmployeeTravel.getDvPrePaidEmployeeExpenses();
075            for (DisbursementVoucherNonEmployeeExpense prePaidExpense : prePaidExpenses) {
076                TravelCompanyCode travelCompanyCode = retrieveCompany(prePaidExpense.getDisbVchrExpenseCode(), prePaidExpense.getDisbVchrExpenseCompanyName());
077    
078                if (ObjectUtils.isNull(travelCompanyCode)) {
079                    String fullPropertyName = this.buildFullPropertyName(DV_PRE_PAID_EMPLOYEE_EXPENSES_PROPERTY_PATH, index, KFSPropertyConstants.DISB_VCHR_EXPENSE_COMPANY_NAME);
080                    errors.putErrorWithoutFullErrorPath(fullPropertyName, KFSKeyConstants.ERROR_EXISTENCE, "Company ");
081                    isValid = false;
082                }
083    
084                index++;
085            }
086            
087            errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT);
088            
089            return isValid;
090        }
091    
092        // build the full name of a document property
093        protected String buildFullPropertyName(String propertyPath, int index, String propertyName) {
094            String fileNamePattern = "{0}[{1}].{2}";
095    
096            return MessageFormat.format(fileNamePattern, propertyPath, index, propertyName);
097        }
098    
099        // Retrieves the Company object from the company name
100        protected TravelCompanyCode retrieveCompany(String companyCode, String companyName) {
101            TravelCompanyCode travelCompanyCode = new TravelCompanyCode();
102            travelCompanyCode.setCode(companyCode);
103            travelCompanyCode.setName(companyName);
104            return (TravelCompanyCode) SpringContext.getBean(BusinessObjectService.class).retrieve(travelCompanyCode);
105        }
106    
107        /**
108         * Sets the accountingDocumentForValidation attribute value.
109         * 
110         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
111         */
112        public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
113            this.accountingDocumentForValidation = accountingDocumentForValidation;
114        }
115    
116        /**
117         * Gets the accountingDocumentForValidation attribute. 
118         * @return Returns the accountingDocumentForValidation.
119         */
120        public AccountingDocument getAccountingDocumentForValidation() {
121            return accountingDocumentForValidation;
122        }
123    }