示例#1
0
        public ImportResults Import()
        {
            ReportInformation(String.Format("Starting import from file \"{0}\".", this.InputPath));
            ReportInformation(String.Format("Connecting to CRM server at {0}", this._factory.ServerUrl));

            _crmService = _factory.GetCrmService();

            ImportResults results = new ImportResults();

            ImportData      importData = DeserializeRecords();
            MissingMetadata missingMetadata;

            if (IsMetadataMissing(importData, out missingMetadata))
            {
                results.Success         = false;
                results.MissingMetadata = missingMetadata;
                results.ErrorMessage    = String.Format("CRM server is missing required metadata.");
            }
            else
            {
                try
                {
                    ImportData invalidReferenceData = RemoveInvalidReferences(importData);

                    _currentRecordCount = 0;
                    _entityRecordCounts = importData.Entities.GroupBy(de => de.Name).ToDictionary(g => g.Key, g => 0);

                    _totalRecordCount = importData.Entities.Count;

                    if (invalidReferenceData != null)
                    {
                        _totalRecordCount += invalidReferenceData.Entities.Count;
                    }

                    ImportRecords(importData, "Importing");

                    if (invalidReferenceData != null)
                    {
                        ImportData unresolvableData = RemoveInvalidReferences(invalidReferenceData);
                        if (unresolvableData != null)
                        {
                            IEnumerable <DynamicEntity> unresolvableEntities =
                                unresolvableData.Entities
                                .OrderBy(e => e.Name);

                            foreach (DynamicEntity unresolvableEntity in unresolvableEntities)
                            {
                                ReportError(String.Format("Could not resolve references on {0} with id of {1}.",
                                                          unresolvableEntity.Name,
                                                          unresolvableEntity.Properties.OfType <KeyProperty>().First().Value.Value));

                                // still count these records towards our progress
                                _currentRecordCount++;
                            }
                        }

                        ImportRecords(invalidReferenceData, "Resolving references on");
                    }
                    results.EntityResults = _entityRecordCounts.Select(pair => new EntityResult(pair.Key, pair.Value));
                    results.Success       = true;
                }
                catch (SoapException ex)
                {
                    results.Success      = false;
                    results.ErrorMessage = String.Format(ex.Detail.InnerText);
                    ReportError(ex.ToString());
                }
                catch (Exception ex)
                {
                    results.Success      = false;
                    results.ErrorMessage = ex.Message;
                    ReportError(ex.ToString());
                }
            }

            return(results);
        }