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