public bool SaveList(List <ThisEntity> list, Credential credential, ref SaveStatus status) { if (credential == null || credential.Id < 1) { status.AddError("Error: the credential identifier was not provided."); return(false); } DBEntity efEntity = new DBEntity(); Entity parent = EntityManager.GetEntity(credential.RowId); if (parent == null || parent.Id == 0) { status.AddError("Error - the parent entity was not found."); return(false); } DeleteAll(parent, ref status); if (list == null || list.Count == 0) { return(true); } status.HasSectionErrors = false; foreach (ThisEntity item in list) { Save(item, parent, ref status); } return(status.WasSectionValid); } //
public static void MapFromDB(DBEntity from, ThisEntity to, bool includingItems = true) { to.Id = from.Id; to.RowId = from.RowId; to.ParentId = from.EntityId; to.Description = from.Description; to.RevocationCriteriaDescription = from.RevocationCriteriaDescription; to.RevocationCriteriaUrl = from.RevocationCriteriaUrl; if (IsValidDate(from.DateEffective)) { to.DateEffective = (( DateTime )from.DateEffective).ToString("yyyy-MM-dd"); } else { to.DateEffective = ""; } to.RevocationCriteriaUrl = from.RevocationCriteriaUrl; if ((from.Entity.EntityBaseName ?? "").Length > 3) { to.ParentSummary = from.Entity.EntityBaseName; } //not used: to.ProfileSummary = SetEntitySummary(to); //no longer using name, but need for the editor list to.ProfileName = to.ProfileSummary; if (IsValidDate(from.Created)) { to.Created = ( DateTime )from.Created; } if (IsValidDate(from.LastUpdated)) { to.LastUpdated = ( DateTime )from.LastUpdated; } if (includingItems) { to.CredentialProfiled = Entity_CredentialManager.GetAll(to.RowId); // to.Jurisdiction = Entity_JurisdictionProfileManager.Jurisdiction_GetAll(to.RowId); to.Region = Entity_JurisdictionProfileManager.Jurisdiction_GetAll(to.RowId, Entity_JurisdictionProfileManager.JURISDICTION_PURPOSE_RESIDENT); } }
} // public static void MapToDB(ThisEntity from, DBEntity to) { //want to ensure fields from create are not wiped to.Id = from.Id; to.ProfileName = from.ProfileName; to.Description = from.Description; to.RevocationCriteriaUrl = from.RevocationCriteriaUrl; to.RevocationCriteriaDescription = from.RevocationCriteriaDescription; if (IsValidDate(from.DateEffective)) { to.DateEffective = DateTime.Parse(from.DateEffective); } else { to.DateEffective = null; } }
} // public static ThisEntity Get(int profileId) { ThisEntity entity = new ThisEntity(); try { using (var context = new EntityContext()) { DBEntity item = context.Entity_RevocationProfile .SingleOrDefault(s => s.Id == profileId); if (item != null && item.Id > 0) { MapFromDB(item, entity); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".Get"); } return(entity); } //
} // /// <summary> /// Persist Revocation Profiles /// </summary> /// <param name="entity"></param> /// <param name="parent"></param> /// <param name="messages"></param> /// <returns></returns> private bool Save(ThisEntity entity, Entity parent, ref SaveStatus status) { bool isValid = true; DBEntity efEntity = new DBEntity(); //Entity parent = EntityManager.GetEntity( credential.RowId ); if (parent == null || parent.Id == 0) { status.AddError("Error - the parent entity was not found."); return(false); } using (var context = new EntityContext()) { bool isEmpty = false; int profNbr = 0; profNbr++; if (ValidateProfile(entity, ref isEmpty, ref status) == false) { return(false); } if (isEmpty) //skip { status.AddWarning("Revocation Profile was empty. "); return(false); } if (entity.Id == 0) { //add efEntity = new DBEntity(); MapToDB(entity, efEntity); efEntity.EntityId = parent.Id; efEntity.Created = efEntity.LastUpdated = DateTime.Now; if (IsValidGuid(entity.RowId)) { efEntity.RowId = entity.RowId; } else { efEntity.RowId = Guid.NewGuid(); } context.Entity_RevocationProfile.Add(efEntity); int 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.AddError(string.Format(" Unable to add Profile: {0} ", string.IsNullOrWhiteSpace(entity.ProfileName) ? "no description" : entity.ProfileName)); } } else { entity.ParentId = parent.Id; efEntity = context.Entity_RevocationProfile.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; int count = context.SaveChanges(); } } } } return(isValid); }