Esempio n. 1
0
        public ImportRowSource BuildRowSource()
        {
            var rowCount = 0;

            var extra = new Dictionary<int, Dictionary<String, List<String>>>();

            // first define the columns...
            var columnBuilder = new ColumnDefinitionBuilder();
            using (ZipFile zipfile = ZipFile.Read(Filename)) {
                foreach (ZipEntry entry in zipfile) {
                    if (entry.FileName.Equals("tasks.csv", StringComparison.OrdinalIgnoreCase)) {
                        GetTaskColumns(entry, columnBuilder);
                    } else if (entry.FileName.Equals("recordedBy.csv", StringComparison.CurrentCultureIgnoreCase)) {
                        AddToExtraData(entry, "recordedBy", extra);
                        var valueExtractor = new ANICCollectorNameFormattingValueExtractor();
                        columnBuilder.ColumnDefinitions.Add(new BVPImportColumnDefinition { OutputColumnName = "Collector(s)", SourceColumnName = "recordedBy", SourceFilename = entry.FileName, ValueExtractor = valueExtractor });
                    } else if (entry.FileName.Equals("associatedMedia.csv", StringComparison.CurrentCultureIgnoreCase)) {
                        AddToExtraData(entry, "associatedMedia", extra);
                        columnBuilder.ColumnDefinitions.Add(new BVPImportColumnDefinition { OutputColumnName = "associatedMedia", SourceColumnName = "associatedMedia", SourceFilename = entry.FileName, ValueExtractor = new AssociatedMediaValueExtractor() });
                    }
                }
            }
            // Now we have all the columns, we can create the staging table
            Service.BeginTransaction();
            Service.CreateImportTable(columnBuilder.ColumnNames);

            // And get the values...
            using (ZipFile zipfile = ZipFile.Read(Filename)) {
                foreach (ZipEntry entry in zipfile) {
                    if (entry.FileName.Equals("tasks.csv")) {
                        IterateOverCSVZipEntry(entry, (parser) => {
                            var taskId = Int32.Parse(parser["taskID"]);
                            Dictionary<String, List<String>> fieldMap = null;
                            if (extra.ContainsKey(taskId)) {
                                fieldMap = extra[taskId];
                            }
                            var values = new List<String>();
                            columnBuilder.ColumnDefinitions.ForEach((coldef) => {
                                var value = coldef.ValueExtractor.ExtractValue(coldef, parser, fieldMap);
                                values.Add(value);
                            });
                            Service.InsertImportRow(values);
                            rowCount++;
                            return true;
                        });
                    }
                }
            }
            Service.CommitTransaction();
            return new ImportRowSource(Service, rowCount);
        }
Esempio n. 2
0
        public ImportRowSource BuildRowSource()
        {
            var rowCount = 0;

            var extra = new Dictionary <int, Dictionary <String, List <String> > >();

            // first define the columns...
            var columnBuilder = new ColumnDefinitionBuilder();

            using (ZipFile zipfile = ZipFile.Read(Filename)) {
                foreach (ZipEntry entry in zipfile)
                {
                    if (entry.FileName.Equals("tasks.csv", StringComparison.OrdinalIgnoreCase))
                    {
                        GetTaskColumns(entry, columnBuilder);
                    }
                    else if (entry.FileName.Equals("recordedBy.csv", StringComparison.CurrentCultureIgnoreCase))
                    {
                        AddToExtraData(entry, "recordedBy", extra);
                        var valueExtractor = new ANICCollectorNameFormattingValueExtractor();
                        columnBuilder.ColumnDefinitions.Add(new BVPImportColumnDefinition {
                            OutputColumnName = "Collector(s)", SourceColumnName = "recordedBy", SourceFilename = entry.FileName, ValueExtractor = valueExtractor
                        });
                    }
                    else if (entry.FileName.Equals("associatedMedia.csv", StringComparison.CurrentCultureIgnoreCase))
                    {
                        AddToExtraData(entry, "associatedMedia", extra);
                        columnBuilder.ColumnDefinitions.Add(new BVPImportColumnDefinition {
                            OutputColumnName = "associatedMedia", SourceColumnName = "associatedMedia", SourceFilename = entry.FileName, ValueExtractor = new AssociatedMediaValueExtractor()
                        });
                    }
                }
            }
            // Now we have all the columns, we can create the staging table
            Service.BeginTransaction();
            Service.CreateImportTable(columnBuilder.ColumnNames);

            // And get the values...
            using (ZipFile zipfile = ZipFile.Read(Filename)) {
                foreach (ZipEntry entry in zipfile)
                {
                    if (entry.FileName.Equals("tasks.csv"))
                    {
                        IterateOverCSVZipEntry(entry, (parser) => {
                            var taskId = Int32.Parse(parser["taskID"]);
                            Dictionary <String, List <String> > fieldMap = null;
                            if (extra.ContainsKey(taskId))
                            {
                                fieldMap = extra[taskId];
                            }
                            var values = new List <String>();
                            columnBuilder.ColumnDefinitions.ForEach((coldef) => {
                                var value = coldef.ValueExtractor.ExtractValue(coldef, parser, fieldMap);
                                values.Add(value);
                            });
                            Service.InsertImportRow(values);
                            rowCount++;
                            return(true);
                        });
                    }
                }
            }
            Service.CommitTransaction();
            return(new ImportRowSource(Service, rowCount));
        }