public ActionResult Create(Message message, HttpPostedFileBase file, HttpPostedFileBase txtFile) { if (file == null) { ModelState.AddModelError(string.Empty, "Please provide an HTML file path"); } if (txtFile == null) { ModelState.AddModelError(string.Empty, "Please provide a Text file path"); } if (ModelState.IsValid) { SaveBlob(message.MessageRef + ".htm", file); SaveBlob(message.MessageRef + ".txt", txtFile); var insertOperation = TableOperation.Insert(message); messageTable.Execute(insertOperation); return RedirectToAction("Index"); } var lists = GetListNames(); ViewBag.ListName = new SelectList(lists, "ListName", "Description"); return View(message); }
// // GET: /Message/Create public ActionResult Create() { var lists = GetListNames(); ViewBag.ListName = new SelectList(lists, "ListName", "Description"); var message = new Message() { ScheduledDate = DateTime.Today.AddDays(7) }; return View(message); }
public async Task<ActionResult> Edit(string partitionKey, string rowKey, Message editedMsg, DateTime scheduledDate, HttpPostedFileBase httpFile, HttpPostedFileBase txtFile) { if (ModelState.IsValid) { var excludePropLst = new List<string>(); excludePropLst.Add("PartitionKey"); excludePropLst.Add("RowKey"); if (httpFile == null) { // They didn't enter a path or navigate to a file, so don't update the file. excludePropLst.Add("HtmlPath"); } else { // They DID enter a path or navigate to a file, assume it's changed. await SaveBlobAsync(editedMsg.MessageRef + ".htm", httpFile); } if (txtFile == null) { excludePropLst.Add("TextPath"); } else { await SaveBlobAsync(editedMsg.MessageRef + ".txt", txtFile); } string[] excludeProperties = excludePropLst.ToArray(); try { UpdateModel(editedMsg, string.Empty, null, excludeProperties); if (editedMsg.PartitionKey == partitionKey) { // Keys didn't change -- update the row. var replaceOperation = TableOperation.Replace(editedMsg); await messageTable.ExecuteAsync(replaceOperation); } else { // Partition key changed -- delete and insert the row. // (Partition key has scheduled date which may be changed; // row key has MessageRef which does not change.) var deleteOperation = TableOperation.Delete(new Message { PartitionKey = partitionKey, RowKey = rowKey, ETag = editedMsg.ETag }); await messageTable.ExecuteAsync(deleteOperation); var insertOperation = TableOperation.Insert(editedMsg); await messageTable.ExecuteAsync(insertOperation); } return RedirectToAction("Index"); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 412) { // Concurrency error var currentMsg = FindRow(partitionKey, rowKey); if (currentMsg.Status != "Pending") { throw new Exception("Message can't be edited because it isn't in Pending status."); } if (currentMsg.ListName != editedMsg.ListName) { ModelState.AddModelError("ListName", "Current value: " + currentMsg.ListName); } if (currentMsg.SubjectLine != editedMsg.SubjectLine) { ModelState.AddModelError("SubjectLine", "Current value: " + currentMsg.SubjectLine); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); ModelState.SetModelValue("ETag", new ValueProviderResult(currentMsg.ETag, currentMsg.ETag, null)); } else { throw; } } } var lists = await GetListNamesAsync(); ViewBag.ListName = new SelectList(lists, "ListName", "Description", editedMsg.ListName); return View(editedMsg); }
private void CheckAndArchiveIfComplete(Message messageToCheck) { // Get the list of emails to be sent for this message: all SendEmail rows // for this message. string pkrkFilter = TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, messageToCheck.PartitionKey), TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, "message")); var query = new TableQuery<SendEmail>().Where(pkrkFilter); var emailToBeSent = messageTable.ExecuteQuery(query).FirstOrDefault(); if (emailToBeSent != null) { return; } // All emails have been sent; copy the message row to the archive table. // Insert the message row in the messagearchive table var messageToDelete = new Message { PartitionKey = messageToCheck.PartitionKey, RowKey = messageToCheck.RowKey, ETag = "*" }; messageToCheck.Status = "Complete"; var insertOrReplaceOperation = TableOperation.InsertOrReplace(messageToCheck); messagearchiveTable.Execute(insertOrReplaceOperation); // Delete the message row from the message table. var deleteOperation = TableOperation.Delete(messageToDelete); messageTable.Execute(deleteOperation); }
private void ProcessMessage(Message messageToProcess, string restartFlag) { // Get Mailing List info to get the "From" email address. var retrieveOperation = TableOperation.Retrieve<MailingList>(messageToProcess.ListName, "mailinglist"); var retrievedResult = mailingListTable.Execute(retrieveOperation); var mailingList = retrievedResult.Result as MailingList; if (mailingList == null) { Trace.TraceError("Mailing list not found: " + messageToProcess.ListName + " for message: " + messageToProcess.MessageRef); return; } // Get email addresses for this Mailing List. string filter = TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, messageToProcess.ListName), TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.NotEqual, "mailinglist")); var query = new TableQuery<Subscriber>().Where(filter); var subscribers = mailingListTable.ExecuteQuery(query).ToList(); foreach (Subscriber subscriber in subscribers) { // Verify that the subscriber email address has been verified. if (subscriber.Verified == false) { Trace.TraceInformation("Subscriber " + subscriber.EmailAddress + " not Verified, so not queuing "); continue; } // Create a SendEmail entity for this email. var sendEmailRow = new SendEmail { PartitionKey = messageToProcess.PartitionKey, RowKey = messageToProcess.MessageRef.ToString() + subscriber.EmailAddress, EmailAddress = subscriber.EmailAddress, EmailSent = false, MessageRef = messageToProcess.MessageRef, ScheduledDate = messageToProcess.ScheduledDate, FromEmailAddress = mailingList.FromEmailAddress, SubjectLine = messageToProcess.SubjectLine, SubscriberGUID = subscriber.SubscriberGUID, ListName = mailingList.ListName }; // When we try to add the entity to the SendEmail table, // an exception might happen if this worker role went // down after processing some of the email addresses and then restarted. // In that case the row might already be present, so we do an Upsert operation. try { var upsertOperation = TableOperation.InsertOrReplace(sendEmailRow); messageTable.Execute(upsertOperation); } catch (Exception ex) { string err = "Error creating SendEmail row: " + ex.Message; if (ex.InnerException != null) { err += " Inner Exception: " + ex.InnerException; } Trace.TraceError(err); } // Create the queue message. string queueMessageString = sendEmailRow.PartitionKey + "," + sendEmailRow.RowKey + "," + restartFlag; var queueMessage = new CloudQueueMessage(queueMessageString); sendEmailQueue.AddMessage(queueMessage); } Trace.TraceInformation("ProcessMessage end PK: " + messageToProcess.PartitionKey); }