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.module.purap.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.*;
20  import org.kuali.ole.module.purap.document.PurchasingDocument;
21  import org.kuali.ole.module.purap.document.PurchasingDocumentBase;
22  import org.kuali.ole.sys.OLEConstants;
23  import org.kuali.ole.sys.OLEKeyConstants;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26  import org.kuali.ole.sys.service.PostalCodeValidationService;
27  import org.kuali.ole.sys.service.impl.OleParameterConstants;
28  import org.kuali.ole.vnd.VendorPropertyConstants;
29  import org.kuali.ole.vnd.businessobject.VendorAddress;
30  import org.kuali.ole.vnd.businessobject.VendorDetail;
31  import org.kuali.ole.vnd.businessobject.VendorHeader;
32  import org.kuali.ole.vnd.document.service.VendorService;
33  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
34  import org.kuali.rice.kns.service.DataDictionaryService;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.kuali.rice.krad.util.MessageMap;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  public class PurchasingProcessVendorValidation extends PurchasingAccountsPayableProcessVendorValidation {
43  
44      private VendorService vendorService;
45      private ParameterService parameterService;
46      private PostalCodeValidationService postalCodeValidationService;
47  
48      @Override
49      public boolean validate(AttributedDocumentEvent event) {
50          boolean valid = true;
51          PurchasingDocument purDocument = (PurchasingDocument) event.getDocument();
52          MessageMap errorMap = GlobalVariables.getMessageMap();
53          errorMap.clearErrorPath();
54          errorMap.addToErrorPath(PurapConstants.VENDOR_ERRORS);
55  
56          valid &= super.validate(event);
57  
58          if (purDocument.getRequisitionSourceCode() != null && !purDocument.getRequisitionSourceCode().equals(PurapConstants.RequisitionSources.B2B)) {
59  
60              //If there is a vendor and the transmission method is FAX and the fax number is blank, display
61              //error that the fax number is required.
62              if (purDocument.getVendorHeaderGeneratedIdentifier() != null && purDocument.getPurchaseOrderTransmissionMethodCode().equals(PurapConstants.POTransmissionMethods.FAX) && StringUtils.isBlank(purDocument.getVendorFaxNumber())) {
63                  valid &= false;
64                  String attributeLabel = SpringContext.getBean(DataDictionaryService.class).
65                          getDataDictionary().getBusinessObjectEntry(VendorAddress.class.getName()).
66                          getAttributeDefinition(VendorPropertyConstants.VENDOR_FAX_NUMBER).getLabel();
67                  errorMap.putError(VendorPropertyConstants.VENDOR_FAX_NUMBER, OLEKeyConstants.ERROR_REQUIRED, attributeLabel);
68              }
69          }
70  
71          VendorDetail vendorDetail = vendorService.getVendorDetail(purDocument.getVendorHeaderGeneratedIdentifier(), purDocument.getVendorDetailAssignedIdentifier());
72          if (ObjectUtils.isNull(vendorDetail)) {
73              return valid;
74          }
75          VendorHeader vendorHeader = vendorDetail.getVendorHeader();
76  
77          // make sure that the vendor is not debarred
78          if (vendorDetail.isVendorDebarred()) {
79              if (parameterService.getParameterValueAsBoolean(OLEConstants.OptionalModuleNamespaces.PURCHASING_ACCOUNTS_PAYABLE, "Requisition", PurapParameterConstants.SHOW_DEBARRED_VENDOR_WARNING_IND)) {
80                  if (StringUtils.isEmpty(((PurchasingDocumentBase) purDocument).getJustification())) {
81                      errorMap.putWarning(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.WARNING_DEBARRED_VENDOR, vendorDetail.getVendorName());
82                      valid &= false;
83                  }
84              } else {
85                  errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_DEBARRED_VENDOR);
86                  valid &= false;
87              }
88          }
89  
90          // make sure that the vendor is of allowed type
91          List<String> allowedVendorTypes = new ArrayList<String>(parameterService.getParameterValuesAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapRuleConstants.PURAP_VENDOR_TYPE_ALLOWED_ON_REQ_AND_PO));
92          if (allowedVendorTypes != null && !allowedVendorTypes.isEmpty()) {
93              if (ObjectUtils.isNotNull(vendorHeader) && ObjectUtils.isNotNull(vendorHeader.getVendorTypeCode()) && !allowedVendorTypes.contains(vendorHeader.getVendorTypeCode())) {
94                  valid &= false;
95                  errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_INVALID_VENDOR_TYPE);
96              }
97          }
98  
99          // make sure that the vendor is active
100         if (!vendorDetail.isActiveIndicator()) {
101             valid &= false;
102             errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_INACTIVE_VENDOR);
103         }
104 
105         // validate vendor address
106         postalCodeValidationService.validateAddress(purDocument.getVendorCountryCode(), purDocument.getVendorStateCode(), purDocument.getVendorPostalCode(), PurapPropertyConstants.VENDOR_STATE_CODE, PurapPropertyConstants.VENDOR_POSTAL_CODE);
107 
108         errorMap.clearErrorPath();
109         return valid;
110 
111     }
112 
113     public VendorService getVendorService() {
114         return vendorService;
115     }
116 
117     public void setVendorService(VendorService vendorService) {
118         this.vendorService = vendorService;
119     }
120 
121     public ParameterService getParameterService() {
122         return parameterService;
123     }
124 
125     public void setParameterService(ParameterService parameterService) {
126         this.parameterService = parameterService;
127     }
128 
129     /**
130      * Gets the postalCodeValidationService attribute.
131      *
132      * @return Returns the postalCodeValidationService
133      */
134 
135     public PostalCodeValidationService getPostalCodeValidationService() {
136         return postalCodeValidationService;
137     }
138 
139     /**
140      * Sets the postalCodeValidationService attribute.
141      *
142      * @param postalCodeValidationService The postalCodeValidationService to set.
143      */
144     public void setPostalCodeValidationService(PostalCodeValidationService postalCodeValidationService) {
145         this.postalCodeValidationService = postalCodeValidationService;
146     }
147 }