Ejemplo n.º 1
0
        public bool VerifyImportPackage(string packageId, ImportExportSummary summary, out string errorMessage)
        {
            bool isValid;

            errorMessage = string.Empty;
            var importFolder = Path.Combine(ExportFolder, packageId);

            if (!IsValidImportFolder(importFolder))
            {
                return(false);
            }
            var dbPath = UnPackDatabase(importFolder);

            try
            {
                using (var ctx = new ExportImportRepository(dbPath))
                {
                    if (summary != null)
                    {
                        BuildJobSummary(packageId, ctx, summary);
                    }
                    isValid = true;
                }
            }
            catch (Exception ex)
            {
                isValid      = false;
                errorMessage = "Package is not valid. Technical Details:" + ex.Message;
            }
            return(isValid);
        }
Ejemplo n.º 2
0
        public HttpResponseMessage VerifyImportPackage(string packageId)
        {
            var    controller = new ImportController();
            string message;
            var    summary = new ImportExportSummary();
            var    isValid = controller.VerifyImportPackage(packageId, summary, out message);

            summary.ConvertToLocal(this.UserInfo);
            return(isValid
                ? this.Request.CreateResponse(HttpStatusCode.OK, summary)
                : this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, message));
        }
Ejemplo n.º 3
0
        public void CreatePackageManifest(ExportImportJob exportJob, ExportFileInfo exportFileInfo,
                                          ImportExportSummary summary)
        {
            var filePath    = Path.Combine(ExportFolder, exportJob.Directory, Constants.ExportManifestName);
            var portal      = PortalController.Instance.GetPortal(exportJob.PortalId);
            var packageInfo = new ImportPackageInfo
            {
                Summary     = summary,
                PackageId   = exportJob.Directory,
                Name        = exportJob.Name,
                Description = exportJob.Description,
                ExporTime   = exportJob.CreatedOnDate,
                PortalName  = portal?.PortalName
            };

            Util.WriteJson(filePath, packageInfo);
        }
Ejemplo n.º 4
0
        protected static ImportExportSummary BuildJobSummary(int jobId)
        {
            var summaryItems = new SummaryList();
            var controller   = EntitiesController.Instance;
            var job          = controller.GetJobById(jobId);
            var exportDto    = job.JobType == JobType.Export
                ? JsonConvert.DeserializeObject <ExportDto>(job.JobObject)
                : JsonConvert.DeserializeObject <ImportDto>(job.JobObject).ExportDto;

            var importExportSummary = new ImportExportSummary
            {
                IncludeDeletions         = exportDto.IncludeDeletions,
                IncludeExtensions        = exportDto.IncludeExtensions,
                IncludePermissions       = exportDto.IncludePermissions,
                IncludeProfileProperties = exportDto.IncludeProperfileProperties,
                IncludeContent           = exportDto.IncludeContent,
                FromDate       = exportDto.FromDateUtc,
                ToDate         = exportDto.ToDateUtc,
                ExportMode     = exportDto.ExportMode,
                ExportFileInfo = job.JobType == JobType.Export
                    ? GetExportFileInfo(Path.Combine(ExportFolder, job.Directory, Constants.ExportManifestName))
                    : JsonConvert.DeserializeObject <ImportDto>(job.JobObject).ExportFileInfo,
            };

            var checkpoints = EntitiesController.Instance.GetJobChekpoints(jobId);

            if (!checkpoints.Any())
            {
                return(importExportSummary);
            }

            var implementors = Util.GetPortableImplementors();

            summaryItems.AddRange(checkpoints.Select(checkpoint => new SummaryItem
            {
                TotalItems         = checkpoint.TotalItems,
                ProcessedItems     = checkpoint.ProcessedItems <= checkpoint.TotalItems ? checkpoint.ProcessedItems : checkpoint.TotalItems,
                ProgressPercentage = Convert.ToInt32(checkpoint.Progress),
                Category           = checkpoint.Category,
                Order     = implementors.FirstOrDefault(x => x.Category == checkpoint.Category)?.Priority ?? 0,
                Completed = checkpoint.Completed,
            }));
            importExportSummary.SummaryItems = summaryItems;
            return(importExportSummary);
        }
Ejemplo n.º 5
0
        /// <summary>Runs the export job.</summary>
        /// <param name="exportJob">The export job.</param>
        /// <param name="result">The result.</param>
        /// <param name="scheduleHistoryItem">The schedule history item.</param>
        public void Export(ExportImportJob exportJob, ExportImportResult result, ScheduleHistoryItem scheduleHistoryItem)
        {
            var exportDto = JsonConvert.DeserializeObject <ExportDto>(exportJob.JobObject);

            if (exportDto == null)
            {
                exportJob.CompletedOnDate = DateUtils.GetDatabaseUtcTime();
                exportJob.JobStatus       = JobStatus.Failed;
                return;
            }

            this.timeoutSeconds = GetTimeoutPerSlot();
            var dbName = Path.Combine(ExportFolder, exportJob.Directory, Constants.ExportDbName);
            var finfo  = new FileInfo(dbName);

            dbName = finfo.FullName;

            var checkpoints = EntitiesController.Instance.GetJobChekpoints(exportJob.JobId);

            // Delete so we start a fresh export database; only if there is no previous checkpoint exists
            if (checkpoints.Count == 0)
            {
                if (finfo.Directory != null && finfo.Directory.Exists)
                {
                    finfo.Directory.Delete(true);
                }

                // Clear all the files in finfo.Directory. Create if doesn't exists.
                finfo.Directory?.Create();
                result.AddSummary("Starting Exporting Repository", finfo.Name);
            }
            else
            {
                if (finfo.Directory != null && finfo.Directory.Exists)
                {
                    result.AddSummary("Resuming Exporting Repository", finfo.Name);
                }
                else
                {
                    scheduleHistoryItem.AddLogNote("Resuming data not found.");
                    result.AddSummary("Resuming data not found.", finfo.Name);
                    return;
                }
            }

            exportJob.JobStatus = JobStatus.InProgress;

            // there must be one parent implementor at least for this to work
            var implementors   = Util.GetPortableImplementors().ToList();
            var parentServices = implementors.Where(imp => string.IsNullOrEmpty(imp.ParentCategory)).ToList();

            implementors = implementors.Except(parentServices).ToList();
            var nextLevelServices = new List <BasePortableService>();
            var includedItems     = GetAllCategoriesToInclude(exportDto, implementors);

            if (includedItems.Count == 0)
            {
                scheduleHistoryItem.AddLogNote("Export NOT Possible");
                scheduleHistoryItem.AddLogNote("<br/>No items selected for exporting");
                result.AddSummary("Export NOT Possible", "No items selected for exporting");
                exportJob.CompletedOnDate = DateUtils.GetDatabaseUtcTime();
                exportJob.JobStatus       = JobStatus.Failed;
                return;
            }

            scheduleHistoryItem.AddLogNote($"<br/><b>SITE EXPORT Preparing Check Points. JOB #{exportJob.JobId}: {exportJob.Name}</b>");
            this.PrepareCheckPoints(exportJob.JobId, parentServices, implementors, includedItems, checkpoints);

            scheduleHistoryItem.AddLogNote($"<br/><b>SITE EXPORT Started. JOB #{exportJob.JobId}: {exportJob.Name}</b>");
            scheduleHistoryItem.AddLogNote($"<br/>Between [{exportDto.FromDateUtc ?? Constants.MinDbTime}] and [{exportDto.ToDateUtc:g}]");
            var firstIteration = true;

            AddJobToCache(exportJob);

            using (var ctx = new ExportImportRepository(dbName))
            {
                ctx.AddSingleItem(exportDto);
                do
                {
                    foreach (var service in parentServices.OrderBy(x => x.Priority))
                    {
                        if (exportJob.IsCancelled)
                        {
                            exportJob.JobStatus = JobStatus.Cancelled;
                            break;
                        }

                        if (implementors.Count > 0)
                        {
                            // collect children for next iteration
                            var children =
                                implementors.Where(imp => service.Category.Equals(imp.ParentCategory, IgnoreCaseComp));
                            nextLevelServices.AddRange(children);
                            implementors = implementors.Except(nextLevelServices).ToList();
                        }

                        if ((firstIteration && includedItems.Any(x => x.Equals(service.Category, IgnoreCaseComp))) ||
                            (!firstIteration && includedItems.Any(x => x.Equals(service.ParentCategory, IgnoreCaseComp))))
                        {
                            var serviceAssembly = service.GetType().Assembly.GetName().Name;
                            service.Result                  = result;
                            service.Repository              = ctx;
                            service.CheckCancelled          = CheckCancelledCallBack;
                            service.CheckPointStageCallback = this.CheckpointCallback;
                            service.CheckPoint              = checkpoints.FirstOrDefault(cp => cp.Category == service.Category && cp.AssemblyName == serviceAssembly);

                            if (service.CheckPoint == null)
                            {
                                service.CheckPoint = new ExportImportChekpoint
                                {
                                    JobId        = exportJob.JobId,
                                    Category     = service.Category,
                                    AssemblyName = serviceAssembly,
                                    StartDate    = DateUtils.GetDatabaseUtcTime(),
                                };

                                // persist the record in db
                                this.CheckpointCallback(service);
                            }
                            else if (service.CheckPoint.StartDate == Null.NullDate)
                            {
                                service.CheckPoint.StartDate = DateUtils.GetDatabaseUtcTime();
                            }

                            try
                            {
                                service.ExportData(exportJob, exportDto);
                            }
                            finally
                            {
                                this.AddLogsToDatabase(exportJob.JobId, result.CompleteLog);
                            }

                            scheduleHistoryItem.AddLogNote("<br/>Exported: " + service.Category);
                        }
                    }

                    firstIteration = false;
                    parentServices = new List <BasePortableService>(nextLevelServices);
                    nextLevelServices.Clear();
                    if (implementors.Count > 0 && parentServices.Count == 0)
                    {
                        // WARN: this is a case where there is a broken parent-children hierarchy
                        //      and/or there are BasePortableService implementations without a known parent.
                        parentServices = implementors;
                        implementors.Clear();
                        scheduleHistoryItem.AddLogNote(
                            "<br/><b>Orphaned services:</b> " + string.Join(",", parentServices.Select(x => x.Category)));
                    }
                }while (parentServices.Count > 0 && !this.TimeIsUp);

                RemoveTokenFromCache(exportJob);
            }

            if (this.TimeIsUp)
            {
                result.AddSummary(
                    $"Job time slot ({this.timeoutSeconds} sec) expired",
                    "Job will resume in the next scheduler iteration");
            }
            else if (exportJob.JobStatus == JobStatus.InProgress)
            {
                // Create Export Summary for manifest file.
                var summary = new ImportExportSummary();
                using (var ctx = new ExportImportRepository(dbName))
                {
                    BaseController.BuildJobSummary(exportJob.Directory, ctx, summary);
                }

                DoPacking(exportJob, dbName);

                // Complete the job.
                exportJob.JobStatus = JobStatus.Successful;
                SetLastJobStartTime(scheduleHistoryItem.ScheduleID, exportJob.CreatedOnDate);

                var exportController = new ExportController();
                var exportFileInfo   = new ExportFileInfo
                {
                    ExportPath = exportJob.Directory,
                    ExportSize = Util.FormatSize(GetExportSize(Path.Combine(ExportFolder, exportJob.Directory))),
                };

                summary.ExportFileInfo = exportFileInfo;
                exportController.CreatePackageManifest(exportJob, exportFileInfo, summary);
            }
        }
Ejemplo n.º 6
0
        protected internal static void BuildJobSummary(string packageId, IExportImportRepository repository, ImportExportSummary summary)
        {
            var summaryItems = new SummaryList();
            var implementors = Util.GetPortableImplementors();
            var exportDto    = repository.GetSingleItem <ExportDto>();

            foreach (var implementor in implementors)
            {
                implementor.Repository = repository;
                summaryItems.Add(new SummaryItem
                {
                    TotalItems = implementor.GetImportTotal(),
                    Category   = implementor.Category,
                    Order      = implementor.Priority,
                });
            }

            summary.ExportFileInfo           = GetExportFileInfo(Path.Combine(ExportFolder, packageId, Constants.ExportManifestName));
            summary.FromDate                 = exportDto.FromDateUtc;
            summary.ToDate                   = exportDto.ToDateUtc;
            summary.SummaryItems             = summaryItems;
            summary.IncludeDeletions         = exportDto.IncludeDeletions;
            summary.IncludeContent           = exportDto.IncludeContent;
            summary.IncludeExtensions        = exportDto.IncludeExtensions;
            summary.IncludePermissions       = exportDto.IncludePermissions;
            summary.IncludeProfileProperties = exportDto.IncludeProperfileProperties;
            summary.ExportMode               = exportDto.ExportMode;
        }