Exemple #1
0
 /// <summary>
 /// Create a data source object.
 /// </summary>
 /// <param name="xmlReader">The XML reader.</param>
 /// <param name="settings">The reader settings.</param>
 /// <returns>A data source.</returns>
 private IDataSource CreateDataSource(XmlReader xmlReader, EntityXmlImportSettings settings)
 {
     return(new XmlPackageSource
     {
         XmlReader = xmlReader,
         Version = Format.XmlVer2
     });
 }
Exemple #2
0
 /// <summary>
 /// Interface for providing XML import.
 /// </summary>
 /// <param name="xml">ID of entity to export.</param>
 /// <param name="settings">Export settings.</param>
 public EntityXmlImportResult ImportXml(string xml, EntityXmlImportSettings settings)
 {
     using (StringReader stringReader = new StringReader(xml))
         using (XmlReader xmlReader = XmlReader.Create(stringReader))
         {
             return(ImportXml(xmlReader, settings));
         }
 }
Exemple #3
0
        /// <summary>
        /// Interface for providing XML import.
        /// </summary>
        /// <param name="xmlReader">Xml Writer to write the exported entity to.</param>
        /// <param name="settings">Export settings.</param>
        public EntityXmlImportResult ImportXml(XmlReader xmlReader, EntityXmlImportSettings settings)
        {
            if (xmlReader == null)
            {
                throw new ArgumentNullException(nameof(xmlReader));
            }
            if (settings == null)
            {
                settings = EntityXmlImportSettings.Default;
            }

            Settings = settings;
            var context = new ProcessingContext( );

            context.Report.StartTime = DateTime.Now;

            long tenantId = RequestContext.TenantId;
            IEnumerable <Guid> rootGuids;

            /////
            // Create source to load app data from tenant
            /////
            try
            {
                using (IDataSource source = CreateDataSource(xmlReader, settings))
                {
                    CheckImportSecurity(source, context);

                    rootGuids = ImportEntity(tenantId, source, settings, context);
                }
            }
            catch (ImportDependencyException)
            {
                return(new EntityXmlImportResult(new long[] { })
                {
                    ErrorMessage = "The import failed because some dependencies could not be found."
                });
            }

            context.Report.EndTime = DateTime.Now;

            // Prepare result
            List <long> rootIds = rootGuids.Select(Entity.GetIdFromUpgradeId).ToList( );

            return(new EntityXmlImportResult(rootIds));
        }
        public HttpResponseMessage <ImportResult> ImportXmlGetData([FromUri] string fileId, [FromUri] string fileName = null, [FromUri] bool ignoreDeps = false)
        {
            IEntityXmlImporter importer = Factory.EntityXmlImporter;

            EntityXmlImportResult serviceResult;

            // Decode settings
            var settings = new EntityXmlImportSettings
            {
                IgnoreMissingDependencies = ignoreDeps
            };

            // Import
            using (Stream stream = FileRepositoryHelper.GetTemporaryFileDataStream(fileId))
                using (var xmlReader = XmlReader.Create(stream))
                {
                    serviceResult = importer.ImportXml(xmlReader, settings);
                }

            // Format result
            ImportResult webResult;

            if (serviceResult.ErrorMessage != null)
            {
                webResult = new ImportResult
                {
                    Success = false,
                    Message = serviceResult.ErrorMessage
                };
            }
            else
            {
                IEntityRepository entityRepository = Factory.EntityRepository;
                var entities = entityRepository.Get <Resource>(serviceResult.RootEntities);

                webResult = new ImportResult
                {
                    Success  = true,
                    Entities = entities.Select(FormatEntity).ToList( )
                };
            }

            return(new HttpResponseMessage <ImportResult>(webResult));
        }
        /// <summary>
        ///     Imports the entity
        /// </summary>
        /// <param name="tenantName">Name of the tenant.</param>
        /// <param name="packagePath">The package path.</param>
        /// <param name="ignoreMissingDeps"></param>
        /// <param name="context">The context.</param>
        public static void ImportEntity(string tenantName, string packagePath, bool ignoreMissingDeps, IProcessingContext context = null)
        {
            if (string.IsNullOrEmpty(tenantName))
            {
                throw new ArgumentNullException(nameof(tenantName));
            }
            if (string.IsNullOrEmpty(packagePath))
            {
                throw new ArgumentNullException(nameof(packagePath));
            }

            if (context == null)
            {
                context = new ProcessingContext();
            }

            context.Report.StartTime = DateTime.Now;

            long tenantId = TenantHelper.GetTenantId(tenantName, true);

            /////
            // Create source to load app data from tenant
            /////
            using (IDataSource importSource = FileManager.CreateDataSource(packagePath))
            {
                EntityXmlImportSettings settings = new EntityXmlImportSettings
                {
                    IgnoreMissingDependencies = ignoreMissingDeps
                };
                EntityXmlImporter importer = ( EntityXmlImporter )Factory.EntityXmlImporter;
                try
                {
                    importer.ImportEntity(tenantId, importSource, settings, context);
                }
                catch (EntityXmlImporter.ImportDependencyException)
                {
                    context.WriteError("Import failed due to missing dependencies. Use -ignoreMissingDeps True to ignore.");
                }
            }

            context.Report.EndTime = DateTime.Now;
        }
Exemple #6
0
        /// <summary>
        /// Check the context for missing dependencies, and if so report them.
        /// Exception will roll back the transaction.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="settings"></param>
        private void CheckForMissingDependencies(IProcessingContext context, EntityXmlImportSettings settings)
        {
            if (settings?.IgnoreMissingDependencies == true)
            {
                return;
            }
            if (context.Report.MissingDependencies.Count == 0)
            {
                return;
            }

            StringBuilder sb = new StringBuilder( );

            sb.AppendLine("The import failed because there are missing dependencies.");
            foreach (MissingDependency <object> dep in context.Report.MissingDependencies)
            {
                sb.AppendLine(dep.GetLogMessage( ));
            }
            EventLog.Application.WriteWarning(sb.ToString( ));
            throw new ImportDependencyException( );
        }
Exemple #7
0
        /// <summary>
        ///     Imports the entity
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="importSource"></param>
        /// <param name="settings"></param>
        /// <param name="context"></param>
        internal IEnumerable <Guid> ImportEntity(long tenantId, IDataSource importSource, EntityXmlImportSettings settings, IProcessingContext context)
        {
            IList <Guid> rootGuids = GetRootGuidsFromMetadata(importSource, context);

            using (IDataSource baseline = GetBaselineSourceForImport(tenantId, rootGuids))
                using (TenantMergeTarget target = new TenantMergeTarget
                {
                    TenantId = tenantId,
                    IgnoreExternalReferences = true
                })
                {
                    /////
                    // Copy the data
                    /////
                    using (var processor = new MergeProcessor(context)
                    {
                        OldVersion = baseline,
                        NewVersion = importSource,
                        Target = target
                    })
                    {
                        processor.MergeData( );

                        CheckForMissingDependencies(context, settings);

                        target.Commit( );
                    }
                }

            CacheManager.ClearCaches(tenantId);

            return(rootGuids);
        }