private bool IsValid(ThisEntity item, ref List <string> messages) { bool isValid = true; int initialCount = messages.Count; if (item.EntityTypeId == 0) { messages.Add("An entityTypeId is required"); } if (item.RecordId == 0) { messages.Add("An entity recordId is required"); } if (item.StatusId == 0) { messages.Add("A valid StatusId is required (1 = pending, 2 = complete)"); } if (item.IsUpdateOrDeleteTypeId < 1 || item.IsUpdateOrDeleteTypeId > 2) { messages.Add("A valid IsUpdateOrDeleteTypeId is required (1 = update, 2 = delete"); } if (messages.Count > initialCount) { isValid = false; } return(isValid); }
public static List <ThisEntity> GetAllPendingReindex(ref List <String> messages, int entityTypeId = 0) { List <ThisEntity> list = new List <ThisEntity>(); ThisEntity entity = new ThisEntity(); using (var context = new EntityContext()) { List <DBEntity> results = context.SearchPendingReindex .Where(s => s.IsUpdateOrDeleteTypeId == 1 && s.StatusId == 1 && (entityTypeId == 0 || s.EntityTypeId == entityTypeId) ) .OrderBy(s => s.EntityTypeId).ThenBy(s => s.Created) .ToList(); if (results != null && results.Count > 0) { foreach (var item in results) { entity = new ThisEntity(); MapFromDB(item, entity, true); list.Add(entity); } } } return(list); }
public static void MapToDB(ThisEntity fromEntity, DBEntity to) { //want to ensure fields from create are not wiped if (to.Id < 1) { if (IsValidDate(fromEntity.Created)) { to.Created = fromEntity.Created; } to.CreatedById = fromEntity.CreatedById; } to.Id = fromEntity.Id; //to.Name = fromEntity.Name; to.Description = fromEntity.Description; to.CredentialId = fromEntity.ParentId; if (IsValidDate(fromEntity.LastUpdated)) { to.LastUpdated = fromEntity.LastUpdated; } to.LastUpdatedById = fromEntity.LastUpdatedById; }
/// <summary> /// SearchPendingReindexes Add /// </summary> /// <param name="entity"></param> /// <param name="messages"></param> /// <returns></returns> private int Add(ThisEntity entity, ref List <String> messages) { DBEntity efEntity = new DBEntity(); if (!IsValid(entity, ref messages)) { return(0); } try { using (var context = new EntityContext()) { //check if a pending record exists DBEntity exists = context.SearchPendingReindex .FirstOrDefault(s => s.EntityTypeId == entity.EntityTypeId && s.RecordId == entity.RecordId && s.StatusId == 1); if (exists != null && exists.Id > 0) { //could ignore,or check for a change of request type of add or delete if (exists.IsUpdateOrDeleteTypeId == entity.IsUpdateOrDeleteTypeId) { return(exists.Id); } //otherwise do an update? Update(entity, ref messages); return(exists.Id); } MapToDB(entity, efEntity); efEntity.Created = System.DateTime.Now; efEntity.LastUpdated = System.DateTime.Now; context.SearchPendingReindex.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("Error - the profile was not saved. "); string message = string.Format(thisClassName + ".Add. Failed. The process appeared to not work, but was not an exception, so we have no message, or no clue. EntityTypeId: {0}, RecordId: {1}", entity.EntityTypeId, entity.RecordId); // EmailManager.NotifyAdmin( thisClassName + ". Add Failed", message ); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(). EntityTypeId: {0}, RecordId: {1}", entity.EntityTypeId, entity.RecordId)); } return(efEntity.Id); }
/// <summary> /// Update a ThisEntity /// - base only, caller will handle parts? /// </summary> /// <param name="entity"></param> /// <param name="statusMessage"></param> /// <returns></returns> public bool Update(ThisEntity entity, ref string statusMessage) { bool isValid = false; int count = 0; try { using (var context = new EntityContext()) { if (!IsValid(entity, ref messages)) { return(0); } DBEntity efEntity = context.Credential_ConnectionProfile .SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { //for updates, chances are some fields will not be in interface, don't map these (ie created stuff) MapToDB(entity, efEntity); if (HasStateChanged(context)) { efEntity.LastUpdated = System.DateTime.Now; efEntity.LastUpdatedById = entity.LastUpdatedById; count = context.SaveChanges(); //can be zero if no data changed if (count >= 0) { isValid = true; } else { //?no info on error statusMessage = "Error - the update was not successful. "; string message = string.Format(thisClassName + ".ConditionProfile_Update Failed", "Attempted to update a ThisEntity. The process appeared to not work, but was not an exception, so we have no message, or no clue. CredentialId: {0}, Id: {1}, updatedById: {2}", entity.ParentId, entity.Id, entity.LastUpdatedById); EmailManager.NotifyAdmin(thisClassName + ". ConditionProfile_Update Failed", message); } } //continue with parts regardless //opMgr.ConditionProfile_UpdateParts( entity, false, ref statusMessage ); } else { statusMessage = "Error - update failed, as record was not found."; } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".Update. id: {0}", entity.Id)); } return(isValid); }
/// <summary> /// Update a Record /// </summary> /// <param name="entity"></param> /// <param name="messages"></param> /// <returns></returns> public bool Update(ThisEntity entity, ref List <String> messages) { bool isValid = false; int count = 0; try { if (!IsValid(entity, ref messages)) { return(false); } using (var context = new EntityContext()) { DBEntity efEntity = context.SearchPendingReindex .FirstOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { //for updates, chances are some fields will not be in interface, don't map these (ie created stuff) MapToDB(entity, efEntity); if (HasStateChanged(context)) { efEntity.LastUpdated = System.DateTime.Now; count = context.SaveChanges(); //can be zero if no data changed if (count >= 0) { isValid = true; } else { //?no info on error messages.Add("Error - the update was not successful. "); string message = string.Format(thisClassName + ".Update Failed. The process appeared to not work, but was not an exception, so we have no message, or no clue. EntityTypeId: {0}, RecordId: {1}", entity.EntityTypeId, entity.RecordId); //EmailManager.NotifyAdmin( thisClassName + ". ConditionProfile_Update Failed", message ); } } } else { messages.Add("Error - update failed, as record was not found."); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".Update. EntityTypeId: {0}, RecordId: {1}", entity.EntityTypeId, entity.RecordId)); } return(isValid); }
public static void MapToDB(ThisEntity fromEntity, DBEntity to) { //want to ensure fields from create are not wiped if (to.Id < 1) { } to.Id = fromEntity.Id; to.EntityTypeId = fromEntity.EntityTypeId; to.RecordId = fromEntity.RecordId; to.StatusId = fromEntity.StatusId; to.IsUpdateOrDeleteTypeId = fromEntity.IsUpdateOrDeleteTypeId; }
public int AddDeleteRequest(int entityTypeId, int recordId, ref List <String> messages) { if (recordId < 1) { return(0); } ThisEntity entity = new ThisEntity() { EntityTypeId = entityTypeId, RecordId = recordId, IsUpdateOrDeleteTypeId = Reindex_Delete_Request, StatusId = 1 }; return(Add(entity, ref messages)); }
/// <summary> /// SearchPendingReindexes Add /// </summary> /// <param name="entityTypeId"></param> /// <param name="recordId"></param> /// <param name="actionTypeId">1-add; 2-delete</param> /// <param name="messages"></param> /// <returns></returns> public int Add(int entityTypeId, int recordId, int actionTypeId, ref List <String> messages) { if (recordId < 1) { return(0); } ThisEntity entity = new ThisEntity() { EntityTypeId = entityTypeId, RecordId = recordId, IsUpdateOrDeleteTypeId = actionTypeId, StatusId = 1 }; return(Add(entity, ref messages)); }
private bool IsValid(ThisEntity item, ref List <string> messages) { bool isValid = true; if (string.IsNullOrWhiteSpace(item.ProfileName)) { messages.Add("Error: missing profile name"); } if (messages.Count > 0) { isValid = false; } return(isValid); }
public static ThisEntity Get(int id, bool includeProperties = false) { ThisEntity entity = new ThisEntity(); using (var context = new EntityContext()) { DBEntity item = context.SearchPendingReindex .SingleOrDefault(s => s.Id == id); if (item != null && item.Id > 0) { MapFromDB(item, entity, true); } } return(entity); }
public static void MapFromDB(DBEntity fromEntity, ThisEntity to, bool includingProperties = false) { to.Id = fromEntity.Id; to.EntityTypeId = fromEntity.EntityTypeId; to.RecordId = fromEntity.RecordId; to.StatusId = fromEntity.StatusId; to.IsUpdateOrDeleteTypeId = (int)fromEntity.IsUpdateOrDeleteTypeId; if (IsValidDate(fromEntity.Created)) { to.Created = ( DateTime )fromEntity.Created; } if (IsValidDate(fromEntity.LastUpdated)) { to.LastUpdated = ( DateTime )fromEntity.LastUpdated; } }
public static void MapFromDB(DBEntity fromEntity, ThisEntity to, bool includingProperties = false) { to.Id = fromEntity.Id; to.RowId = fromEntity.RowId; to.ParentId = fromEntity.CredentialId; to.ProfileName = fromEntity.Name; to.Description = fromEntity.Description; //.... if (IsValidDate(fromEntity.Created)) { to.Created = ( DateTime )fromEntity.Created; } to.CreatedById = fromEntity.CreatedById == null ? 0 : ( int )fromEntity.CreatedById; if (IsValidDate(fromEntity.LastUpdated)) { to.LastUpdated = ( DateTime )fromEntity.LastUpdated; } to.LastUpdatedById = fromEntity.LastUpdatedById == null ? 0 : ( int )fromEntity.LastUpdatedById; }
public static ThisEntity Get(int id, bool includeProperties = false) { ThisEntity entity = new ThisEntity(); using (var context = new EntityContext()) { DBEntity item = context.Credential_ConnectionProfile .SingleOrDefault(s => s.Id == id); if (item != null && item.Id > 0) { MapFromDB(item, entity, true); if (includeProperties) { //TBD } } } return(entity); }
/// <summary> /// add a ThisEntity /// </summary> /// <param name="entity"></param> /// <param name="statusMessage"></param> /// <returns></returns> public int Add(ThisEntity entity, ref List <String> messages) { DBEntity efEntity = new DBEntity(); entity.ParentId = credential.Id; using (var context = new EntityContext()) { try { if (!IsValid(entity, ref messages)) { return(0); } MapToDB(entity, efEntity); efEntity.CredentialId = credential.Id; if (efEntity.RowId == null || efEntity.RowId.ToString() == DEFAULT_GUID) { efEntity.RowId = Guid.NewGuid(); } efEntity.CreatedById = credential.LastUpdatedById; efEntity.Created = System.DateTime.Now; efEntity.LastUpdatedById = credential.LastUpdatedById; efEntity.LastUpdated = System.DateTime.Now; context.Credential_ConnectionProfile.Add(efEntity); // submit the change to database int count = context.SaveChanges(); if (count > 0) { entity.Id = efEntity.Id; //opMgr.ConditionProfile_UpdateParts( entity, true, ref statusMessage ); return(efEntity.Id); } else { //?no info on error messages.Add("Error - the profile was not saved. "); string message = string.Format("ConditionProfileManager. ConditionProfile_Add Failed", "Attempted to add a ThisEntity. The process appeared to not work, but was not an exception, so we have no message, or no clue.ThisEntity. CredentialId: {0}, createdById: {1}", entity.ParentId, entity.CreatedById); EmailManager.NotifyAdmin(thisClassName + ". ConditionProfile_Add Failed", message); } } catch (System.Data.ThisEntity.Validation.DbEntityValidationException dbex) { //LoggingHelper.LogError( dbex, thisClassName + string.Format( ".ContentAdd() DbEntityValidationException, Type:{0}", entity.TypeId ) ); string message = thisClassName + string.Format(".ConditionProfile_Add() DbEntityValidationException, CredentialId: {0}", credential.Id); foreach (var eve in dbex.EntityValidationErrors) { message += string.Format("\rEntity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.ThisEntity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { message += string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } LoggingHelper.LogError(message, true); } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(), parentId: {0}", entity.ParentId)); } } return(efEntity.Id); }