示例#1
0
        public void Import(TextReader reader)
        {
            var csv = new CsvReader(reader);

            mapping.Apply(csv.Configuration);

            var records = csv.GetRecords <Record>();

            int n        = 1;
            var keywords = new List <MetadataKeyword>();

            try
            {
                foreach (var record in records)
                {
                    if (String.IsNullOrWhiteSpace(record.Id))
                    {
                        record.Id = Helpers.AddCollection(Guid.NewGuid().ToString());
                    }
                    var result = recordService.Insert(record, userInfo);

                    if (!result.Success)
                    {
                        if (!SkipBadRecords)
                        {
                            throw new ImportException(String.Format("Import failed due to validation errors at record {0}: {1}",
                                                                    n, result.Validation.Errors.ToConcatenatedString(e => e.Message, "; ")));
                        }
                    }

                    n++;
                    Results.Add(result);

                    keywords.AddRange(result.Record.Gemini.Keywords);
                }
            }
            catch (CsvHelperException ex)
            {
                string info = (string)ex.Data["CsvHelper"];
                throw new ImportException("CsvHelper exception: " + info, ex);
            }

            // import new vocabs and keywords
            foreach (var vocab in mapping.RequiredVocabularies)
            {
                // if vocab already exists, Insert returns a VocabularyServiceResult with Success=false, which we just ignore
                vocabularyService.Insert(vocab);
            }
            vocabularyService.AddKeywordsToExistingControlledVocabs(keywords);
        }