/// <summary> /// Deletes a CMO message from the persistence storage medium. /// </summary> /// <param name="candidateID">The CFIS ID of the candidate recipient of the message to delete.</param> /// <param name="messageID">The ID of the message to delete.</param> /// <param name="version">The version of the message to update.</param> /// <returns>true if the CMO message instance was deleted successfully; otherwise, false.</returns> public bool Delete(string candidateID, int messageID, byte[] version) { using (Data.CmoEntities context = new Data.CmoEntities()) { var msg = context.CmoMessages.FirstOrDefault(m => m.CandidateId == candidateID && m.MessageId == messageID && m.Version == version && !m.PostDate.HasValue); if (msg != null) { // delete attachments first foreach (var a in msg.CmoAttachments) { if (!Delete(GetCmoAttachment(candidateID, messageID, a.AttachmentId))) { return(false); } } context.DeleteObject(msg); try { if (context.SaveChanges() > 0) { return(true); } } catch (OptimisticConcurrencyException) { } } } return(false); }
/// <summary> /// Updates a CMO category instance in the persistence storage medium by overwriting the existing record. /// </summary> /// <param name="category">The CMO category instance to update.</param> /// <returns>true if this instance was saved successfuly; otherwise, false.</returns> public bool Update(CmoCategory category) { if (category != null) { using (Data.CmoEntities context = new Data.CmoEntities()) { var match = context.CmoCategories.FirstOrDefault(c => c.CategoryId == category.ID); if (match != null) { match.CategoryName = category.Name; match.Description = category.Description; match.Hidden = category.Hidden; if (category.MessageTemplateTitle != null) { match.TemplateTitle = category.MessageTemplateTitle; } if (category.MessageTemplateBody != null) { match.TemplateBody = category.MessageTemplateBody; } match.TemplateEditable = category.MessageTemplateEditable; try { return(context.SaveChanges(SaveOptions.DetectChangesBeforeSave) > 0); } catch (OptimisticConcurrencyException) { } } } } return(false); }
/// <summary> /// Sets the tolling information for a tolling letter CMO message. /// </summary> /// <param name="message">The CMO message to update.</param> /// <returns>true if the tolling information was set or cleared successfully; otherwise, false.</returns> /// <remarks>The tolling information can only be successfully set if the message is a tolling letter and cleared if the message is not a tolling letter. The information will automatically be cleared for messages that are not tolling letters regardless of the message's tolling values.</remarks> private bool SetCmoMessageTolling(CmoMessage message) { if (message == null) { return(false); } string candidateID = message.CandidateID; int messageID = message.ID; TollingLetter letter = message.TollingLetter; if (message.IsInadequateResponseLetter) { // force interpretation of inadequate response letters as tolling letters if (letter == null) { letter = message.TollingLetter = GetTollingLetter(CPConvert.ToCfisCode(AuditReportType.IdrInadequateResponse), message.IsIdrAdditionalInadequateLetter || message.IsDarAdditionalInadequateLetter ? "ADDINA" : "INARES", "INAD"); } if (!message.TollingEventNumber.HasValue) { message.TollingEventNumber = int.MinValue; } } bool hasTolling = message.TollingEventNumber.HasValue && letter != null; bool isTolling = message.IsTollingLetter; using (Data.CmoEntities context = new Data.CmoEntities()) { var tl = context.CmoTollingLetters.FirstOrDefault(l => l.CandidateId == message.CandidateID && l.MessageId == message.ID); if (tl != null) { if (isTolling) { tl.EventNumber = message.TollingEventNumber ?? int.MinValue; } else { context.DeleteObject(tl); } } else if (isTolling) { context.AddToCmoTollingLetters(Data.CmoTollingLetter.CreateCmoTollingLetter(candidateID, messageID, message.TollingEventNumber ?? int.MinValue, letter.ID)); } try { int updates = context.SaveChanges(); return(isTolling ? updates > 0 : !hasTolling); } catch (OptimisticConcurrencyException) { return(false); } } }
/// <summary> /// Updates a CMO message attachment instance in the persistence storage medium by overwriting the existing record. /// </summary> /// <param name="attachment">The CMO message attachment to update.</param> /// <returns>true if this instance was saved successfuly; otherwise, false.</returns> public bool Update(CmoAttachment attachment) { using (Data.CmoEntities context = new Data.CmoEntities()) { var data = context.CmoAttachments.FirstOrDefault(a => a.CandidateId == attachment.CandidateID && a.MessageId == attachment.MessageID && a.AttachmentId == attachment.ID); if (data != null) { data.AttachmentName = attachment.FileName; try { return(context.SaveChanges() > 0); } catch (OptimisticConcurrencyException) { } } return(false); } }
/// <summary> /// Sets the post election audit request type for an audit review CMO message. /// </summary> /// <param name="message">The CMO message to update.</param> /// <returns>true if the type was set or cleared successfully; otherwise, false.</returns> /// <remarks>The post election audit request type can only be successfully set if the message is a post election audit request and cleared if the message is not a post election audit request. The type will automatically be cleared for messages that are not post election audit requests regardless of the value in <paramref name="message"/>.</remarks> private bool SetCmoMessagePostElectionAuditRequestType(CmoMessage message) { if (message == null) { return(false); } AuditRequestType?type = message.PostElectionAuditRequestType; bool hasRequestType = type.HasValue; bool isPea = message.IsPostElectionAudit && hasRequestType; bool isRepost = hasRequestType && (type == AuditRequestType.FirstRepost || type == AuditRequestType.SecondRepost); bool isSecond = hasRequestType && (type == AuditRequestType.SecondRepost || type == AuditRequestType.SecondRequest); using (Data.CmoEntities context = new Data.CmoEntities()) { var request = context.CmoPostElectionRequests.FirstOrDefault(r => r.CandidateId == message.CandidateID && r.MessageId == message.ID); if (request != null) { // update or delete existing row if (isPea) { request.Repost = isRepost; request.SecondRequest = isSecond; } else { // always delete row if not a request, but return true only if there is no longer a request type context.DeleteObject(request); } } else if (isPea) { // no post election audit request entries were found, so try to add a new one if applicable context.AddToCmoPostElectionRequests(Data.CmoPostElectionRequest.CreateCmoPostElectionRequest(message.CandidateID, message.ID, isRepost, isSecond)); } try { int updates = context.SaveChanges(); return(isPea ? updates > 0 : !hasRequestType); } catch (OptimisticConcurrencyException) { return(false); } } }
/// <summary> /// Sets the audit review number for an audit review CMO message. /// </summary> /// <param name="message">The CMO message to update.</param> /// <returns>true if the number was set or cleared successfully; otherwise, false.</returns> /// <remarks>The audit review number can only be successfully set if the message is an audit review and cleared if the message is not an audit review. The number will automatically be cleared for messages that are not audit reviews regardless of the message's review number value.</remarks> private bool SetCmoMessageAuditReviewNumber(CmoMessage message) { if (message == null) { return(false); } bool hasNumber = message.AuditReviewNumber.HasValue; bool isReview = message.IsAuditReview && hasNumber; using (Data.CmoEntities context = new Data.CmoEntities()) { var review = context.CmoAuditReviews.FirstOrDefault(r => r.CandidateId == message.CandidateID && r.MessageId == message.ID); if (review != null) { // update or delete existing row if (isReview) { review.ReviewNumber = message.AuditReviewNumber.Value; } else { // always delete row if not audit review, but return true only if there is no longer a review number context.DeleteObject(review); } } else if (isReview) { // no audit review entries were found, so try to add a new one if applicable context.AddToCmoAuditReviews(Data.CmoAuditReview.CreateCmoAuditReview(message.CandidateID, message.ID, message.AuditReviewNumber.Value)); } try { int updates = context.SaveChanges(); return(isReview ? updates > 0 : !hasNumber); } catch (OptimisticConcurrencyException) { return(false); } } }
/// <summary> /// Deletes a CMO message attachment instance from the persistence storage medium. /// </summary> /// <param name="attachment">The CMO message attachment to delete.</param> /// <returns>true if this instance was deleted successfully; otherwise, false.</returns> public bool Delete(CmoAttachment attachment) { if (attachment != null) { using (Data.CmoEntities context = new Data.CmoEntities()) { var data = context.CmoAttachments.FirstOrDefault(a => a.CandidateId == attachment.CandidateID && a.MessageId == attachment.MessageID && a.AttachmentId == attachment.ID); if (data != null) { context.DeleteObject(data); try { return(CmoRepository.Repository != null && context.SaveChanges() > 0 && CmoRepository.Repository.Delete(attachment)); } catch (OptimisticConcurrencyException) { return(false); } } } } return(true); }