public int MoveToDiscussion(ManagedSecurityContext sec, int targetid)
        {
            GetACL().Check(sec, DataOperation.Delete);

            ManagedDiscussion target_discussion = new ManagedDiscussion(Session, targetid);

            target_discussion.GetACL().Check(sec, DataOperation.Create);

            // create the target thread
            DiscussionThread target_thread = new DiscussionThread();

            target_thread.Discussion = target_discussion.Instance;
            target_thread.Modified   = mInstance.Modified;
            target_thread.Created    = mInstance.Created;
            Session.Save(target_thread);
            if (target_discussion.Instance.Modified < mInstance.Modified)
            {
                target_discussion.Instance.Modified = mInstance.Modified;
                Session.Save(target_discussion.Instance);
            }
            // copy the post to a discusison post
            DiscussionPost target_post = new DiscussionPost();

            target_post.AccountId        = mInstance.AccountId;
            target_post.Body             = mInstance.Body;
            target_post.Created          = mInstance.Created;
            target_post.Modified         = mInstance.Modified;
            target_post.Sticky           = false;
            target_post.Subject          = mInstance.Title;
            target_post.DiscussionThread = target_thread;
            Session.Save(target_post);
            // set the new post as parent of all replies
            int comments_discussion_id = ManagedDiscussion.GetOrCreateDiscussionId(
                Session, typeof(AccountBlogPost).Name, mInstance.Id, sec);
            Discussion comments_discussion = Session.Load <Discussion>(comments_discussion_id);

            foreach (DiscussionThread thread in Collection <DiscussionThread> .GetSafeCollection(comments_discussion.DiscussionThreads))
            {
                foreach (DiscussionPost post in thread.DiscussionPosts)
                {
                    post.DiscussionThread = target_thread;
                    if (post.DiscussionPostParent == null)
                    {
                        post.DiscussionPostParent = target_post;
                    }
                }
                Session.Delete(thread);
            }
            // delete the current post that became a discussion post
            Session.Delete(mInstance);
            return(target_post.Id);
        }
        public int MoveToAccountBlog(ManagedSecurityContext sec, int targetid)
        {
            GetACL().Check(sec, DataOperation.Delete);

            ManagedAccountBlog blog = new ManagedAccountBlog(Session, targetid);

            blog.GetACL().Check(sec, DataOperation.Create);

            // copy the post to a blog post
            AccountBlogPost t_post = new AccountBlogPost();

            t_post.AccountBlog    = Session.Load <AccountBlog>(targetid);
            t_post.AccountId      = mInstance.AccountId;
            t_post.EnableComments = true;
            t_post.Created        = mInstance.Created;
            t_post.Modified       = mInstance.Modified;
            t_post.Title          = mInstance.Subject;
            t_post.Body           = mInstance.Body;
            t_post.AccountName    = ManagedAccount.GetAccountNameWithDefault(Session, mInstance.AccountId);
            Session.Save(t_post);

            // move the comments thread to the blog comments
            int        discussion_id     = ManagedDiscussion.GetOrCreateDiscussionId(Session, "AccountBlogPost", t_post.Id, sec);
            Discussion target_discussion = Session.Load <Discussion>(discussion_id);

            // create the target thread
            DiscussionThread target_thread = new DiscussionThread();

            target_thread.Created    = mInstance.Created;
            target_thread.Modified   = mInstance.DiscussionThread.Modified;
            target_thread.Discussion = target_discussion;
            Session.Save(target_thread);

            // attach the post and all child posts to the target thread
            MoveToDiscussionThread(mInstance, target_thread);

            // nullify each child's parent
            foreach (DiscussionPost post in mInstance.DiscussionPosts)
            {
                post.DiscussionPostParent = null;
            }

            // delete the current post that became a blog entry
            Session.Delete(mInstance);
            return(t_post.Id);
        }
        protected override void Save(ManagedSecurityContext sec)
        {
            mInstance.Modified = DateTime.UtcNow;
            bool fNew = (mInstance.Id == 0);

            if (mInstance.Id == 0)
            {
                mInstance.Created = mInstance.Modified;
            }
            base.Save(sec);
            // create an admin account that owns the group
            if (fNew)
            {
                TransitAccountGroupAccount t_admin = new TransitAccountGroupAccount();
                t_admin.AccountGroupId  = mInstance.Id;
                t_admin.AccountId       = sec.Account.Id;
                t_admin.IsAdministrator = true;
                ManagedAccountGroupAccount m_admin = new ManagedAccountGroupAccount(Session);
                m_admin.CreateOrUpdate(t_admin, ManagedAccount.GetAdminSecurityContext(Session));
                ManagedDiscussion.GetOrCreateDiscussionId(Session, typeof(AccountGroup).Name, mInstance.Id, sec);
            }
        }
        public void Leave(int accountid, ManagedSecurityContext sec)
        {
            GetACL().Check(sec, DataOperation.Retreive);

            Account adminaccount      = null;
            bool    fHasAdministrator = false;
            bool    fMember           = false;

            foreach (AccountGroupAccount account in Collection <AccountGroupAccount> .GetSafeCollection(mInstance.AccountGroupAccounts))
            {
                if (account.Account.Id == accountid)
                {
                    Session.Delete(account);
                    fMember = true;
                }
                else if (account.IsAdministrator)
                {
                    // has at least one administrator left
                    fHasAdministrator = true;
                    adminaccount      = account.Account;
                }
            }

            if (!fMember)
            {
                throw new Exception("Not a member of the group.");
            }

            if (!fHasAdministrator)
            {
                // deleted the last administrator
                AccountGroupAccount admin = new AccountGroupAccount();
                admin.Account = ManagedAccount.GetAdminAccount(Session);
                if (admin.Account.Id == accountid)
                {
                    // the systme administrator tried to leave the group, he was last and is automatically re-added
                    throw new Exception("System administrator cannot be the last to leave a group.");
                }

                adminaccount = admin.Account;

                admin.AccountGroup    = mInstance;
                admin.Created         = admin.Modified = DateTime.UtcNow;
                admin.IsAdministrator = true;
                Session.Save(admin);
            }

            // orphan any invitations that this account sent
            foreach (AccountGroupAccountInvitation invitation in Collection <AccountGroupAccountInvitation> .GetSafeCollection(mInstance.AccountGroupAccountInvitations))
            {
                if (invitation.Requester.Id == accountid)
                {
                    invitation.Requester = adminaccount;
                    Session.Save(invitation);
                }
            }

            // orhphan group discussion
            int        discussion_id = ManagedDiscussion.GetOrCreateDiscussionId(Session, typeof(AccountGroup).Name, Id, sec);
            Discussion discussion    = Session.Load <Discussion>(discussion_id);

            if (discussion.Account.Id == accountid)
            {
                discussion.Account = adminaccount;
                Session.Save(discussion);
            }
        }