Advertisement
Guest User

i_can_csv

a guest
Nov 23rd, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. Public Class DbManager
  2. Private _masterFilePath As String = ""
  3. Private _slaveFilePath As String = ""
  4. Private _masterItemsList As New List(Of MasterItem)
  5. Private _slaveItemList As New List(Of SlaveItem)
  6. Private _masterFileHeaderText As String = ""
  7. Private _slaveFileHeaderText As String = ""
  8. Private Const MASTER_FILE_COLUMN_COUNT As Int32 = 7
  9. Private Const SLAVE_FILE_COLUMN_COUNT As Int32 = 19
  10. Private Const SLAVE_FILE_HEADER_TEXT As String = "Item ID,Name,Category,Description,Variant 1 - Name,Variant 1 - Price,Variant 1 - SKU,Variant 2 - Name,Variant 2 - Price,Variant 2 - SKU,Variant 3 - Name,Variant 3 - Price,Variant 3 - SKU,Variant 4 - Name,Variant 4 - Price,Variant 4 - SKU,Variant 5 - Name,Variant 5 - Price,Variant 5 - SKU"
  11.  
  12.  
  13.  
  14. Public Sub OpenMasterFile(ByVal filePath As String)
  15. If System.IO.File.Exists(filePath) Then
  16. _masterItemsList.Clear()
  17. Dim sr As New System.IO.StreamReader(filePath)
  18. Dim origLine As String = ""
  19. Dim tmpLine As String = ""
  20. Dim splitLines() As String
  21. Dim tmpItem As New MasterItem
  22. Dim lineNumber As Int32 = 1
  23. Dim headerLine As String = sr.ReadLine
  24. If headerLine.Split(","c).Count() <> MASTER_FILE_COLUMN_COUNT Then
  25. MessageBox.Show("OpenMasterFile():: Invalid header for Master File on line 1 ! Expected " & MASTER_FILE_COLUMN_COUNT.ToString() & " columns, but got: " & Environment.NewLine _
  26. & headerLine & Environment.NewLine & "UNABLE TO CONTINUE!")
  27. sr.Close() : sr.Dispose()
  28. Exit Sub
  29. End If
  30. _masterFileHeaderText = headerLine ''need to backup for when we write a new output file
  31. While sr.Peek <> -1
  32. origLine = sr.ReadLine
  33. tmpLine = origLine
  34. splitLines = tmpLine.Split(","c)
  35. lineNumber += 1
  36. If splitLines(1).StartsWith("""") Then ''has special ItemName - including commas and double-quote marks
  37. tmpItem.InternalID = splitLines(0)
  38. Dim specialLine As String = tmpLine.Substring(tmpLine.IndexOf(","c) + 2) ''start of name + trailing info
  39. tmpItem.Name = ParseSpecialCSVvalue(specialLine)
  40. specialLine = specialLine.Substring(tmpItem.Name.Length + 2) ''+1 get rid of comma after item name
  41. If specialLine.StartsWith("""") Then
  42. 'special VariationName also
  43. tmpItem.VariationName = ParseSpecialCSVvalue(specialLine)
  44. specialLine = specialLine.Substring(tmpItem.VariationName.Length + 1) ''+1 get rid of comma after variant name
  45. splitLines = specialLine.Split(","c)
  46. tmpItem.SKU = splitLines(0)
  47. tmpItem.AlertThreshold = splitLines(1)
  48. tmpItem.CurrentQuantity = splitLines(2)
  49. tmpItem.NewQuantity = splitLines(3)
  50. Else
  51. 'normal or no VariationName
  52. splitLines = specialLine.Split(","c)
  53. tmpItem.VariationName = splitLines(0)
  54. tmpItem.SKU = splitLines(1)
  55. tmpItem.AlertThreshold = splitLines(2)
  56. tmpItem.CurrentQuantity = splitLines(3)
  57. tmpItem.NewQuantity = splitLines(4)
  58. End If
  59. ElseIf splitLines.Count = MASTER_FILE_COLUMN_COUNT Then
  60. tmpItem.InternalID = splitLines(0)
  61. tmpItem.Name = splitLines(1)
  62. tmpItem.VariationName = splitLines(2)
  63. tmpItem.SKU = splitLines(3)
  64. tmpItem.AlertThreshold = splitLines(4)
  65. tmpItem.CurrentQuantity = splitLines(5)
  66. tmpItem.NewQuantity = splitLines(6)
  67.  
  68. Else
  69. MessageBox.Show("OpenMasterFile():: Invalid item on line: " & lineNumber.ToString() & ". Not " & MASTER_FILE_COLUMN_COUNT.ToString() & " columns?! SKIPPING this line!" & Environment.NewLine _
  70. & "Actual line: " & Environment.NewLine & tmpLine)
  71. Continue While
  72. End If
  73. _masterItemsList.Add(tmpItem)
  74. End While
  75. sr.Close()
  76. sr.Dispose()
  77. Else
  78. Throw New Exception("DatabaseManager::OpenMasterItemFile() filepath does not exist! Unable to open 'Master Items File'")
  79. End If
  80. MsgBox("total items: " & _masterItemsList.Count.ToString())
  81. End Sub
  82. Public Sub OpenSlaveFile(ByVal filePath As String)
  83. If System.IO.File.Exists(filePath) Then
  84. Dim sr As New System.IO.StreamReader(filePath)
  85. Dim entireFileText As String = sr.ReadToEnd() : sr.Close() : sr.Dispose()
  86. Dim tmpItem As New SlaveItem
  87. Dim errorCount As Int32 = 0
  88. ''find header text
  89. Dim splitFileLines() As String = entireFileText.Split(ChrW(Keys.LineFeed))
  90.  
  91. If splitFileLines(0) <> SLAVE_FILE_HEADER_TEXT Then ''invalid file?
  92. MessageBox.Show("OpenSlaveFile():: Invalid header for Slave File!" & Environment.NewLine _
  93. & "Actual value: " & splitFileLines(0) & Environment.NewLine & Environment.NewLine & "UNABLE TO CONTINUE!")
  94. sr.Close() : sr.Dispose()
  95. Exit Sub
  96. End If
  97. Dim tmpIndex As Int32 = 0
  98. Dim tmpLine As String = ""
  99. Dim lineCount As Int32 = 0
  100. splitFileLines(0) = ""
  101. For Each fileLine As String In splitFileLines
  102. If fileLine <> "" Then
  103. tmpLine = fileLine
  104. tmpIndex = tmpLine.IndexOf(","c)
  105. tmpItem.ItemID = tmpLine.Substring(0, tmpIndex)
  106. tmpLine = tmpLine.Substring(tmpIndex + 1)
  107. ''Name
  108. If tmpLine.StartsWith(""""",") Then ''no ItemName!
  109. MessageBox.Show("OpenSlaveFile:: Item with no name!" & Environment.NewLine & "Actual line: " & fileLine)
  110. tmpItem.ItemName = ""
  111. errorCount += 1
  112. tmpLine = tmpLine.Substring(3)
  113. ElseIf tmpLine(0) = """" Then ''special ItemName
  114. tmpLine = tmpLine.Substring(1)
  115. tmpItem.ItemName = ParseSpecialCSVvalue(tmpLine)
  116. tmpLine = tmpLine.Substring(tmpItem.ItemName.Length + 2)
  117. Else ''regular ItemName
  118. tmpItem.ItemName = tmpLine.Substring(0, tmpLine.IndexOf(","c))
  119. tmpLine = tmpLine.Substring(tmpItem.ItemName.Length + 1)
  120. End If
  121. ''Category
  122. If tmpLine.StartsWith(""""",") Then
  123. tmpItem.Category = "" ''no Category
  124. errorCount += 1
  125. tmpLine = tmpLine.Substring(3)
  126. ElseIf tmpLine(0) = """" Then
  127. tmpItem.Category = ParseSpecialCSVvalue(tmpLine) ''special Category
  128. tmpLine = tmpLine.Substring(tmpItem.Category.Length + 2)
  129. Else
  130. tmpItem.Category = tmpLine.Substring(0, tmpLine.IndexOf(","c)) ''normal Category
  131. tmpLine = tmpLine.Substring(tmpItem.Category.Length + 1)
  132. End If
  133. ''Description
  134. If tmpLine.StartsWith(""""",") Then ''no Description!
  135. tmpItem.Description = ""
  136. ElseIf tmpLine(0) = """" Then ''special Description
  137. tmpItem.Description = ParseSpecialCSVvalue(tmpLine)
  138. tmpLine = tmpLine.Substring(tmpItem.Description.Length + 1)
  139. Else
  140. tmpItem.Description = tmpLine.Substring(0, tmpLine.IndexOf(","c))
  141. End If
  142. ''Variant1to5(name,price,sku)
  143. '' HERE:
  144. lineCount += 1
  145. _slaveItemList.Add(tmpItem)
  146. End If
  147.  
  148. Next
  149. MessageBox.Show(lineCount.ToString() & " items scanned." & Environment.NewLine _
  150. & "Lines containing errors: " & errorCount.ToString, "SLAVE File")
  151. Else
  152. Throw New Exception("DatabaseManager::OpenSlaveItemFile() filepath does not exist! Unable to open 'Slave Items File'")
  153. End If
  154.  
  155. End Sub
  156. Public Structure MasterItem
  157. Public InternalID As String
  158. Public Name As String
  159. Public VariationName As String
  160. Public SKU As String
  161. Public AlertThreshold As String
  162. Public CurrentQuantity As String
  163. Public NewQuantity As String
  164. Public Sub New(ByVal newInternalID As String, ByVal newItemName As String, ByVal newVariationName As String, ByVal newSKU As String, ByVal newAlertThreshold As String, ByVal newCurrentQuantity As String, ByVal newNewQuantity As String)
  165. InternalID = newInternalID
  166. Name = newItemName
  167. VariationName = newVariationName
  168. SKU = newSKU
  169. AlertThreshold = newAlertThreshold
  170. CurrentQuantity = newCurrentQuantity
  171. NewQuantity = newNewQuantity
  172. End Sub
  173. End Structure
  174. Public Structure SlaveItem
  175. Public ItemID As String
  176. Public ItemName As String
  177. Public Category As String
  178. Public Description As String
  179. Public Variant1Name, Variant1Price, Variant1SKU As String
  180. Public Variant2Name, Variant2Price, Variant2SKU As String
  181. Public Variant3Name, Variant3Price, Variant3SKU As String
  182. Public Variant4Name, Variant4Price, Variant4SKU As String
  183. Public Variant5Name, Variant5Price, Variant5SKU As String
  184. End Structure
  185.  
  186. Private Function ParseSpecialCSVvalue(ByVal textLine As String) As String
  187. For xx As Int32 = 0 To textLine.Length - 2
  188. If textLine(xx) = """" Then
  189. If textLine(xx + 1) = """" Then ''double-quote mark inside of item name
  190. xx += 1 ''skip the aforementioned double-quote character
  191. ElseIf textLine(xx + 1) = "," Then
  192. Return textLine.Substring(0, xx) ''closing quote
  193. Else
  194. MsgBox("ParseSpecialCSVvalue:: Unknown string literal with double-quote. Actual text:" & Environment.NewLine & Environment.NewLine & textLine) '' unknown string literal beginning with " , or formatting error
  195. Exit For
  196. End If
  197. End If
  198. Next
  199. Return "ERROR"
  200. End Function
  201. Public Function GetMasterItemData(ByVal SKU As String) As MasterItem
  202. For Each itm As MasterItem In _masterItemsList
  203. If itm.SKU = SKU Then ''found the item
  204. Return itm
  205. End If
  206. Next
  207. ''if we get here, we never found the item
  208. Return New MasterItem("error", "error", "error", SKU, "error", "error", "error")
  209. End Function
  210.  
  211. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement