# -*- coding: UTF-8 -*-
#    This file is part of arena.
#
#    arena is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by the
#    Free Software Foundation; either version 3 of the License or, at your
#    option, any later version.
#
#    arena is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLITY
#    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
#    License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with arena.  If not, see <http://www.gnu.org/licenses/>.

## @file
#  This file defines a class to manage a user that is part of one or more
#  groups.

from Plugins.User.Users import AUser
from Plugins.User.Groups import AGroupList

## This class represents a user that belongs to one or more groups.
class AGroupedUser(AUser):
    ## Construct a new grouped user instance.
    #
    #  In addition to the parameters expected by AUser.__init__(), this routine
    #  expects that \p params will contain the following items:
    #  - groupCategory -- The name of the category (or table) within the data
    #                     source that is used to store group affiliations for
    #                     users.
    def __init__(Self, fieldsValues=None, **params):
        AUser.__init__(Self, **params)
        
        if "groupCategory" in Self._params:
            Self._groupCategory = Self._params["groupCategory"]
            del Self._params["groupCategory"]
    
    ## Initializer    
    def init(Self):
        result = AUser.init(Self)
        
        if result == True:
            # Establish private attributes
            # The name of the category that will be searched for group 
            # affiliations
            Self._groupCategory = None
            
            # Establish public attributes
            # The list of groups to which the user belongs
            Self.Groups = None
            
        return result
        
    ## Destructor
    def __del__(Self):
        if Self.Groups is not None:
            del Self.Groups
            
        AUser.__del__(Self)
        
    ## Fetch the user from the data source.
    def fetch(Self, userName):
        AUser.fetch(Self, userName)
        
        # Fetch the list of groups to which the user belongs
        Self.Groups = AGroupList(
            DataSource = Self._Source, 
            category = Self._groupCategory,
            user = userName,
            Log = Self._Log
        )
          
        Self.Groups.fetch()
        
    # Properties ---------------------------------------------------------------
    ## Retrieve the name of the category used to search for group information.
    def groupCategory(Self):
        return Self._groupCategory
