Esempio n. 1
0
        public async Task CanGetExportDataFailure(string message)
        {
            var customHeaders = new HeaderDictionary();

            var scheduleRequest = new ScheduleJobRequest {
                Url = "some url", Filename = "dummy"
            };
            var ms = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(scheduleRequest)));
            var fileStreamResult = new FileStreamResult(ms, ContentTypeConstants.ApplicationJson);

            var context = GetMockHangfireContext(typeof(ExportJobTests), TestContext.TestName, message);

            var exception = new Exception(message);
            Mock <IApiClient> apiClient = new Mock <IApiClient>();

            apiClient.Setup(a => a.SendRequest <CompactionExportResult>(It.IsAny <ScheduleJobRequest>(), customHeaders)).Throws(exception);

            Mock <ITransferProxy> transferProxy = new Mock <ITransferProxy>();

            transferProxy.Setup(t => t.Upload(It.IsAny <Stream>(), It.IsAny <string>())).Verifiable();
            transferProxy.Setup(t => t.Download(It.IsAny <string>())).Returns(() => Task.FromResult(fileStreamResult));

            Mock <ITransferProxyFactory> transferProxyFactory = new Mock <ITransferProxyFactory>();

            transferProxyFactory.Setup(x => x.NewProxy(It.IsAny <TransferProxyType>())).Returns(transferProxy.Object);

            Mock <Logging.ILoggerFactory> logger = new Mock <Logging.ILoggerFactory>();

            var exportJob = new ExportJob(apiClient.Object, transferProxyFactory.Object, logger.Object);

            await Assert.ThrowsExceptionAsync <Exception>(() => exportJob.GetExportData(Guid.NewGuid(), customHeaders, context));

            ms.Dispose();
        }
Esempio n. 2
0
        public async Task DownloadAsync()
        {
            EventHandler <ProgressUpdatedEventArgs> eventHandler = (sender, e) =>
            {
                this.DownloadMessage  = e.Message;
                this.DownloadProgress = e.Progress;
            };

            try
            {
                this.ClientState = ClientState.PrepareDownload;
                _dataService.Progress.ProgressChanged += eventHandler;

                var selectedDatasets = this.GetSelectedDatasets().Select(dataset => dataset.Model).ToList();

                // security check
                var projectIds = selectedDatasets.Select(dataset => dataset.Parent.Parent.Id).Distinct();

                foreach (var projectId in projectIds)
                {
                    if (!Utilities.IsProjectAccessible(_userIdService.User, projectId, _databaseManager.Database))
                    {
                        throw new UnauthorizedAccessException($"The current user is not authorized to access project '{projectId}'.");
                    }
                }

                //
                var job = new ExportJob()
                {
                    Owner      = _userIdService.User.Identity.Name,
                    Parameters = this.ExportParameters
                };

                var exportJobService = _serviceProvider.GetRequiredService <JobService <ExportJob> >();

                _exportJobControl = exportJobService.AddJob(job, _dataService.Progress, (jobControl, cts) =>
                {
                    var task = _dataService.ExportDataAsync(this.ExportParameters,
                                                            selectedDatasets,
                                                            cts.Token);

                    return(task);
                });

                var downloadLink = await _exportJobControl.Task;

                if (!string.IsNullOrWhiteSpace(downloadLink))
                {
                    var fileName = downloadLink.Split("/").Last();
                    await _jsRuntime.FileSaveAs(fileName, downloadLink);
                }
            }
            finally
            {
                _dataService.Progress.ProgressChanged -= eventHandler;
                this.ClientState      = ClientState.Normal;
                this.DownloadMessage  = string.Empty;
                this.DownloadProgress = 0;
            }
        }
Esempio n. 3
0
        public void CanGetExportDataSuccess(string message)
        {
            var customHeaders = new HeaderDictionary();

            var scheduleRequest = new ScheduleJobRequest {
                Url = "some url", Filename = "dummy"
            };
            var context = GetMockHangfireContext(typeof(ExportJobTests), TestContext.TestName, message);

            Mock <IApiClient> apiClient = new Mock <IApiClient>();

            apiClient.Setup(a => a.SendRequest <CompactionExportResult>(scheduleRequest, customHeaders)).ReturnsAsync(new CompactionExportResult());

            Mock <ITransferProxy> transferProxy = new Mock <ITransferProxy>();

            transferProxy.Setup(t => t.Upload(It.IsAny <Stream>(), It.IsAny <string>())).Verifiable();

            Mock <ITransferProxyFactory> transferProxyFactory = new Mock <ITransferProxyFactory>();

            transferProxyFactory.Setup(x => x.NewProxy(It.IsAny <TransferProxyType>())).Returns(transferProxy.Object);

            Mock <Logging.ILoggerFactory> logger = new Mock <Logging.ILoggerFactory>();

            var exportJob = new ExportJob(apiClient.Object, transferProxyFactory.Object, logger.Object);
            var result    = exportJob.GetExportData(Guid.NewGuid(), customHeaders, context);
        }
Esempio n. 4
0
 public MCWorldExporter(ExportJob job, bool useDefaultPostProcessors, bool useSplatmaps) : this(job)
 {
     if (useSplatmaps)
     {
         postProcessor = new SplatmappedSurfacePostProcessor(job.data.filename, 255, regionOffsetX * 512, regionOffsetZ * 512, job.data.GridWidth, job.data.GridHeight);
     }
 }
Esempio n. 5
0
        public MCWorldExporter(ExportJob job)
        {
            regionOffsetX = job.exportNumX + job.settings.GetCustomSetting("mcaOffsetX", 0);
            regionOffsetZ = job.exportNumZ + job.settings.GetCustomSetting("mcaOffsetZ", 0);
            int xmin        = regionOffsetX * 512;
            int zmin        = regionOffsetZ * 512;
            var hmapFlipped = job.data.GetDataGridFlipped();

            heightmapLengthX = hmapFlipped.GetLength(0);
            heightmapLengthZ = hmapFlipped.GetLength(1);
            worldBounds      = new Bounds(xmin, zmin, xmin + heightmapLengthX - 1, zmin + heightmapLengthZ - 1);
            heightmap        = new byte[heightmapLengthX, heightmapLengthZ];
            for (int x = 0; x < heightmapLengthX; x++)
            {
                for (int z = 0; z < heightmapLengthZ; z++)
                {
                    heightmap[x, z] = (byte)MathUtils.Clamp((float)Math.Round(hmapFlipped[x, z], MidpointRounding.AwayFromZero), 0, 255);
                }
            }
            regionNumX = (int)Math.Ceiling(heightmapLengthX / 512f);
            regionNumZ = (int)Math.Ceiling(heightmapLengthZ / 512f);
            if (heightmapLengthX % 512 > 0 || heightmapLengthZ % 512 > 0)
            {
                ConsoleOutput.WriteWarning("Input heightmap is not a multiple of 512. Void borders will be present in the world.");
            }
        }
Esempio n. 6
0
        public string CreateExportJob(DateTime startDate, DateTime endDate)
        {
            var json            = marketoAPIHelper.CreateExportJobRequest(startDate, endDate);
            var createJobResult = JsonConvert.DeserializeObject <RequestResult>(json);

            if (!createJobResult.success)
            {
                var errorRes = JsonConvert.DeserializeObject <RequestResultError>(json);
                ErrorHandling("Create Export Job", errorRes);

                return(null);
            }

            var exportJob = new ExportJob()
            {
                ExportId  = createJobResult.result.Single().exportId,
                Format    = createJobResult.result.Single().format,
                Status    = createJobResult.result.Single().status,
                CreatedAt = createJobResult.result.Single().createdAt
            };

            _unitOfWork.ExportJobs.AddExportJob(exportJob);
            _unitOfWork.Complete();

            return(exportJob.ExportId);
        }
Esempio n. 7
0
        public static bool WriteWorldSave(ExportJob job, bool decorate, bool useSplatmaps)
        {
            IExporter exporter = new MCWorldExporter(job, decorate, useSplatmaps);

            ExportUtility.WriteFile(exporter, job.FilePath, ExportUtility.GetFormatFromIdenfifier("MCW"));
            return(true);
        }
Esempio n. 8
0
 public override bool Export(ExportJob job)
 {
     if (job.format.Identifier.StartsWith("3DM"))
     {
         return(WriteFile3D(job.data, job.FilePath, job.format));
     }
     return(false);
 }
Esempio n. 9
0
        void AssertExport(HeightData data, string filetype, string path)
        {
            var format = ExportUtility.GetFormatFromIdenfifier(filetype);

            path = Path.ChangeExtension(Path.Combine(outputPath, path), format.Extension);
            var job = new ExportJob(data, format, new ExportSettings(), outputPath, path);

            job.Export();
            Assert.IsTrue(File.Exists(path), "Written file not found");
        }
Esempio n. 10
0
        public void CanGetS3KeyForExport()
        {
            return;

            var jobId       = "Some id";
            var filename    = "dummy";
            var key         = ExportJob.GetS3Key(jobId, filename);
            var expectedKey = $"3dpm/{jobId}/{filename}";

            Assert.AreEqual(expectedKey, key, "Wrong S3 key");
        }
Esempio n. 11
0
 public override bool Export(ExportJob job)
 {
     if (job.format.IsFormat("MCR") || job.format.IsFormat("MCR-RAW"))
     {
         return(WriteFileMCA(job, !job.format.IsFormat("MCR-RAW"), job.settings.GetCustomSetting("mcaUseSplatmaps", false)));
     }
     else if (job.format.IsFormat("IMG_MCR"))
     {
         ExportUtility.WriteFile(new OverviewmapExporter(job.data.filename, true), job.FilePath, job.format);
     }
     else if (job.format.IsFormat("MCW"))
     {
         return(WriteWorldSave(job, true, job.settings.GetCustomSetting("mcaUseSplatmaps", false)));
     }
     return(false);
 }
Esempio n. 12
0
 public override void EditFileName(ExportJob job, FileNameBuilder nameBuilder)
 {
     if (job.settings.ContainsFormat("MCR"))
     {
         nameBuilder.gridNum       = (job.exportNumX + job.settings.GetCustomSetting("mcaOffsetX", 0), job.exportNumZ + job.settings.GetCustomSetting("mcaOffsetZ", 0));
         nameBuilder.gridNumFormat = "r.{0}.{1}";
     }
     if (job.format.Identifier == "MCW")
     {
         nameBuilder.gridNumFormat = "";
     }
     if (job.format.IsFormat("IMG_MCR"))
     {
         nameBuilder.suffix = "overview";
     }
 }
Esempio n. 13
0
 public override void EditFileName(ExportJob job, FileNameBuilder nameBuilder)
 {
     if (job.format.IsFormat("IMG_PNG-HM"))
     {
         nameBuilder.suffix = "_height";
     }
     if (job.format.IsFormat("IMG_PNG-HM-S"))
     {
         nameBuilder.suffix = "_height_s";
     }
     else if (job.format.IsFormat("IMG_PNG-NM"))
     {
         nameBuilder.suffix = "_normal";
     }
     else if (job.format.IsFormat("IMG_PNG-HS"))
     {
         nameBuilder.suffix = "_hillshade";
     }
 }
Esempio n. 14
0
        public void CanGetDownloadLink()
        {
            var jobId        = "Some id";
            var filename     = "dummy";
            var presignedUrl = "some presigned url";
            Mock <ITransferProxy>        transferProxy        = new Mock <ITransferProxy>();
            Mock <ITransferProxyFactory> transferProxyFactory = new Mock <ITransferProxyFactory>();

            transferProxyFactory.Setup(x => x.NewProxy(It.IsAny <TransferProxyType>())).Returns(transferProxy.Object);

            Mock <IApiClient> apiClient = new Mock <IApiClient>();

            transferProxy.Setup(t => t.GeneratePreSignedUrl(It.IsAny <string>())).Returns(presignedUrl);

            Mock <Logging.ILoggerFactory> logger = new Mock <Logging.ILoggerFactory>();

            var exportJob  = new ExportJob(apiClient.Object, transferProxyFactory.Object, logger.Object);
            var actualLink = exportJob.GetDownloadLink(jobId, filename);

            Assert.AreEqual(presignedUrl, actualLink);
        }
Esempio n. 15
0
 /// <summary>
 /// Deletes and of the TimerJob WorkItems for the GlymaExportWorkItemTimerJob if they still exist and are in error state.
 /// </summary>
 /// <param name="job">The ExportJob</param>
 /// <param name="site">The SPSite</param>
 /// <param name="web">The SPWeb</param>
 private void CleanupErrorWorkItems(ExportJob job, SPSite site, SPWeb web)
 {
     //ensure that only jobs in error state are attempted to be removed
     if (job.Status == ExportStatus.Error)
     {
         try
         {
             SPWorkItemCollection workItemsCollection = new SPWorkItemCollection(site, GlymaExportWorkItemTimerJob.WorkItemTypeId);
             uint   colCount, rowCount = 0;
             object workItems = null;
             site.GetWorkItems(workItemsCollection, out colCount, out rowCount, out workItems);
             if (workItemsCollection.Count > 0)
             {
                 SPWorkItemCollection subCollection = workItemsCollection.SubCollection(site, web, 0, (uint)workItemsCollection.Count);
                 subCollection.DeleteWorkItem(job.Id);
             }
         }
         catch (Exception)
         {
         }
     }
 }
Esempio n. 16
0
        /// <summary>
        /// Deletes an ExportJob if it's in the Scheduled, Completed, or Error ExportStates.
        /// </summary>
        /// <param name="job">The ExportJob to delete</param>
        /// <returns>The ExportJob that was deleted</returns>
        public ExportJobResponse DeleteExportJob(ExportJob job)
        {
            ExportJobResponse response = new ExportJobResponse();

            try
            {
                Guid webID  = SPContext.Current.Web.ID;
                Guid siteID = SPContext.Current.Site.ID;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPList exportsList = null;
                    using (SPSite site = new SPSite(siteID))
                    {
                        using (SPWeb web = site.OpenWeb(webID))
                        {
                            if (web != null && site != null)
                            {
                                exportsList = web.TryGetList(web.GetServerRelativeListUrlPrefix() + "GlymaExports");
                                if (exportsList != null)
                                {
                                    SPQuery query = new SPQuery();
                                    query.Query   = "<Where>" +
                                                    "<Eq><FieldRef Name='Title' /><Value Type='Text'>" + job.Id.ToString() + "</Value></Eq>" +
                                                    "</Where>";
                                    SPListItemCollection exports = exportsList.GetItems(query);
                                    // There can only be one ExportJob with the job ID (unique values enforced in SP list for Title column)
                                    if (exports.Count > 0)
                                    {
                                        SPListItem exportItem = exports[0];
                                        if (exportItem != null)
                                        {
                                            string exportStatusStr    = exportItem["ExportStatus"] as string;
                                            ExportStatus exportStatus = (ExportStatus)Enum.Parse(typeof(ExportStatus), exportStatusStr, true);

                                            if (exportStatus == ExportStatus.Scheduled)
                                            {
                                                SPWorkItemCollection workItemsCollection = new SPWorkItemCollection(site, GlymaExportWorkItemTimerJob.WorkItemTypeId);
                                                uint colCount, rowCount = 0;
                                                object workItems        = null;
                                                site.GetWorkItems(workItemsCollection, out colCount, out rowCount, out workItems); //gets all work items for this site of the type GlymaExportWorkItemTimerJob
                                                if (workItemsCollection.Count > 0)
                                                {
                                                    // Delete the work item that this export job created
                                                    SPWorkItemCollection subCollection = workItemsCollection.SubCollection(site, web, 0, (uint)workItemsCollection.Count);
                                                    subCollection.DeleteWorkItem(job.Id);
                                                }
                                            }

                                            if (exportStatus == ExportStatus.Scheduled || exportStatus == ExportStatus.Completed || exportStatus == ExportStatus.Error)
                                            {
                                                exportItem.Delete(); //delete the item after it has been cancelled
                                            }
                                            else if (exportStatus == ExportStatus.Processing)
                                            {
                                                throw new Exception("The export job is currently processing and cannot be deleted.");
                                            }
                                        }
                                        response.ExportJob = job;
                                    }
                                }
                                else
                                {
                                    throw new Exception("Failed to find the Glyma Exports list.");
                                }
                            }
                            else
                            {
                                throw new Exception("The SPSite and/or the SPWeb were null.");
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExportError error = new ExportError()
                {
                    ErrorMessage = "Failed to cancel the Glyma map export job."
                };
                throw new FaultException <ExportError>(error, ex.ToString());
            }

            return(response);
        }
Esempio n. 17
0
        public JobStatusResult GetJobStatus(string jobId)
        {
            log.LogInformation($"GetJobStatus: jobId={jobId}");

            var jobData = JobStorage.Current.GetConnection()?.GetJobData(jobId);
            var status  = jobData?.State;

            if (string.IsNullOrEmpty(status))
            {
                throw new ServiceException(HttpStatusCode.BadRequest,
                                           new ContractExecutionResult(ContractExecutionStatesEnum.ValidationError,
                                                                       $"Missing job details for {jobId}"));
            }

            log.LogInformation($"GetJobStatus: {jobId} status={status}");
            string         key          = null;
            string         downloadLink = null;
            FailureDetails details      = null;

            if (status.Equals(Hangfire.States.SucceededState.StateName, StringComparison.OrdinalIgnoreCase))
            {
                if (Request.Path.Value.Contains("export"))
                {
                    // Attempt to get the download link that should have been set in the job
                    key          = JobStorage.Current.GetConnection().GetJobParameter(jobId, ExportJob.S3_KEY_STATE_KEY);
                    downloadLink = JobStorage.Current.GetConnection().GetJobParameter(jobId, ExportJob.DOWNLOAD_LINK_STATE_KEY);
                    log.LogInformation($"Getting export job {jobId} downloadLink={downloadLink}");

                    if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(downloadLink))
                    {
                        log.LogWarning("S3Key or Downloadlink not set in background job, attempting to find it via the original request");
                        var filename = (jobData?.Job.Args[0] as ScheduleJobRequest).Filename ?? jobId;
                        key          = ExportJob.GetS3Key(jobId, filename);
                        downloadLink = exportJob.GetDownloadLink(jobId, filename);
                    }
                }
            }
            else if (status.Equals(Hangfire.States.DeletedState.StateName, StringComparison.OrdinalIgnoreCase))
            {
                var detailsJson = JobStorage.Current.GetConnection().GetJobParameter(jobId, ExportFailedState.EXPORT_DETAILS_KEY);
                log.LogDebug($"GetJobStatus: detailsJson={detailsJson}");
                if (!string.IsNullOrEmpty(detailsJson))
                {
                    details = JsonConvert.DeserializeObject <FailureDetails>(detailsJson);
                }
            }

            // Change behavior so it's not a breaking change for the UI.

            if (details != null &&
                status.Equals(Hangfire.States.DeletedState.StateName, StringComparison.OrdinalIgnoreCase) &&
                details.Result.Code != ContractExecutionStatesEnum.ExecutedSuccessfully)
            {
                status = Hangfire.States.FailedState.StateName;
            }

            var result = new JobStatusResult {
                Key = key, Status = status, DownloadLink = downloadLink, FailureDetails = details
            };

            log.LogInformation($"GetJobStatus: result={JsonConvert.SerializeObject(result)}");
            return(result);
        }
Esempio n. 18
0
        public void OnStartTest()
        {
            string[] args = new string[] { };
            DateTime now  = DateTime.Now;
            //obtencion de la configuracion

            // obtencion de la configuracion
            var appconfig = ConfigurationManager.AppSettings;

            // hh:mm:ss de inicio
            int.TryParse(appconfig["hour"], out int configHH);
            int.TryParse(appconfig["minute"], out int configmm);
            int.TryParse(appconfig["second"], out int configss);
            double.TryParse(appconfig["periodInHours"], out double freqH);

            // periodo
            var freq = TimeSpan.FromHours(freqH).TotalMilliseconds;

            var dueTime = Convert.ToInt32(TimeSpan.FromMilliseconds(
                                              new DateTime(now.Year, now.Month, now.Day, configHH, configmm, configss)
                                              .Subtract(now).TotalMilliseconds
                                              ).TotalMilliseconds);

            if (dueTime < 0)
            {
                dueTime = Convert.ToInt32(TimeSpan.FromMilliseconds(
                                              new DateTime(now.Year, now.Month, now.Day, configHH, configmm, configss)
                                              .AddDays(1).Subtract(now).TotalMilliseconds
                                              ).TotalMilliseconds);
            }


            var logDir = Path.Combine(Environment.GetFolderPath(SpecialFolder.CommonApplicationData),
                                      "Makesoft", "Farmatic Export Data");
            var executionLogFilePath = Path.Combine(logDir, $"datestamp_-exportjoblog.txt");

            if (!Directory.Exists(logDir))
            {
                Directory.CreateDirectory(logDir);
            }
            job = new ExportJob("CS_FarmaticDB");
            using (TextWriter writer = new FileInfo(Path.Combine(logDir, "log.txt")).AppendText())
            {
                var dbbCSConfig = ConfigurationManager.ConnectionStrings["CS_FarmaticDB"];
                writer.WriteLine($"{DateTime.Now.ToString("hh:mm:ss:fff")}: ExportJob.Export - start at {configHH}:{configmm}:{configss}");
                if (!int.TryParse(ConfigurationManager.AppSettings["DaysToResend"], out int daysResend))
                {
                    throw new Exception($"DaysToResend config was not found or invalid, it must be a positive int");
                }
                daysResend = Math.Abs(daysResend);
                job.Config = new JobConfig
                {
                    FarmaticConnectionString = dbbCSConfig.ConnectionString,
                    ProviderConnectionString = dbbCSConfig.ProviderName,
                    APIEndpoint           = ConfigurationManager.AppSettings["APIEndpoint"],
                    APIUser               = ConfigurationManager.AppSettings["APIUser"],
                    APIPwd                = ConfigurationManager.AppSettings["APIPwd"],
                    JWTAuthRoute          = ConfigurationManager.AppSettings["APITokenEndpoint"],
                    APIGetVentaData       = ConfigurationManager.AppSettings["APIGetVentaData"],
                    APIPostVentaData      = ConfigurationManager.AppSettings["APIPostVentaData"],
                    APIPostVentaDataRange = ConfigurationManager.AppSettings["APIPostVentaDataRange"],
                    APICodUsuario         = ConfigurationManager.AppSettings["APICodUsuario"],
                    DaysToResend          = daysResend,
                    UseAPIRangeMethod     = bool.Parse(ConfigurationManager.AppSettings["UseAPIRangeMethod"])
                };

                writer.WriteLine(job.Config.ToString());

                bool retVal = job.ValidateConfig(out Exception ex);
                writer.WriteLine($"Valid config: {retVal}");

                if (!retVal)
                {
                    writer.WriteLine($"Exception found: {ex.ToString()}");
                }
            }


            job.CompleteHandler += Job_CompleteHandler;

            timerDelegate = new TimerCallback(job.Export);
            timer         = new Timer(timerDelegate, new
            {
                ConnectionString = ConfigurationManager.ConnectionStrings["CS_FarmaticDB"],
                APIEndpoint      = ConfigurationManager.AppSettings["APIEndpoint"]
            }, dueTime, int.Parse(Math.Abs(freq).ToString()));

            while (!jobCompleted)
            {
                Thread.CurrentThread.Join(300);
            }
        }
Esempio n. 19
0
 public override bool Export(ExportJob job)
 {
     return(WriteFileImage(job.data, job.FilePath, job.format));
 }
 public void AddExportJob(ExportJob exportJob)
 {
     _context.ExportJobs.Add(exportJob);
 }
Esempio n. 21
0
        public ActionResult <ExportJob> CreateExportJob(ExportParameters parameters)
        {
            if (_databaseManager.Database == null)
            {
                return(this.StatusCode(503, "The database has not been loaded yet."));
            }

            parameters.Begin = parameters.Begin.ToUniversalTime();
            parameters.End   = parameters.End.ToUniversalTime();

            // translate channel paths to datasets
            List <DatasetInfo> datasets;

            try
            {
                datasets = parameters.ChannelPaths.Select(channelPath =>
                {
                    if (!_databaseManager.Database.TryFindDataset(channelPath, out var dataset))
                    {
                        throw new ValidationException($"Could not find the channel with path '{channelPath}'.");
                    }

                    return(dataset);
                }).ToList();
            }
            catch (ValidationException ex)
            {
                return(this.UnprocessableEntity(ex.GetFullMessage(includeStackTrace: false)));
            }

            // check that there is anything to export
            if (!datasets.Any())
            {
                return(this.BadRequest("The list of channel paths is empty."));
            }

            // security check
            var projectIds = datasets.Select(dataset => dataset.Parent.Parent.Id).Distinct();

            foreach (var projectId in projectIds)
            {
                if (!Utilities.IsProjectAccessible(this.HttpContext.User, projectId, _databaseManager.Database))
                {
                    return(this.Unauthorized($"The current user is not authorized to access project '{projectId}'."));
                }
            }

            //
            var job = new ExportJob()
            {
                Owner      = this.User.Identity.Name,
                Parameters = parameters
            };

            var dataService = _serviceProvider.GetRequiredService <DataService>();

            try
            {
                var jobControl = _exportJobService.AddJob(job, dataService.Progress, (jobControl, cts) =>
                {
                    var userIdService = _serviceProvider.GetRequiredService <UserIdService>();
                    var task          = dataService.ExportDataAsync(parameters, datasets, cts.Token);

                    return(task);
                });

                return(this.Accepted($"{this.GetBasePath()}{this.Request.Path}/{jobControl.Job.Id}/status", jobControl.Job));
            }
            catch (ValidationException ex)
            {
                return(this.UnprocessableEntity(ex.GetFullMessage(includeStackTrace: false)));
            }
        }
Esempio n. 22
0
        /// <summary>
        /// Gets the ExportJobs for the particular map schema type for a domain and root map.
        /// </summary>
        /// <param name="mapType">OPTIONALLY: The map schema type</param>
        /// <param name="domainUid">The DomainUid for the exports</param>
        /// <param name="rootMapUid">The RootMapUid for the exports</param>
        /// <returns>A list of all the ExportJobs that match the criteria supplied as arguments</returns>
        private ExportJobsResponse GetExportJobsImp(MapType?mapType, Guid domainUid, Guid rootMapUid)
        {
            ExportJobsResponse response = new ExportJobsResponse();

            response.ExportJobs = new Dictionary <Guid, ExportJob>();

            try
            {
                Guid webID  = SPContext.Current.Web.ID;
                Guid siteID = SPContext.Current.Site.ID;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(siteID))
                    {
                        using (SPWeb web = site.OpenWeb(webID))
                        {
                            if (web != null)
                            {
                                SPList exportsList = web.TryGetList(web.GetServerRelativeListUrlPrefix() + "GlymaExports"); //TODO get the name from a constant
                                if (exportsList != null)
                                {
                                    SPQuery query = new SPQuery();
                                    if (mapType.HasValue)
                                    {
                                        query.Query = "<Where><And><And>" +
                                                      "<Eq><FieldRef Name='DomainUid' /><Value Type='Text'>" + domainUid.ToString() + "</Value></Eq>" +
                                                      "<Eq><FieldRef Name='RootMapUid' /><Value Type='Text'>" + rootMapUid.ToString() + "</Value></Eq>" +
                                                      "</And><Eq><FieldRef Name='MapType' /><Value Type='Text'>" + mapType.ToString() + "</Value></Eq></And></Where>" +
                                                      "<OrderBy><FieldRef Name='Created' Ascending='TRUE'></FieldRef></OrderBy>";
                                    }
                                    else
                                    {
                                        query.Query = "<Where><And>" +
                                                      "<Eq><FieldRef Name='DomainUid' /><Value Type='Text'>" + domainUid.ToString() + "</Value></Eq>" +
                                                      "<Eq><FieldRef Name='RootMapUid' /><Value Type='Text'>" + rootMapUid.ToString() + "</Value></Eq>" +
                                                      "</And></Where>" +
                                                      "<OrderBy><FieldRef Name='Created' Ascending='TRUE'></FieldRef></OrderBy>";
                                    }
                                    //get the exports for this domain/rootmap in ascending order
                                    SPListItemCollection exports = exportsList.GetItems(query);
                                    foreach (SPListItem export in exports)
                                    {
                                        ExportJob exportJob = GetExportJob(export);
                                        response.ExportJobs.Add(exportJob.Id, exportJob);

                                        if (exportJob.Status == ExportStatus.Error)
                                        {
                                            //maintenance task if the export job had an error clear the TimerJobWorkItem if it was left behind
                                            CleanupErrorWorkItems(exportJob, site, web);
                                        }
                                    }
                                }
                                else
                                {
                                    throw new Exception("Failed to find the Glyma Exports list.");
                                }
                            }
                            else
                            {
                                throw new Exception("The SPWeb was not found.");
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExportError error = new ExportError()
                {
                    ErrorMessage = "Failed to get the export jobs for the site."
                };
                throw new FaultException <ExportError>(error, ex.ToString());
            }

            return(response);
        }
Esempio n. 23
0
        private ExportJob GetExportJob(SPListItem export)
        {
            ExportJob exportJob = new ExportJob();

            try
            {
                string workItemId = export[SPBuiltInFieldId.Title] as string;
                workItemId.Trim(); //ensure it is just the GUID in case anyone has messed with the list
                exportJob.Id = new Guid(workItemId);

                exportJob.Created = (DateTime)export[SPBuiltInFieldId.Created];
                string createdBy = export[SPBuiltInFieldId.Author] as string;
                exportJob.CreatedBy = new GlymaUser()
                {
                    Name = createdBy
                };


                if (export.Attachments.Count > 0)
                {
                    string fileName    = export.Attachments[0] as string;
                    string linkAddress = export.Attachments.UrlPrefix + fileName;
                    exportJob.Link = linkAddress;
                }
                string exportStatus = export["ExportStatus"] as string;
                exportJob.Status = (ExportStatus)Enum.Parse(typeof(ExportStatus), exportStatus, true);

                string exportType = export["ExportType"] as string;
                exportJob.Type = (ExportType)Enum.Parse(typeof(ExportType), exportType, true);

                string mapType = export["MapType"] as string;
                exportJob.MapType = (MapType)Enum.Parse(typeof(MapType), mapType, true);;

                string exportProperties = export["ExportProperties"] as string;
                if (!string.IsNullOrEmpty(exportProperties))
                {
                    try
                    {
                        ExportPropertiesDictionary serializableDict = new ExportPropertiesDictionary(export["ExportProperties"] as string);
                        exportJob.ExportProperties = serializableDict as IDictionary <string, string>;
                    }
                    catch (Exception)
                    {
                        //failed to parse the ExportProperties so assume they are null
                    }
                }

                try
                {
                    Double exportPercentageComplete = (Double)export["PercentageComplete"];
                    exportPercentageComplete     = exportPercentageComplete * 100;
                    exportJob.PercentageComplete = (int)exportPercentageComplete;
                }
                catch (Exception)
                {
                    exportJob.PercentageComplete = 0;
                }

                //TODO: determine if the export job is current (have any changes occurred to the map since last export
                exportJob.IsCurrent = true;
            }
            catch (Exception)
            {
                //Failed to read the Export Job from the SPListItem, could be a Guid ID wasn't of the correct format
            }

            return(exportJob);
        }
Esempio n. 24
0
        /// <summary>
        /// Creates an ExportJob and schedules the WorkItem for the timer job that processes the exports.
        /// </summary>
        /// <param name="domainUid">The DominUid for the map being exported</param>
        /// <param name="rootMapUid">The RootMapUid for the map being exported</param>
        /// <param name="exportProperties">The export properties for the export</param>
        /// <param name="mapType">The map type (schema) for the map being exported</param>
        /// <param name="exportType">The output format for the export</param>
        /// <returns>The ExportJob that was created</returns>
        public ExportJobResponse CreateExportJob(Guid domainUid, Guid rootMapUid, IDictionary <string, string> exportProperties, MapType mapType, ExportType exportType)
        {
            ExportJobResponse response = new ExportJobResponse();

            try
            {
                Guid   webID       = SPContext.Current.Web.ID;
                Guid   siteID      = SPContext.Current.Site.ID;
                SPUser currentUser = null;
                using (SPSite site = new SPSite(siteID))
                {
                    using (SPWeb web = site.OpenWeb(webID))
                    {
                        if (web != null)
                        {
                            currentUser = web.CurrentUser;
                        }
                    }
                }
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    int userId         = -1;
                    SPList exportsList = null;
                    int listItemIdNum  = -1;

                    using (SPSite site = new SPSite(siteID))
                    {
                        using (SPWeb web = site.OpenWeb(webID))
                        {
                            if (web != null)
                            {
                                if (currentUser == null)
                                {
                                    //The current user shouldn't be null, it should have been resolved outside of this RunWithElevatedPrivileges delegate
                                    currentUser = web.CurrentUser;
                                }
                                if (currentUser != null)
                                {
                                    userId = currentUser.ID;

                                    exportsList = web.TryGetList(web.GetServerRelativeListUrlPrefix() + "GlymaExports"); //TODO get the name from a constant
                                    if (exportsList != null)
                                    {
                                        //the text payload will contain the properties serialized into a simple XML format
                                        ExportPropertiesDictionary serializableDict = new ExportPropertiesDictionary(exportProperties);
                                        string textPayload = serializableDict.ConvertToXml();

                                        Guid workItemId = Guid.NewGuid(); // Create a unique id for the work item and export job
                                        listItemIdNum   = CreateExportJobListEntry(exportsList, domainUid, rootMapUid, mapType, exportType, workItemId, textPayload, userId);

                                        // if the list item was created then create the export job, if it wasn't it was because an export
                                        // for this particular root map of this type was already scheduled (changing the properties doesn't have effect)
                                        if (listItemIdNum != -1)
                                        {
                                            site.AddWorkItem(workItemId,                                            //gWorkItemId - A Guid that identifies the work item
                                                             DateTime.Now.ToUniversalTime(),                        //schdDateTime - represents a time in universal time for when the work item should take place
                                                             GlymaExportWorkItemTimerJob.WorkItemTypeId,            //gWorkItemType - this must be the GUID used in the GlymaExportWorkItemTimerJob
                                                             web.ID,                                                //gWebId - The identifier of the web containing the list
                                                             exportsList.ID,                                        //gParentId - The list ID
                                                             listItemIdNum,                                         //nItemId - The list item ID number
                                                             true,                                                  //fSetWebId - true to set the Web identifier
                                                             exportsList.Items.GetItemById(listItemIdNum).UniqueId, //gItemGuid - The unique identifier of the list item
                                                             domainUid,                                             //gBatchId - A Guid context identifier for the work item engine
                                                             userId,                                                //nUserId - SPUser ID number
                                                             null,                                                  //rgbBinaryPayload - not used
                                                             textPayload,                                           //strTextPayload
                                                             Guid.Empty);                                           //gProcessingId - needs to be Guid.Empty

                                            ExportJob scheduledJob = new ExportJob();
                                            scheduledJob.Created   = (DateTime)exportsList.Items.GetItemById(listItemIdNum)[SPBuiltInFieldId.Created];
                                            scheduledJob.CreatedBy = new GlymaUser()
                                            {
                                                Name = currentUser.Name
                                            };
                                            scheduledJob.Id                 = workItemId;
                                            scheduledJob.IsCurrent          = true;
                                            scheduledJob.Status             = ExportStatus.Scheduled;
                                            scheduledJob.Type               = exportType;
                                            scheduledJob.MapType            = mapType;
                                            scheduledJob.ExportProperties   = exportProperties;
                                            scheduledJob.PercentageComplete = 0;
                                            response.ExportJob              = scheduledJob;
                                        }
                                        else
                                        {
                                            //already scheduled so throw an exception to be handled with an error
                                            throw new Exception(string.Format("A scheduled export job already exists for the Glyma map: DomainUid: {0}, RootMapUid: {1}, Export Type: {2}, Map Type: {3}",
                                                                              domainUid.ToString(), rootMapUid.ToString(), exportType.ToString(), mapType.ToString()));
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception("Failed to find the Glyma Exports list.");
                                    }
                                }
                                else
                                {
                                    throw new Exception("The current user was not able to be determined.");
                                }
                            }
                            else
                            {
                                throw new Exception("The SPSite and/or the SPWeb were null.");
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExportError error = new ExportError()
                {
                    ErrorMessage = "Failed to create the Glyma map export job."
                };
                throw new FaultException <ExportError>(error, ex.ToString());
            }

            return(response);
        }