Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.24 KB | None | 0 0
  1. //csvParser.cs
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Microsoft.VisualBasic.FileIO;
  9. using Product = ProductHelper.Product;
  10. using csvWriter = cWriter.csvWriter;
  11.  
  12.  
  13. namespace cParser {
  14.  
  15.     public class csvParser {
  16.  
  17.         private List<string> _handle;
  18.         private List<string> _title;
  19.         private List<string> _body;
  20.         private List<string> _vendor;
  21.         private List<string> _type;
  22.         private List<string> _tags;
  23.         private List<string> _mehrwertSteuer;
  24.         private List<string> _status;
  25.         private List<string> _seoTitel;
  26.         private List<string> _seoBeschreibung;
  27.         private List<string> _versandGewicht;
  28.         private List<string> _nameOption1;
  29.         private List<string> _wertOption1;
  30.         private List<string> _nameOption2;
  31.         private List<string> _wertOption2;
  32.         private List<string> _nameOption3;
  33.         private List<string> _wertOption3;
  34.         private List<string> _sku;
  35.         private List<string> _preis;
  36.         private List<string> _einkaufsPreis;
  37.         private List<string> _barcode;
  38.         private List<string> _aufLager;
  39.         private List<string> _stringiantenId;
  40.         private List<string> _productId;
  41.  
  42.         private List<Product> _products;
  43.         private string path;
  44.         private bool read;
  45.         private void readCsv() {
  46.            
  47.             using TextFieldParser cParser = new TextFieldParser(path);
  48.             cParser.CommentTokens = new string[] { "#" };
  49.             cParser.SetDelimiters(new string[] { "," });
  50.             cParser.HasFieldsEnclosedInQuotes = true;
  51.  
  52.             // Skip the row with the column names
  53.             cParser.ReadLine();
  54.  
  55.             while (!cParser.EndOfData) {
  56.  
  57.                 // Read current line fields, pointer moves to the next line.
  58.                 string[] fields = cParser.ReadFields();
  59.  
  60.                
  61.                 this._handle.Add(fields[0]);
  62.                 this._title.Add(fields[1]);
  63.                 this._body.Add(fields[2]);
  64.                 this._vendor.Add(fields[3]);
  65.                 this._type.Add(fields[4]);
  66.                 this._tags.Add(fields[5]);
  67.                 this._mehrwertSteuer.Add(fields[6]);
  68.                 this._status.Add(fields[7]);
  69.                 this._seoTitel.Add(fields[8]);
  70.                 this._seoBeschreibung.Add(fields[9]);
  71.                 this._versandGewicht.Add(fields[10]);
  72.                 this._nameOption1.Add(fields[11]);
  73.                 this._wertOption1.Add(fields[12]);
  74.                 this._nameOption2.Add(fields[13]);
  75.                 this._wertOption2.Add(fields[14]);
  76.                 this._nameOption3.Add(fields[15]);
  77.                 this._wertOption3.Add(fields[16]);
  78.                 this._sku.Add(fields[17]);
  79.                 this._preis.Add(fields[18]);
  80.                 this._einkaufsPreis.Add(fields[19]);
  81.                 this._barcode.Add(fields[20]);
  82.                 this._aufLager.Add(fields[21]);
  83.                 this._stringiantenId.Add(fields[22]);
  84.                 this._productId.Add(fields[23]);
  85.  
  86.             }
  87.            
  88.             this.read = true;
  89.  
  90.         }
  91.  
  92.         private void addHandle() {
  93.  
  94.             List<string> tList = new List<string>();
  95.             this._handle.ForEach(tList.Add);
  96.  
  97.             foreach (string handle in tList) {
  98.  
  99.                 string temp = handle.ToLower().Replace(" ", "-");
  100.                 this._handle.Remove(handle);
  101.                 this._handle.Add(temp);
  102.  
  103.             }
  104.  
  105.             this.addVendor();
  106.  
  107.         }
  108.  
  109.         private void addVendor() {
  110.  
  111.             // Assuming that the vendor is the Word before the first spacing
  112.  
  113.             List<string> tList = new List<string>();
  114.             this._vendor.ForEach(tList.Add);
  115.  
  116.             foreach (string vendor in tList) {
  117.  
  118.                 string temp = vendor.Split(" ")[0];
  119.                 this._vendor.Remove(vendor);
  120.                 this._vendor.Add(temp);
  121.  
  122.             }
  123.  
  124.             this.addTags();
  125.         }
  126.  
  127.         private void addTags() {
  128.  
  129.             // Example: 187 ELLO & RAFFA => 187,ello,&,raffa
  130.             List<string> tList = new List<string>();
  131.             this._tags.ForEach(tList.Add);
  132.  
  133.             foreach (string tag in tList) {
  134.  
  135.                 string temp = tag.ToLower().Replace(" ", ",");
  136.                 this._tags.Remove(tag);
  137.                 this._tags.Add(temp);
  138.  
  139.             }
  140.  
  141.             replacePublished();
  142.  
  143.         }
  144.  
  145.         private void replacePublished() {
  146.  
  147.             // ?? requires a "Published" column (equal to our Status)
  148.            
  149.             for (int i = 0; i < this._status.Count(); i++) {
  150.                 if (this._status[i] == "YES") {
  151.  
  152.                     this._status[i] = "TRUE";
  153.  
  154.                 }
  155.                 else{
  156.  
  157.                     this._status[i] = "FALSE";
  158.  
  159.                 }
  160.  
  161.             }
  162.  
  163.  
  164.             for (int i = 0; i < this._title.Count; i++)
  165.             {
  166.                 Product product = new Product(_handle.ElementAt(i), _title.ElementAt(i), _body.ElementAt(i),
  167.                     _vendor.ElementAt(i),
  168.                     _type.ElementAt(i), _tags.ElementAt(i), _status.ElementAt(i), _nameOption1.ElementAt(i),
  169.                     _wertOption1.ElementAt(i),
  170.                     _nameOption2.ElementAt(i), _wertOption2.ElementAt(i), _nameOption3.ElementAt(i),
  171.                     _wertOption3.ElementAt(i),
  172.                     _sku.ElementAt(i), _versandGewicht.ElementAt(i), "shopify", _aufLager.ElementAt(i), "deny",
  173.                     "manual",
  174.                     _preis.ElementAt(i), "", _barcode.ElementAt(i).ToString(), "", _title.ElementAt(i), _seoTitel.ElementAt(0),
  175.                     _seoBeschreibung.ElementAt(i),
  176.                     "", "", _einkaufsPreis.ElementAt(i));
  177.  
  178.                 this._products.Add(product);
  179.             }
  180.            
  181.  
  182.         }
  183.  
  184.         public void start(){
  185.  
  186.             this.readCsv();
  187.  
  188.             while (!read) ;
  189.            
  190.             this.addHandle();
  191.  
  192.         }
  193.  
  194.         public void printEverything() {
  195.  
  196.             Console.Write("handle: ");
  197.             this._handle.ForEach(Console.Write);
  198.             Console.WriteLine();
  199.  
  200.             Console.Write("title: ");
  201.             this._title.ForEach(Console.Write);
  202.             Console.WriteLine();
  203.  
  204.             Console.Write("body: ");
  205.             this._body.ForEach(Console.Write);
  206.             Console.WriteLine();
  207.  
  208.             Console.Write("vendor: ");
  209.             this._vendor.ForEach(Console.Write);
  210.             Console.WriteLine();
  211.  
  212.             Console.Write("type: ");
  213.             this._type.ForEach(Console.Write);
  214.             Console.WriteLine();
  215.  
  216.             Console.Write("tags: ");
  217.             this._tags.ForEach(Console.Write);
  218.             Console.WriteLine();
  219.  
  220.             Console.Write("tax: ");
  221.             this._mehrwertSteuer.ForEach(Console.Write);
  222.             Console.WriteLine();
  223.  
  224.             Console.Write("status: ");
  225.             this._status.ForEach(Console.Write);
  226.             Console.WriteLine();
  227.  
  228.             Console.Write("seoTitle: ");
  229.             this._seoTitel.ForEach(Console.Write);
  230.             Console.WriteLine();
  231.  
  232.             Console.Write("seoDescription: ");
  233.             this._seoBeschreibung.ForEach(Console.Write);
  234.             Console.WriteLine();
  235.  
  236.             Console.Write("weight: ");
  237.             this._versandGewicht.ForEach(Console.Write);
  238.             Console.WriteLine();
  239.  
  240.             Console.Write("nameOption1: ");
  241.             this._nameOption1.ForEach(Console.Write);
  242.             Console.WriteLine();
  243.  
  244.             Console.Write("valueOption1: ");
  245.             this._wertOption1.ForEach(Console.Write);
  246.             Console.WriteLine();
  247.  
  248.             Console.Write("nameOption2: ");
  249.             this._nameOption2.ForEach(Console.Write);
  250.             Console.WriteLine();
  251.  
  252.             Console.Write("valueOption2: ");
  253.             this._wertOption2.ForEach(Console.Write);
  254.             Console.WriteLine();
  255.  
  256.             Console.Write("nameOption3: ");
  257.             this._nameOption3.ForEach(Console.Write);
  258.             Console.WriteLine();
  259.  
  260.             Console.Write("valueOption3: ");
  261.             this._wertOption3.ForEach(Console.Write);
  262.             Console.WriteLine();
  263.  
  264.             Console.Write("sku: ");
  265.             this._sku.ForEach(Console.Write);
  266.             Console.WriteLine();
  267.  
  268.             Console.Write("price: ");
  269.             this._preis.ForEach(Console.Write);
  270.             Console.WriteLine();
  271.  
  272.             Console.Write("purchasingPrice: ");
  273.             this._einkaufsPreis.ForEach(Console.Write);
  274.             Console.WriteLine();
  275.  
  276.             Console.Write("barcode: ");
  277.             this._barcode.ForEach(Console.Write);
  278.             Console.WriteLine();
  279.  
  280.             Console.Write("inStock: ");
  281.             this._aufLager.ForEach(Console.Write);
  282.             Console.WriteLine();
  283.  
  284.             Console.Write("stringiantId: ");
  285.             this._stringiantenId.ForEach(Console.Write);
  286.             Console.WriteLine();
  287.  
  288.             Console.Write("productId: ");
  289.             this._productId.ForEach(Console.Write);
  290.             Console.WriteLine();
  291.  
  292.             Console.WriteLine("--------------------products start--------------------");
  293.             foreach (Product p in this._products) {
  294.  
  295.                 p.printEverything();
  296.  
  297.             }
  298.             Console.WriteLine("---------------------products end---------------------");
  299.  
  300.         }
  301.  
  302.         public csvParser() {
  303.  
  304.             this.path = @"killme.csv";
  305.             this._handle = new List<string>();
  306.             this._title = new List<string>();
  307.             this._body = new List<string>();
  308.             this._vendor = new List<string>();
  309.             this._type = new List<string>();
  310.             this._tags = new List<string>();
  311.             this._mehrwertSteuer = new List<string>();
  312.             this._status = new List<string>();
  313.             this._seoTitel = new List<string>();
  314.             this._seoBeschreibung = new List<string>();
  315.             this._versandGewicht = new List<string>();
  316.             this._nameOption1 = new List<string>();
  317.             this._wertOption1 = new List<string>();
  318.             this._nameOption2 = new List<string>();
  319.             this._wertOption2 = new List<string>();
  320.             this._nameOption3 = new List<string>();
  321.             this._wertOption3 = new List<string>();
  322.             this._sku = new List<string>();
  323.             this._preis = new List<string>();
  324.             this._einkaufsPreis = new List<string>();
  325.             this._barcode = new List<string>();
  326.             this._aufLager = new List<string>();
  327.             this._stringiantenId = new List<string>();
  328.             this._productId = new List<string>();
  329.             this._products = new List<Product>();
  330.             this.read = false;
  331.  
  332.         }
  333.  
  334.         public csvParser(string path) {
  335.  
  336.             this.path = path;
  337.             this._handle = new List<string>();
  338.             this._title = new List<string>();
  339.             this._body = new List<string>();
  340.             this._vendor = new List<string>();
  341.             this._type = new List<string>();
  342.             this._tags = new List<string>();
  343.             this._mehrwertSteuer = new List<string>();
  344.             this._status = new List<string>();
  345.             this._seoTitel = new List<string>();
  346.             this._seoBeschreibung = new List<string>();
  347.             this._versandGewicht = new List<string>();
  348.             this._nameOption1 = new List<string>();
  349.             this._wertOption1 = new List<string>();
  350.             this._nameOption2 = new List<string>();
  351.             this._wertOption2 = new List<string>();
  352.             this._nameOption3 = new List<string>();
  353.             this._wertOption3 = new List<string>();
  354.             this._sku = new List<string>();
  355.             this._preis = new List<string>();
  356.             this._einkaufsPreis = new List<string>();
  357.             this._barcode = new List<string>();
  358.             this._aufLager = new List<string>();
  359.             this._stringiantenId = new List<string>();
  360.             this._productId = new List<string>();
  361.             this._products = new List<Product>();
  362.             this.read = false;
  363.  
  364.         }
  365.  
  366.         ~csvParser() {
  367.  
  368.  
  369.  
  370.         }
  371.  
  372.     }
  373.  
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement