public static void RunBatchPowerFaidsProcess() { Guid processGuid = Guid.NewGuid(); using (var dbContext = new CampusLogicContext()) { // Get all records to process var records = dbContext.PowerFaidsRecords.Where(p => p.ProcessGuid == null); if (records.Any()) { // Lock to prevent from being processed again dbContext.Database.ExecuteSqlCommand($"update [dbo].[PowerFaidsRecord] set [ProcessGuid] = '{processGuid}' from [dbo].[PowerFaidsRecord] where [Id] in (select [Id] from [dbo].[PowerFaidsRecord] where [ProcessGuid] is NULL)"); // Get the records to process var recordsToProcess = dbContext.PowerFaidsRecords.Where(p => p.ProcessGuid == processGuid).Select(p => p).ToList(); var powerFaidsList = new List <PowerFaidsDto>(); foreach (var record in recordsToProcess) { try { PowerFaidsDto powerFaidsRecord = JsonConvert.DeserializeObject <PowerFaidsDto>(record.Json); powerFaidsList.Add(powerFaidsRecord); } catch (Exception) { dynamic data = JObject.Parse(record.Json); NotificationService.ErrorNotification("PowerFAIDS", $"Error occurred while processing PowerFAIDS record. EventId: {data.EventId}"); // Delete the bad record dbContext.Database.ExecuteSqlCommand($"DELETE FROM [dbo].[PowerFaidsRecord] WHERE [Id] = {record.Id}"); } } try { ProcessPowerFaidsRecords(powerFaidsList); } catch (Exception) { NotificationService.ErrorNotification("PowerFAIDS", $"Error occurred while generating XML. Affected events: {string.Join(", ", powerFaidsList.Select(x => x.EventId).ToList())}"); } // Delete the records to never be processed again! dbContext.Database.ExecuteSqlCommand($"DELETE FROM [dbo].[PowerFaidsRecord] WHERE [ProcessGuid] = '{processGuid}'"); } } }
/// <summary> /// Handles PowerFAIDS integration. /// Will either queue up the eventData to be written to a file later /// or save the single record as an XML file. /// </summary> /// <param name="eventData"></param> private static void PowerFaidsHandler(EventNotificationData eventData) { try { // Get the settings the user has defined for PowerFAIDS var generalSettings = campusLogicConfigSection.PowerFaidsSettings; var awardYearToken = eventData.PropertyValues[EventPropertyConstants.AwardYear].Value <string>(); if (!string.IsNullOrEmpty(awardYearToken)) { awardYearToken = awardYearToken.Split('-')[0]; } var eventSettings = campusLogicConfigSection.PowerFaidsSettings.PowerFaidsSettingCollectionConfig.GetPowerFaidsSettingList(); var eventSetting = eventSettings .FirstOrDefault(e => !eventData.PropertyValues[EventPropertyConstants.SvTransactionCategoryId].IsNullOrEmpty() && eventData.PropertyValues[EventPropertyConstants.SvTransactionCategoryId].Value <string>() == e.TransactionCategory && e.Event == eventData.PropertyValues[EventPropertyConstants.EventNotificationId].Value <string>()); if (eventSetting != null) { var recordToProcess = new PowerFaidsDto() { EventId = eventData.PropertyValues[EventPropertyConstants.Id].Value <string>(), AwardYearToken = awardYearToken, AlternateId = eventData.PropertyValues[EventPropertyConstants.StudentId].Value <string>(), FilePath = generalSettings.FilePath, Outcome = eventSetting.Outcome, ShortName = eventSetting.ShortName, RequiredFor = eventSetting.RequiredFor, Status = eventSetting.Status, EffectiveDate = eventData.PropertyValues[EventPropertyConstants.DateTimeUtc].Value <DateTime>().ToString("yyyy-MM-dd"), DocumentLock = eventSetting.DocumentLock, VerificationOutcome = eventSetting.VerificationOutcome, VerificationOutcomeLock = eventSetting.VerificationOutcomeLock }; if (generalSettings.IsBatch.Value == false) { try { PowerFaidsService.ProcessPowerFaidsRecords(new List <PowerFaidsDto>() { recordToProcess }); } catch (Exception) { throw new Exception($"Error occurred while processing PowerFAIDS record. EventId: {recordToProcess.EventId}"); } } else { var json = JsonConvert.SerializeObject(recordToProcess).Replace("'", "''"); using (var dbContext = new CampusLogicContext()) { // Insert the record into the PowerFaidsRecord table so that it can be processed by the Automated PowerFAIDS job. dbContext.Database.ExecuteSqlCommand($"INSERT INTO [dbo].[PowerFaidsRecord]([Json], [ProcessGuid]) VALUES('{json}', NULL)"); } } } else { throw new Exception($"Error occured while processing PowerFAIDS record. Cannot find mapping for Event Notification: {eventData.PropertyValues[EventPropertyConstants.EventNotificationId].Value<string>()} and Transaction Category: {eventData.PropertyValues[EventPropertyConstants.SvTransactionCategoryId].Value<string>()}"); } } catch (Exception e) { NotificationService.ErrorNotification("PowerFAIDS", e); throw; } }