# Path: rwslib/rws_requests/odm_adapter.py
# class AuditRecordsRequest(QueryOptionGetRequest):
#     """Clinical Audit Records Dataset"""
# 
#     KNOWN_QUERY_OPTIONS = ["studyoid", "startid", "per_page"]
# 
#     def __init__(self, project_name, environment_name, startid=1, per_page=100):
#         """
#         :param str project_name: Project Name
#         :param str environment_name: Environment Name
#         :param int startid: Starting Audit
#         :param int per_page: Page Size
#         """
#         self.project_name = project_name
#         self.environment_name = environment_name
#         self.startid = startid
#         self.per_page = per_page
# 
#     @property
#     def studyoid(self):
#         """Create studyoid from project name and environment"""
#         return "%s(%s)" % (self.project_name, self.environment_name)
# 
#     def url_path(self):
#         """Return url path list"""
#         return self.make_url(
#             "datasets", "ClinicalAuditRecords.odm", **self._querystring()
#         )
# 
# Path: rwslib/extras/audit_event/parser.py
# def parse(data, eventer):
#     """Parse the XML data, firing events from the eventer"""
#     parser = etree.XMLParser(target=ODMTargetParser(eventer))
#     return etree.XML(data, parser)  # Returns value of close


from rwslib.rws_requests.odm_adapter import AuditRecordsRequest
from six.moves.urllib.parse import urlparse, parse_qs
from rwslib.extras.audit_event.parser import parse
import logging
# -*- coding: utf-8 -*-
__author__ = 'isparks'



class ODMAdapter(object):
    """A self-contained data fetcher and parser using a RWSConnection and an event class provided by the user"""
    def __init__(self, rws_connection, study, environment, eventer):
        self.rws_connection = rws_connection
        self.eventer = eventer
        self.study = study
        self.environment = environment
        self.start_id = 0

    def get_next_start_id(self):
        """If link for next result set has been passed, extract it and get the next set start id"""
        link = self.rws_connection.last_result.links.get("next", None)
        if link:
            link = link['url']
            p = urlparse(link)
            start_id = int(parse_qs(p.query)['startid'][0])
            return start_id

        return None

    def run(self, start_id=0, max_pages=-1, per_page=1000, **kwargs):
        page = 0
        self.start_id = start_id
        while max_pages == -1 or (page < max_pages):


# Path: rwslib/rws_requests/odm_adapter.py
# class AuditRecordsRequest(QueryOptionGetRequest):
#     """Clinical Audit Records Dataset"""
# 
#     KNOWN_QUERY_OPTIONS = ["studyoid", "startid", "per_page"]
# 
#     def __init__(self, project_name, environment_name, startid=1, per_page=100):
#         """
#         :param str project_name: Project Name
#         :param str environment_name: Environment Name
#         :param int startid: Starting Audit
#         :param int per_page: Page Size
#         """
#         self.project_name = project_name
#         self.environment_name = environment_name
#         self.startid = startid
#         self.per_page = per_page
# 
#     @property
#     def studyoid(self):
#         """Create studyoid from project name and environment"""
#         return "%s(%s)" % (self.project_name, self.environment_name)
# 
#     def url_path(self):
#         """Return url path list"""
#         return self.make_url(
#             "datasets", "ClinicalAuditRecords.odm", **self._querystring()
#         )
# 
# Path: rwslib/extras/audit_event/parser.py
# def parse(data, eventer):
#     """Parse the XML data, firing events from the eventer"""
#     parser = etree.XMLParser(target=ODMTargetParser(eventer))
#     return etree.XML(data, parser)  # Returns value of close


# Path: rwslib/extras/audit_event/main.py
from rwslib.rws_requests.odm_adapter import AuditRecordsRequest
from six.moves.urllib.parse import urlparse, parse_qs
from rwslib.extras.audit_event.parser import parse
import logging

# -*- coding: utf-8 -*-
__author__ = 'isparks'



class ODMAdapter(object):
    """A self-contained data fetcher and parser using a RWSConnection and an event class provided by the user"""
    def __init__(self, rws_connection, study, environment, eventer):
        self.rws_connection = rws_connection
        self.eventer = eventer
        self.study = study
        self.environment = environment
        self.start_id = 0

    def get_next_start_id(self):
        """If link for next result set has been passed, extract it and get the next set start id"""
        link = self.rws_connection.last_result.links.get("next", None)
        if link:
            link = link['url']
            p = urlparse(link)
            start_id = int(parse_qs(p.query)['startid'][0])
            return start_id

        return None

    def run(self, start_id=0, max_pages=-1, per_page=1000, **kwargs):
        page = 0
        self.start_id = start_id
        while max_pages == -1 or (page < max_pages):


            req = AuditRecordsRequest(self.study, self.environment, startid=self.start_id, per_page=per_page)