private bool RemoveResource(RadTreeNode node, int?insertAfter) { TreeDataNodeType dataNodeType = (TreeDataNodeType)Enum.Parse(typeof(TreeDataNodeType), node.Attributes["DataNodeType"], true); //If the node is a resource add it to the list and remove it from the tree //otherwise process its children if (dataNodeType != TreeDataNodeType.OrgUnit) { int orgUnitId = Convert.ToInt32(node.ParentNode.Attributes["OrgUnitId"]); int resourceId = Convert.ToInt32(node.Attributes["ResourceId"]); EF.Resource resource = _resources.FirstOrDefault(r => r.ResourceId == resourceId); if (resource != null) { EF.OrgUnitResource orgResource = resource.OrgUnitResources.FirstOrDefault(or => or.OrgUnitId == orgUnitId); if (orgResource != null) { EF.DataContext.Current.DeleteObject(orgResource); } } node.Remove(); return(true); } return(false); }
private void UpdateOrgUnits(int?ParentOrgUnitId, RadTreeNodeCollection Nodes, bool delete) { //Ideally OrgUnit would have an Entity Reference to itself which would mean a sub tree could be deleted //by simply deleting it's the root node and we would not need to SaveChanges for every new node just to get //the OrgUnitId to pass down the tree. However, this jhas not been done as the RadTree requires a ParentId and Id //to do its binding. EF4 will fix this by allowing us to have both an FK Id AND an Entity Reference foreach (RadTreeNode node in Nodes) { TreeDataNodeType dataNodeType = (TreeDataNodeType)Enum.Parse(typeof(TreeDataNodeType), node.Attributes["DataNodeType"], true); //If Node is an OrgUnit if (dataNodeType == TreeDataNodeType.OrgUnit) { int orgUnitId = 0; EF.OrgUnit orgUnit = null; //New nodes do not have a OrgUnitId if (!string.IsNullOrEmpty(node.Attributes["OrgUnitId"])) { orgUnitId = int.Parse(node.Attributes["OrgUnitId"]); orgUnit = _orgUnits.FirstOrDefault(ou => ou.OrgUnitId == orgUnitId); } //If the node has been deleted or one of its ancestors, delete it if it is NOT new //then delete its children if (node.Visible == false || delete) { if (orgUnit != null) { foreach (EF.OrgUnitResource our in orgUnit.OrgUnitResources) { EF.DataContext.Current.DeleteObject(our); } EF.DataContext.Current.DeleteObject(orgUnit); EF.DataContext.Current.SaveChanges(); } } //If not deleting, then add or update else { //Create the OrgUnit if it's new if (orgUnit == null) { orgUnit = new EF.OrgUnit(); EF.DataContext.Current.AddToOrgUnits(orgUnit); _orgUnits.Add(orgUnit); } //Update changed properties if (orgUnit.ParentOrgUnitId != ParentOrgUnitId) { orgUnit.ParentOrgUnitId = ParentOrgUnitId; } if (orgUnit.Name != node.Text) { orgUnit.Name = node.Text; } EF.DataContext.Current.SaveChanges(); UpdateOrgUnits(orgUnit.OrgUnitId, node.Nodes, false); } } else { } } }