1 package org.kuali.ole.vnd.document.validation.impl;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.ole.select.document.OlePurchaseOrderDocument;
5 import org.kuali.ole.select.document.OleRequisitionDocument;
6 import org.kuali.ole.vnd.VendorKeyConstants;
7 import org.kuali.ole.vnd.VendorPropertyConstants;
8 import org.kuali.ole.vnd.businessobject.*;
9 import org.kuali.rice.kns.document.MaintenanceDocument;
10 import org.kuali.rice.krad.service.KRADServiceLocator;
11
12 import java.util.*;
13
14
15
16
17
18
19
20
21 public class OleVendorRule extends VendorRule {
22
23 @Override
24 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
25 boolean valid = processValidation(document);
26 VendorDetail vendorDetail = (VendorDetail)document.getNewMaintainableObject().getDataObject();
27
28 List<VendorContact> vendorContacts = vendorDetail.getVendorContacts();
29 for (Iterator<VendorContact> iterator = vendorContacts.iterator(); iterator.hasNext(); ) {
30 VendorContact vendorContact = iterator.next();
31 vendorDetail.getVendorContactPhoneNumberMap().put(vendorContacts.indexOf(vendorContact),vendorContact.getVendorContactPhoneNumbers());
32 }
33 List<VendorTransmissionFormatDetail> vendorTransmissionFormatDetailList = vendorDetail.getVendorTransmissionFormat();
34 List formatId = new ArrayList();
35 if(!vendorDetail.isNonBillable()){
36 if(vendorDetail.getPaymentMethodId() == null || vendorDetail.getPaymentMethodId().equals("")){
37 putFieldError(VendorPropertyConstants.PAYMENT_METHOD_ID,VendorKeyConstants.PAYMENT_METHOD_ID_REQUIRED);
38 valid &= false;
39 }
40 if(vendorDetail.getCurrencyTypeId() == null || vendorDetail.getCurrencyTypeId().equals("")){
41 putFieldError(VendorPropertyConstants.CURRENCY_TYPE,VendorKeyConstants.CURRENCY_TYPE_REQUIRED);
42 valid &= false;
43 }
44 if(vendorDetail.getVendorHeader() != null && (vendorDetail.getVendorHeader().getVendorTypeCode() == null || vendorDetail.getVendorHeader().getVendorTypeCode().equals(""))){
45 putFieldError(VendorPropertyConstants.VENDOR_TYPE_CODE,VendorKeyConstants.VENDOR_TYPE_CODE_REQUIRED);
46 valid &= false;
47 }
48 if(vendorDetail.getVendorHeader()!=null && (vendorDetail.getVendorHeader().getVendorOwnershipCode() == null || vendorDetail.getVendorHeader().getVendorOwnershipCode().equals(""))){
49 putFieldError(VendorPropertyConstants.VENDOR_OWNERSHIP_CODE,VendorKeyConstants.OWNERSHIP_TYPE_CODE_REQUIRED);
50 valid &= false;
51 }
52 }
53 for(VendorTransmissionFormatDetail vendorTransmissionFormatDetail : vendorTransmissionFormatDetailList) {
54 if (formatId.contains(vendorTransmissionFormatDetail.getVendorTransmissionFormatId())) {
55 putFieldError(VendorPropertyConstants.VENDOR_TRANSMISSION_FORMAT, VendorKeyConstants.OLE_VENDOR_DUPLICATE_TRANS_FORMAT);
56 valid &= false;
57 }
58 formatId.add(vendorTransmissionFormatDetail.getVendorTransmissionFormatId());
59 }
60 HashMap vendor = new HashMap();
61 if (!vendorDetail.isActiveIndicator()) {
62 vendor.put("vendorHeaderGeneratedIdentifier", vendorDetail.getVendorHeaderGeneratedIdentifier());
63 vendor.put("vendorDetailAssignedIdentifier", vendorDetail.getVendorDetailAssignedIdentifier());
64 List<OleRequisitionDocument> oleRequisitionDocuments = (List<OleRequisitionDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleRequisitionDocument.class, vendor);
65 List<OlePurchaseOrderDocument> purchaseOrderDocuments = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderDocument.class, vendor);
66 if (oleRequisitionDocuments.size() > 0) {
67 putFieldError(VendorPropertyConstants.VENDOR_ACTIVE_INDICATOR, VendorKeyConstants.OLE_VENDOR_REQUISITION_LINKED);
68 }
69 if (purchaseOrderDocuments.size() > 0) {
70 putFieldError(VendorPropertyConstants.VENDOR_ACTIVE_INDICATOR, VendorKeyConstants.OLE_VENDOR_PURCHSEORDER_LINKED);
71 }
72 }
73 vendor = new HashMap();
74 vendor.put("gokbId", vendorDetail.getGokbId());
75 List<VendorDetail> vendorDetails = (List<VendorDetail>)KRADServiceLocator.getBusinessObjectService().findMatching(VendorDetail.class, vendor);
76 if (vendorDetail.getVendorHeaderGeneratedIdentifier() == null && vendorDetail.getVendorDetailAssignedIdentifier() == null) {
77 if (vendorDetails.size() > 0) {
78 putFieldError(VendorPropertyConstants.VENDOR_GOKB_ID, VendorKeyConstants.OLE_VENDOR_GOKB_EXISTS);
79 valid &= false;
80 }
81 } else {
82 if (vendorDetails.size() > 0) {
83 for(VendorDetail detail:vendorDetails){
84 if(detail.isActiveIndicator() && ! detail.getVendorName().equalsIgnoreCase( vendorDetail.getVendorName())){
85 putFieldError(VendorPropertyConstants.VENDOR_GOKB_ID, VendorKeyConstants.OLE_VENDOR_GOKB_EXISTS);
86 valid &= false;
87 }
88 }
89
90 }
91 }
92 return valid & super.processCustomRouteDocumentBusinessRules(document);
93 }
94
95 private boolean processValidation(MaintenanceDocument document) {
96 boolean valid = true;
97 VendorDetail vendorDetail = (VendorDetail) document.getNewMaintainableObject().getDataObject();
98 valid &= processExternalVendorCode(vendorDetail);
99
100 return valid;
101 }
102
103 protected boolean processExternalVendorCode(VendorDetail vendorDetail) {
104 boolean valid = true;
105 List<VendorAlias> vendorAliases = vendorDetail.getVendorAliases();
106 List<String> aliasList=new ArrayList<>();
107 if(vendorAliases.size()>0) {
108 for(VendorAlias vendorAlias : vendorAliases) {
109 if(!aliasList.contains(vendorAlias.getVendorAliasType().getAliasType()))
110 {
111 aliasList.add(vendorAlias.getVendorAliasType().getAliasType());
112 } else{
113 putFieldError(VendorPropertyConstants.VENDOR_SEARCH_ALIASES, VendorKeyConstants.OLE_VENDOR_DUPLICATE_VARIANT_NAME);
114 valid &= false;
115 }
116 }
117 }
118 if (StringUtils.isBlank(vendorDetail.getVendorName())) {
119
120
121 if (StringUtils.isBlank(vendorDetail.getVendorFirstName()) && StringUtils.isBlank(vendorDetail.getVendorLastName())) {
122
123
124
125 putFieldError(VendorPropertyConstants.VENDOR_ALIAS_NAME, VendorKeyConstants.OLE_VENDOR_EMPTY_NAME);
126 valid &= false;
127 }
128
129
130 }
131
132 return valid;
133 }
134 }