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.module.ar.document.validation.impl;
017    
018    import static org.kuali.kfs.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
019    
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.kuali.kfs.module.ar.ArConstants;
026    import org.kuali.kfs.module.ar.ArKeyConstants;
027    import org.kuali.kfs.module.ar.ArPropertyConstants;
028    import org.kuali.kfs.module.ar.businessobject.CashControlDetail;
029    import org.kuali.kfs.module.ar.businessobject.Customer;
030    import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceDetail;
031    import org.kuali.kfs.module.ar.businessobject.OrganizationOptions;
032    import org.kuali.kfs.module.ar.businessobject.PaymentMedium;
033    import org.kuali.kfs.module.ar.document.CashControlDocument;
034    import org.kuali.kfs.sys.KFSConstants;
035    import org.kuali.kfs.sys.KFSPropertyConstants;
036    import org.kuali.kfs.sys.businessobject.ChartOrgHolder;
037    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry;
038    import org.kuali.kfs.sys.context.SpringContext;
039    import org.kuali.kfs.sys.document.validation.GenericValidation;
040    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
041    import org.kuali.rice.kns.service.BusinessObjectService;
042    import org.kuali.rice.kns.service.DocumentService;
043    import org.kuali.rice.kns.service.ParameterService;
044    import org.kuali.rice.kns.util.GlobalVariables;
045    import org.kuali.rice.kns.util.ObjectUtils;
046    
047    public class CashControlRefDocNumberValidation extends GenericValidation {
048    
049        private CashControlDocument cashControlDocument;
050        private DocumentService documentService;
051        
052        public boolean validate(AttributedDocumentEvent event) {
053            
054            boolean isValid = true;
055            GlobalVariables.getMessageMap().addToErrorPath(KFSConstants.DOCUMENT_PROPERTY_NAME);
056            String paymentMedium = cashControlDocument.getCustomerPaymentMediumCode();
057            if (ArConstants.PaymentMediumCode.CASH.equalsIgnoreCase(paymentMedium)) {
058                String refDocNumber = cashControlDocument.getReferenceFinancialDocumentNumber();
059                try {
060                    Long.parseLong(refDocNumber);
061                    if (StringUtils.isBlank(refDocNumber)) {
062                        GlobalVariables.getMessageMap().putError(ArPropertyConstants.CashControlDocumentFields.REFERENCE_FINANCIAL_DOC_NBR, ArKeyConstants.ERROR_REFERENCE_DOC_NUMBER_CANNOT_BE_NULL_FOR_PAYMENT_MEDIUM_CASH);
063                        isValid = false;
064                    }
065                    else {
066                        boolean docExists = SpringContext.getBean(DocumentService.class).documentExists(refDocNumber);
067                        if (!docExists) {
068                            GlobalVariables.getMessageMap().putError(ArPropertyConstants.CashControlDocumentFields.REFERENCE_FINANCIAL_DOC_NBR, ArKeyConstants.ERROR_REFERENCE_DOC_NUMBER_MUST_BE_VALID_FOR_PAYMENT_MEDIUM_CASH);
069                            isValid = false;
070                        }
071                    }
072                }
073                catch (NumberFormatException nfe) {
074                    GlobalVariables.getMessageMap().putError(ArPropertyConstants.CashControlDocumentFields.REFERENCE_FINANCIAL_DOC_NBR, ArKeyConstants.ERROR_REFERENCE_DOC_NUMBER_MUST_BE_VALID_FOR_PAYMENT_MEDIUM_CASH);
075                    isValid = false;
076                }
077    
078            }
079            GlobalVariables.getMessageMap().removeFromErrorPath(KFSConstants.DOCUMENT_PROPERTY_NAME);
080            return isValid;
081        }
082    
083        public CashControlDocument getCashControlDocument() {
084            return cashControlDocument;
085        }
086    
087        public void setCashControlDocument(CashControlDocument cashControlDocument) {
088            this.cashControlDocument = cashControlDocument;
089        }
090    
091        public DocumentService getDocumentService() {
092            return documentService;
093        }
094    
095        public void setDocumentService(DocumentService documentService) {
096            this.documentService = documentService;
097        }
098    }