<commit_before>'''
This module provides the functionality to mediate the interaction between the RMAS ESB and the 
OpenEthics API, it handles messages recieved by the ESB, and depending on the message type, 
interacts with the OpenEthics API appropriatley.

@author: jasonmarshall
'''
import logging
from rmas_oe_adapter.parser import parse_event, parse_proposal_payload
from rmas_oe_adapter.mapping import get_ethics_user, persist_proposal_ethics_application_link
from rmas_oe_adapter.api import create_ethics_application


def handle_proposal_created(payload):
    '''
        This function handles the proposal created event. It will parse the payload
        to get the information that it needs to then create an OpenEthics application via the 
        OpenEthics API
    
    '''
    logging.info('handling proposal created message!')
    #if so retrieve the key information from the cerif payload:
    
    proposal_details = parse_proposal_payload(payload)
    
    ethics_user = get_ethics_user(proposal_details['principle_investigator_id'])
    
    if ethics_user:
        application_id = create_ethics_application(proposal_details['project_title'], ethics_user)
        persist_proposal_ethics_application_link(proposal_details['proposal_id'], application_id)
    
def handle_event(event):
    '''
    
        Handles the the received RMAS event.
        
        Currently it only handles proposal-created events.
        
        @param event the raw string of the rmas event
    '''
    logging.info('handling event: %s' % event)
    
    event_type, payload = parse_event(event)
    
    
    if event_type.lower() == 'proposal-created':
        handle_proposal_created(payload)<commit_msg>refactored the handle_proposal_created function<commit_after>'''
This module provides the functionality to mediate the interaction between the RMAS ESB and the 
OpenEthics API, it handles messages recieved by the ESB, and depending on the message type, 
interacts with the OpenEthics API appropriatley.

@author: jasonmarshall
'''
import logging
from rmas_oe_adapter.parser import parse_event, parse_proposal_payload
from rmas_oe_adapter.mapping import persist_proposal_ethics_application_link
from rmas_oe_adapter.api import create_ethics_application


def handle_proposal_created(payload):
    '''
        This function handles the proposal created event. It will parse the payload
        to get the information that it needs to then create an OpenEthics application via the 
        OpenEthics API
    
    '''
    logging.info('handling proposal created message!')
    #if so retrieve the key information from the cerif payload:
    
    proposal_details = parse_proposal_payload(payload)
    
    application_uri = create_ethics_application(proposal_details['project_title'], 
                                               email=proposal_details['principle_investigator_principle_email'])
    if application_uri != None:
        persist_proposal_ethics_application_link(proposal_details['proposal_id'], application_uri)
    
def handle_event(event):
    '''
    
        Handles the received RMAS event.
        
        Currently it only handles proposal-created events but in future this would fork out to different
        event based handler functions
        
        @param event the raw string of the rmas event
    '''
    logging.info('handling event: %s' % event)
    
    event_type, payload = parse_event(event)
    
    
    if event_type.lower() == 'proposal-created':
        handle_proposal_created(payload)
{'fraction_non_alphanumeric': 0.05596382136800452, 'fraction_numerical': 0.0, 'mean_word_length': 3.6080729166666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1974444', 'n_tokens_mistral': 963, 'n_tokens_neox': 923, 'n_words': 302}