public async Task <bool> DeleteActivity(ActivityNode activityNode, IList list)
        {
            var result = await authorisationManagerServiceProxy.DeleteActivity(activityNode.Id);

            if (result)
            {
                list.RemoveNested(activityNode, a => a.Id.Equals(activityNode.Id));
            }

            return(result);
        }
        private ActivityNode GetActivityNode(Activity activity)
        {
            var activityNode = new ActivityNode(activity);

            activity.Activities.ToList().ForEach(a =>
            {
                var an        = GetActivityNode(a);
                an.ParentId   = activity.Id;
                an.ParentType = ParentType.ActivityNode;

                activityNode.Activities.Add(an);
            });
            return(activityNode);
        }
        public void AddActivity(ActivityNode activity)
        {
            if (!Activities.Any(a => a.Id.Equals(activity.Id)))
            {
                var clone = activity.DeepClone();
                clone.ParentType = ParentType.ActivityNode;
                clone.ParentId = Id;

                Activities.Add(clone);

                if (!Activity.Activities.Any(a => a.Id.Equals(activity.Id)))
                {
                    Activity.Activities.Add(clone.Activity);
                }
            }
        }
        public void AddActivity(ActivityNode activity)
        {
            if (!Activities.Any(a => a.Id.Equals(activity.Id)))
            {
                var clone = activity.DeepClone();
                clone.ParentType = ParentType.RoleNode;
                clone.ParentId   = Id;

                Activities.Add(clone);

                if (!Role.Activities.Any(a => a.Id.Equals(activity.Id)))
                {
                    Role.Activities.Add(clone.Activity);
                }
            }
        }
        public async Task <bool> AddActivity(ActivityNode activityNode, RoleNode targetRoleNode, IEnumerable <RoleNode> targets)
        {
            var result = await authorisationManagerServiceProxy.AddActivityToRole(targetRoleNode.Id, activityNode.Id);

            if (result)
            {
                foreach (var role in targets)
                {
                    if (role.Activities.All(a => a.Id != activityNode.Id))
                    {
                        role.AddActivity(activityNode);
                    }
                }
            }

            return(result);
        }
        public async Task <bool> RemoveActivityFromRole(ActivityNode activityNode, IList <RoleNode> roles)
        {
            if (activityNode.ParentType == ParentType.RoleNode)
            {
                var result = await authorisationManagerServiceProxy.RemoveActivityFromRole(activityNode.Id, activityNode.ParentId);

                if (result)
                {
                    var parentRoles = roles.Where(r => r.Id.Equals(activityNode.ParentId));
                    foreach (var role in parentRoles)
                    {
                        role.RemoveActivity(activityNode.Id);
                    }
                }

                return(result);
            }

            return(false);
        }
        public async Task <bool> RemoveActivityFromActivity(ActivityNode activityNode, IList <ActivityNode> activities)
        {
            if (activityNode.ParentType == ParentType.ActivityNode)
            {
                var result = await authorisationManagerServiceProxy.RemoveActivityFromActivity(activityNode.Id, activityNode.ParentId);

                if (result)
                {
                    var parentActivities = activities.Where(a => a.Id.Equals(activityNode.ParentId));
                    foreach (var activity in parentActivities)
                    {
                        activity.RemoveActivity(activityNode.Id);
                    }
                }

                return(result);
            }

            return(false);
        }
        public async Task<ActivityNode> SaveActivity(ActivityNode activityNode, IEnumerable<ActivityNode> activities)
        {
            var activity = await authorisationManagerServiceProxy.SaveActivity(activityNode.Activity).ConfigureAwait(false);
            var savedActivityNode = GetActivityNode(activity);

            if (activityNode.Id.Equals(0))
            {
                activityNode.Id = savedActivityNode.Id;
                activityNode.Text = savedActivityNode.Text;
                activityNode.Code = savedActivityNode.Code;
                activityNode.Description = savedActivityNode.Description;
            }

            foreach (var a in activities)
            {
                a.Text = savedActivityNode.Text;
                a.Code = savedActivityNode.Code;
                a.Description = savedActivityNode.Description;
            }

            return activityNode;
        }
        public async Task <ActivityNode> SaveActivity(ActivityNode activityNode, IEnumerable <ActivityNode> activities)
        {
            var activity = await authorisationManagerServiceProxy.SaveActivity(activityNode.Activity).ConfigureAwait(false);

            var savedActivityNode = GetActivityNode(activity);

            if (activityNode.Id.Equals(0))
            {
                activityNode.Id          = savedActivityNode.Id;
                activityNode.Text        = savedActivityNode.Text;
                activityNode.Code        = savedActivityNode.Code;
                activityNode.Description = savedActivityNode.Description;
            }

            foreach (var a in activities)
            {
                a.Text        = savedActivityNode.Text;
                a.Code        = savedActivityNode.Code;
                a.Description = savedActivityNode.Description;
            }

            return(activityNode);
        }
        private async void AddActivity(ActivityNode activityNode, NodeEntityBase target)
        {
            try
            {
                IsBusy = true;

                if (AuthorisationManagerServiceManager.TargetNodeIsDropCandidate(target, activityNode))
                {
                    return;
                }

                if (target is ActivityNode)
                {
                    var targets = Activities.Flatten<ActivityNode>(t => t.Id.Equals(target.Id), Roles, Users);
                    var result = await authorisationManagerServiceManager.AddActivity(activityNode, (ActivityNode) target, targets);
                }
                else if (target is RoleNode)
                {
                    var targets = Roles.Flatten<RoleNode>(t => t.Id.Equals(target.Id), Users);
                    var result = await authorisationManagerServiceManager.AddActivity(activityNode, (RoleNode)target, targets);
                }
                else
                {
                    throw new Exception(
                        string.Format(
                            "Invalid drop target. '{0}' can only be dropped onto a role or another activity.",
                            activityNode.Text));
                }

                ResetStatus();
            }
            catch (Exception ex)
            {
                ShowMessage(new Message()
                {
                    MessageType = MessageTypeEnum.Error,
                    Text = ex.Message
                }, true);

                IsBusy = false;
            }
            finally
            {                
                OnPropertyChanged("");
            }
        }
        private async void SaveActivity(ActivityNode activityNode)
        {
            try
            {
                IsBusy = true;

                var newActivity = activityNode.Id.Equals(0);

                var duplicateActivities = Activities.Flatten<ActivityNode>(a => a.Id.Equals(activityNode.Id), Roles,
                    Users);

                var savedActivity =
                    await authorisationManagerServiceManager.SaveActivity(activityNode, duplicateActivities);

                if (savedActivity != null)
                {
                    if (newActivity)
                    {
                        Activities.Add(activityNode);
                    }
                }

                ResetStatus();
            }
            catch (Exception ex)
            {
                ShowMessage(new Message()
                {
                    MessageType = MessageTypeEnum.Error,
                    Text = ex.Message
                }, true);

                IsBusy = false;
            }
            finally
            {
                OnPropertyChanged("");   
            }
        }
        public async Task<bool> RemoveActivityFromActivity(ActivityNode activityNode, IList<ActivityNode> activities)
        {
            if (activityNode.ParentType == ParentType.ActivityNode)
            {
                var result = await authorisationManagerServiceProxy.RemoveActivityFromActivity(activityNode.Id, activityNode.ParentId);
                if (result)
                {
                    var parentActivities = activities.Where(a => a.Id.Equals(activityNode.ParentId));
                    foreach (var activity in parentActivities)
                    {
                        activity.RemoveActivity(activityNode.Id);
                    }
                }

                return result;
            }

            return false;
        }
        private ActivityNode GetActivityNode(Activity activity)
        {
            var activityNode = new ActivityNode(activity);
            activity.Activities.ToList().ForEach(a =>
            {
                var an = GetActivityNode(a);
                an.ParentId = activity.Id;
                an.ParentType = ParentType.ActivityNode;

                activityNode.Activities.Add(an);
            });
            return activityNode;
        }
        public async Task<bool> AddActivity(ActivityNode activityNode, RoleNode targetRoleNode, IEnumerable<RoleNode> targets)
        {
            var result = await authorisationManagerServiceProxy.AddActivityToRole(targetRoleNode.Id, activityNode.Id);
            if (result)
            {
                foreach (var role in targets)
                {
                    if (role.Activities.All(a => a.Id != activityNode.Id))
                    {
                        role.AddActivity(activityNode);
                    }
                }
            }

            return result;
        }
        public async Task<bool> RemoveActivityFromRole(ActivityNode activityNode, IList<RoleNode> roles)
        {
            if (activityNode.ParentType == ParentType.RoleNode)
            {
                var result = await authorisationManagerServiceProxy.RemoveActivityFromRole(activityNode.Id, activityNode.ParentId);
                if (result)
                {
                    var parentRoles = roles.Where(r => r.Id.Equals(activityNode.ParentId));
                    foreach (var role in parentRoles)
                    {
                        role.RemoveActivity(activityNode.Id);
                    }
                }

                return result;
            }

            return false;
        }
        private async void RemoveActivity(ActivityNode activityNode)
        {
            if (activityNode.ParentType == ParentType.None)
            {
                ShowMessage(new Message()
                {
                    MessageType = MessageTypeEnum.Warn,
                    Text = string.Format("Can't remove activity {0} as it has no parent.", activityNode.Text)
                }, true);
                return;
            }

            try
            {
                IsBusy = true;

                if (activityNode.ParentType == ParentType.ActivityNode)
                {
                    var activities = Activities.Flatten<ActivityNode>(Roles, Users).ToList();
                    var result =
                        await authorisationManagerServiceManager.RemoveActivityFromActivity(activityNode, activities);
                }
                else if (activityNode.ParentType == ParentType.RoleNode)
                {
                    var roles = Roles.Flatten<RoleNode>(Users).ToList();
                    var result = await authorisationManagerServiceManager.RemoveActivityFromRole(activityNode, roles);
                }

                ResetStatus();
            }
            catch (Exception ex)
            {
                ShowMessage(new Message()
                {
                    MessageType = MessageTypeEnum.Error,
                    Text = ex.Message
                }, true);

                IsBusy = false;
            }
            finally
            {
                OnPropertyChanged("");
            }
        }
        private async void DeleteActivity(ActivityNode activityNode)
        {
            try
            {
                IsBusy = true;
                var aggregatedList = Activities.Merge(Roles, Users);
                var result = await authorisationManagerServiceManager.DeleteActivity(activityNode, aggregatedList);
                SelectedItem = null;
                ResetStatus();
            }
            catch (Exception ex)
            {
                ShowMessage(new Message()
                {
                    MessageType = MessageTypeEnum.Error,
                    Text = ex.Message
                }, true);

                IsBusy = false;
            }
            finally
            {
                OnPropertyChanged("");
            }
        }
        public async Task<bool> DeleteActivity(ActivityNode activityNode, IList list)
        {
            var result = await authorisationManagerServiceProxy.DeleteActivity(activityNode.Id);
            if (result)
            {
                list.RemoveNested(activityNode, a => a.Id.Equals(activityNode.Id));
            }

            return result;
        }