/// <summary> /// Recursively tries to get a movements page from the MP API. /// </summary> public static SearchPage <Movement> GetMovementsPage(Int32 offset, Int32 limit, OAuthResponse authorization, DateTime dateFrom, DateTime dateTo, int retryNumber = 0) { AccountsHelper ah = new AccountsHelper(); // Set access token ah.AccessToken = GetAccessToken(authorization); // Prepare API call arguments List <KeyValuePair <string, string> > args = new List <KeyValuePair <string, string> >(); if (authorization.IsAdmin) { args.Add(new KeyValuePair <string, string>("user_id", authorization.UserId.ToString())); } // Try optimize the query by site id, taking advantage of the search sharding if (authorization.SiteId != null) { args.Add(new KeyValuePair <string, string>("site_id", authorization.SiteId)); } args.Add(new KeyValuePair <string, string>("sort", "date_created")); args.Add(new KeyValuePair <string, string>("criteria", "desc")); args.Add(new KeyValuePair <string, string>("offset", offset.ToString())); args.Add(new KeyValuePair <string, string>("limit", limit.ToString())); args.Add(new KeyValuePair <string, string>("range", "date_created")); args.Add(new KeyValuePair <string, string>("begin_date", HttpUtility.UrlEncode(dateFrom.GetDateTimeFormats('s')[0].ToString() + ".000Z"))); args.Add(new KeyValuePair <string, string>("end_date", HttpUtility.UrlEncode(dateTo.GetDateTimeFormats('s')[0].ToString() + ".000Z"))); // Call API SearchPage <Movement> searchPage = null; try { searchPage = ah.SearchMovements(args); } catch (RESTAPIException raex) { // Retries the same call until max is reached if (retryNumber <= MAX_RETRIES) { LogHelper.WriteLine("SearchMovements breaks. Retry num: " + retryNumber.ToString()); BackendHelper.GetMovementsPage(offset, limit, authorization, dateFrom, dateTo, retryNumber + 1); } else { // then breaks throw raex; } } if (searchPage != null) { return(searchPage); } else { LogHelper.WriteLine("null"); return(null); } }
/// <summary> /// Generate button working thread. /// </summary> private void DoWork() { int apiQueryPageSize = 0; List <Collection> collections = null; SearchPage <Collection> collectionsPage = null; string exceptionMessage = String.Empty; System.IO.StreamWriter file = null; string fileExtension = ""; List <Movement> movements = null; SearchPage <Movement> movementsPage = null; int offset = 0; ReportFormats reportFormat; ReportTypes reportType; // Define file extension and report format if (excelRadioButton.Checked) { reportFormat = ReportFormats.ExcelReport; fileExtension = ".xml"; } else { reportFormat = ReportFormats.CSVReport; fileExtension = ".csv"; } // Set API query page size if (_authorization.IsAdmin) { apiQueryPageSize = ADMIN_API_QUERY_PAGE_SIZE; } else { apiQueryPageSize = USER_API_QUERY_PAGE_SIZE; } // TODO: Simplify this, same logic twice! try { // Do the first API search progressBar.Invoke(new DoLoadingCallback(this.DoLoading), null); if (collectionsRadioButton.Checked) { reportType = ReportTypes.CollectionsReport; collectionsPage = BackendHelper.GetCollectionsPage(offset, apiQueryPageSize, _authorization, reportFromPicker.Value, reportToPicker.Value); collections = collectionsPage.Results; progressBar.Invoke(new UpdateProgressBarMaximumCallback(this.UpdateProgressBarMaximum), new object[] { collectionsPage.Total.Value }); } else { reportType = ReportTypes.MovementsReport; movementsPage = BackendHelper.GetMovementsPage(offset, apiQueryPageSize, _authorization, reportFromPicker.Value, reportToPicker.Value); movements = movementsPage.Results; progressBar.Invoke(new UpdateProgressBarMaximumCallback(this.UpdateProgressBarMaximum), new object[] { movementsPage.Total.Value }); } offset += apiQueryPageSize; progressBar.Invoke(new StopLoadingCallback(this.StopLoading), null); // Check for Excel limitation if ((reportFormat == ReportFormats.ExcelReport) && (progressBar.Maximum > 60000)) { MessageBox.Show("The number of rows exceeds Excel report size. Choose CSV instead."); progressBar.Invoke(new EnableGenerateControlsCallback(this.EnableGenerateControls), null); return; } // Set file name string fileName = folderTextBox.Text; if (fileName.Substring(fileName.Length - 1) != "\\") { fileName += "\\"; } fileName += fileNameTextBox.Text + fileExtension; // Open file file = new System.IO.StreamWriter(fileName); // Set Report Writter ReportWritter reportWritter = ReportWritterFactory.GetReportWritter(file, reportFormat); reportWritter.ProgressBar = progressBar; reportWritter.ProgressText = progressLabel; // Write report header reportWritter.WriteHeader(reportType, progressBar.Maximum); if (collectionsRadioButton.Checked) { // Write first page reportWritter.WriteCollections(collections); // loop for more collections pages while (collectionsPage.Total.Value > offset && _continue) { // api call progressBar.Invoke(new DoLoadingCallback(this.DoLoading), null); collectionsPage = BackendHelper.GetCollectionsPage(offset, apiQueryPageSize, _authorization, reportFromPicker.Value, reportToPicker.Value); collections = collectionsPage.Results; progressBar.Invoke(new StopLoadingCallback(this.StopLoading), null); if (collectionsPage.Total.Value != progressBar.Maximum) // validate total rows consistency { throw new RESTAPIException(500, "Internal Server Error", "Max rows violation", "Max rows violation: current-" + collectionsPage.Total.Value.ToString() + ", previous-" + progressBar.Maximum.ToString()); } // write to file reportWritter.WriteCollections(collections); offset += apiQueryPageSize; } } else { // Write first page reportWritter.WriteMovements(movements); // loop for more movements pages while (movementsPage.Total.Value > offset && _continue) { // api call progressBar.Invoke(new DoLoadingCallback(this.DoLoading), null); movementsPage = BackendHelper.GetMovementsPage(offset, apiQueryPageSize, _authorization, reportFromPicker.Value, reportToPicker.Value); movements = movementsPage.Results; progressBar.Invoke(new StopLoadingCallback(this.StopLoading), null); if (movementsPage.Total.Value != progressBar.Maximum) // validate total rows consistency { throw new RESTAPIException(500, "Internal Server Error", "Max rows violation", "Max rows violation: current-" + movementsPage.Total.Value.ToString() + ", previous-" + progressBar.Maximum.ToString()); } // write to file reportWritter.WriteMovements(movements); offset += apiQueryPageSize; } } // Write report footer and close the file reportWritter.WriteFooter(); file.Close(); } catch (RESTAPIException raex) { exceptionMessage = "MP API Exception: " + raex.Cause; } catch (IOException ex) { exceptionMessage = "File exception: " + ex.Message; } catch (Exception e) { exceptionMessage = "Internal Exception: " + e.Message; } finally { // if error if (exceptionMessage != String.Empty) { // try closing the file try { file.Close(); } catch { } // Stop loading cursor progressBar.Invoke(new StopLoadingCallback(this.StopLoading), null); // Show error message MessageBox.Show(exceptionMessage); // Enable generate buttons progressBar.Invoke(new EnableGenerateControlsCallback(this.EnableGenerateControls), null); } } // Finish progressBar.Invoke(new UpdateProgressBarFinishedCallback(this.UpdateProgressBarFinished), null); }