//returns false, if a move is not possible public bool Move(ScenarioGroupDetail detail, ScenarioGroup newParent) { //detail.ScenarioGroupID = newParent.ScenarioGroupID; ScenarioGroup checkParent = newParent; //check for circular reference while ((checkParent != null) && (checkParent != detail.ScenarioGroup)) { checkParent = checkParent.ScenarioGroupDetail.ScenarioGroupParent; } //circular reference check failed - abort movement if (checkParent == detail.ScenarioGroup) return false; detail.ScenarioGroupParent = newParent; detail.ScenarioGroupParentID = newParent.ScenarioGroupID; //reset the values - we have new version, which is unapproved //it will be finalised during save if (detail.Status == EntityStatus.Approved) detail.Default(Context.UserName); return true; }
public ScenarioGroupDetail Add(ScenarioGroup parent) { ScenarioGroupDetail detail = new ScenarioGroupDetail(); detail.Default(Context.UserName); ScenarioGroup scenarioGroup = new ScenarioGroup(); scenarioGroup.Default(Context.UserName); if (Context.ScenarioGroups.Local.Count() > 0) scenarioGroup.ScenarioGroupID = Context.ScenarioGroups.Local.Max(x => x.ScenarioGroupID) + 1; else scenarioGroup.ScenarioGroupID = 1; Context.ScenarioGroups.Add(scenarioGroup); scenarioGroup.ScenarioGroupDetails.Add(detail); detail.ScenarioGroup = scenarioGroup; detail.ScenarioGroupID = scenarioGroup.ScenarioGroupID; if (parent != null) { detail.ScenarioGroupParent = parent; detail.ScenarioGroupParentID = parent.ScenarioGroupDetail.ScenarioGroupID; } detail.Name = "Scenario Group Name"; return detail; }
internal static void AddScenarioGroups(ScenarioGeneratorModel context) { DateTime now = context.AsOfDate; ScenarioGroup sg = new ScenarioGroup() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; context.ScenarioGroups.Add(sg); ScenarioGroupDetail sgd; sgd = new ScenarioGroupDetail() { Name = "Macro (v1)", StartTime = now.AddDays(-100), EndTime = now.AddDays(-50), CreatedAt = now.AddDays(-120), CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", ModifiedAt = now.AddDays(-110), ModifiedBy = "C", Latest = false }; sg.ScenarioGroupDetails.Add(sgd); sgd = new ScenarioGroupDetail() { Name = "Macro (v2)", StartTime = now.AddDays(-50), EndTime = now.AddDays(-20), CreatedAt = now.AddDays(-75), CreatedBy = "A", ApprovedAt = now.AddDays(-50), ApprovedBy = "B", ModifiedAt = now.AddDays(-60), ModifiedBy = "C", Latest = false }; sg.ScenarioGroupDetails.Add(sgd); sgd = new ScenarioGroupDetail() { Name = "Macro (v3)", StartTime = now.AddDays(-20), EndTime = DateTime.MaxValue, CreatedAt = now.AddDays(-30), CreatedBy = "A", ApprovedAt = now.AddDays(-20), ApprovedBy = "B", ModifiedAt = now.AddDays(-35), ModifiedBy = "C", Latest = false }; sg.ScenarioGroupDetails.Add(sgd); sgd = new ScenarioGroupDetail() { Name = "Macro", StartTime = DateTime.MinValue, EndTime = DateTime.MaxValue, CreatedAt = now.AddDays(-10), CreatedBy = "A", ModifiedAt = now.AddDays(-5), ModifiedBy = "C", Latest = true }; sg.ScenarioGroupDetails.Add(sgd); ScenarioGroup sgc = new ScenarioGroup() { StartTime = now.AddDays(-80), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-80), ApprovedBy = "B", Latest = true }; context.ScenarioGroups.Add(sgc); sgd = new ScenarioGroupDetail() { Name = "Cyprus Contagion (v1)", StartTime = now.AddDays(-80), EndTime = now.AddDays(-40), CreatedAt = now.AddDays(-80), CreatedBy = "A", ApprovedAt = now.AddDays(-80), ApprovedBy = "B", ModifiedAt = now.AddDays(-80), ModifiedBy = "C", Latest = false }; sgc.ScenarioGroupDetails.Add(sgd); sgd.ScenarioGroupParent = sg; sgd = new ScenarioGroupDetail() { Name = "Cyprus Contagion (v2)", StartTime = now.AddDays(-40), EndTime = now.AddDays(-10), CreatedAt = now.AddDays(-50), CreatedBy = "A", ApprovedAt = now.AddDays(-40), ApprovedBy = "B", ModifiedAt = now.AddDays(-45), ModifiedBy = "C", Latest = false }; sgc.ScenarioGroupDetails.Add(sgd); sgd.ScenarioGroupParent = sg; sgd = new ScenarioGroupDetail() { Name = "Cyprus Contagion (v3)", StartTime = now.AddDays(-10), EndTime = DateTime.MaxValue, CreatedAt = now.AddDays(-20), CreatedBy = "A", ApprovedAt = now.AddDays(-10), ApprovedBy = "B", ModifiedAt = now.AddDays(-12), ModifiedBy = "C", Latest = false }; sgc.ScenarioGroupDetails.Add(sgd); sgd.ScenarioGroupParent = sg; sgd = new ScenarioGroupDetail() { Name = "Cyprus Contagion", StartTime = DateTime.MinValue, EndTime = DateTime.MaxValue, CreatedAt = now.AddDays(-5), CreatedBy = "A", ModifiedAt = now.AddDays(-3), ModifiedBy = "C", Latest = true }; sgc.ScenarioGroupDetails.Add(sgd); sgd.ScenarioGroupParent = sg; sgc = new ScenarioGroup() { StartTime = now.AddDays(-55), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-55), ApprovedBy = "B", Latest = true }; context.ScenarioGroups.Add(sgc); sgd = new ScenarioGroupDetail() { Name = "Double Dip Recession (v1)", StartTime = now.AddDays(-55), EndTime = now.AddDays(-17), CreatedAt = now.AddDays(-80), CreatedBy = "A", ApprovedAt = now.AddDays(-55), ApprovedBy = "B", ModifiedAt = now.AddDays(-80), ModifiedBy = "C", ScenarioGroupParent = sg, Latest = false }; sgc.ScenarioGroupDetails.Add(sgd); sgd.ScenarioGroupParent = sg; sgd = new ScenarioGroupDetail() { Name = "Double Dip Recession", StartTime = now.AddDays(-17), EndTime = DateTime.MaxValue, CreatedAt = now.AddDays(-50), CreatedBy = "A", ApprovedAt = now.AddDays(-17), ApprovedBy = "B", ModifiedAt = now.AddDays(-18), ModifiedBy = "C", ScenarioGroupParent = sg, Latest = true }; sgc.ScenarioGroupDetails.Add(sgd); sgd.ScenarioGroupParent = sg; sgc = new ScenarioGroup() { StartTime = now.AddDays(-25), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-55), ApprovedBy = "B", Latest = true }; context.ScenarioGroups.Add(sgc); sgd = new ScenarioGroupDetail() { Name = "Euro Breaks Up", StartTime = now.AddDays(-25), EndTime = DateTime.MaxValue, CreatedAt = now.AddDays(-80), CreatedBy = "A", ModifiedAt = now.AddDays(-18), ModifiedBy = "C", Latest = true }; sgc.ScenarioGroupDetails.Add(sgd); sgd.ScenarioGroupParent = sg; sgc = new ScenarioGroup() { StartTime = DateTime.MinValue, EndTime = DateTime.MaxValue, CreatedAt = now.AddDays(-80), CreatedBy = "A", Latest = true }; context.ScenarioGroups.Add(sgc); sgd = new ScenarioGroupDetail() { Name = "Eurozone Crisis", StartTime = DateTime.MinValue, EndTime = DateTime.MaxValue, CreatedAt = now.AddDays(-80), CreatedBy = "A", Latest = true }; sgc.ScenarioGroupDetails.Add(sgd); context.SaveChanges(); }
public void TestSaveNewScenarioGroup() { string name = "New Scenario Group"; ScenarioGroup main; ScenarioGroupDetail detail; using (var context = new ScenarioGeneratorModel(UserName, Connection)) { main = new ScenarioGroup(); main.Default(UserName); context.ScenarioGroups.Add(main); detail = new ScenarioGroupDetail(); detail.Default(UserName); detail.Name = name; main.ScenarioGroupDetails.Add(detail); context.SaveChanges(); } using (var context = new ScenarioGeneratorModel(UserName, Connection)) { context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); Assert.AreEqual(1, context.ScenarioGroupDetails.Where(x => x.Name == name).Count()); detail = context.ScenarioGroupDetails.Where(x => x.Name == name).FirstOrDefault(); main = detail.ScenarioGroup; Assert.AreEqual(name, detail.Name); Assert.AreEqual(true, detail.Latest); Assert.AreEqual(DateTime.MinValue, detail.StartTime); Assert.AreEqual(DateTime.MaxValue, detail.EndTime); Assert.AreEqual(EntityStatus.Unapproved, detail.Status); Assert.AreEqual(true, main.Latest); Assert.AreEqual(DateTime.MinValue, main.StartTime); Assert.AreEqual(DateTime.MaxValue, main.EndTime); Assert.AreEqual(DateTime.MaxValue, main.EndTime); Assert.AreEqual(EntityStatus.Unapproved, main.Status); } }
protected override void BeforeSave() { DateTime timestamp = DateTime.UtcNow; //find all changed enities var modifiedEntities = Context.ChangeTracker.Entries<ScenarioGroupDetail>() .Where(p => p.State == EntityState.Modified) .Select(p => p.Entity); foreach (ScenarioGroupDetail detail in modifiedEntities) { //main entity, for which we keep versioning ScenarioGroup mainEntity = detail.ScenarioGroup; ICollection<ScenarioGroupDetail> mainEntityDetails = mainEntity.ScenarioGroupDetails; if (detail.Status == EntityStatus.ExpiredInSession) { foreach (ScenarioGroupDetail item in mainEntityDetails) { if (detail.ScenarioGroupDetailID != item.ScenarioGroupDetailID) { item.EndTime = detail.EndTime; item.ModifiedBy = detail.ModifiedBy; item.ModifiedAt = detail.ModifiedAt; } } mainEntity.EndTime = detail.EndTime; mainEntity.ModifiedBy = detail.ModifiedBy; mainEntity.ModifiedAt = detail.ModifiedAt; } if (detail.Status == EntityStatus.ApprovedInSession) { //entity has been approved //mark existing details as expired foreach (ScenarioGroupDetail item in mainEntityDetails) { if (detail.ScenarioGroupDetailID != item.ScenarioGroupDetailID) { item.Latest = false; item.EndTime = detail.ApprovedAt.Value; detail.StartTime = detail.ApprovedAt.Value; } } //check if main entity is approved - if not copy details if (mainEntity.Status != EntityStatus.Approved) { mainEntity.ApprovedAt = detail.ApprovedAt; mainEntity.ApprovedBy = detail.ApprovedBy; mainEntity.StartTime = detail.StartTime; } } else if (detail.Status == EntityStatus.UnapprovedInSession) { //new version of the entity //we are creating a copy of currently edited entity //and add it back to context ScenarioGroupDetail copy = new ScenarioGroupDetail(); mainEntity.ScenarioGroupDetails.Add(copy); //copy values from original Context.Entry(copy).CurrentValues.SetValues(detail); //reset auditing copy.ResetCopy(); //revert the changes - done implicitly Context.Entry(detail).State = EntityState.Unchanged; //remove latest flag detail.Latest = false; } } }