private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            if (GravityFormsAdapter.MainWindow.sqlLogID > 0)
            {
                try
                {
                    SQLServer.SaveErrorLog(GravityFormsAdapter.MainWindow.sqlLogID, e.Exception.Message);
                }
                catch
                {
                    // Ignore errors writing the error log
                }
            }

            // If in automatic mode we need to quit silently even if unsuccessful
            if (!GravityFormsAdapter.MainWindow.config.AutomaticMode)
            {
                var res = MessageBox.Show("Exception occurred: " + e.Exception.Message + "\n" + e.Exception.StackTrace + "\n\nIf this persists please contact support. Attempt to continue?", "Exception occurred", MessageBoxButton.YesNo, MessageBoxImage.Error);
                if (res == MessageBoxResult.Yes)
                {
                    e.Handled = true;
                    return;
                }
            }
        }
Beispiel #2
0
        private async Task Run()
        {
            if (config.CurrentEntryID < 1)
            {
                throw new Exception("The current entry ID is less than 1, but it starts at 1");
            }

            if (config.WriteSQLLogs)
            {
                sqlLogID = SQLServer.WriteNewLog();
            }

            dgdForms.ItemsSource = null;
            var formDetails = new Dictionary <string, GravityDataStructures.GravityForm>();

            var apiType = config.APIType;

            if (config.FetchForms)
            {
                progressBar.Minimum = 0;
                progressBar.Maximum = 100;
                progressBar.Value   = 0;
                lblStatus.Content   = "Filtering allowed form IDs";
                var validForms = config.FormIDs.Split(',').Where(f => f.Length > 0).Select(f => f.Trim()).ToList();

                progressBar.Value = 10;
                lblStatus.Content = "Fetching form headers";
                var formsJson = await GetJSON("forms", apiType);

                progressBar.Value = 20;
                lblStatus.Content = "Parsing form headers";
                var formHeaders = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, GravityDataStructures.GravityForm> >(formsJson);
                //var formHeaders= envelope.response;

                progressBar.Value = 25;
                lblStatus.Content = "Fetching form details";
                foreach (var f in formHeaders.Where(frm => validForms.Count == 0 || validForms.Contains(frm.Key)))
                {
                    var formDetailJson = await GetJSON("forms/" + f.Value.id, apiType);

                    lblStatus.Content = "Fetching form " + f.Value.id;
                    var formDetail = Newtonsoft.Json.JsonConvert.DeserializeObject <GravityDataStructures.GravityForm>(formDetailJson);

                    formDetails.Add(f.Key, formDetail);

                    dgdForms.ItemsSource = null;
                    dgdForms.ItemsSource = formDetails.Values.ToList();

                    progressBar.Value = (progressBar.Value + 5) % 100;
                }
            }


            lblStatus.Content = "Fetching entries, from " + config.CurrentEntryID;

            var newEntries = new List <GravityDataStructures.GravityEntry>();

            dgdEntries.ItemsSource = null;
            dgdEntries.ItemsSource = newEntries;
            var done = false;

            var    isSuccess    = true;
            string errorMessage = null;

            do
            {
                dgdEntries.ItemsSource = null;
                dgdEntries.ItemsSource = newEntries;

                progressBar.Value = (progressBar.Value + 5) % 100;

                try
                {
                    var entryJSON = await GetJSON("entries/" + config.CurrentEntryID.ToString(), apiType);

                    var entryDictionary = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(entryJSON);
                    if (entryDictionary.ContainsKey("form_id"))
                    {
                        var form_id = entryDictionary["form_id"]?.ToString() ?? "";

                        if (formDetails.ContainsKey(form_id))
                        {
                            newEntries.Add(new GravityDataStructures.GravityEntry(formDetails[form_id], entryDictionary));
                        }
                    }

                    config.CurrentEntryID++;

                    lblStatus.Content = "Fetching entries, from " + config.CurrentEntryID;
                }
                catch (PageNotFoundException ex)
                {
                    lblStatus.Content = " Entry not found at " + config.CurrentEntryID + ". Finished fetching.";
                    done = true;
                }
                catch (Exception ex)
                {
                    Log("Error retrieving new entry: " + ex.Message);
                    lblStatus.Content = "Error fetching: " + ex.Message;
                    isSuccess         = false;
                    errorMessage      = ex.Message;
                    done = true;
                }
            }while (!done);
            dgdEntries.ItemsSource = null;
            dgdEntries.ItemsSource = newEntries;

            progressBar.Value = 100;

            lblStatus.Content = "Fetch complete at " + config.CurrentEntryID + ". Fetched " + newEntries.Count + " new entries.";

            SaveEntriesToJSON(newEntries);

            if (config.FetchForms)
            {
                SaveFormsToJSON(formDetails.Values.ToList());
            }

            if (config.SaveToSQL)
            {
                SaveEntriesToSQL(newEntries);

                if (config.FetchForms)
                {
                    SaveFormsToSQL(formDetails.Values.ToList());
                }
            }

            if (config.WriteSQLLogs)
            {
                if (isSuccess)
                {
                    SQLServer.SaveSuccessLog(sqlLogID, newEntries.Count);
                }
                else
                {
                    SQLServer.SaveErrorLog(sqlLogID, errorMessage);
                }
            }

            // Save directly, then reload the config to the screen to refresh the current entry ID
            config.Save();

            LoadConfig();
        }