Пример #1
0
        /// <summary>
        /// Appends the log.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        protected string AppendLog(string message, bool FetchNextBatch = false)
        {
            if (LogFile == null)
            {
                return(message);
            }

            if (LogFile.Any(s => s.StartsWith(string.Format(API_RECORDS_PROCESS_MESSAGE, _activeState))) && message.StartsWith(string.Format(API_RECORDS_PROCESS_MESSAGE, _activeState)))
            {
                var index = new List <string>(LogFile).FindIndex(FindIndex);

                if (index > -1)
                {
                    LogFile[index] = message + ((FetchNextBatch) ? " Please Stand by fetching next batch..." : null);
                }
            }
            else
            {
                LogFile.Add(message);
            }

            return(message);
        }
Пример #2
0
        /// <summary>
        /// Imports the file asynchronous.
        /// </summary>
        /// <param name="ct">The ct.</param>
        /// <returns></returns>
        async Task <int> ImportFileAsync(CancellationToken ct)
        {
            var uiContext = SynchronizationContext.Current;


            return(await Task.Run(() =>
            {
                var import = new PhysiciansImporter(); // Create importer and set ValueChanged Event.
                try
                {
                    //DisableTableIndexes(_tableNames);
                    if (LogFile != null && LogFile.Any())
                    {
                        uiContext.Send(x => LogFile.Clear(), null);
                        uiContext.Send(x => AppendLog(""), null);
                    }

                    var obj = new object();

                    import.FetchingBatchChanged += (o, e) =>
                    {
                        lock (obj)
                        {
                            _activeState = import.CurrentState;
                            uiContext.Send(x => AppendLog(string.Format("{2} {0:0,0} of {1:0,0}", import.ImportedPhysicianCount,
                                                                        import.TotalPhysicians, string.Format(API_RECORDS_PROCESS_MESSAGE, import.CurrentState)), import.FetchingBatch), null);
                        }
                    };

                    import.ValueChanged += (o, e) =>
                    {
                        if (import.ImportedPhysicianCount == 0 ||
                            import.ImportedPhysicianCount > import.TotalPhysicians)
                        {
                            return;
                        }

                        lock (obj)
                        {
                            uiContext.Send(x => AppendLog(string.Format("{2} {0:0,0} of {1:0,0}", import.ImportedPhysicianCount,
                                                                        import.TotalPhysicians, string.Format(API_RECORDS_PROCESS_MESSAGE, import.CurrentState))), null);
                        }
                    };

                    lock (obj)
                    {
                        uiContext.Send(x => AppendLog(string.Format("--- Getting Physician Data...")), null);
                    }

                    var stopWatch = new Stopwatch();
                    foreach (var state in DataContextObject.SelectedStates.ToList()) // Iterate through user selected states
                    {
                        if (ct != null && ct.IsCancellationRequested)
                        {
                            break;
                        }


                        _activeState = state;

                        stopWatch.Reset();
                        stopWatch.Start();

                        var displayState = state;
                        lock (obj)
                        {
                            uiContext.Send(x => AppendLog(string.Format("Calling API to fetch {0} state physicians", displayState)), null);
                        }
                        //import.ImportPhysicians(state); // Import per state
                        import.PhysicianBulkImportTargetName = DataContextObject.TargetType.EntityTableName();
                        import.DatasetId = DataContextObject.DatasetItem.Id;

                        // remove false parameter to run asynchronously.
                        import.ImportPhysiciansTest3(state, ct, false);

                        if (import.HasErrors)
                        {
                            uiContext.Send(x => AppendLog("Error occurred. Continuing on."), null);
                            continue;
                        }
                        import.FetchingBatch = false;
                        uiContext.Send(x => AppendLog(string.Format("{2} {0} of {1}", import.ImportedPhysicianCount,
                                                                    import.TotalPhysicians, string.Format(API_RECORDS_PROCESS_MESSAGE, displayState))), null);
                        stopWatch.Stop();

                        lock (obj)
                        {
                            uiContext.Send(x => AppendLog(string.Format("--- Total # of Physicans records saved: {0:0,0} ", import.TotalPhysiciansSaved)), null);
                            uiContext.Send(x => AppendLog(string.Format("--- Total # of Medical Practices records saved: {0:0,0}", import.TotalMedicalPracticesSaved)), null);
                            uiContext.Send(x => AppendLog(string.Format("--- Total execution time in seconds: {0}", Math.Round(TimeSpan.FromMilliseconds(stopWatch.ElapsedMilliseconds).TotalSeconds, 0))), null);
                        }
                    }

                    import.DeleteTempDirectory();

                    stopWatch = null;
                }
                catch (Exception exc)
                {
                    _errorsOccurred = true;
                    var excToUse = exc.GetBaseException();
                    if (exc is AggregateException)
                    {
                        excToUse = (exc as AggregateException).GetBaseException();
                    }
                    Logger.Write(excToUse.GetBaseException());

                    if (excToUse is OperationCanceledException)
                    {
                        _isUserCancelled = true;
                        return 0;
                    }

                    EventAggregator.GetEvent <ErrorNotificationEvent>().Publish(excToUse.GetBaseException());
                    return 0;
                }
                finally
                {
                    //EnableTableIndexes(_tableNames);
                    //TruncateTable(DataContextObject.TargetType.EntityTableName());

                    if (_isUserCancelled)
                    {
                        uiContext.Send(x => AppendLog(string.Format("User cancelled physician API import process for State(s): {0}", string.Join(",", DataContextObject.SelectedStates))), null);
                    }
                    else
                    {
                        if (!_errorsOccurred)
                        {
                            uiContext.Send(x => AppendLog(string.Format("Finished Downloading {0} API records.", import.TotalCombinedPhysicians)), null);
                        }
                        else
                        {
                            uiContext.Send(x => AppendLog(string.Format("An error occurred while process physician API records for State(s): {0}", string.Join(",", DataContextObject.SelectedStates))), null);
                        }
                    }

                    LogFile?.ToList().ForEach(log => Logger.Information(log));
                }
                return 1;
            }, ct));
        }