Exemplo n.º 1
0
        private void SaveFormsToSQL(List <GravityDataStructures.GravityForm> forms)
        {
            if (forms.Count > 0)
            {
                lblStatus.Content = "Saving " + forms.Count + " new records to SQL.";
                progressBar.Value = 0;
                for (int i = 0; i < forms.Count; i++)
                {
                    SQLServer.SaveForms(forms[i]);
                    progressBar.Value = Math.Floor((((double)i) / ((double)forms.Count) * 100.0));
                }

                progressBar.Value = 100;
                lblStatus.Content = forms.Count + " new entries written out to SQL.";
            }
            else
            {
                lblStatus.Content = "No new entries to fetch.";
            }
        }
Exemplo n.º 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();
        }