# 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
    def __str__(self):

        return "project %s [%s]: %s" \
                 % (self.code,
                    self.domain,
                    self.description)

    def __eq__(self, other):

        return other.code == self.code

    def find(self, db):
        """Search the Project in the database based on its code. If exactly
           one project matches in database, set project_id attribute properly
           and returns its value. If more than one project matches, raises
           HPCStatsDBIntegrityError. If no project is found, returns None.
        """

        req = """
                SELECT project_id
                  FROM Project
                 WHERE project_code = %s
              """
        params = ( self.code, )
        db.execute(req, params)
        nb_rows = db.cur.rowcount
        if nb_rows == 0:
            logger.debug("project %s not found in DB", str(self))
            return None
        elif nb_rows > 1:

# 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

    def __str__(self):

        return "project %s [%s]: %s" \
                 % (self.code,
                    self.domain,
                    self.description)

    def __eq__(self, other):

        return other.code == self.code

    def find(self, db):
        """Search the Project in the database based on its code. If exactly
           one project matches in database, set project_id attribute properly
           and returns its value. If more than one project matches, raises
           HPCStatsDBIntegrityError. If no project is found, returns None.
        """

        req = """
                SELECT project_id
                  FROM Project
                 WHERE project_code = %s
              """
        params = ( self.code, )
        db.execute(req, params)
        nb_rows = db.cur.rowcount
        if nb_rows == 0:
            logger.debug("project %s not found in DB", str(self))
            return None
        elif nb_rows > 1:

            raise HPCStatsDBIntegrityError(