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; }
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); } }
//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; }