/// <summary>
        /// Flush reporing information out to the 'import run' entity.
        /// </summary>
        public void Flush( )
        {
            // Reorder messages so that they're in ascending row order
            // (Batching causes messages to appear all over the place)
            if (_failReasons != null)
            {
                RowNumberComparer comparer = new RowNumberComparer( );
                _failReasons.Sort(comparer);

                // Move messages into string builder.
                foreach (string message in _failReasons)
                {
                    _message.AppendLine(message);
                }
                _failReasons.Clear( );
            }

            using (new SecurityBypassContext( ))
            {
                _importRun.ImportRecordsSucceeded = _recordsSucceeded;
                _importRun.ImportRecordsFailed    = _recordsFailed;
                if (_messageChanged)
                {
                    _importRun.ImportMessages = _message.ToString( );
                }
                _importRun.Save( );
            }
        }
Example #2
0
        /// <summary>
        ///     Creates an importRun entity - does not save it.
        /// </summary>
        /// <param name="config">The import configuration.</param>
        /// <param name="message"></param>
        /// <returns>Returns the ID of the import run.</returns>
        private void CreateFailedImportRunEntity(ScheduledImportConfig config, string message)
        {
            var importConfig = config.SicImportConfig;

            // Create a new import run
            ImportRun importRun = Entity.Create <ImportRun>();

            importRun.ImportFileName       = config.SicUrl ?? string.Empty;
            importRun.ImportRunStatus_Enum = WorkflowRunState_Enumeration.WorkflowRunFailed;
            importRun.ImportConfigUsed     = importConfig;
            importRun.ImportMessages       = message;
            importRun.ImportRunStarted     = DateTime.UtcNow;
            importRun.ImportRunFinished    = DateTime.UtcNow;

            importRun.Save();
        }
        /// <summary>
        ///     Cancel Import operation.
        /// </summary>
        /// <param name="importRunId">Import run ID.</param>
        /// <returns>Returns the import status info.</returns>
        public ImportResultInfo CancelImportOperation(long importRunId)
        {
            ImportRun importRun = GetImportRun(importRunId).AsWritable <ImportRun>( );

            // Check status - only cancel if still running
            ImportStatus importStatus = ImportHelpers.GetImportStatus(importRun);

            if (importStatus == ImportStatus.InProgress)
            {
                // Cancel
                importRun.ImportRunStatus_Enum = WorkflowRunState_Enumeration.WorkflowRunCancelled;
                importRun.Save( );
            }

            ImportResultInfo result = GetImportResultInfo(importRun);

            return(result);
        }
Example #4
0
        /// <summary>
        /// Start processing an import run.
        /// </summary>
        /// <param name="importRunId">ID of the import run to process.</param>
        public void StartImport(long importRunId)
        {
            using (new SecurityBypassContext( ))
            {
                ImportRun importRun = _entityRepository.Get <ImportRun>(importRunId);
                if (importRun == null)
                {
                    throw new ArgumentException(nameof(importRunId));
                }

                importRun = importRun.AsWritable <ImportRun>( );

                // Verify that import run is ready to run.
                if (importRun.ImportRunStatus_Enum != WorkflowRunState_Enumeration.WorkflowRunQueued)
                {
                    throw new Exception("Import run is not marked as queued, or has already been started.");
                }

                // Mark it as started
                importRun.ImportRunStatus_Enum = WorkflowRunState_Enumeration.WorkflowRunStarted;
                importRun.ImportRunStarted     = DateTime.UtcNow;
                importRun.Save( );

                // Trap errors
                try
                {
                    string verb = "imported";
                    if (importRun.ImportTestRun == true)
                    {
                        verb = "verified";
                        importRun.ImportMessages += "This is a test import. No records are being saved.\r\n";
                    }

                    StartImportSafe(importRun);

                    if (importRun.ImportRecordsTotal == 0 || importRun.ImportRecordsTotal == null)
                    {
                        throw new Exception($"No records were found to be {verb}.");
                    }
                    if (importRun.ImportRecordsSucceeded == 0)
                    {
                        throw new Exception($"No records were successfully {verb}.");
                    }

                    // Mark run as completed
                    if (importRun.ImportRunStatus_Enum != WorkflowRunState_Enumeration.WorkflowRunCancelled)
                    {
                        importRun.ImportRunStatus_Enum = WorkflowRunState_Enumeration.WorkflowRunCompleted;
                    }
                }
                catch (Exception ex)
                {
                    importRun.ImportRunStatus_Enum = WorkflowRunState_Enumeration.WorkflowRunFailed;
                    importRun.ImportMessages       = "Failed: " + ex.Message + "\r\n" + importRun.ImportMessages; // TODO : Something better than this
                }
                finally
                {
                    importRun.ImportRunFinished = DateTime.UtcNow;
                    importRun.Save( );
                }
            }
        }
Example #5
0
        /// <summary>
        /// Start processing an import run, after checking has been performed.
        /// </summary>
        /// <param name="importRun">A writable import run entity. Caller will save.</param>
        internal void StartImportSafe(ImportRun importRun)
        {
            ImportConfig importConfig = importRun.ImportConfigUsed;

            if (importConfig == null)
            {
                throw new ConnectorConfigException("Import configuration could not be loaded.");
            }
            if (importConfig.ImportConfigMapping == null)
            {
                throw new ConnectorConfigException("Import configuration has no mapping.");
            }

            IObjectsReader         recordsReader = null;
            IReaderToEntityAdapter entityAdapter;
            IImportReporter        importRunReporter;
            IRecordImporter        recordImporter;
            ICancellationWatcher   cancellationWatcher;
            BatchRunner            batchRunner;

            try
            {
                // Reads records out of the file to count
                recordsReader = GetRecordsReader(importRun, importConfig);
                importRun.ImportRecordsTotal = recordsReader.GetObjects( ).Count( );
                importRun.Save( );

                // Re-reads records out of the file for importing
                recordsReader = GetRecordsReader(importRun, importConfig);

                // Writes records into entities
                entityAdapter = GetEntityAdapter(importConfig);

                // Create a reporter to process progress notifications
                importRunReporter = new ImportRunReporter(importRun);

                // Create a reporter to process progress notifications
                cancellationWatcher = new ImportRunCancellationWatcher(importRun);

                // Activate record impoter
                bool testRun = importRun.ImportTestRun == true;
                recordImporter = _recordImporterActivator(entityAdapter, importRunReporter, importConfig.ImportConfigMapping, testRun);
                if (recordImporter == null)
                {
                    throw new Exception("recordImporter failed to activate.");
                }

                // Connects the reader to the writer
                batchRunner = new BatchRunner
                {
                    ObjectsReader       = recordsReader,
                    RecordImporter      = recordImporter,
                    CancellationWatcher = cancellationWatcher
                };

                // Go! Run as user
                using (new SecurityBypassContext(false))
                {
                    batchRunner.ProcessAll( );
                }
            }
            finally
            {
                recordsReader?.Dispose( );
            }
        }