# 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
        return record

    def extend_list(self, record, list_label, elements, last_update_label=None):
        current_revision = self._get_current_revision(record.record_id)
        self._check_optimistic_lock(current_revision, record.version)
        self._move_record_to_archive(current_revision)
        drf = self._get_drivers_factory()
        with drf.get_driver() as driver:
            last_update = driver.extend_list(record.record_id, list_label, elements,
                                             last_update_label, True)
        record.last_update = last_update
        record.increase_version()
        return record

    def remove_from_list(self, record, list_label, element, last_update_label=None):
        current_revision = self._get_current_revision(record.record_id)
        self._check_optimistic_lock(current_revision, record.version)
        self._move_record_to_archive(current_revision)
        drf = self._get_drivers_factory()
        with drf.get_driver() as driver:
            last_update = driver.remove_from_list(record.record_id, list_label, element,
                                                  last_update_label, True)
        record.last_update = last_update
        record.increase_version()
        return record

    def restore_revision(self, record_id, revision):
        original_record = self.get_revision(record_id, revision,
                                            convert_to_clinical_record=True)
        if not original_record:

# 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

        return record

    def extend_list(self, record, list_label, elements, last_update_label=None):
        current_revision = self._get_current_revision(record.record_id)
        self._check_optimistic_lock(current_revision, record.version)
        self._move_record_to_archive(current_revision)
        drf = self._get_drivers_factory()
        with drf.get_driver() as driver:
            last_update = driver.extend_list(record.record_id, list_label, elements,
                                             last_update_label, True)
        record.last_update = last_update
        record.increase_version()
        return record

    def remove_from_list(self, record, list_label, element, last_update_label=None):
        current_revision = self._get_current_revision(record.record_id)
        self._check_optimistic_lock(current_revision, record.version)
        self._move_record_to_archive(current_revision)
        drf = self._get_drivers_factory()
        with drf.get_driver() as driver:
            last_update = driver.remove_from_list(record.record_id, list_label, element,
                                                  last_update_label, True)
        record.last_update = last_update
        record.increase_version()
        return record

    def restore_revision(self, record_id, revision):
        original_record = self.get_revision(record_id, revision,
                                            convert_to_clinical_record=True)
        if not original_record:

            raise MissingRevisionError('Unable to retrieve version %d for record %s' %