View Javadoc

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