Esempio n. 1
0
        public void CsvUtilityReadCsvTest()
        {
            var rows = CsvUtility.SelectAllRows("TestCsv.csv");

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

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

            Assert.IsTrue(phoneString.StartsWith("0"));
        }
        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"));
        }
        public void DeleteProductData()
        {
            var products = CsvUtility
                           .SelectAllRows(Path.Combine(WorkFolder, @"Products.csv"))
                           .Select(r => r.GetFieldAsString("Name"))
                           .ToArray();

            DeleteAllMatchingName(Entities.product, products);
            var unitGroups = CsvUtility
                             .SelectAllRows(Path.Combine(WorkFolder, @"uomschedule.csv"))
                             .Select(r => r.GetFieldAsString("Name"))
                             .ToArray();

            DeleteAllMatchingName(Entities.uomschedule, unitGroups);
            var priceLists = CsvUtility
                             .SelectAllRows(Path.Combine(WorkFolder, @"Price Lists.csv"))
                             .Select(r => r.GetFieldAsString("Name"))
                             .ToArray();

            DeleteAllMatchingName(Entities.pricelevel, priceLists);
        }
Esempio n. 4
0
        public void DeploymentImportCsvsTestProductsAndPricings()
        {
            //imports product configurations in csv files
            PrepareTests();
            var workFolder = ClearFilesAndData();

            File.Copy(@"Price List Items.csv", Path.Combine(workFolder, @"Price List Items.csv"));
            File.Copy(@"Price Lists.csv", Path.Combine(workFolder, @"Price Lists.csv"));
            File.Copy(@"Products.csv", Path.Combine(workFolder, @"Products.csv"));
            File.Copy(@"uom.csv", Path.Combine(workFolder, @"uom.csv"));
            File.Copy(@"uomschedule.csv", Path.Combine(workFolder, @"uomschedule.csv"));

            //lets delete all these items
            var products = CsvUtility
                           .SelectAllRows(Path.Combine(workFolder, @"Products.csv"))
                           .Select(r => r.GetFieldAsString("Name"))
                           .ToArray();

            DeleteAllMatchingName(Entities.product, products);
            var unitGroups = CsvUtility
                             .SelectAllRows(Path.Combine(workFolder, @"uomschedule.csv"))
                             .Select(r => r.GetFieldAsString("Name"))
                             .ToArray();

            DeleteAllMatchingName(Entities.uomschedule, unitGroups);
            var priceLists = CsvUtility
                             .SelectAllRows(Path.Combine(workFolder, @"Price Lists.csv"))
                             .Select(r => r.GetFieldAsString("Name"))
                             .ToArray();

            DeleteAllMatchingName(Entities.pricelevel, priceLists);

            //run the import and verify no errors
            var application = CreateAndLoadTestApplication <ImportCsvsModule>();
            var request     = new ImportCsvsRequest
            {
                CsvsToImport = new[]
                {
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"Price List Items.csv"))
                    },
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"Price Lists.csv"))
                    },
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"Products.csv"))
                    },
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"uom.csv"))
                    },
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"uomschedule.csv"))
                    },
                },
                DateFormat = DateFormat.American
            };
            var response = application.NavigateAndProcessDialog <ImportCsvsModule, ImportCsvsDialog, ImportCsvsResponse>(request);

            Assert.IsFalse(response.HasError);

            //okay lets get the last created price list item
            var query = XrmService.BuildQuery(Entities.productpricelevel, null, null, null);

            query.Orders.Add(new OrderExpression(Fields.productpricelevel_.createdon, OrderType.Descending));
            var latestPriceListItem = XrmService.RetrieveFirst(query);
            //verify it has a price
            var initialPrice = latestPriceListItem.GetMoneyValue(Fields.productpricelevel_.amount);

            Assert.IsTrue(initialPrice > 0);
            //now lets set it something else so we can verify it gets updated after the second run
            latestPriceListItem.SetMoneyField(Fields.productpricelevel_.amount, initialPrice.Value + 1);
            latestPriceListItem = UpdateFieldsAndRetreive(latestPriceListItem, Fields.productpricelevel_.amount);

            //run again and verify no errors
            request = new ImportCsvsRequest
            {
                CsvsToImport = new[]
                {
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"Price List Items.csv"))
                    },
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"Price Lists.csv"))
                    },
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"Products.csv"))
                    },
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"uom.csv"))
                    },
                    new ImportCsvsRequest.CsvToImport {
                        SourceCsv = new FileReference(Path.Combine(workFolder, @"uomschedule.csv"))
                    },
                },
                DateFormat = DateFormat.American
            };
            response = application.NavigateAndProcessDialog <ImportCsvsModule, ImportCsvsDialog, ImportCsvsResponse>(request);
            Assert.IsFalse(response.HasError);
            //verify the price list item we updated is changed
            latestPriceListItem = Refresh(latestPriceListItem);
            Assert.AreEqual(initialPrice, latestPriceListItem.GetMoneyValue(Fields.productpricelevel_.amount));
        }
Esempio n. 5
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));
            }
        }