Skip to content

Instantly share code, notes, and snippets.

@pocketkk
Created March 30, 2015 20:12
Show Gist options
  • Save pocketkk/8ec883ac81a4e59a46d0 to your computer and use it in GitHub Desktop.
Save pocketkk/8ec883ac81a4e59a46d0 to your computer and use it in GitHub Desktop.
Check Product Pairs
''' <summary>
''' Checks a customer product table to see if product dependencies are met
''' </summary>
''' <param name="customerProducts"></param>
''' <param name="isThrowError"></param>
''' <returns>Dictionary with key value pairs of product_ids</returns>
''' <remarks></remarks>
'''
Protected Function CheckProductPairings(ByVal customerProducts As DataSetApplication.CustomerProductDataTable, _
Optional ByVal isThrowError As Boolean = True) As Dictionary(Of String, String)
Dim _pairedProductREFs As New DataSetSystem.ProductDataTable
Dim _pairedProductREF As DataSetSystem.ProductRow
Dim _unpairedProductsDict As New Dictionary(Of String, String)
'CHECK PRODUCTS TO SEE IF ANY ARE PAIRED AND THAT THE PAIRED PRODUCT EXISTS IN CUSTOMER PRODUCTS
For i As Integer = 0 To customerProducts.Count - 1
s_byColumns.Clear()
_pairedProductREF = _pairedProductREFs.NewProductRow()
_pairedProductREF.Product_ID = customerProducts(i).CustomerProduct_ProductID
s_byColumns.Add(s_productREFs.Product_IDColumn.ColumnName)
If Not SqlDataOperator.SelectByColumns(s_dataResult, s_sqlConnectionSystem, _pairedProductREFs, _pairedProductREF, s_byColumns) Then
Throw New Exception(s_dataResult.DataException.Message)
End If
If _pairedProductREFs.Rows.Count <> 1 Then
Throw New Exception(String.Format("{0} returned more than one record.", customerProducts(i).CustomerProduct_ProductID))
End If
'IF PRODUCT HAS A PAIRED PRODUCT, LOOP THROUGH AND SEE IF IT EXISTS IN DATATABLE
If Not _pairedProductREFs(0).IsProduct_ProductIDNull Then
Dim _isPaired As Boolean = False
For j As Integer = 0 To s_customerProductUIs.Count - 1
If _pairedProductREFs(0).Product_ProductID = customerProducts(j).CustomerProduct_ProductID Then
_isPaired = True
End If
Next
If Not _isPaired Then
_unpairedProductsDict.Add(_pairedProductREFs(0).Product_ID, _pairedProductREFs(0).Product_ProductID)
If isThrowError Then
s_messageFlag = MESSAGEFLAG.ERROR_FORMDATA
GLOBAL_SUB_ADDERROR(MESSAGEFLAG.ERROR_FORMDATA, s_errorCache)
ERROR_FORMDATA.Text = String.Format("PRODUCT {0} IS PAIRED WITH {1}<BR >ADD OR REMOVE PRODUCT(S) TO RESOLVE DEPENDENCY", _pairedProductREFs(0).Product_ProductID, _
_pairedProductREFs(0).Product_ID)
Throw New Exception()
End If
End If
End If
Next
Return _unpairedProductsDict
End Function
@Trevor1929
Copy link

You will need to define s_byColumns and s_messageFlag as well as MESSAGEFLAG.ERROR_FORMDATA. We define these in our code-behind normally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment