# Path: HPCStats/Exceptions.py
# class HPCStatsDBIntegrityError(HPCStatsException):
# 
#     """Class for DB intregrity errors exceptions in HPCStats"""
# 
#     def __init__(self, msg):
# 
#         super(HPCStatsDBIntegrityError, self).__init__(msg)
# 
# class HPCStatsRuntimeError(HPCStatsException):
# 
#     """Class for runtime errors exceptions in HPCStats"""
# 
#     def __init__(self, msg):
# 
#         super(HPCStatsRuntimeError, self).__init__(msg)
# 
# Path: HPCStats/Model/User.py
# class User(object):
# 
#     """Model class for the Userhpc table."""
# 
#     def __init__(self, login, firstname, lastname, department, user_id=None):
# 
#         self.user_id = user_id
#         self.login = login
#         self.lastname = lastname
#         self.firstname = firstname
#         self.department = department
# 
#     def __str__(self):
# 
#         return str(self.firstname) + " " + str(self.lastname) \
#                + " [" + str(self.department) + "] " + str(self.login)
# 
#     def __eq__(self, other):
# 
#         return self.login == other.login
# 
#     def find(self, db):
#         """Search the User in the database based on its login. If exactly
#            one user matches in database, set user_id attribute properly
#            and returns its value. If more than one user matches, raises
#            HPCStatsDBIntegrityError. If no user is found, returns None.
#         """
# 
#         req = """
#                 SELECT userhpc_id
#                   FROM Userhpc
#                  WHERE userhpc_login = %s
#               """
#         params = ( self.login, )
#         db.execute(req, params)
#         nb_rows = db.cur.rowcount
#         if nb_rows == 0:
#             logger.debug("user %s not found in DB", str(self))
#             return None
#         elif nb_rows > 1:
#             raise HPCStatsDBIntegrityError(
#                     "several user_id found in DB for user %s" \
#                       % (str(self)))
#         else:
#             self.user_id = db.cur.fetchone()[0]
#             logger.debug("user %s found in DB with id %d",
#                           str(self),
#                           self.user_id )
#             return self.user_id
# 
#     def save(self, db):
#         """Insert User in database. You must make sure that the User does
#            not already exist in database yet (typically using User.find()
#            method else there is a risk of future integrity errors because of
#            duplicated users. If user_id attribute is set, it raises
#            HPCStatsRuntimeError.
#         """
# 
#         if self.user_id is not None:
#             raise HPCStatsRuntimeError(
#                     "could not insert user %s since already existing in "\
#                     "database" \
#                       % (str(self)))
# 
#         req = """
#                 INSERT INTO Userhpc ( userhpc_login,
#                                       userhpc_firstname,
#                                       userhpc_name,
#                                       userhpc_department)
#                 VALUES ( %s, %s, %s, %s)
#                 RETURNING userhpc_id
#               """
#         params = ( self.login,
#                    self.firstname,
#                    self.lastname,
#                    self.department )
# 
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)
#         self.user_id = db.cur.fetchone()[0]
# 
#     def update(self, db):
#         """Update User firstname, lastname and department fields in database.
#            Raises HPCStatsRuntimeError is user_id is None.
#         """
# 
#         if self.user_id is None:
#             raise HPCStatsRuntimeError(
#                     "could not update user %s since not found in database" \
#                       % (str(self)))
# 
#         req = """
#                 UPDATE Userhpc
#                    SET userhpc_firstname = %s,
#                        userhpc_name = %s,
#                        userhpc_department = %s
#                 WHERE userhpc_id = %s
#               """
#         params = ( self.firstname,
#                    self.lastname,
#                    self.department,
#                    self.user_id )
# 
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)


import logging
from HPCStats.Exceptions import HPCStatsDBIntegrityError, HPCStatsRuntimeError
from HPCStats.Model.User import User
                SELECT account_uid
                  FROM Account
                 WHERE userhpc_id = %s
                   AND cluster_id = %s
              """
        params = ( self.user.user_id,
                   self.cluster.cluster_id )
        db.execute(req, params)
        nb_rows = db.cur.rowcount
        if nb_rows == 0:
            logger.debug("account %s not found in DB", str(self))
            self.exists = False
        elif nb_rows > 1:
            raise HPCStatsDBIntegrityError(
                    "several account found in DB for account %s" \
                      % (str(self)))
        else:
            logger.debug("account %s found in DB", str(self))
            self.exists = True
        return self.exists

    def load(self, db):
        """Load the Account based on the content of the DB and set objects
           attributes accordingly. It calls existing() method if needed and
           raises HPCStatsRuntimeError if the Account is not found in DB.
        """

        if self.exists is None: # not checked yet
            self.existing(db)
        if self.exists is False:

# Path: HPCStats/Exceptions.py
# class HPCStatsDBIntegrityError(HPCStatsException):
# 
#     """Class for DB intregrity errors exceptions in HPCStats"""
# 
#     def __init__(self, msg):
# 
#         super(HPCStatsDBIntegrityError, self).__init__(msg)
# 
# class HPCStatsRuntimeError(HPCStatsException):
# 
#     """Class for runtime errors exceptions in HPCStats"""
# 
#     def __init__(self, msg):
# 
#         super(HPCStatsRuntimeError, self).__init__(msg)
# 
# Path: HPCStats/Model/User.py
# class User(object):
# 
#     """Model class for the Userhpc table."""
# 
#     def __init__(self, login, firstname, lastname, department, user_id=None):
# 
#         self.user_id = user_id
#         self.login = login
#         self.lastname = lastname
#         self.firstname = firstname
#         self.department = department
# 
#     def __str__(self):
# 
#         return str(self.firstname) + " " + str(self.lastname) \
#                + " [" + str(self.department) + "] " + str(self.login)
# 
#     def __eq__(self, other):
# 
#         return self.login == other.login
# 
#     def find(self, db):
#         """Search the User in the database based on its login. If exactly
#            one user matches in database, set user_id attribute properly
#            and returns its value. If more than one user matches, raises
#            HPCStatsDBIntegrityError. If no user is found, returns None.
#         """
# 
#         req = """
#                 SELECT userhpc_id
#                   FROM Userhpc
#                  WHERE userhpc_login = %s
#               """
#         params = ( self.login, )
#         db.execute(req, params)
#         nb_rows = db.cur.rowcount
#         if nb_rows == 0:
#             logger.debug("user %s not found in DB", str(self))
#             return None
#         elif nb_rows > 1:
#             raise HPCStatsDBIntegrityError(
#                     "several user_id found in DB for user %s" \
#                       % (str(self)))
#         else:
#             self.user_id = db.cur.fetchone()[0]
#             logger.debug("user %s found in DB with id %d",
#                           str(self),
#                           self.user_id )
#             return self.user_id
# 
#     def save(self, db):
#         """Insert User in database. You must make sure that the User does
#            not already exist in database yet (typically using User.find()
#            method else there is a risk of future integrity errors because of
#            duplicated users. If user_id attribute is set, it raises
#            HPCStatsRuntimeError.
#         """
# 
#         if self.user_id is not None:
#             raise HPCStatsRuntimeError(
#                     "could not insert user %s since already existing in "\
#                     "database" \
#                       % (str(self)))
# 
#         req = """
#                 INSERT INTO Userhpc ( userhpc_login,
#                                       userhpc_firstname,
#                                       userhpc_name,
#                                       userhpc_department)
#                 VALUES ( %s, %s, %s, %s)
#                 RETURNING userhpc_id
#               """
#         params = ( self.login,
#                    self.firstname,
#                    self.lastname,
#                    self.department )
# 
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)
#         self.user_id = db.cur.fetchone()[0]
# 
#     def update(self, db):
#         """Update User firstname, lastname and department fields in database.
#            Raises HPCStatsRuntimeError is user_id is None.
#         """
# 
#         if self.user_id is None:
#             raise HPCStatsRuntimeError(
#                     "could not update user %s since not found in database" \
#                       % (str(self)))
# 
#         req = """
#                 UPDATE Userhpc
#                    SET userhpc_firstname = %s,
#                        userhpc_name = %s,
#                        userhpc_department = %s
#                 WHERE userhpc_id = %s
#               """
#         params = ( self.firstname,
#                    self.lastname,
#                    self.department,
#                    self.user_id )
# 
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)


# Path: HPCStats/Model/Account.py
import logging
from HPCStats.Exceptions import HPCStatsDBIntegrityError, HPCStatsRuntimeError
from HPCStats.Model.User import User

                SELECT account_uid
                  FROM Account
                 WHERE userhpc_id = %s
                   AND cluster_id = %s
              """
        params = ( self.user.user_id,
                   self.cluster.cluster_id )
        db.execute(req, params)
        nb_rows = db.cur.rowcount
        if nb_rows == 0:
            logger.debug("account %s not found in DB", str(self))
            self.exists = False
        elif nb_rows > 1:
            raise HPCStatsDBIntegrityError(
                    "several account found in DB for account %s" \
                      % (str(self)))
        else:
            logger.debug("account %s found in DB", str(self))
            self.exists = True
        return self.exists

    def load(self, db):
        """Load the Account based on the content of the DB and set objects
           attributes accordingly. It calls existing() method if needed and
           raises HPCStatsRuntimeError if the Account is not found in DB.
        """

        if self.exists is None: # not checked yet
            self.existing(db)
        if self.exists is False:

            raise HPCStatsRuntimeError(