public void CsvUtilityReadCsvTest()
        {
            Assert.Inconclusive("Need to set the file TestCsv.csv Copy To Output Directory which breaks VSIX build");

            CsvUtility.ConstructTextSchema(null, "TestCsv.csv");
            var rows = CsvUtility.SelectAllRows("TestCsv.csv");

            Assert.IsTrue(rows.Count() == 3);
            var textString = rows.Last().GetFieldAsString("TextStringField");

            Assert.IsTrue(textString.Length >= 300);
            var phoneString = rows.Last().GetFieldAsString("PhoneNumber");

            Assert.IsTrue(phoneString.StartsWith("0"));
        }
Exemple #2
0
        private void ImportCsvs(ImportCsvsRequest request, LogController controller, ImportCsvsResponse response)
        {
            var organisationSettings = new OrganisationSettings(XrmService);

            controller.LogLiteral("Preparing Import");
            var csvFiles = request.FolderOrFiles == ImportCsvsRequest.CsvImportOption.Folder
                ? FileUtility.GetFiles(request.Folder.FolderPath).Where(f => f.EndsWith(".csv"))
                : request.CsvsToImport.Select(c => c.Csv.FileName).ToArray();

            var entities      = new List <Entity>();
            var countToImport = csvFiles.Count();
            var countImported = 0;

            //this part maps the csvs into entity clr objects
            //then will just call the shared import method
            foreach (var csvFile in csvFiles)
            {
                try
                {
                    //try think if better way
                    controller.UpdateProgress(countImported++, countToImport, string.Format("Reading {0}", csvFile));
                    var getTypeResponse = GetTargetType(XrmService, csvFile);
                    var type            = getTypeResponse.LogicalName;
                    var primaryField    = XrmService.GetPrimaryNameField(type);
                    var fileInfo        = new FileInfo(csvFile);
                    CsvUtility.ConstructTextSchema(fileInfo.Directory.FullName, Path.GetFileName(csvFile));
                    var rows      = CsvUtility.SelectAllRows(csvFile);
                    var rowNumber = 0;
                    foreach (var row in rows)
                    {
                        rowNumber++;
                        try
                        {
                            var entity     = new Entity(type);
                            var keyColumns =
                                rows.First()
                                .GetColumnNames()
                                .Where(c => c.StartsWith("key|"));

                            if (keyColumns.Any())
                            {
                                var fieldValues = new Dictionary <string, object>();
                                foreach (var key in keyColumns)
                                {
                                    var fieldName   = MapColumnToFieldSchemaName(XrmService, type, key);
                                    var columnValue = row.GetFieldAsString(key);
                                    if (columnValue != null)
                                    {
                                        columnValue = columnValue.Trim();
                                    }

                                    fieldValues.Add(fieldName, columnValue);
                                }
                                var matchingEntity = GetMatchingEntities(type, fieldValues);
                                if (matchingEntity.Count() > 1)
                                {
                                    throw new Exception(string.Format("Specified Match By Name But More Than One {0} Record Matched To The Keys Of {1}", type, string.Join(",", fieldValues.Select(kv => kv.Key + "=" + kv.Value))));
                                }
                                if (matchingEntity.Count() == 1)
                                {
                                    entity.Id = matchingEntity.First().Id;
                                }
                            }
                            else if (request.MatchByName && !getTypeResponse.IsRelationship)
                            {
                                var primaryFieldColumns =
                                    rows.First()
                                    .GetColumnNames()
                                    .Where(c => MapColumnToFieldSchemaName(XrmService, type, c) == primaryField).ToArray();
                                var primaryFieldColumn = primaryFieldColumns.Any() ? primaryFieldColumns.First() : null;
                                if (request.MatchByName && primaryFieldColumn.IsNullOrWhiteSpace())
                                {
                                    throw new NullReferenceException(string.Format("Match By Name Was Specified But No Column In The CSV Matched To The Primary Field {0} ({1})", XrmService.GetFieldLabel(primaryField, type), primaryField));
                                }

                                var columnValue = row.GetFieldAsString(primaryFieldColumn);
                                if (columnValue != null)
                                {
                                    columnValue = columnValue.Trim();
                                }
                                var matchingEntity = GetMatchingEntities(type, primaryField, columnValue);
                                if (matchingEntity.Count() > 1)
                                {
                                    throw new Exception(string.Format("Specified Match By Name But More Than One {0} Record Matched To Name Of {1}", type, columnValue));
                                }
                                if (matchingEntity.Count() == 1)
                                {
                                    entity.Id = matchingEntity.First().Id;
                                }
                            }
                            foreach (var column in row.GetColumnNames())
                            {
                                var field = MapColumnToFieldSchemaName(XrmService, type, column);
                                if (true)//(getTypeResponse.IsRelationship || XrmService.IsWritable(field, type))
                                {
                                    var stringValue = row.GetFieldAsString(column);
                                    if (stringValue != null)
                                    {
                                        stringValue = stringValue.Trim();
                                    }
                                    if (getTypeResponse.IsRelationship)
                                    {
                                        //bit of hack
                                        //for csv relationships just set to a string and map it later
                                        //as the referenced record may not be created yet
                                        entity.SetField(field, stringValue);
                                    }
                                    else if (XrmService.IsLookup(field, type))
                                    {
                                        //for lookups am going to set to a empty guid and allow the import part to replace with a correct guid
                                        if (!stringValue.IsNullOrWhiteSpace())
                                        {
                                            entity.SetField(field,
                                                            new EntityReference(XrmService.GetLookupTargetEntity(field, type),
                                                                                Guid.Empty)
                                            {
                                                Name = stringValue
                                            });
                                        }
                                    }
                                    else
                                    {
                                        entity.SetField(field, XrmService.ParseField(field, type, stringValue, request.DateFormat == DateFormat.American));
                                    }
                                }
                            }
                            if (XrmService.FieldExists("transactioncurrencyid", type) &&
                                !entity.GetLookupGuid("transactioncurrencyid").HasValue)
                            {
                                entity.SetLookupField("transactioncurrencyid", organisationSettings.BaseCurrencyId,
                                                      "transactioncurrency");
                            }
                            entities.Add(entity);
                        }
                        catch (Exception ex)
                        {
                            response.AddResponseItem(new ImportCsvsResponseItem(string.Format("Error Parsing Row {0} To Entity", rowNumber), csvFile, ex));
                        }
                    }
                }
                catch (Exception ex)
                {
                    response.AddResponseItem(new ImportCsvsResponseItem("Not Imported", csvFile, ex));
                }
            }
            var imports = DoImport(entities, controller, request.MaskEmails);

            foreach (var item in imports)
            {
                response.AddResponseItem(new ImportCsvsResponseItem(item));
            }
        }
        public CsvRecordService(FileReference fileReference)
        {
            FileReference = fileReference;

            CsvUtility.ConstructTextSchema(CsvFolder, CsvNamePart);
        }