/// <summary> /// Convert the SaveMap <see cref="EntityInfo"/> entries of the <see cref="CType"/> /// into EntityInfo entries for the <see cref="SType"/>. /// </summary> /// <param name="saveMap"> /// The "SaveMap" passed into the <see cref="ContextProvider.BeforeSaveEntities"/> method. /// </param> /// <param name="entityInfoCreator"> /// Function that creates a new <see cref="EntityInfo"/> for a /// given entity and optional <see cref="EntityState"/>. /// See <see cref="ContextProvider.CreateEntityInfo"/>. /// </param> /// <param name="beforeSaveEntity"> /// Optional function to validate an individual entity before it can save; /// see <see cref="ContextProvider.BeforeSaveEntity"/>"/>; /// </param> /// <remarks> /// Updates the "SaveMap" by converting those <see cref="EntityInfo"/> objects /// constructed from the JSON "saveBundle" in terms of the client's entity type /// into corresponding EntityInfos expressed in terms of the server entity type. /// Converts with the <see cref="MapEntityToServer"/>. /// <para> /// Call it inside your <see cref="ContextProvider.BeforeSaveEntities"/> /// override or delegate method. /// </para> /// </remarks> public SaveMap ConvertBeforeSaveMap( SaveMap saveMap, EntityInfoCreator entityInfoCreator, BeforeSaveEntityDelegate beforeSaveEntity = null) { List <EntityInfo> cGroup, sGroup; if (saveMap.TryGetValue(cType, out cGroup)) { saveMap.Remove(cType); // don't save CType entities } else { return(saveMap); // this CType is not in the saveMap } if (!saveMap.TryGetValue(sType, out sGroup)) { sGroup = new List <EntityInfo>(); saveMap.Add(sType, sGroup); } foreach (var cEntityInfo in cGroup) { var sEntity = MapEntityToServer(cEntityInfo); if (sEntity == null) { continue; } var mappedEntityInfo = entityInfoCreator(sEntity, cEntityInfo.EntityState); mappedEntityInfo.OriginalValuesMap = MapOriginalValues(cEntityInfo.OriginalValuesMap); mappedEntityInfo.AutoGeneratedKey = MapAutoGeneratedKey(sEntity, cEntityInfo.AutoGeneratedKey); mappedEntityInfo.ForceUpdate = cEntityInfo.ForceUpdate; // TODO: Fix this deficiency // Unfortunately, UnmappedValuesMap is "protected internal" right now so can't copy //mappedEntityInfo.UnmappedValuesMap = entityInfo.UnmappedValuesMap; if (beforeSaveEntity == null || beforeSaveEntity(mappedEntityInfo)) { sGroup.Add(mappedEntityInfo); } } return(saveMap); }
public static SaveMap MergeMap(this SaveMap saveMap, SaveMap newMap) { foreach (var map in newMap) { List <EntityInfo> infos; var hasKey = saveMap.TryGetValue(map.Key, out infos); if (hasKey) { saveMap[map.Key].AddRange(infos); } else { saveMap.Add(map.Key, map.Value); } } return(saveMap); }
/// <summary> /// Convert the post-save <see cref="SType"/> entities into the <see cref="CType"/> entities /// that the client is expecting. /// </summary> /// <param name="saveMap"> /// The <see cref="SaveResult"/> returned by <see cref="ContextProvider.SaveChanges"/> /// </param> /// <param name="keyMappings"> /// The <see cref="SaveResult"/> returned by <see cref="ContextProvider.SaveChanges"/> /// </param> /// <param name="entityInfoCreator"> /// Function that creates a new <see cref="EntityInfo"/> for a /// given entity and optional <see cref="EntityState"/>. /// See <see cref="ContextProvider.CreateEntityInfo"/>. /// </param> /// <remarks> /// Converts the <see cref="SType"/> entities in the "SaveMap" and <see cref="KeyMapping"/> list /// passed to the <see cref="ContextProvider.AfterSaveEntities"/> after /// the <see cref="ContextProvider.SaveChangesCore"/>. /// It uses the <see cref="MapEntityToClient"/> to convert the <see cref="SType"/> entities into /// corresponding <see cref="CType"/> entities. /// Use <see cref="MapEntityToClient"/> to convert the <see cref="SType"/> entities in /// <see cref="SaveResult.Entities"/> with /// <para> /// Call it in your wrapper around the <see cref="ContextProvider.SaveChanges"/> /// where it can fixup the SaveResult before it is serialized to the client. /// </para> /// </remarks> public SaveMap ConvertAfterSaveMap( SaveMap saveMap, List <KeyMapping> keyMappings, EntityInfoCreator entityInfoCreator) { List <EntityInfo> cGroup, sGroup; if (saveMap.TryGetValue(SType, out sGroup)) { saveMap.Remove(sType); // don't return SType entities to client } else { return(saveMap); // this SType is not in the saveMap } if (!saveMap.TryGetValue(cType, out cGroup)) { cGroup = new List <EntityInfo>(); saveMap.Add(cType, cGroup); } foreach (var sEntityInfo in sGroup) { var cEntity = MapEntityToClient(sEntityInfo); if (cEntity != null) { var mappedEntityInfo = entityInfoCreator(cEntity, sEntityInfo.EntityState); // No other conversions are needed. cGroup.Add(mappedEntityInfo); } } var sName = SType.FullName; var cName = CType.FullName; keyMappings.ForEach(km => { if (km.EntityTypeName == sName) { km.EntityTypeName = cName; } }); return(saveMap); }
public SaveMap BeforeSaveEntities(SaveMap saveMap) { List <StudentOnTheMove> studentsPendingRemoval = new List <StudentOnTheMove>(); if (saveMap.ContainsKey(tWg)) { var grps = ProcessWorkGroup(saveMap[tWg]); if (grps != null) { saveMap.MergeMap(grps); } var groups = (from info in saveMap[tWg] select info.Entity as WorkGroup).ToList(); groups.ForEach(group => { group.ModifiedById = loggedInUser.PersonId; group.ModifiedDate = DateTime.Now; }); } if (saveMap.ContainsKey(tStudInGroup)) { var groupMembers = saveMap[tStudInGroup]; //Need to account for adds when a new group is created. var studentsOnTheMove = (from info in saveMap[tStudInGroup] let sig = info.Entity as CrseStudentInGroup where info.EntityState == EntityState.Modified where info.OriginalValuesMap.ContainsKey("WorkGroupId") select info).ToList(); var unAssignedStudents = (from info in saveMap[tStudInGroup] let sig = info.Entity as CrseStudentInGroup where info.EntityState == EntityState.Deleted select info).ToList(); if (unAssignedStudents.Any()) { unAssignedStudents.ForEach(uas => { var studentEntity = uas.Entity as CrseStudentInGroup; var fromWorkGroupId = Int32.Parse(studentEntity.WorkGroupId.ToString()); //uas.EntityState = Breeze.Persistence.EntityState.Deleted; var member = ctxManager.Context.StudentInGroups .Where(sig => sig.StudentId == studentEntity.StudentId) .Where(sig => sig.WorkGroupId == fromWorkGroupId) .Select(sig => new StudentOnTheMove { //Student = sig, StudentId = sig.StudentId, //IsDeleted = sig.IsDeleted, IsMoving = false, FromWorkGroupId = fromWorkGroupId, //CourseId = studentEntity.CourseId, HasChildren = sig.AuthorOfComments.Any() || sig.AssesseeSpResponses.Any() || sig.AssessorSpResponses.Any() || sig.AssesseeStratResponse.Any() || sig.AssessorStratResponse.Any() || sig.RecipientOfComments.Any() }).ToList(); studentsPendingRemoval.AddRange(member); }); } if (studentsOnTheMove.Any()) { studentsOnTheMove.ForEach(sm => { var studentEntity = sm.Entity as CrseStudentInGroup; var fromWorkGroupId = Int32.Parse(sm.OriginalValuesMap["WorkGroupId"].ToString()); var member = ctxManager.Context.StudentInGroups .Where(sig => sig.StudentId == studentEntity.StudentId) .Where(sig => sig.WorkGroupId == fromWorkGroupId) .Select(sig => new StudentOnTheMove { Student = sig, StudentId = sig.StudentId, IsDeleted = sig.IsDeleted, IsMoving = true, ToWorkGroupId = studentEntity.WorkGroupId, FromWorkGroupId = fromWorkGroupId, CourseId = studentEntity.CourseId, HasChildren = sig.AuthorOfComments.Any() || sig.AssesseeSpResponses.Any() || sig.AssessorSpResponses.Any() || sig.AssesseeStratResponse.Any() || sig.AssessorStratResponse.Any() || sig.RecipientOfComments.Any() || sig.FacultySpResponses.Any() || sig.FacultyStrat != null || sig.FacultyComment != null }).ToList(); studentsPendingRemoval.AddRange(member); }); } var studentsPendingRemovalWithChildren = studentsPendingRemoval .Where(spr => spr.HasChildren).ToList(); var studentsPendingRemovalWithoutChildren = studentsPendingRemoval .Where(spr => !spr.HasChildren).ToList(); if (studentsPendingRemovalWithChildren.Any()) { studentsPendingRemovalWithChildren.ForEach(sprwc => { //if (sprwc.IsMoving) //{ var authorCommentFlags = ctxManager.Context.StudSpCommentFlags .Where(sscf => sscf.AuthorPersonId == sprwc.StudentId) .Where(sscf => sscf.WorkGroupId == sprwc.FromWorkGroupId); var recipientCommentFlags = ctxManager.Context.StudSpCommentFlags .Where(sscf => sscf.RecipientPersonId == sprwc.StudentId) .Where(sscf => sscf.WorkGroupId == sprwc.FromWorkGroupId); var authorOfComments = ctxManager.Context.StudSpComments .Where(ssc => ssc.AuthorPersonId == sprwc.StudentId) .Where(ssc => ssc.WorkGroupId == sprwc.FromWorkGroupId); var recipientOfComments = ctxManager.Context.StudSpComments .Where(ssc => ssc.RecipientPersonId == sprwc.StudentId) .Where(ssc => ssc.WorkGroupId == sprwc.FromWorkGroupId); var assesseeSpResponses = ctxManager.Context.SpResponses .Where(sr => sr.AssesseePersonId == sprwc.StudentId) .Where(sr => sr.WorkGroupId == sprwc.FromWorkGroupId); var assessorSpResponses = ctxManager.Context.SpResponses .Where(sr => sr.AssessorPersonId == sprwc.StudentId) .Where(sr => sr.WorkGroupId == sprwc.FromWorkGroupId); var assesseeStratResponses = ctxManager.Context.SpStratResponses .Where(ssr => ssr.AssesseePersonId == sprwc.StudentId) .Where(ssr => ssr.WorkGroupId == sprwc.FromWorkGroupId); var assessorStratResponses = ctxManager.Context.SpStratResponses .Where(ssr => ssr.AssessorPersonId == sprwc.StudentId) .Where(ssr => ssr.WorkGroupId == sprwc.FromWorkGroupId); var facSpResponses = ctxManager.Context.FacSpResponses .Where(fsr => fsr.AssesseePersonId == sprwc.StudentId) .Where(fsr => fsr.WorkGroupId == sprwc.FromWorkGroupId); var facStratResponse = ctxManager.Context.FacStratResponses .Where(fsr => fsr.AssesseePersonId == sprwc.StudentId) .Where(fsr => fsr.WorkGroupId == sprwc.FromWorkGroupId); var facComments = ctxManager.Context.FacSpComments .Where(fsc => fsc.RecipientPersonId == sprwc.StudentId) .Where(fsc => fsc.WorkGroupId == sprwc.FromWorkGroupId); var facCommentsFlag = ctxManager.Context.FacSpCommentFlags .Where(fscf => fscf.RecipientPersonId == sprwc.StudentId) .Where(fscf => fscf.WorkGroupId == sprwc.FromWorkGroupId); if (authorOfComments.Any()) { if (authorCommentFlags.Any()) { ctxManager.Context.StudSpCommentFlags.RemoveRange(authorCommentFlags); } ctxManager.Context.StudSpComments.RemoveRange(authorOfComments); } if (recipientOfComments.Any()) { if (recipientCommentFlags.Any()) { ctxManager.Context.StudSpCommentFlags.RemoveRange(recipientCommentFlags); } ctxManager.Context.StudSpComments.RemoveRange(recipientOfComments); } if (assesseeSpResponses.Any()) { ctxManager.Context.SpResponses.RemoveRange(assesseeSpResponses); } if (assessorSpResponses.Any()) { ctxManager.Context.SpResponses.RemoveRange(assessorSpResponses); } if (assesseeStratResponses.Any()) { ctxManager.Context.SpStratResponses.RemoveRange(assesseeStratResponses); } if (assessorStratResponses.Any()) { ctxManager.Context.SpStratResponses.RemoveRange(assessorStratResponses); } if (facSpResponses.Any()) { ctxManager.Context.FacSpResponses.RemoveRange(facSpResponses); } if (facStratResponse.Any()) { ctxManager.Context.FacStratResponses.RemoveRange(facStratResponse); } if (facComments.Any()) { ctxManager.Context.FacSpComments.RemoveRange(facComments); } if (facCommentsFlag.Any()) { ctxManager.Context.FacSpCommentFlags.RemoveRange(facCommentsFlag); } //} if (sprwc.IsMoving) { ctxManager.Context.StudentInGroups.Remove(sprwc.Student); } }); ctxManager.Context.SaveChanges(); } if (studentsPendingRemovalWithoutChildren.Any()) { studentsPendingRemovalWithoutChildren.ForEach(sprwoc => { if (sprwoc.IsMoving) { ctxManager.Context.StudentInGroups.Remove(sprwoc.Student); ctxManager.Context.SaveChanges(); } //ctxManager.Context.Entry(sprwoc.Student).State = System.Data.Entity.EntityState.Deleted; }); //ctxManager.Context.SaveChanges(); } var studentsToBeAddedBack = studentsPendingRemoval .Where(spr => spr.IsMoving).ToList(); ////Students that were previously deleted with children. studentsOnTheMove.ForEach(info => saveMap.Remove(tStudInGroup)); if (studentsToBeAddedBack.Any()) { List <EntityInfo> toAddInfos; toAddInfos = new List <EntityInfo>(); studentsToBeAddedBack.ForEach(stab => { var toAdd = new CrseStudentInGroup { StudentId = stab.StudentId, CourseId = stab.CourseId, WorkGroupId = stab.ToWorkGroupId, HasAcknowledged = false, IsDeleted = false, ModifiedById = loggedInUser.PersonId, ModifiedDate = DateTime.Now }; var toAddEi = ctxManager.CreateEntityInfo(toAdd); toAddInfos.Add(toAddEi); }); saveMap.Add(tStudInGroup, toAddInfos); } } return(saveMap); }