/// <summary>
        /// Add or update new user group.
        /// </summary>
		public GXUserGroupUpdateResponse Put(GXUserGroupUpdateRequest request)
		{
            IAuthSession s = this.GetSession(false);
            //Normal user can't change user group name or add new one.
            if (!GuruxAMI.Server.GXBasicAuthProvider.CanUserEdit(s))
            {
                throw new ArgumentException("Access denied.");
            }
            long adderId = Convert.ToInt64(s.Id);
            List<GXEventsItem> events = new List<GXEventsItem>();
            lock (Db)
            {
                using (var trans = Db.OpenTransaction(IsolationLevel.ReadCommitted))
                {
                    bool superAdmin = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s);
                    //Add new user groups           
                    foreach (GXAmiUserGroup it in request.UserGroups)
                    {
                        if (string.IsNullOrEmpty(it.Name))
                        {
                            throw new ArgumentException("Invalid name.");
                        }
                        //If new user group.
                        if (it.Id == 0)
                        {
                            it.Added = DateTime.Now.ToUniversalTime();
                            Db.Insert(it);
#if !SS4
                            it.Id = Db.GetLastInsertId();
#else
                            it.Id = Db.LastInsertId();
#endif                            
                            //Add adder to user group if adder is not super admin.
                            if (!superAdmin)
                            {
                                GXAmiUserGroupUser g = new GXAmiUserGroupUser();
                                g.UserID = Convert.ToInt64(s.Id);
                                g.UserGroupID = it.Id;
                                g.Added = DateTime.Now.ToUniversalTime();
                                Db.Insert(g);
                            }
                            events.Add(new GXEventsItem(ActionTargets.UserGroup, Actions.Add, it));
                        }
                        else //Update user group.
                        {
                            if (!superAdmin)
                            {
                                //User can't update user data if he do not have access to the user group.
                                long[] groups1 = GXUserGroupService.GetUserGroups(Db, adderId);
                                long[] groups2 = GXUserGroupService.GetUserGroups(Db, it.Id);
                                bool found = false;
                                foreach (long it1 in groups1)
                                {
                                    foreach (long it2 in groups2)
                                    {
                                        if (it1 == it2)
                                        {
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (found)
                                    {
                                        break;
                                    }
                                }
                                if (!found)
                                {
                                    throw new ArgumentException("Access denied.");
                                }
                            }
                            //Get Added time.
#if !SS4
                            GXAmiUserGroup orig = Db.GetById<GXAmiUserGroup>(it.Id);
#else
                            GXAmiUserGroup orig = Db.SingleById<GXAmiUserGroup>(it.Id);                            
#endif                            
                            it.Added = orig.Added.ToUniversalTime();
                            Db.Update(it);
                            events.Add(new GXEventsItem(ActionTargets.UserGroup, Actions.Edit, it));
                        }
                    }
                    trans.Commit();
                }
            }
            AppHost host = this.ResolveService<AppHost>();
            host.SetEvents(Db, this.Request, adderId, events);
            return new GXUserGroupUpdateResponse(request.UserGroups);
        }
        /// <summary>
        /// Add or update new user group.
        /// </summary>
        public GXUserGroupUpdateResponse Put(GXUserGroupUpdateRequest request)
        {
            IAuthSession s = this.GetSession(false);

            //Normal user can't change user group name or add new one.
            if (!GuruxAMI.Server.GXBasicAuthProvider.CanUserEdit(s))
            {
                throw new ArgumentException("Access denied.");
            }
            long adderId = Convert.ToInt64(s.Id);
            List <GXEventsItem> events = new List <GXEventsItem>();

            lock (Db)
            {
                using (var trans = Db.OpenTransaction(IsolationLevel.ReadCommitted))
                {
                    bool superAdmin = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s);
                    //Add new user groups
                    foreach (GXAmiUserGroup it in request.UserGroups)
                    {
                        if (string.IsNullOrEmpty(it.Name))
                        {
                            throw new ArgumentException("Invalid name.");
                        }
                        //If new user group.
                        if (it.Id == 0)
                        {
                            it.Added = DateTime.Now.ToUniversalTime();
                            Db.Insert(it);
#if !SS4
                            it.Id = Db.GetLastInsertId();
#else
                            it.Id = Db.LastInsertId();
#endif
                            //Add adder to user group if adder is not super admin.
                            if (!superAdmin)
                            {
                                GXAmiUserGroupUser g = new GXAmiUserGroupUser();
                                g.UserID      = Convert.ToInt64(s.Id);
                                g.UserGroupID = it.Id;
                                g.Added       = DateTime.Now.ToUniversalTime();
                                Db.Insert(g);
                            }
                            events.Add(new GXEventsItem(ActionTargets.UserGroup, Actions.Add, it));
                        }
                        else //Update user group.
                        {
                            if (!superAdmin)
                            {
                                //User can't update user data if he do not have access to the user group.
                                long[] groups1 = GXUserGroupService.GetUserGroups(Db, adderId);
                                long[] groups2 = GXUserGroupService.GetUserGroups(Db, it.Id);
                                bool   found   = false;
                                foreach (long it1 in groups1)
                                {
                                    foreach (long it2 in groups2)
                                    {
                                        if (it1 == it2)
                                        {
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (found)
                                    {
                                        break;
                                    }
                                }
                                if (!found)
                                {
                                    throw new ArgumentException("Access denied.");
                                }
                            }
                            //Get Added time.
#if !SS4
                            GXAmiUserGroup orig = Db.GetById <GXAmiUserGroup>(it.Id);
#else
                            GXAmiUserGroup orig = Db.SingleById <GXAmiUserGroup>(it.Id);
#endif
                            it.Added = orig.Added.ToUniversalTime();
                            Db.Update(it);
                            events.Add(new GXEventsItem(ActionTargets.UserGroup, Actions.Edit, it));
                        }
                    }
                    trans.Commit();
                }
            }
            AppHost host = this.ResolveService <AppHost>();
            host.SetEvents(Db, this.Request, adderId, events);
            return(new GXUserGroupUpdateResponse(request.UserGroups));
        }