# Path: pyehr/ehr/services/dbmanager/drivers/factory.py
# class DriversFactory(object):
# 
#     def __init__(self, driver, host, database, repository=None,
#                  port=None, user=None, passwd=None, index_service=None,
#                  logger=None):
#         self.driver = driver
#         self.host = host
#         self.database = database
#         self.repository = repository
#         self.port = port
#         self.user = user
#         self.passwd = passwd
#         self.index_service = index_service
#         self.logger = logger or get_logger('drivers-factory')
# 
#     def get_driver(self):
#         if self.driver == 'mongodb':
#             from pymongo import version as vsn
#             if int(vsn.split(".")[0]) < 3:
#                 from mongo_pm2 import MongoDriverPM2
#                 return MongoDriverPM2(self.host, self.database, self.repository,
#                                self.port, self.user, self.passwd,
#                                self.index_service, self.logger)
#             else:
#                 from mongo_pm3 import MongoDriverPM3
#                 return MongoDriverPM3(self.host, self.database, self.repository,
#                                self.port, self.user, self.passwd,
#                                self.index_service, self.logger)
#         elif self.driver == 'elasticsearch':
#             from elastic_search import ElasticSearchDriver
#             return ElasticSearchDriver([{"host":self.host,"port":self.port}],
#                                        self.database, self.repository,
#                                        user=self.user, passwd=self.passwd,
#                                        index_service=self.index_service, logger=self.logger)
#         else:
#             raise UnknownDriverError('Unknown driver: %s' % self.driver)
# 
# Path: pyehr/ehr/services/dbmanager/errors.py
# class OptimisticLockError(Exception):
#     pass
# 
# class RedundantUpdateError(Exception):
#     pass
# 
# class MissingRevisionError(Exception):
#     pass
# 
# class ConfigurationError(Exception):
#     pass


from hashlib import md5
from operator import attrgetter
from pyehr.ehr.services.dbmanager.drivers.factory import DriversFactory
from pyehr.utils import get_logger
from pyehr.ehr.services.dbmanager.errors import OptimisticLockError,\
    RedundantUpdateError, MissingRevisionError, ConfigurationError
    import simplejson as json
    import json

    def _get_drivers_factory(self, write_on_archive=False):
        if write_on_archive:
            repository = self.ehr_versioning_repository
            database = self.versioning_database or self.database
        else:
            repository = self.ehr_repository
            database = self.database
        return DriversFactory(
            driver=self.driver,
            host=self.host,
            database=database,
            repository=repository,
            port=self.port,
            user=self.user,
            passwd=self.passwd,
            logger=self.logger,
            index_service=self.index_service,
        )

    def _check_index_service(self):
        if not self.index_service:
            raise ConfigurationError('Operation not allowed, missing IndexService')

    def _check_redundant_update(self, new_record, old_record):
        new_record_hash = md5()
        new_record_hash.update(json.dumps(new_record.to_json()))
        old_record_hash = md5()
        old_record_hash.update(json.dumps(old_record.to_json()))
        if new_record_hash.hexdigest() == old_record_hash.hexdigest():

# Path: pyehr/ehr/services/dbmanager/drivers/factory.py
# class DriversFactory(object):
# 
#     def __init__(self, driver, host, database, repository=None,
#                  port=None, user=None, passwd=None, index_service=None,
#                  logger=None):
#         self.driver = driver
#         self.host = host
#         self.database = database
#         self.repository = repository
#         self.port = port
#         self.user = user
#         self.passwd = passwd
#         self.index_service = index_service
#         self.logger = logger or get_logger('drivers-factory')
# 
#     def get_driver(self):
#         if self.driver == 'mongodb':
#             from pymongo import version as vsn
#             if int(vsn.split(".")[0]) < 3:
#                 from mongo_pm2 import MongoDriverPM2
#                 return MongoDriverPM2(self.host, self.database, self.repository,
#                                self.port, self.user, self.passwd,
#                                self.index_service, self.logger)
#             else:
#                 from mongo_pm3 import MongoDriverPM3
#                 return MongoDriverPM3(self.host, self.database, self.repository,
#                                self.port, self.user, self.passwd,
#                                self.index_service, self.logger)
#         elif self.driver == 'elasticsearch':
#             from elastic_search import ElasticSearchDriver
#             return ElasticSearchDriver([{"host":self.host,"port":self.port}],
#                                        self.database, self.repository,
#                                        user=self.user, passwd=self.passwd,
#                                        index_service=self.index_service, logger=self.logger)
#         else:
#             raise UnknownDriverError('Unknown driver: %s' % self.driver)
# 
# Path: pyehr/ehr/services/dbmanager/errors.py
# class OptimisticLockError(Exception):
#     pass
# 
# class RedundantUpdateError(Exception):
#     pass
# 
# class MissingRevisionError(Exception):
#     pass
# 
# class ConfigurationError(Exception):
#     pass


# Path: pyehr/ehr/services/dbmanager/dbservices/version_manager.py
from hashlib import md5
from operator import attrgetter
from pyehr.ehr.services.dbmanager.drivers.factory import DriversFactory
from pyehr.utils import get_logger
from pyehr.ehr.services.dbmanager.errors import OptimisticLockError,\
    RedundantUpdateError, MissingRevisionError, ConfigurationError
    import simplejson as json
    import json


    def _get_drivers_factory(self, write_on_archive=False):
        if write_on_archive:
            repository = self.ehr_versioning_repository
            database = self.versioning_database or self.database
        else:
            repository = self.ehr_repository
            database = self.database
        return DriversFactory(
            driver=self.driver,
            host=self.host,
            database=database,
            repository=repository,
            port=self.port,
            user=self.user,
            passwd=self.passwd,
            logger=self.logger,
            index_service=self.index_service,
        )

    def _check_index_service(self):
        if not self.index_service:
            raise ConfigurationError('Operation not allowed, missing IndexService')

    def _check_redundant_update(self, new_record, old_record):
        new_record_hash = md5()
        new_record_hash.update(json.dumps(new_record.to_json()))
        old_record_hash = md5()
        old_record_hash.update(json.dumps(old_record.to_json()))
        if new_record_hash.hexdigest() == old_record_hash.hexdigest():

            raise RedundantUpdateError('Redundant update, old and new record are identical')