# 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/Domain.py
# class Domain(object):
# 
#     """Model class for the Domain table."""
# 
#     def __init__(self, key, name):
#        
#         self.key = key
#         self.name = name
# 
#         self.exists = None
# 
#     def __str__(self):
# 
#         return "domain [%s] %s" % (self.key, self.name)
# 
#     def __eq__(self, domain):
# 
#         return domain.key == self.key
# 
#     def existing(self, db):
#         """Returns True if the domain already exists in database (same key), or
#            False if not.
#         """
# 
#         req = """
#                 SELECT domain_id
#                   FROM Domain
#                  WHERE domain_id = %s
#               """
#         params = ( self.key, )
#         db.execute(req, params)
#         nb_rows = db.cur.rowcount
#         if nb_rows == 0:
#             logger.debug("domain %s not found in DB", str(self))
#             self.exists = False
#         elif nb_rows > 1:
#             raise HPCStatsDBIntegrityError(
#                     "several domain found in DB for domain %s" \
#                       % (str(self)))
#         else:
#             logger.debug("domain %s found in DB", str(self))
#             self.exists = True
#         return self.exists
# 
#     def save(self, db):
#         """Insert Domain in database. It first makes sure that the Domain does
#            not already exist in database yet by calling Domain.existing()
#            method if needed. If the domain already exists in database, it
#            raises HPCStatsRuntimeError.
#         """
# 
#         if self.exists is None: # not checked yet
#             self.existing(db)
#         if self.exists is True:
#             raise HPCStatsRuntimeError(
#                     "could not insert domain %s since already existing in "\
#                     "database" % (str(self)))
# 
#         req = """
#                 INSERT INTO Domain ( domain_id,
#                                      domain_name )
#                 VALUES ( %s, %s )
#               """
#         params = ( self.key,
#                    self.name )
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)
#         self.exists = True
# 
#     def update(self, db):
#         """Update Domain name in database. Raises HPCStatsRuntimeError if
#            exists is False.
#         """
# 
#         if self.exists is None: # not checked yet
#             self.existing(db)
#         if self.exists is False:
#             raise HPCStatsRuntimeError(
#                     "could not update domain %s since not found in database" \
#                       % (str(self)))
# 
#         req = """
#                 UPDATE Domain
#                    SET domain_name = %s
#                  WHERE domain_id = %s
#               """
#         params = ( self.name,
#                    self.key )
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)


import logging
from HPCStats.Exceptions import HPCStatsDBIntegrityError, HPCStatsRuntimeError
from HPCStats.Model.Domain import Domain
                         self.project_id )
            return self.project_id

    def load(self, db):
        """Load the Project based on the content of the DB and set objects
           attributes accordingly. The project_id attribute must have been
           set previously, typically by calling find() method. It raises
           raises HPCStatsRuntimeError if the Project is not found in DB.
        """

        if self.project_id is None:
            raise HPCStatsRuntimeError(
                    "could not load project %s since not found in database" \
                      % (str(self)))

        req = """
                SELECT project_code,
                       project_description,
                       domain_id
                  FROM Project
                 WHERE project_id = %s
              """
        params = ( self.project_id, )
        db.execute(req, params)
        # We know here there is only one result thanks to existing() method
        result = db.cur.fetchone()
        self.code = result[0]
        self.description = result[1]
        # build the Domain object with the key found in DB
        domain_key = result[2]

# 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/Domain.py
# class Domain(object):
# 
#     """Model class for the Domain table."""
# 
#     def __init__(self, key, name):
#        
#         self.key = key
#         self.name = name
# 
#         self.exists = None
# 
#     def __str__(self):
# 
#         return "domain [%s] %s" % (self.key, self.name)
# 
#     def __eq__(self, domain):
# 
#         return domain.key == self.key
# 
#     def existing(self, db):
#         """Returns True if the domain already exists in database (same key), or
#            False if not.
#         """
# 
#         req = """
#                 SELECT domain_id
#                   FROM Domain
#                  WHERE domain_id = %s
#               """
#         params = ( self.key, )
#         db.execute(req, params)
#         nb_rows = db.cur.rowcount
#         if nb_rows == 0:
#             logger.debug("domain %s not found in DB", str(self))
#             self.exists = False
#         elif nb_rows > 1:
#             raise HPCStatsDBIntegrityError(
#                     "several domain found in DB for domain %s" \
#                       % (str(self)))
#         else:
#             logger.debug("domain %s found in DB", str(self))
#             self.exists = True
#         return self.exists
# 
#     def save(self, db):
#         """Insert Domain in database. It first makes sure that the Domain does
#            not already exist in database yet by calling Domain.existing()
#            method if needed. If the domain already exists in database, it
#            raises HPCStatsRuntimeError.
#         """
# 
#         if self.exists is None: # not checked yet
#             self.existing(db)
#         if self.exists is True:
#             raise HPCStatsRuntimeError(
#                     "could not insert domain %s since already existing in "\
#                     "database" % (str(self)))
# 
#         req = """
#                 INSERT INTO Domain ( domain_id,
#                                      domain_name )
#                 VALUES ( %s, %s )
#               """
#         params = ( self.key,
#                    self.name )
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)
#         self.exists = True
# 
#     def update(self, db):
#         """Update Domain name in database. Raises HPCStatsRuntimeError if
#            exists is False.
#         """
# 
#         if self.exists is None: # not checked yet
#             self.existing(db)
#         if self.exists is False:
#             raise HPCStatsRuntimeError(
#                     "could not update domain %s since not found in database" \
#                       % (str(self)))
# 
#         req = """
#                 UPDATE Domain
#                    SET domain_name = %s
#                  WHERE domain_id = %s
#               """
#         params = ( self.name,
#                    self.key )
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)


# Path: HPCStats/Model/Project.py
import logging
from HPCStats.Exceptions import HPCStatsDBIntegrityError, HPCStatsRuntimeError
from HPCStats.Model.Domain import Domain

                         self.project_id )
            return self.project_id

    def load(self, db):
        """Load the Project based on the content of the DB and set objects
           attributes accordingly. The project_id attribute must have been
           set previously, typically by calling find() method. It raises
           raises HPCStatsRuntimeError if the Project is not found in DB.
        """

        if self.project_id is None:
            raise HPCStatsRuntimeError(
                    "could not load project %s since not found in database" \
                      % (str(self)))

        req = """
                SELECT project_code,
                       project_description,
                       domain_id
                  FROM Project
                 WHERE project_id = %s
              """
        params = ( self.project_id, )
        db.execute(req, params)
        # We know here there is only one result thanks to existing() method
        result = db.cur.fetchone()
        self.code = result[0]
        self.description = result[1]
        # build the Domain object with the key found in DB
        domain_key = result[2]

        self.domain = Domain(domain_key, None)