public static void MapFromDB(EM.ConceptScheme_Concept from, Concept to) { to.Id = from.Id; to.RowId = from.RowId; to.PrefLabel = from.PrefLabel; to.CTID = from.CTID.ToLower(); to.Definition = from.Definition; to.Note = from.Note; to.IsTopConcept = from.IsTopConcept ?? true; // if (IsValidDate(from.Created)) { to.Created = ( DateTime )from.Created; } to.CreatedById = from.CreatedById; if (IsValidDate(from.LastUpdated)) { to.LastUpdated = ( DateTime )from.LastUpdated; } to.LastUpdatedById = from.LastUpdatedById; //TODO //to.CreatedBy = SetLastActionBy( to.CreatedById, from.AccountCreatedBy ); //to.LastUpdatedBy = SetLastActionBy( to.LastUpdatedById, from.AccountLastUpdatedBy ); }
/// <summary> /// TBD-may just store the concepts as Json on concept scheme /// </summary> /// <param name="entity"></param> /// <param name="userId"></param> /// <param name="messages"></param> /// <returns></returns> public bool HandleConceptsSimple(ThisEntity entity, ref SaveStatus status) { bool isValid = true; //first phase: delete all or do a replace var efEntity = new EM.ConceptScheme_Concept(); if (!entity.HasConcepts.Any()) { status.AddError("HandleConceptsSimple - a list of concepts/progression level mames are required for this method."); return(false); } using (var context = new EntityContext()) { try { var existing = context.ConceptScheme_Concept.Where(s => s.ConceptSchemeId == entity.Id).ToList(); var incomingCTIDs = entity.HasConcepts.Select(x => x.CTID).ToList(); //delete records which are not selected var notExisting = existing.Where(x => !incomingCTIDs.Contains(x.CTID)).ToList(); foreach (var item in notExisting) { context.ConceptScheme_Concept.Remove(item); context.SaveChanges(); } //only get profiles where not existing var existingCTIDs = existing.Select(x => x.CTID).ToList(); var newConcepts = entity.HasConcepts.Where(y => !existingCTIDs.Contains(y.CTID)).ToList(); var existingConcepts = entity.HasConcepts.Where(y => existingCTIDs.Contains(y.CTID)).ToList(); // if (existing != null && existing.Count() > 0 && entity.HasConcepts.Count() > 0) { //LoggingHelper.DoTrace( 6, string.Format( thisClassName + ".Replace. Existing: {0}, input: {1}, Not existing(to delete): {2}, newProfiles: {3}", existing.Count(), entity.HasConcepts.Count(), notExisting.Count(), newProfiles.Count() ) ); if (existing.Count() != entity.HasConcepts.Count()) { } } // //**need to handle updates foreach (var item in existingConcepts) { var updateConcept = entity.HasConcepts.FirstOrDefault(s => s.CTID == item.CTID); item.PrefLabel = updateConcept.PrefLabel; item.Definition = updateConcept.Definition; item.Note = updateConcept.Note; item.LastUpdated = System.DateTime.Now; context.SaveChanges(); } // foreach (var item in newConcepts) { //if there are no existing, optimize by not doing check. What about duplicates? efEntity = new EM.ConceptScheme_Concept { ConceptSchemeId = entity.Id, PrefLabel = item.PrefLabel, Definition = item.Definition, Note = item.Note, RowId = Guid.NewGuid(), CTID = item.CTID, IsTopConcept = true, Created = DateTime.Now, LastUpdated = DateTime.Now }; context.ConceptScheme_Concept.Add(efEntity); var count = context.SaveChanges(); } //foreach } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".HandleConceptsSimple(), ConceptScheme: {0}", entity.Name)); status.AddError(thisClassName + string.Format(".HandleConceptsSimple(), ConceptScheme: {0}. Message: ", entity.Name) + ex.Message); isValid = false; } } return(isValid); }