public static void MapFromDB(DBEntity from, ThisEntity to, bool includingItems = true) { to.Id = from.Id; to.RowId = from.RowId; to.ParentId = from.ParentEntityId; if (from.Entity != null) { to.ParentRowId = from.Entity.EntityUid; } to.ProfileName = from.Name; to.ContactType = from.ContactType; //to.ContactOption = from.ContactOption; string summary = "Contact Point "; if (string.IsNullOrWhiteSpace(to.ProfileName)) { if (!string.IsNullOrWhiteSpace(to.ContactType)) { to.ProfileName = to.ContactType; to.ContactType = ""; } } if (string.IsNullOrWhiteSpace(to.ProfileName)) { if (string.IsNullOrWhiteSpace(to.ProfileName) && from.Entity != null) { to.ProfileName = from.Entity.EntityBaseName ?? "Contact Point"; } else { to.ProfileName = summary; } } if (includingItems) { to.SocialMedia = Entity_ReferenceManager.GetAll(to.RowId, CodesManager.PROPERTY_CATEGORY_ORGANIZATION_SOCIAL_MEDIA); to.PhoneNumber = Entity_ReferenceManager.GetAll(to.RowId, CodesManager.PROPERTY_CATEGORY_PHONE_TYPE); to.Email = Entity_ReferenceManager.GetAll(to.RowId, CodesManager.PROPERTY_CATEGORY_EMAIL_TYPE); to.FaxNumber = Entity_ReferenceManager.GetAllToList(to.RowId, CodesManager.PROPERTY_CATEGORY_PHONE_TYPE_FAX); } if (IsValidDate(from.Created)) { to.Created = ( DateTime )from.Created; } if (IsValidDate(from.LastUpdated)) { to.LastUpdated = ( DateTime )from.LastUpdated; } }
public bool Delete(int recordId, ref string statusMessage) { bool isOK = true; using (var context = new EntityContext()) { DBEntity p = context.Entity_ContactPoint.FirstOrDefault(s => s.Id == recordId); if (p != null && p.Id > 0) { context.Entity_ContactPoint.Remove(p); int count = context.SaveChanges(); } else { statusMessage = string.Format("Contact Point record was not found: {0}", recordId); isOK = false; } } return(isOK); }
} // public static void MapToDB(ThisEntity from, DBEntity to) { //want to ensure fields from create are not wiped if (to.Id == 0) { if (IsValidDate(from.Created)) { to.Created = from.Created; } } to.Id = from.Id; to.Name = from.ProfileName; to.ContactType = from.ContactType; //to.ContactOption = from.ContactOption; //to.Email = from.Email; //to.Telephone = from.Telephone; //to.Fax = from.FaxNumber; //to.SocialMedia = from.SocialMedia; }
} // public static ThisEntity Get(int profileId) { ThisEntity entity = new ThisEntity(); try { using (var context = new EntityContext()) { DBEntity item = context.Entity_ContactPoint .SingleOrDefault(s => s.Id == profileId); if (item != null && item.Id > 0) { MapFromDB(item, entity, true); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".Get"); } return(entity); } //
/// <summary> /// Persist ContactPoint /// </summary> /// <param name="entity"></param> /// <param name="parentUid"></param> /// <param name="userId"></param> /// <param name="messages"></param> /// <returns></returns> public bool Save(ThisEntity entity, Guid parentUid, ref SaveStatus status) { bool isValid = true; int count = 0; DBEntity efEntity = new DBEntity(); if (!IsValidGuid(parentUid)) { status.AddWarning("Error: the parent identifier was not provided."); return(false); } Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddWarning("Error - the parent entity was not found."); return(false); } using (var context = new EntityContext()) { if (ValidateProfile(entity, ref status) == false) { return(false); } if (entity.Id == 0) { //add efEntity = new DBEntity(); MapToDB(entity, efEntity); efEntity.ParentEntityId = parent.Id; efEntity.Created = efEntity.LastUpdated = DateTime.Now; efEntity.RowId = Guid.NewGuid(); context.Entity_ContactPoint.Add(efEntity); count = context.SaveChanges(); //update profile record so doesn't get deleted entity.Id = efEntity.Id; entity.ParentId = parent.Id; entity.RowId = efEntity.RowId; if (count == 0) { status.AddWarning(string.Format(" Unable to add Contact Point: {0} <br\\> ", string.IsNullOrWhiteSpace(entity.ProfileName) ? "no description" : entity.ProfileName)); } else { UpdateParts(entity, ref status); } } else { entity.ParentId = parent.Id; efEntity = context.Entity_ContactPoint.SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { entity.RowId = efEntity.RowId; //update MapToDB(entity, efEntity); //has changed? if (HasStateChanged(context)) { efEntity.LastUpdated = System.DateTime.Now; count = context.SaveChanges(); } //always check parts UpdateParts(entity, ref status); } } } return(isValid); }