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);
        }
        /// <summary>
        /// Finds the resource units which have been newly added into an org unit and
        /// creates links in the OrgUnitResource table to that effect
        /// Returns the IDs of resources added
        /// </summary>
        /// <param name="orgUnit"></param>
        private IEnumerable <int> AddResourcesToOrgUnit(EF.OrgUnit orgUnit)
        {
            List <int> addedResourceIDs = new List <int>();

            foreach (RadListBoxItem item in lbResourcesInGroup.Items)
            {
                int         resourceId = int.Parse(item.Value);
                EF.Resource resource   = _resources.FirstOrDefault(r => r.ResourceId == resourceId);
                //If the resource was found and its OrgUnit has changed, update it
                if (resource != null && resource.OrgUnitResources == null || !resource.OrgUnitResources.Any(or => or.OrgUnitId == orgUnit.OrgUnitId))
                {
                    EF.OrgUnitResource our = new EF.OrgUnitResource();
                    our.OrgUnit          = orgUnit;
                    our.Resource         = resource;
                    our.CreateUserId     = this.Page.User.Identity.Name;
                    our.CreateDateTime   = DateTime.Now;
                    our.LastUpdateDate   = DateTime.Now;
                    our.LastUpdateUserId = this.Page.User.Identity.Name;
                    addedResourceIDs.Add(resourceId);

                    EF.DataContext.Current.SaveChanges();
                }
            }

            return(addedResourceIDs);
        }