/// <summary>
    /// Schedules and downloads a report.
    /// </summary>
    /// <param name="service">The report service instance.</param>
    /// <param name="queryId">The query id to be used for generating reports.
    /// </param>
    /// <param name="reportFilePath">The file path to which the downloaded report
    /// should be saved.</param>
    private bool ScheduleAndDownloadReport(ReportRemoteService service, long queryId,
        string reportFilePath) {
      // Create report request and submit it to the server.
      ReportRequest reportRequest = new ReportRequest();
      reportRequest.queryId = queryId;

      ReportInfo reportInfo = service.runDeferredReport(reportRequest);
      long reportId = reportInfo.reportId;
      reportRequest.reportId = reportId;

      while (reportInfo.status.name != "COMPLETE") {
        Thread.Sleep(TIME_BETWEEN_CHECKS);
        reportInfo = service.getReport(reportRequest);
        if (reportInfo.status.name == "ERROR") {
          throw new Exception("Deferred report failed with errors. Run in the UI to " +
              "troubleshoot.");
        }
      }

      FileStream fs = null;
      try {
        fs = File.OpenWrite(reportFilePath);
        byte[] data = MediaUtilities.GetAssetDataFromUrl(reportInfo.url);
        fs.Write(data, 0, data.Length);
        return true;
      } catch {
        return false;
      } finally {
        if (fs != null) {
          fs.Close();
        }
      }
    }
    /// <summary>
    /// Schedules and downloads a report.
    /// </summary>
    /// <param name="loginService">The login service instance.</param>
    /// <param name="reportService">The report service instance.</param>
    /// <param name="userName">The user name to be used for authentication
    /// purposes.</param>
    /// <param name="password">The password to be used for authentication
    /// purposes.</param>
    /// <param name="queryId">The query id to be used for generating reports.
    /// </param>
    /// <param name="filePath">The file path to which the downloaded report
    /// should be saved.</param>
    public void ScheduleAndDownloadReport(LoginRemoteService loginService,
        ReportRemoteService reportService, string userName, string password, long queryId,
        string filePath) {

      // Override the credentials in App.config with the ones the user
      // provided.
      string authToken = loginService.authenticate(userName, password).token;

      reportService.Token.Username = userName;
      reportService.Token.Password = authToken;

      // Create report request and submit it to the server.
      ReportRequest reportRequest = new ReportRequest();
      reportRequest.queryId = queryId;

      try {
        ReportInfo reportInfo = reportService.runDeferredReport(reportRequest);
        long reportId = reportInfo.reportId;
        Console.WriteLine("Report with ID '{0}' has been scheduled.", reportId);

        reportRequest.reportId = reportId;

        while (reportInfo.status.name != "COMPLETE") {
          Console.WriteLine("Still waiting for report with ID '{0}', current status is '{1}'.",
              reportId, reportInfo.status.name);
          Console.WriteLine("Waiting 10 minutes before checking on report status.");
          // Wait 10 minutes.
          Thread.Sleep(TIME_BETWEEN_CHECKS);
          reportInfo = reportService.getReport(reportRequest);
          if (reportInfo.status.name == "ERROR") {
            throw new Exception("Deferred report failed with errors. Run in the UI to " +
                "troubleshoot.");
          }
        }

        using (FileStream fs = File.OpenWrite(filePath)) {
          byte[] bytes = MediaUtilities.GetAssetDataFromUrl(reportInfo.url);
          fs.Write(bytes, 0, bytes.Length);
        }

        Console.WriteLine("Report successfully downloaded to '{0}'.", filePath);
      } catch (Exception ex) {
        Console.WriteLine("Failed to schedule and download report. Exception says \"{0}\"",
            ex.Message);
      }
    }