public int Add(ThisEntity entity, ref List <string> messages) { DBEntity efEntity = new DBEntity(); using (var context = new EntityContext()) { try { //won't have ctid if encountered exception efEntity.Ctid = entity.Ctid ?? ""; efEntity.EntityTypedId = entity.EntityTypedId; efEntity.DocumentUpdatedAt = entity.DocumentUpdatedAt; efEntity.EnvelopeId = entity.EnvelopeId; //efEntity.Message = entity.Message; //See Import.Message efEntity.Payload = entity.Payload; //efEntity.ResourcePublicKey = entity.ResourcePublicKey; efEntity.DownloadDate = System.DateTime.Now; efEntity.IsMostRecentDownload = true; //set any existing downloads for this entity to not most recent ResetIsMostRecentDownload(efEntity.Ctid); context.Import_Staging.Add(efEntity); // submit the change to database int count = context.SaveChanges(); if (count > 0) { entity.Id = efEntity.Id; return(efEntity.Id); } else { //?no info on error messages.Add(thisClassName + "Error - the add was not successful. "); string message = string.Format(thisClassName + ".Add() Failed", "Attempted to add a Import document. The process appeared to not work, but was not an exception, so we have no message, or no clue. EntityTypeId: {0}; EnvelopeId: {1}", entity.EntityTypedId, entity.EnvelopeId); EmailManager.NotifyAdmin(thisClassName + ".Add() Failed", message); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "Import"); messages.Add("Error - the save was not successful. " + message); } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(), EntityTypeId: {0}; EnvelopeId: {1}", entity.EntityTypedId, entity.EnvelopeId)); messages.Add("Unexpected system error. The site administration has been notified."); } } return(entity.Id); } //
}// /// <summary> /// Get most recent import Record for ctid /// </summary> /// <param name="ctid"></param> /// <returns></returns> public static ThisEntity GetByCtid(string ctid) { ThisEntity entity = new ThisEntity(); if (string.IsNullOrWhiteSpace(ctid)) { return(null); } try { using (var context = new EntityContext()) { List <DBEntity> list = context.Import_Staging .Where(s => s.Ctid == ctid) .OrderByDescending(s => s.Id) .Take(1) .ToList(); if (list != null && list.Count > 0) { DBEntity dbentity = list[0]; entity = new ThisEntity { EnvelopeId = dbentity.EnvelopeId, Ctid = dbentity.Ctid, EntityTypedId = dbentity.EntityTypedId, Payload = dbentity.Payload, IsMostRecentDownload = dbentity.IsMostRecentDownload ?? false, DownloadDate = dbentity.DownloadDate }; if (dbentity.DocumentUpdatedAt != null) { entity.DocumentUpdatedAt = ( DateTime )dbentity.DocumentUpdatedAt; } } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".GetByCtid"); } return(entity); }//
/// <summary> /// get all to enable deleting /// NOTE: there can be many entries for an entity in Import staging. do a uniqueness check. /// </summary> /// <returns></returns> public static List <ThisEntity> GetAll(int entityTypeId = 0) { ThisEntity entity = new ThisEntity(); List <ThisEntity> list = new List <ThisEntity>(); try { string prevCTID = ""; using (var context = new EntityContext()) { List <DBEntity> search = context.Import_Staging .Where(s => (entityTypeId == 0 || s.EntityTypedId == entityTypeId)) .OrderBy(s => s.EntityTypedId) .ThenBy(x => x.EnvelopeId) .ThenByDescending(s => s.DownloadDate) .ToList(); if (search != null && search.Count > 0) { foreach (DBEntity item in search) { entity = new ThisEntity(); entity.EnvelopeId = item.EnvelopeId; entity.Ctid = item.Ctid; entity.EntityTypedId = item.EntityTypedId; if (prevCTID != entity.Ctid.ToLower()) { list.Add(entity); } prevCTID = entity.Ctid.ToLower(); } } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".GetAll"); } return(list); }//
/// <summary> /// Add envelope and related data to the staging table /// </summary> /// <param name="item"></param> /// <param name="entityTypeId"></param> /// <param name="ctid"></param> /// <param name="importSuccessfull"></param> /// <param name="importErrorMsg"></param> /// <param name="messages"></param> /// <returns></returns> public int Add(ReadEnvelope item, int entityTypeId, string ctid, bool importSuccessfull, string importErrorMsg, ref List <string> messages) { ThisEntity entity = new ThisEntity(); entity.EntityTypedId = entityTypeId; entity.EnvelopeId = item.EnvelopeIdentifier; entity.Ctid = ctid; entity.ResourcePublicKey = item.ResourcePublicKey; DateTime updateDate = new DateTime(); if (DateTime.TryParse(item.NodeHeaders.UpdatedAt.Replace("UTC", "").Trim(), out updateDate)) { entity.DocumentUpdatedAt = updateDate; } entity.Message = importErrorMsg; entity.Payload = item.DecodedResource.ToString(); return(new EntityMgr().Add(entity, ref messages)); }