def pull_hub_roles():
    '''
    Pull role list from hub
    
    Return role list of hub
    @since 1.1
    '''
    signature = 'pull_hub_roles'
    logginghelper.method_enter(logger, signature)
    request_to_node = create_node_request('/general_service')

    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerRoleListRequest>' \
        '</PartnerRoleListRequest>'
    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), \
            cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s', resp_content)
        root = ElementTree.fromstring(resp_content)
        roleElements = root.findall('./Role')
        roles = []
        for roleElement in roleElements:
            role_id = roleElement.findtext('./RoleID')
            name = roleElement.findtext('./Name')
            roles.append({'id': role_id, 'name': name})
        logginghelper.method_exit(logger, signature, roles)
        return roles
    except urllib.error.HTTPError as e:
        handleHTTPError(e, signature)
def pull_partner_statistics(partner_id):
    """
    This method pulls partner statistics.
    
    Parameters:
    - partner_id : the partner ID
    
    Returns:
    None
    """
    signature = "pull_partner_statistics(partner_id)"
    logginghelper.method_enter(logger, signature, partner_id)
    # Send request to HFPP network node
    request_to_node = urllib.request.Request(HFPP_NODE_HTTP_SERVICE_BASE_URL + '/general_service')
    request_to_node.add_header('Content-Type','application/xml;charset=utf-8')
    request_to_node.add_header('x-hfpp-username', HFPP_PARTNER_USERNAME)
    request_to_node.add_header('x-hfpp-password', HFPP_PARTNER_PASSWORD)
    
    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerStatisticsRequest>' \
        '<PartnerID>{partner_id}</PartnerID>' \
        '</PartnerStatisticsRequest>'.format(partner_id=partner_id)

    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), timeout=PARTNER_REQUEST_TIMEOUT, cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
    
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s',resp_content)
        root = ElementTree.fromstring(resp_content)
        count_of_data_requests_received = int(root.findtext('./NumberOfDataRequestsReceived'))
        count_of_data_requests_sent = int(root.findtext('./NumberOfDataRequestsInitiated'))
        count_of_data_requests_responded = int(root.findtext('./NumberOfDataRequestsResponded')) - INITIAL_RESPONDED_REQUESTS_VALUE
        count_of_data_requests_declined = int(root.findtext('./NumberOfDataRequestsDeclined'))
        count_of_data_requests_pending = count_of_data_requests_received - count_of_data_requests_responded - count_of_data_requests_declined
        reciprocity = count_of_data_requests_responded * 1.0 / count_of_data_requests_received if count_of_data_requests_received > 0 else 0
        Partner.objects.filter(hfpp_network_id=partner_id).update(
            count_of_data_requests_received=count_of_data_requests_received,
            count_of_data_requests_sent=count_of_data_requests_sent,
            count_of_data_requests_responded=count_of_data_requests_responded,
            count_of_data_requests_declined=count_of_data_requests_declined,
            count_of_data_requests_pending=count_of_data_requests_pending,
            reciprocity=reciprocity
        )
    except urllib.error.HTTPError as e:
    
        # Parse response XML
        resp_content = e.read().decode('utf-8')
        logger.debug('response:%s',resp_content)
        try:
            root = ElementTree.fromstring(resp_content)
            # Not succeeded
            # 400, 401, 403 or 500
            error_code = root.findtext('./ErrorCode')
            error_message = root.findtext('./ErrorMessage')
            # Log error code and error message
            logging.error('error code:%s',error_code)
            logging.error('error message:%s',error_message)
        except Exception as e:
            logging.exception("")
    logginghelper.method_exit(logger, signature)
def pull_hub_partner(partner_id):
    '''
    Pull partner from hub
    
    Parameters:
    - partner_id : the partner id
    
    Return partner of hub
    @since 1.1
    '''
    signature = 'pull_hub_partner(partner_id)'
    logginghelper.method_enter(logger, signature, {'partner_id': partner_id})
    request_to_node = create_node_request('/general_service')

    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerGetRequest>' \
        '<PartnerID>{partner_id}</PartnerID>' \
        '</PartnerGetRequest>'.format(partner_id=partner_id)
    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), \
            cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s', resp_content)
        root = ElementTree.fromstring(resp_content)
        username = root.findtext('./Username')
        organization_name = root.findtext('./OrganizationName')
        role_id = root.findtext('./RoleID')
        auto_retrieve_cached_data = root.findtext('./AutoRetrieveCachedData')
        partner = {'username':username,'organization_name':organization_name,'role_id':role_id, \
            'auto_retrieve_cached_data':auto_retrieve_cached_data}
        logginghelper.method_exit(logger, signature, partner)
        return partner
    except urllib.error.HTTPError as e:
        handleHTTPError(e, signature)
def delete_hub_partner(partner_id):
    '''
    Pull partner from hub
    
    Parameters:
    - partner_id : the partner id
        
    Return partner id if success
    @since 1.1
    '''
    signature = 'delete_hub_partner(partner_id)'
    logginghelper.method_enter(logger, signature, {'partner_id': partner_id})
    request_to_node = create_node_request('/general_service')

    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerDeleteRequest>' \
        '<PartnerID>{partner_id}</PartnerID>' \
        '</PartnerDeleteRequest>'.format(partner_id=partner_id)
    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), \
            cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s', resp_content)
        root = ElementTree.fromstring(resp_content)
        partner_id = root.findtext('./PartnerID')
        logginghelper.method_exit(logger, signature, partner_id)
        return partner_id

    except urllib.error.HTTPError as e:
        handleHTTPError(e, signature)
def delete_hub_partner(partner_id):
    '''
    Pull partner from hub
    
    Parameters:
    - partner_id : the partner id
        
    Return partner id if success
    @since 1.1
    '''
    signature = 'delete_hub_partner(partner_id)'
    logginghelper.method_enter(logger, signature, {'partner_id':partner_id})
    request_to_node = create_node_request('/general_service')

    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerDeleteRequest>' \
        '<PartnerID>{partner_id}</PartnerID>' \
        '</PartnerDeleteRequest>'.format(partner_id=partner_id)
    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), \
            cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s',resp_content)
        root = ElementTree.fromstring(resp_content)
        partner_id = root.findtext('./PartnerID')
        logginghelper.method_exit(logger, signature, partner_id)
        return partner_id

    except urllib.error.HTTPError as e:
        handleHTTPError(e, signature)
def pull_hub_partner(partner_id):
    '''
    Pull partner from hub
    
    Parameters:
    - partner_id : the partner id
    
    Return partner of hub
    @since 1.1
    '''
    signature = 'pull_hub_partner(partner_id)'
    logginghelper.method_enter(logger, signature, {'partner_id':partner_id})
    request_to_node = create_node_request('/general_service')

    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerGetRequest>' \
        '<PartnerID>{partner_id}</PartnerID>' \
        '</PartnerGetRequest>'.format(partner_id=partner_id)
    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), \
            cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s',resp_content)
        root = ElementTree.fromstring(resp_content)
        username = root.findtext('./Username')
        organization_name = root.findtext('./OrganizationName')
        role_id = root.findtext('./RoleID')
        auto_retrieve_cached_data = root.findtext('./AutoRetrieveCachedData')
        partner = {'username':username,'organization_name':organization_name,'role_id':role_id, \
            'auto_retrieve_cached_data':auto_retrieve_cached_data}
        logginghelper.method_exit(logger, signature, partner)
        return partner
    except urllib.error.HTTPError as e:
        handleHTTPError(e, signature)
def pull_hub_roles():
    '''
    Pull role list from hub
    
    Return role list of hub
    @since 1.1
    '''
    signature = 'pull_hub_roles'
    logginghelper.method_enter(logger, signature)
    request_to_node = create_node_request('/general_service')

    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerRoleListRequest>' \
        '</PartnerRoleListRequest>'
    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), \
            cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s',resp_content)
        root = ElementTree.fromstring(resp_content)
        roleElements = root.findall('./Role')
        roles = []
        for roleElement in roleElements:
            role_id = roleElement.findtext('./RoleID')
            name = roleElement.findtext('./Name')
            roles.append({'id':role_id, 'name': name})
        logginghelper.method_exit(logger, signature, roles)
        return roles
    except urllib.error.HTTPError as e:
        handleHTTPError(e, signature)
def run_pull_partner_statistics_job():
    """
    Run pull partner statistics job.
    
    Parameters:
    None
    
    Returns:
    None
    """
    signature = "run_pull_partner_statistics_job()"
    logginghelper.method_enter(logger, signature)
    for partner in Partner.objects.all():
        pull_partner_statistics(partner.hfpp_network_id)
    logginghelper.method_exit(logger, signature)
def run_study_finalization_job():
    """
    Run study finalization job.
    
    Parameters:
    None
    
    Returns:
    None
    """
    signature = "run_study_finalization_job()"
    logginghelper.method_enter(logger, signature)
    completed_on = datetime.datetime.now()
    Study.objects.filter(expiration_time__lt=datetime.datetime.now(), status__exact=1).update(status=2, completed_on=completed_on)
    logginghelper.method_exit(logger, signature)
def edit_hub_partner(partner_id,
                     username,
                     organization_name,
                     role_id,
                     auto_retrieve_cached_data,
                     password=''):
    '''
    Edit partner of hub

    Parameters:
    - partner_id : the partner id
    - username : the username
    - organization_name : the organization nae
    - role_id : the role id
    - auto_retrieve_cached_data : whether auto retrieve cached data
    - password : the password

    Return partner id if success
    @since 1.1
    '''
    signature = 'edit_hub_partner(partner_id, username, organization_name, role_id, auto_retrieve_cached_data, password)'
    logginghelper.method_enter(logger, signature, {'partner_id': partner_id, 'username':username, \
        'organization_name': organization_name, 'role_id': role_id, \
        'auto_retrieve_cached_data': auto_retrieve_cached_data, 'password': password})
    request_to_node = create_node_request('/general_service')

    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerEditRequest>' \
        '<PartnerID>{partner_id}</PartnerID>' \
        '<Username>{username}</Username>' \
        '<Password>{password}</Password>' \
        '<OrganizationName>{organization_name}</OrganizationName>' \
        '<RoleID>{role_id}</RoleID>' \
        '<AutoRetrieveCachedData>{auto_retrieve_cached_data}</AutoRetrieveCachedData>' \
        '</PartnerEditRequest>'.format(partner_id=partner_id,username=username,organization_name=organization_name,\
            role_id=role_id,auto_retrieve_cached_data=auto_retrieve_cached_data,password=password)
    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), \
            cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s', resp_content)
        root = ElementTree.fromstring(resp_content)
        partner_id = root.findtext('./PartnerID')
        logginghelper.method_exit(logger, signature, partner_id)
        return partner_id
    except urllib.error.HTTPError as e:
        handleHTTPError(e, signature)
def edit_hub_partner(partner_id, username, organization_name, role_id, auto_retrieve_cached_data, password = ''):
    '''
    Edit partner of hub

    Parameters:
    - partner_id : the partner id
    - username : the username
    - organization_name : the organization nae
    - role_id : the role id
    - auto_retrieve_cached_data : whether auto retrieve cached data
    - password : the password

    Return partner id if success
    @since 1.1
    '''
    signature = 'edit_hub_partner(partner_id, username, organization_name, role_id, auto_retrieve_cached_data, password)'
    logginghelper.method_enter(logger, signature, {'partner_id': partner_id, 'username':username, \
        'organization_name': organization_name, 'role_id': role_id, \
        'auto_retrieve_cached_data': auto_retrieve_cached_data, 'password': password})
    request_to_node = create_node_request('/general_service')

    request_xml = '<?xml version="1.0" encoding="utf-8"?>' \
        '<PartnerEditRequest>' \
        '<PartnerID>{partner_id}</PartnerID>' \
        '<Username>{username}</Username>' \
        '<Password>{password}</Password>' \
        '<OrganizationName>{organization_name}</OrganizationName>' \
        '<RoleID>{role_id}</RoleID>' \
        '<AutoRetrieveCachedData>{auto_retrieve_cached_data}</AutoRetrieveCachedData>' \
        '</PartnerEditRequest>'.format(partner_id=partner_id,username=username,organization_name=organization_name,\
            role_id=role_id,auto_retrieve_cached_data=auto_retrieve_cached_data,password=password)
    try:
        response_from_node = urllib.request.urlopen(request_to_node, request_xml.encode(), \
            cafile=CA_CERTIFICATE_FILE, cadefault=CA_DEFAULT)
        resp_content = response_from_node.read().decode('utf-8')
        logger.debug('response:%s',resp_content)
        root = ElementTree.fromstring(resp_content)
        partner_id = root.findtext('./PartnerID')
        logginghelper.method_exit(logger, signature, partner_id)
        return partner_id
    except urllib.error.HTTPError as e:
        handleHTTPError(e, signature)
def handleHTTPError(e, signature):
    """
    Handle HTTP error
    
    Parameters:
    - claimType : claim data type
    """
    # Parse response XML
    resp_content = e.read().decode('utf-8')
    logger.debug('response:%s', resp_content)
    try:
        root = ElementTree.fromstring(resp_content)
        # Not succeeded
        # 400, 401, 403 or 500
        error_code = root.findtext('./ErrorCode')
        error_message = root.findtext('./ErrorMessage')
        # Log error code and error message
        logginghelper.method_exit(logger, signature, \
            'HTTP error code:%s, error message:%s'%(error_code, error_message))
    except Exception as e:
        logger.exception("")
def handleHTTPError(e, signature):
    """
    Handle HTTP error
    
    Parameters:
    - claimType : claim data type
    """
    # Parse response XML
    resp_content = e.read().decode('utf-8')
    logger.debug('response:%s',resp_content)
    try:
        root = ElementTree.fromstring(resp_content)
        # Not succeeded
        # 400, 401, 403 or 500
        error_code = root.findtext('./ErrorCode')
        error_message = root.findtext('./ErrorMessage')
        # Log error code and error message
        logginghelper.method_exit(logger, signature, \
            'HTTP error code:%s, error message:%s'%(error_code, error_message))
    except Exception as e:
        logger.exception("")