Пример #1
0
        private List <Tuple <string, int, int?> > ProcessCsv(string filepath, char delimiter = ',', char quoteCharacter = '\"', char quoteEscapeCharacter = '\"')
        {
            List <Tuple <string, int, int?> > result = new List <Tuple <string, int, int?> >();

            using (var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(filepath))
            {
                parser.SetDelimiter(delimiter);
                parser.SetQuoteCharacter(quoteCharacter);
                parser.SetQuoteEscapeCharacter(quoteEscapeCharacter);

                while (!parser.EndOfData)
                {
                    var line = parser.ReadFields();

                    int startingPage = 0;
                    if (Int32.TryParse(line[1], out startingPage))
                    {
                        int  endingPage      = 0;
                        bool endingPageGiven = (line.Length > 2) && Int32.TryParse(line[2], out endingPage);
                        result.Add(new Tuple <string, int, int?>(line[0], startingPage, endingPageGiven ? (int?)endingPage : null));
                    }
                    else
                    {
                        Properties.Settings.Default.Log += $"The Song \"{line[0]}\" has no page number ({line[1]}, {line[2]})." + System.Environment.NewLine;
                    }
                }
            }
            Properties.Settings.Default.Save();
            return(result);
        }
Пример #2
0
        public static void CreateWithConfigurationOptions(TextReader csvReader)
        {
            var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(csvReader);

            parser.SetDelimiter('|');
            parser.Delimiters = new[] { "|" };
            parser.SetQuoteCharacter('\'');
            parser.SetQuoteEscapeCharacter('\\');
            parser.HasFieldsEnclosedInQuotes = false;
            parser.TrimWhiteSpace            = true;
        }
Пример #3
0
 public Waypoint(string wptText)
 {
     if (wptText.Length > 0)
     {
         using (var strm = wptText.ToStream())
             using (var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(strm))
             {
                 parser.SetDelimiter(' ');
                 var csvLine = parser.ReadFields();
                 X         = double.Parse((csvLine[1].Trim('(', ')')));
                 Y         = double.Parse((csvLine[2].Trim('(', ')')));
                 Name      = csvLine[3];
                 TimeStamp = DateTime.Parse(csvLine[4]);
             }
     }
 }
Пример #4
0
        public override async Task <string> Run(string arg, CommercePipelineExecutionContext context)
        {
            var importPolicy = context.GetPolicy <ImportSellableItemsPolicy>();
            var sellableItemComparerByProductId  = new ImportSellableItemComparer(SellableItemComparerConfiguration.ByProductId);
            var sellableItemComparerByImportData = new ImportSellableItemComparer(SellableItemComparerConfiguration.ByImportData);

            LogInitialization(context, importPolicy);

            try
            {
                var filePath = CommerceCommander.Command <GetFileCommand>().Process(context.CommerceContext, importPolicy.FileFolderPath, importPolicy.FilePrefix, importPolicy.FileExtention);
                if (string.IsNullOrEmpty(filePath))
                {
                    context.Logger.LogInformation($"{Name} - Skipping execution as there are no files to process.");
                    return(null);
                }

                using (var reader = new StreamReader(filePath))
                {
                    using (var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(reader))
                    {
                        parser.SetDelimiter(importPolicy.FileGroupSeparator);
                        // skip header
                        if (!parser.EndOfData)
                        {
                            parser.ReadFields();
                        }

                        while (!parser.EndOfData)
                        {
                            var importRawLines = new List <string[]>();
                            for (int i = 0; !parser.EndOfData && i <= importPolicy.ItemsPerBatch - 1; i++)
                            {
                                importRawLines.Add(parser.ReadFields());
                            }

                            var importItems = await CommerceCommander.Command <TransformImportToSellableItemsCommand>().Process(context.CommerceContext, importRawLines);

                            var existingItems = await CommerceCommander.Command <GetSellableItemsBulkCommand>().Process(context.CommerceContext, importItems);

                            var newItems     = importItems.Except(existingItems, sellableItemComparerByProductId).ToList();
                            var changedItems = existingItems.Except(importItems, sellableItemComparerByImportData).ToList();

                            await CommerceCommander.Command <CopyImportToSellableItemsCommand>().Process(context.CommerceContext, importItems, changedItems);

                            var associationsToCreate = importItems.SelectMany(i => i.GetPolicy <TransientImportSellableItemDataPolicy>().ParentAssociationsToCreateList).ToList();
                            var associationsToRemove = importItems.SelectMany(i => i.GetPolicy <TransientImportSellableItemDataPolicy>().ParentAssociationsToRemoveList).ToList();

                            RemoveTransientData(importItems);

                            await CommerceCommander.Command <PersistEntityBulkCommand>().Process(context.CommerceContext, newItems.Union(changedItems));

                            await CommerceCommander.Command <AssociateToParentBulkCommand>().Process(context.CommerceContext, associationsToCreate);

                            // TODO: Need to test the disassociate, haven't had time yet
                            await CommerceCommander.Command <DisassociateToParentBulkCommand>().Process(context.CommerceContext, associationsToRemove);

                            await Task.Delay(importPolicy.SleepBetweenBatches);
                        }
                    }
                }

                CommerceCommander.Command <MoveFileCommand>().Process(context.CommerceContext, importPolicy.FileArchiveFolderPath, filePath);
            }
            catch (Exception ex)
            {
                context.Abort(await context.CommerceContext.AddMessage(
                                  context.GetPolicy <KnownResultCodes>().Error,
                                  Name,
                                  new object[1] {
                    ex
                },
                                  $"{Name} Import Exception: {ex.Message}"),
                              context);
            }

            return(null);
        }