/// <summary> /// Handles the SaveClick event of the mdRunNcoa control. This is the Start NCOA dialog. Save = Start /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void mdRunNcoa_SaveClick(object sender, EventArgs e) { Ncoa ncoa = new Ncoa(); var sparkDataConfig = Ncoa.GetSettings(); sparkDataConfig.NcoaSettings.PersonFullName = CurrentPerson != null ? CurrentPerson.FullName : null; sparkDataConfig.NcoaSettings.CurrentReportStatus = "Start"; Ncoa.SaveSettings(sparkDataConfig); using (RockContext rockContext = new RockContext()) { ServiceJob job = new ServiceJobService(rockContext).Get(Rock.SystemGuid.ServiceJob.GET_NCOA.AsGuid()); if (job != null) { var transaction = new Rock.Transactions.RunJobNowTransaction(job.Id); // Process the transaction on another thread System.Threading.Tasks.Task.Run(() => transaction.Execute()); mdGridWarning.Show(string.Format("The '{0}' job has been started.", job.Name), ModalAlertType.Information); lbStartNcoa.Enabled = false; } } mdRunNcoa.Hide(); }
/// <summary> /// Handles the CheckedChanged event of the cbNcoaAckPrice control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void cbNcoaAckPrice_CheckedChanged(object sender, EventArgs e) { // Update Spark Data settings _sparkDataConfig = Ncoa.GetSettings(); _sparkDataConfig.NcoaSettings.IsAckPrice = cbNcoaAckPrice.Checked; Rock.Web.SystemSettings.SetValue(SystemSetting.SPARK_DATA, _sparkDataConfig.ToJson()); // Update if Run Manually button is enabled SetStartNcoaEnabled(); }
/// <summary> /// Handles the Click event of the btnSaveEdit control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnSaveEdit_Click(object sender, EventArgs e) { // Get Spark Data _sparkDataConfig = Ncoa.GetSettings(); _sparkDataConfig.GlobalNotificationApplicationGroupId = grpNotificationGroupEdit.GroupId; _sparkDataConfig.SparkDataApiKey = txtSparkDataApiKeyEdit.Text; Rock.Web.SystemSettings.SetValue(SystemSetting.SPARK_DATA, _sparkDataConfig.ToJson()); GetSettings(); }
/// <summary> /// Sets the Run Manually start NCOA button enabled state. /// </summary> private void SetStartNcoaEnabled() { if (_sparkDataConfig == null) { _sparkDataConfig = Ncoa.GetSettings(); } if (_sparkDataConfig.NcoaSettings.CurrentReportStatus.Contains("Pending")) { lbStartNcoa.Enabled = false; } else { lbStartNcoa.Enabled = cbNcoaAcceptTerms.Checked && cbNcoaAckPrice.Checked && cbNcoaConfiguration.Checked && !bbtnNcoaSaveConfig.Enabled && _sparkDataConfig.NcoaSettings.IsValid(); } }
/// <summary> /// Handles the CheckedChanged event when enabling/disabling the NCOA option. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void cbNcoaConfiguration_CheckedChanged(object sender, EventArgs e) { _sparkDataConfig = Ncoa.GetSettings(); _sparkDataConfig.NcoaSettings.IsEnabled = cbNcoaConfiguration.Checked; Rock.Web.SystemSettings.SetValue(SystemSetting.SPARK_DATA, _sparkDataConfig.ToJson()); // Save job active status using (var rockContext = new RockContext()) { var ncoaJob = new ServiceJobService(rockContext).Get(Rock.SystemGuid.ServiceJob.GET_NCOA.AsGuid()); if (ncoaJob != null) { ncoaJob.IsActive = cbNcoaConfiguration.Checked; rockContext.SaveChanges(); } } SetPanels(); }
/// <summary> /// Saves the settings. /// </summary> private void SaveSettings() { // NCOA Configuration Rock.Web.SystemSettings.SetValue(SystemSetting.NCOA_MINIMUM_MOVE_DISTANCE_TO_INACTIVATE, nbNcoaMinMoveDistance.Text); Rock.Web.SystemSettings.SetValue(SystemSetting.NCOA_SET_48_MONTH_AS_PREVIOUS, cbNcoa48MonAsPrevious.Checked.ToString()); Rock.Web.SystemSettings.SetValue(SystemSetting.NCOA_SET_INVALID_AS_PREVIOUS, cbNcoaInvalidAddressAsPrevious.Checked.ToString()); // Get Spark Data _sparkDataConfig = Ncoa.GetSettings(); _sparkDataConfig.NcoaSettings.PersonDataViewId = dvpNcoaPersonDataView.SelectedValue.AsIntegerOrNull(); _sparkDataConfig.NcoaSettings.RecurringEnabled = cbNcoaRecurringEnabled.Checked; _sparkDataConfig.NcoaSettings.RecurrenceInterval = nbNcoaRecurrenceInterval.Text.AsInteger(); _sparkDataConfig.NcoaSettings.IsEnabled = cbNcoaConfiguration.Checked; _sparkDataConfig.NcoaSettings.IsAckPrice = cbNcoaAckPrice.Checked; _sparkDataConfig.NcoaSettings.IsAcceptedTerms = cbNcoaAcceptTerms.Checked; _sparkDataConfig.NcoaSettings.InactiveRecordReasonId = dvpNcoaInactiveRecordReason.SelectedValueAsId(); Rock.Web.SystemSettings.SetValue(SystemSetting.SPARK_DATA, _sparkDataConfig.ToJson()); bbtnNcoaSaveConfig.Enabled = false; SetStartNcoaEnabled(); }
/// <summary> /// Handles the SaveClick event of the mdRunNcoa control. This is the Start NCOA dialog. Save = Start /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void mdRunNcoa_SaveClick(object sender, EventArgs e) { Ncoa ncoa = new Ncoa(); var sparkDataConfig = Ncoa.GetSettings(); sparkDataConfig.NcoaSettings.PersonFullName = CurrentPerson != null ? CurrentPerson.FullName : null; sparkDataConfig.NcoaSettings.CurrentReportStatus = "Start"; Ncoa.SaveSettings(sparkDataConfig); using (RockContext rockContext = new RockContext()) { ServiceJob job = new ServiceJobService(rockContext).Get(Rock.SystemGuid.ServiceJob.GET_NCOA.AsGuid()); if (job != null) { new ProcessRunJobNow.Message { JobId = job.Id }.Send(); mdGridWarning.Show(string.Format("The '{0}' job has been started.", job.Name), ModalAlertType.Information); lbStartNcoa.Enabled = false; } } mdRunNcoa.Hide(); }
/// <summary> /// Gets the settings. /// </summary> private void GetSettings() { // Get Spark Data settings _sparkDataConfig = Ncoa.GetSettings(); if (_sparkDataConfig.SparkDataApiKey.IsNullOrWhiteSpace()) { pnlSparkDataEdit.Visible = false; pnlSignIn.Visible = true; pnlAccountStatus.Visible = false; pwNcoaConfiguration.Visible = false; bbtnNcoaSaveConfig.Visible = false; txtSparkDataApiKeyLogin.Text = _sparkDataConfig.SparkDataApiKey; grpNotificationGroupLogin.GroupId = _sparkDataConfig.GlobalNotificationApplicationGroupId; } else { pnlSparkDataEdit.Visible = false; pnlSignIn.Visible = false; pnlAccountStatus.Visible = true; pwNcoaConfiguration.Visible = true; bbtnNcoaSaveConfig.Visible = true; // Get NCOA configuration settings nbNcoaMinMoveDistance.Text = Rock.Web.SystemSettings.GetValue(SystemSetting.NCOA_MINIMUM_MOVE_DISTANCE_TO_INACTIVATE); cbNcoa48MonAsPrevious.Checked = Rock.Web.SystemSettings.GetValue(SystemSetting.NCOA_SET_48_MONTH_AS_PREVIOUS).AsBoolean(); cbNcoaInvalidAddressAsPrevious.Checked = Rock.Web.SystemSettings.GetValue(SystemSetting.NCOA_SET_INVALID_AS_PREVIOUS).AsBoolean(); txtSparkDataApiKeyEdit.Text = _sparkDataConfig.SparkDataApiKey; grpNotificationGroupEdit.GroupId = _sparkDataConfig.GlobalNotificationApplicationGroupId; // Get NCOA settings if (_sparkDataConfig.NcoaSettings == null) { _sparkDataConfig.NcoaSettings = new NcoaSettings(); } dvpNcoaPersonDataView.SetValue(_sparkDataConfig.NcoaSettings.PersonDataViewId); cbNcoaRecurringEnabled.Checked = _sparkDataConfig.NcoaSettings.RecurringEnabled; nbNcoaRecurrenceInterval.Enabled = _sparkDataConfig.NcoaSettings.RecurringEnabled; nbNcoaRecurrenceInterval.Text = _sparkDataConfig.NcoaSettings.RecurrenceInterval.ToStringSafe(); cbNcoaAcceptTerms.Checked = _sparkDataConfig.NcoaSettings.IsAcceptedTerms; cbNcoaAckPrice.Checked = _sparkDataConfig.NcoaSettings.IsAckPrice; if (_sparkDataConfig.NcoaSettings.InactiveRecordReasonId.HasValue) { dvpNcoaInactiveRecordReason.SetValue(_sparkDataConfig.NcoaSettings.InactiveRecordReasonId.Value); } nbNcoaCreditCard.Visible = false; if (_sparkDataConfig.NcoaSettings.CurrentReportStatus == null) { _sparkDataConfig.NcoaSettings.CurrentReportStatus = string.Empty; } if (_sparkDataConfig.SparkDataApiKey.IsNullOrWhiteSpace()) { pnlSignIn.Visible = true; pnlSparkDataEdit.Visible = false; } else { pnlSignIn.Visible = false; bool accountValid = false; SparkDataApi sparkDataApi = new SparkDataApi(); try { var accountStatus = sparkDataApi.CheckAccount(_sparkDataConfig.SparkDataApiKey); switch (accountStatus) { case SparkDataApi.AccountStatus.AccountNoName: hlAccountStatus.LabelType = LabelType.Warning; hlAccountStatus.Text = "Account does not have a name"; break; case SparkDataApi.AccountStatus.AccountNotFound: hlAccountStatus.LabelType = LabelType.Warning; hlAccountStatus.Text = "Account not found"; break; case SparkDataApi.AccountStatus.Disabled: hlAccountStatus.LabelType = LabelType.Warning; hlAccountStatus.Text = "Disabled"; break; case SparkDataApi.AccountStatus.EnabledCardExpired: hlAccountStatus.LabelType = LabelType.Danger; hlAccountStatus.Text = "Enabled - Card expired"; break; case SparkDataApi.AccountStatus.EnabledNoCard: hlAccountStatus.LabelType = LabelType.Warning; hlAccountStatus.Text = "Enabled - No card on file"; nbNcoaCreditCard.Visible = true; break; case SparkDataApi.AccountStatus.EnabledCard: hlAccountStatus.LabelType = LabelType.Success; hlAccountStatus.Text = "Enabled - Card on file"; accountValid = true; break; case SparkDataApi.AccountStatus.InvalidSparkDataKey: hlAccountStatus.LabelType = LabelType.Warning; hlAccountStatus.Text = "Invalid Spark Data Key"; break; case SparkDataApi.AccountStatus.EnabledCardNoExpirationDate: hlAccountStatus.LabelType = LabelType.Warning; hlAccountStatus.Text = "Enabled - Card expiration date not on file"; break; } string cost = sparkDataApi.GetPrice("CF20766E-80F9-E282-432F-6A9E19F0BFF1"); cbNcoaAckPrice.Text = cbNcoaAckPrice.Text.Replace("$xx", "$" + cost); } catch { hlAccountStatus.LabelType = LabelType.Danger; hlAccountStatus.Text = "Error connecting to Spark server"; } cbNcoaConfiguration.Checked = _sparkDataConfig.NcoaSettings.IsEnabled && accountValid; cbNcoaConfiguration.Enabled = accountValid; SetStartNcoaEnabled(); SetPanels(); } } }
/// <summary> /// Job to get a National Change of Address (NCOA) report for all active people's addresses. /// /// Called by the <see cref="IScheduler" /> when a /// <see cref="ITrigger" /> fires that is associated with /// the <see cref="IJob" />. /// </summary> public virtual void Execute(IJobExecutionContext context) { Exception exception = null; // Get the job setting(s) JobDataMap dataMap = context.JobDetail.JobDataMap; SparkDataConfig sparkDataConfig = Ncoa.GetSettings(); if (!sparkDataConfig.NcoaSettings.IsEnabled || !sparkDataConfig.NcoaSettings.IsValid()) { return; } try { Guid?sparkDataApiKeyGuid = sparkDataConfig.SparkDataApiKey.AsGuidOrNull(); if (sparkDataApiKeyGuid == null) { exception = new Exception($"Spark Data Api Key '{sparkDataConfig.SparkDataApiKey.ToStringSafe()}' is empty or invalid. The Spark Data Api Key can be configured in System Settings > Spark Data Settings."); return; } switch (sparkDataConfig.NcoaSettings.CurrentReportStatus) { case "": case null: if (sparkDataConfig.NcoaSettings.RecurringEnabled) { StatusStart(sparkDataConfig); } break; case "Start": StatusStart(sparkDataConfig); break; case "Failed": StatusFailed(sparkDataConfig); break; case "Pending: Report": StatusPendingReport(sparkDataConfig); break; case "Pending: Export": StatusPendingExport(sparkDataConfig); break; case "Complete": StatusComplete(sparkDataConfig); break; } } catch (Exception ex) { exception = ex; } finally { if (exception != null) { context.Result = $"NCOA Job failed: {exception.Message}"; sparkDataConfig.NcoaSettings.CurrentReportStatus = "Failed"; sparkDataConfig.Messages.Add($"NOCA job failed: {RockDateTime.Now.ToString()} - {exception.Message}"); Ncoa.SaveSettings(sparkDataConfig); try { var ncoa = new Ncoa(); ncoa.SentNotification(sparkDataConfig, "failed"); } catch { } if (sparkDataConfig.SparkDataApiKey.IsNotNullOrWhiteSpace() && sparkDataConfig.NcoaSettings.FileName.IsNotNullOrWhiteSpace()) { SparkDataApi sparkDataApi = new SparkDataApi(); } Exception ex = new AggregateException("NCOA job failed.", exception); HttpContext context2 = HttpContext.Current; ExceptionLogService.LogException(ex, context2); throw ex; } else { string msg; if (sparkDataConfig.NcoaSettings.CurrentReportStatus == "Complete") { using (RockContext rockContext = new RockContext()) { NcoaHistoryService ncoaHistoryService = new NcoaHistoryService(rockContext); msg = $"NCOA request processed, {ncoaHistoryService.Count()} {(ncoaHistoryService.Count() == 1 ? "address" : "addresses")} processed, {ncoaHistoryService.MovedCount()} {(ncoaHistoryService.MovedCount() > 1 ? "were" : "was")} marked as 'moved'"; } } else { msg = $"Job complete. NCOA status: {sparkDataConfig.NcoaSettings.CurrentReportStatus}"; } context.Result = msg; sparkDataConfig.Messages.Add($"{msg}: {RockDateTime.Now.ToString()}"); Ncoa.SaveSettings(sparkDataConfig); } } }