private void ProcessImportModulePackages(ImportDto importDto)
        {
            var packageZipFile = $"{Globals.ApplicationMapPath}{Constants.ExportFolder}{this.exportImportJob.Directory.TrimEnd('\\', '/')}\\{Constants.ExportZipPackages}";
            var tempFolder     = $"{Path.GetDirectoryName(packageZipFile)}\\{DateTime.Now.Ticks}";

            if (File.Exists(packageZipFile))
            {
                CompressionUtil.UnZipArchive(packageZipFile, tempFolder);
                var exportPackages = this.Repository.GetAllItems <ExportPackage>().ToList();

                this.CheckPoint.TotalItems = this.CheckPoint.TotalItems <= 0 ? exportPackages.Count : this.CheckPoint.TotalItems;
                if (this.CheckPointStageCallback(this))
                {
                    return;
                }

                if (this.CheckPoint.Stage == 0)
                {
                    try
                    {
                        foreach (var exportPackage in exportPackages)
                        {
                            this.ProcessImportModulePackage(exportPackage, tempFolder, importDto.CollisionResolution);

                            this.CheckPoint.ProcessedItems++;
                            this.CheckPoint.Progress = this.CheckPoint.ProcessedItems * 100.0 / exportPackages.Count;
                            if (this.CheckPointStageCallback(this))
                            {
                                break;
                            }
                        }

                        this.CheckPoint.Stage++;
                        this.CheckPoint.Completed = true;
                    }
                    finally
                    {
                        this.CheckPointStageCallback(this);
                        try
                        {
                            FileSystemUtils.DeleteFolderRecursive(tempFolder);
                        }
                        catch (Exception)
                        {
                            // ignore
                        }
                    }
                }
            }
            else
            {
                this.CheckPoint.Completed = true;
                this.CheckPointStageCallback(this);
                this.Result.AddLogEntry("PackagesFileNotFound", "Packages file not found. Skipping packages import", ReportLevel.Warn);
            }
        }
示例#2
0
        public override void ImportData(ExportImportJob importJob, ImportDto importDto)
        {
            if (CheckCancelled(importJob))
            {
                return;
            }
            //Stage 1: Portals files unzipped.
            //Stage 2: All folders and files imported.
            //Stage 3: Synchronization completed.
            //Skip the export if all the folders have been processed already.
            if (CheckPoint.Stage >= 2 || CheckPoint.Completed)
            {
                return;
            }

            var totalFolderImported            = 0;
            var totalFolderPermissionsImported = 0;
            var totalFilesImported             = 0;
            var skip           = GetCurrentSkip();
            var currentIndex   = skip;
            var portalId       = importJob.PortalId;
            var portal         = PortalController.Instance.GetPortal(portalId);
            var assetsFile     = string.Format(_assetsFolder, importJob.Directory.TrimEnd('\\').TrimEnd('/'));
            var userFolderPath = string.Format(UsersAssetsTempFolder, portal.HomeDirectoryMapPath.TrimEnd('\\'));

            if (CheckPoint.Stage == 0)
            {
                if (!File.Exists(assetsFile))
                {
                    Result.AddLogEntry("AssetsFileNotFound", "Assets file not found. Skipping assets import",
                                       ReportLevel.Warn);
                    CheckPoint.Completed = true;
                    CheckPointStageCallback(this);
                }
                else
                {
                    CompressionUtil.UnZipArchive(assetsFile, portal.HomeDirectoryMapPath,
                                                 importDto.CollisionResolution == CollisionResolution.Overwrite);
                    //Stage 1: Once unzipping of portal files is completed.
                    CheckPoint.Stage++;
                    CheckPoint.StageData = null;
                    CheckPoint.Progress  = 10;
                    if (CheckPointStageCallback(this))
                    {
                        return;
                    }
                }
            }

            if (CheckPoint.Stage == 1)
            {
                try
                {
                    //Stage 2 starts
                    var sourceFolders = Repository.GetAllItems <ExportFolder>(x => x.CreatedOnDate, true, skip).ToList();

                    var totalFolders = sourceFolders.Any() ? sourceFolders.Count : 0;
                    //Update the total items count in the check points. This should be updated only once.
                    CheckPoint.TotalItems = CheckPoint.TotalItems <= 0 ? totalFolders : CheckPoint.TotalItems;
                    if (CheckPointStageCallback(this))
                    {
                        return;
                    }

                    foreach (var sourceFolder in sourceFolders)
                    {
                        if (CheckCancelled(importJob))
                        {
                            break;
                        }
                        // PROCESS FOLDERS
                        //Create new or update existing folder
                        if (ProcessFolder(importJob, importDto, sourceFolder))
                        {
                            totalFolderImported++;

                            //Include permissions only if permissions were exported in package.
                            if (importDto.ExportDto.IncludePermissions)
                            {
                                // PROCESS FOLDER PERMISSIONS
                                var sourceFolderPermissions =
                                    Repository.GetRelatedItems <ExportFolderPermission>(sourceFolder.Id).ToList();
                                //Replace folderId for each permission with new one.
                                sourceFolderPermissions.ForEach(x =>
                                {
                                    x.FolderId   = Convert.ToInt32(sourceFolder.FolderId);
                                    x.FolderPath = sourceFolder.FolderPath;
                                });

                                // PROCESS FOLDER PERMISSIONS
                                //File local files in the system related to the folder path.
                                var localPermissions =
                                    CBO.FillCollection <ExportFolderPermission>(DataProvider.Instance()
                                                                                .GetFolderPermissionsByPath(portalId, sourceFolder.FolderPath,
                                                                                                            DateUtils.GetDatabaseUtcTime().AddYears(1), null));

                                foreach (var folderPermission in sourceFolderPermissions)
                                {
                                    ProcessFolderPermission(importJob, importDto, folderPermission,
                                                            localPermissions);
                                }
                                totalFolderPermissionsImported += sourceFolderPermissions.Count;
                            }

                            // PROCESS FILES
                            var sourceFiles =
                                Repository.GetRelatedItems <ExportFile>(sourceFolder.Id).ToList();
                            //Replace folderId for each file with new one.
                            sourceFiles.ForEach(x =>
                            {
                                x.FolderId = Convert.ToInt32(sourceFolder.FolderId);
                                x.Folder   = sourceFolder.FolderPath;
                            });

                            //File local files in the system related to the folder
                            var localFiles =
                                CBO.FillCollection <ExportFile>(DataProvider.Instance()
                                                                .GetFiles(portalId, sourceFolder.FolderId,
                                                                          DateUtils.GetDatabaseUtcTime().AddYears(1), null));

                            foreach (var file in sourceFiles)
                            {
                                ProcessFiles(importJob, importDto, file, localFiles);
                            }
                            totalFilesImported += sourceFiles.Count;
                        }

                        currentIndex++;
                        CheckPoint.ProcessedItems++;
                        CheckPoint.Progress = 10 + CheckPoint.ProcessedItems * 90.0 / totalFolders;
                        //After every 10 items, call the checkpoint stage. This is to avoid too many frequent updates to DB.
                        if (currentIndex % 10 == 0 && CheckPointStageCallback(this))
                        {
                            return;
                        }
                    }
                    currentIndex         = 0;
                    CheckPoint.Completed = true;
                    CheckPoint.Stage++;
                    CheckPoint.Progress = 100;
                }
                finally
                {
                    CheckPoint.StageData = currentIndex > 0
                        ? JsonConvert.SerializeObject(new { skip = currentIndex })
                        : null;
                    CheckPointStageCallback(this);

                    Result.AddSummary("Imported Folders", totalFolderImported.ToString());
                    Result.AddSummary("Imported Folder Permissions", totalFolderPermissionsImported.ToString());
                    Result.AddSummary("Imported Files", totalFilesImported.ToString());

                    if (Directory.Exists(userFolderPath) && currentIndex == 0)
                    {
                        Directory.Delete(userFolderPath, true);
                    }
                }
            }
        }
示例#3
0
        public override void ImportData(ExportImportJob importJob, ImportDto importDto)
        {
            if (this.CheckCancelled(importJob))
            {
                return;
            }

            // Skip the export if all the templates have been processed already.
            if (this.CheckPoint.Stage >= 2 || this.CheckPoint.Completed)
            {
                return;
            }

            var portalId       = importJob.PortalId;
            var templatesFile  = string.Format(this.templatesFolder, importJob.Directory.TrimEnd('\\').TrimEnd('/'));
            var totalTemplates = this.GetImportTotal();

            this.CheckPoint.TotalItems = this.CheckPoint.TotalItems <= 0 ? totalTemplates : this.CheckPoint.TotalItems;
            if (this.CheckPointStageCallback(this))
            {
                return;
            }

            if (this.CheckPoint.Stage == 0)
            {
                if (!File.Exists(templatesFile))
                {
                    this.Result.AddLogEntry(
                        "TemplatesFileNotFound",
                        "Templates file not found. Skipping templates import",
                        ReportLevel.Warn);
                    this.CheckPoint.Completed = true;
                    this.CheckPointStageCallback(this);
                }
                else
                {
                    var portal = PortalController.Instance.GetPortal(portalId);

                    CompressionUtil.UnZipArchive(
                        templatesFile,
                        portal.HomeDirectoryMapPath,
                        importDto.CollisionResolution == CollisionResolution.Overwrite);

                    this.Result.AddSummary("Imported templates", totalTemplates.ToString());
                    this.CheckPoint.Stage++;
                    this.CheckPoint.StageData      = null;
                    this.CheckPoint.Progress       = 90;
                    this.CheckPoint.TotalItems     = totalTemplates;
                    this.CheckPoint.ProcessedItems = totalTemplates;
                    if (this.CheckPointStageCallback(this))
                    {
                        return;
                    }
                }
            }

            if (this.CheckPoint.Stage == 1)
            {
                Func <ExportPageTemplate, object> predicate = x => x.Folder;
                var templates = this.Repository.GetAllItems(predicate).Select(x => x.Folder).Distinct();
                templates.ForEach(x => FolderManager.Instance.Synchronize(importJob.PortalId, x));
                this.CheckPoint.Stage++;
                this.CheckPoint.Completed = true;
                this.CheckPoint.Progress  = 100;
                this.CheckPointStageCallback(this);
            }
        }
示例#4
0
        public override void ImportData(ExportImportJob importJob, ImportDto importDto)
        {
            if (this.CheckCancelled(importJob))
            {
                return;
            }

            // Skip the export if all the templates have been processed already.
            if (this.CheckPoint.Stage >= 1 || this.CheckPoint.Completed)
            {
                return;
            }

            this.exportImportJob = importJob;

            var packageZipFile = $"{Globals.ApplicationMapPath}{Constants.ExportFolder}{this.exportImportJob.Directory.TrimEnd('\\', '/')}\\{Constants.ExportZipThemes}";
            var tempFolder     = $"{Path.GetDirectoryName(packageZipFile)}\\{DateTime.Now.Ticks}";

            if (File.Exists(packageZipFile))
            {
                CompressionUtil.UnZipArchive(packageZipFile, tempFolder);
                var exporeFiles    = Directory.Exists(tempFolder) ? Directory.GetFiles(tempFolder, "*.*", SearchOption.AllDirectories) : new string[0];
                var portalSettings = new PortalSettings(importDto.PortalId);
                this.importCount = exporeFiles.Length;

                this.CheckPoint.TotalItems = this.CheckPoint.TotalItems <= 0 ? exporeFiles.Length : this.CheckPoint.TotalItems;
                if (this.CheckPointStageCallback(this))
                {
                    return;
                }

                if (this.CheckPoint.Stage == 0)
                {
                    try
                    {
                        foreach (var file in exporeFiles)
                        {
                            try
                            {
                                var    checkFolder  = file.Replace(tempFolder + "\\", string.Empty).Split('\\')[0];
                                var    relativePath = file.Substring((tempFolder + "\\" + checkFolder + "\\").Length);
                                string targetPath;

                                if (checkFolder == "_default")
                                {
                                    targetPath = Path.Combine(Globals.HostMapPath, relativePath);
                                }
                                else if (checkFolder.EndsWith("-System"))
                                {
                                    targetPath = Path.Combine(portalSettings.HomeSystemDirectoryMapPath, relativePath);
                                }
                                else
                                {
                                    targetPath = Path.Combine(portalSettings.HomeDirectoryMapPath, relativePath);
                                }

                                if (!File.Exists(targetPath) ||
                                    importDto.CollisionResolution == CollisionResolution.Overwrite)
                                {
                                    var directory = Path.GetDirectoryName(targetPath);
                                    if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
                                    {
                                        Directory.CreateDirectory(directory);
                                    }

                                    File.Copy(file, targetPath, true);
                                }

                                this.Result.AddLogEntry("Import Theme File completed", targetPath);
                                this.CheckPoint.ProcessedItems++;
                                this.CheckPoint.Progress = this.CheckPoint.ProcessedItems * 100.0 / exporeFiles.Length;
                                this.CheckPointStageCallback(this); // just to update the counts without exit logic
                            }
                            catch (Exception ex)
                            {
                                this.Result.AddLogEntry("Import Theme error", file);
                                Logger.Error(ex);
                            }
                        }

                        this.CheckPoint.Stage++;
                        this.CheckPoint.Completed = true;
                    }
                    finally
                    {
                        this.CheckPointStageCallback(this);
                        FileSystemUtils.DeleteFolderRecursive(tempFolder);
                    }
                }
            }
            else
            {
                this.CheckPoint.Completed = true;
                this.CheckPointStageCallback(this);
                this.Result.AddLogEntry("ThemesFileNotFound", "Themes file not found. Skipping themes import", ReportLevel.Warn);
            }
        }
示例#5
0
        private void ProcessImportModulePackages(ImportDto importDto)
        {
            var packageZipFile = $"{Globals.ApplicationMapPath}{Constants.ExportFolder}{_exportImportJob.Directory.TrimEnd('\\', '/')}\\{Constants.ExportZipPackages}";
            var tempFolder     = $"{Path.GetDirectoryName(packageZipFile)}\\{DateTime.Now.Ticks}";

            if (File.Exists(packageZipFile))
            {
                CompressionUtil.UnZipArchive(packageZipFile, tempFolder);
                var exportPackages = Repository.GetAllItems <ExportPackage>().ToList();

                CheckPoint.TotalItems = CheckPoint.TotalItems <= 0 ? exportPackages.Count : CheckPoint.TotalItems;
                if (CheckPointStageCallback(this))
                {
                    return;
                }

                if (CheckPoint.Stage == 0)
                {
                    try
                    {
                        foreach (var exportPackage in exportPackages)
                        {
                            try
                            {
                                var filePath = Path.Combine(tempFolder, exportPackage.PackageFileName);
                                if (!File.Exists(filePath))
                                {
                                    continue;
                                }

                                var packageType = exportPackage.PackageType;
                                var packageName = exportPackage.PackageName;
                                var version     = exportPackage.Version;

                                var existPackage = PackageController.Instance.GetExtensionPackage(Null.NullInteger,
                                                                                                  p => p.PackageType == packageType && p.Name == packageName);
                                if (existPackage != null &&
                                    (existPackage.Version > version ||
                                     (existPackage.Version == version &&
                                      importDto.CollisionResolution == CollisionResolution.Ignore)))
                                {
                                    Result.AddLogEntry("Import Package ignores",
                                                       $"{packageName} has higher version {existPackage.Version} installed, ignore import it");
                                    continue;
                                }

                                InstallPackage(filePath);
                                Result.AddLogEntry("Import Package completed", $"{packageName} version: {version}");
                                CheckPoint.ProcessedItems++;
                                CheckPoint.Progress = CheckPoint.ProcessedItems * 100.0 / exportPackages.Count;
                                CheckPointStageCallback(this); // just to update the counts without exit logic
                            }
                            catch (Exception ex)
                            {
                                Result.AddLogEntry("Import Package error",
                                                   $"{exportPackage.PackageName} : {exportPackage.Version} - {ex.Message}");
                                Logger.Error(ex);
                            }
                        }
                        CheckPoint.Stage++;
                        CheckPoint.Completed = true;
                    }
                    finally
                    {
                        CheckPointStageCallback(this);
                        try
                        {
                            FileSystemUtils.DeleteFolderRecursive(tempFolder);
                        }
                        catch (Exception)
                        {
                            //ignore
                        }
                    }
                }
            }
            else
            {
                CheckPoint.Completed = true;
                CheckPointStageCallback(this);
                Result.AddLogEntry("PackagesFileNotFound", "Packages file not found. Skipping packages import", ReportLevel.Warn);
            }
        }