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.purap.document.validation.impl;
017    
018    import java.util.List;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.module.purap.PurapConstants;
022    import org.kuali.kfs.module.purap.PurapKeyConstants;
023    import org.kuali.kfs.module.purap.PurapRuleConstants;
024    import org.kuali.kfs.module.purap.document.PurchasingDocument;
025    import org.kuali.kfs.sys.KFSKeyConstants;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
028    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
029    import org.kuali.kfs.vnd.VendorPropertyConstants;
030    import org.kuali.kfs.vnd.businessobject.VendorAddress;
031    import org.kuali.kfs.vnd.businessobject.VendorDetail;
032    import org.kuali.kfs.vnd.businessobject.VendorHeader;
033    import org.kuali.kfs.vnd.document.service.VendorService;
034    import org.kuali.rice.kns.datadictionary.validation.fieldlevel.PhoneNumberValidationPattern;
035    import org.kuali.rice.kns.service.DataDictionaryService;
036    import org.kuali.rice.kns.service.ParameterService;
037    import org.kuali.rice.kns.util.GlobalVariables;
038    import org.kuali.rice.kns.util.MessageMap;
039    import org.kuali.rice.kns.util.ObjectUtils;
040    
041    public class PurchasingProcessVendorValidation extends PurchasingAccountsPayableProcessVendorValidation {
042        
043        private VendorService vendorService;
044        private ParameterService parameterService;
045        
046        public boolean validate(AttributedDocumentEvent event) {
047            boolean valid = true;
048            PurchasingDocument purDocument = (PurchasingDocument) event.getDocument();
049            MessageMap errorMap = GlobalVariables.getMessageMap();
050            errorMap.clearErrorPath();
051            errorMap.addToErrorPath(PurapConstants.VENDOR_ERRORS);
052            
053            valid &= super.validate(event);        
054            
055            if (!purDocument.getRequisitionSourceCode().equals(PurapConstants.RequisitionSources.B2B)) {
056                
057                //If there is a vendor and the transmission method is FAX and the fax number is blank, display
058                //error that the fax number is required.
059                if (purDocument.getVendorHeaderGeneratedIdentifier() != null && purDocument.getPurchaseOrderTransmissionMethodCode().equals(PurapConstants.POTransmissionMethods.FAX) && StringUtils.isBlank(purDocument.getVendorFaxNumber())) {
060                    valid &= false;
061                    String attributeLabel = SpringContext.getBean(DataDictionaryService.class).
062                    getDataDictionary().getBusinessObjectEntry(VendorAddress.class.getName()).
063                    getAttributeDefinition(VendorPropertyConstants.VENDOR_FAX_NUMBER).getLabel();
064                    errorMap.putError(VendorPropertyConstants.VENDOR_FAX_NUMBER, KFSKeyConstants.ERROR_REQUIRED, attributeLabel);
065                }
066                if (StringUtils.isNotBlank(purDocument.getVendorFaxNumber())) {
067                    PhoneNumberValidationPattern phonePattern = new PhoneNumberValidationPattern();
068                    if (!phonePattern.matches(purDocument.getVendorFaxNumber())) {
069                        valid &= false;
070                        errorMap.putError(VendorPropertyConstants.VENDOR_FAX_NUMBER, PurapKeyConstants.ERROR_FAX_NUMBER_INVALID);
071                    }
072                }
073            }
074    
075            VendorDetail vendorDetail = vendorService.getVendorDetail(purDocument.getVendorHeaderGeneratedIdentifier(), purDocument.getVendorDetailAssignedIdentifier());
076            if (ObjectUtils.isNull(vendorDetail))
077                return valid;
078            VendorHeader vendorHeader = vendorDetail.getVendorHeader();
079    
080            // make sure that the vendor is not debarred
081            if (vendorDetail.isVendorDebarred()) {
082                valid &= false;
083                errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_DEBARRED_VENDOR);
084            }
085    
086            // make sure that the vendor is of allowed type
087            List<String> allowedVendorTypes = parameterService.getParameterValues(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapRuleConstants.PURAP_VENDOR_TYPE_ALLOWED_ON_REQ_AND_PO);
088            if (allowedVendorTypes != null && !allowedVendorTypes.isEmpty()){
089               if (ObjectUtils.isNotNull(vendorHeader) && ObjectUtils.isNotNull(vendorHeader.getVendorTypeCode()) && ! allowedVendorTypes.contains(vendorHeader.getVendorTypeCode())) {
090                        valid &= false;
091                        errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_INVALID_VENDOR_TYPE);
092                }
093            }
094    
095            // make sure that the vendor is active
096            if (!vendorDetail.isActiveIndicator()) {
097                valid &= false;
098                errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_INACTIVE_VENDOR);
099            }
100    
101            errorMap.clearErrorPath();
102            return valid;
103    
104        }
105    
106        public VendorService getVendorService() {
107            return vendorService;
108        }
109    
110        public void setVendorService(VendorService vendorService) {
111            this.vendorService = vendorService;
112        }
113    
114        public ParameterService getParameterService() {
115            return parameterService;
116        }
117    
118        public void setParameterService(ParameterService parameterService) {
119            this.parameterService = parameterService;
120        }
121            
122    }