public RoleResult Save(UserRole userRole, BORoleRightXCollection roleRights)
        {
            try
            {
                if (userRole == null)
                {
                    return RoleResult.Failed;
                }
                else
                {
                    if (userRole.RoleId == SQLPersistent.NULL_INT64)
                    {
                        // Create Role
                        if (UserRole.GetByName(userRole.RoleName) != null)
                            return RoleResult.NameAlreadyExists;

                        BORole role = new BORole();
                        role.Name = userRole.RoleName;
                        role.RoleType = new BORoleType(userRole.RoleTypeId);
                        role.Description = userRole.Description;
                        role.Save();

                        UserRole newUserRole = UserRole.GetByName(userRole.RoleName);
                        if (newUserRole == null)
                        {
                            return RoleResult.Failed;
                        }
                        userRole = newUserRole;
                    }
                    UserRole.UserRoleResult userRoleResult = userRole.Save(roleRights);
                    switch (userRoleResult)
                    {
                        case UserRole.UserRoleResult.Success:
                            return RoleResult.Success;
                        case UserRole.UserRoleResult.NameAlreadyExists:
                            return RoleResult.NameAlreadyExists;
                        case UserRole.UserRoleResult.FailedToUpdateRights:
                            return RoleResult.FailedToUpdateRights;
                        default:
                            return RoleResult.Failed;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException("Failed to update Role.", ex);
                return RoleResult.Failed;
            }
        }
        private bool CreateRole(UserRole userRole, out string message, BOUser user)
        {
            message = string.Empty;
            if (userRole.RoleId == SQLPersistent.NULL_INT64)
            {
                if (UserRole.GetByName(userRole.RoleName) != null)
                {
                    message = string.Format("Failed to create Role: {0}. Role name is already in use.", userRole.RoleName);
                    return false;
                }

                Roles.CreateRole(userRole.RoleName);
                UserRole role = UserRole.GetByName(userRole.RoleName);
                if (userRole == null)
                {
                    message = string.Format("Failed to create Role: {0}", userRole.RoleName);
                    return false;
                }
                userRole.RoleId = role.RoleId;
                BOUserAudit.CreateAudit(user, "Role Created", userRole.RoleName);
            }
            else
            {
                BOUserAudit.CreateAudit(user, "Role Updated", userRole.RoleName);
            }
            return true;
        }
        public static UserRole GetByRoleId(string roleId)
        {
            logger.Trace("GetByRoleId");
            UserRole a = new UserRole();
            SqlConnection con = new SqlConnection(BOBase.GetConnectionString());

            con.Open();

            try
            {
                SqlCommand cmd = new SqlCommand("P_UserRole_GetByRoleId", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SQLPersistent.SetVarCharParameter(cmd.Parameters, "@RoleId", 50, roleId, false);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult);

                try
                {
                    if (rdr.Read())
                        a = new UserRole(rdr);
                    else
                        return null;
                }
                catch (Exception ex)
                {
                    logger.ErrorException("Failed to fetch record", ex);
                    return null;
                }
                finally
                {
                    rdr.Close();
                }
            }
            finally
            {
                con.Close();
            }
            return a;
        }