def insert_unmatched_record(record_dictionary):
    '''Inserts the record into the clin.incoming_data_unmatched directly into the GNUMed database. Takes in a dictionary containing
    the needed data.'''
    try:
        conn = psycopg2.connect(connection_string)
        cursor = conn.cursor()
        insert_query = """SET TRANSACTION READ WRITE;
                    INSERT into clin.incoming_data_unmatched
                    (external_data_id, lastnames, firstnames, gender, type, requestor, request_id, other_info, data)
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s); """

        #print insert_query
        cursor.execute(insert_query, (record_dictionary['external_id'], record_dictionary['last_name'],
                                      record_dictionary['first_name'], record_dictionary['gender'],
                                      record_dictionary['data_type'], record_dictionary['ordering_provider_information']
                                      , record_dictionary['request_id'], record_dictionary['other_info'],
                                      psycopg2.Binary(record_dictionary['data'])))
        conn.commit()
        cursor.close()
        conn.close()
    except:
        error_message = "And error occurred trying to insert record with request_id: \'%s\'\t and external_id: \'%s\'. "\
        % (record_dictionary['request_id'], record_dictionary['external_id'])
        gnumed_error_writer.write_error(error_message)
        exit(gnumed_error_writer.EXIT_CODE_DB_ISSUE)
Example #2
0
def process_hl7_directory(hl7_directory, processed_file_dir=None):
    move_files = False

    try:
        hl7_files = hl7_importer_file_directory_utilities.process_directory('hl7', hl7_directory)
    except IOError:
            error_message = "An error occurred trying to retrieve the hl7 file contents of the directory: %s." % (input_directory)
            gnumed_error_writer.write_error(error_message)
            exit(gnumed_error_writer.EXIT_FILE_OPERATION_ERROR)

    # Process each file.
    for n in hl7_files:
        #TODO Make sure an hl7 file isn't overwritten
        gnumed_hl7_importer(n)
        hl7_importer_file_directory_utilities.handle_processed_file(n, processed_file_dir)
Example #3
0
def process_xml_directory(input_directory, hl7_directory, processed_file_dir=None):
    try:
        xml_files = hl7_importer_file_directory_utilities.process_directory("xml", input_directory)
    except IOError:
        error_message = "An error occurred trying to retrieve the xml file contents of the directory: %s." % (
            input_directory
        )
        gnumed_error_writer.write_error(error_message)
        exit(gnumed_error_writer.EXIT_FILE_OPERATION_ERROR)
    else:

        # Process each file.
        for n in xml_files:
            hl7_path = hl7_importer_file_directory_utilities.create_output_path("hl7", n, hl7_directory, True)

            process_xml_file(n, hl7_path)
            hl7_importer_file_directory_utilities.handle_processed_file(n, processed_file_dir)
Example #4
0
def process_xml_file(in_file, out_file, move_file_path=""):
    """ Opens the xml file  pulls out all of the hl7 messages in the file , then writes them to a text file. If a move_file_path
    is provided, it function moves the xml file to the given directory, otherwise it appends '.px' to all XML files that have been processed."""

    try:
        e_tree = ElementTree.parse(in_file)
    except:
        gnumed_error_writer.write_error("Not a valid XML file")
        exit(gnumed_error_writer.EXIT_CODE_INVALID_XML)

    # Grab all messages in the XML document
    messages = e_tree.findall(".//Message")

    # Check to see if the file is complete.
    # Looks at the MessageCount attribute in the root node and compare with the number of Message nodes
    stated_number = e_tree._root.attrib["MessageCount"]
    if len(messages) != int(stated_number):
        error_message = "Number of message in %s do not match: Listed: %s\t Actual: %s" % (
            in_file,
            (len(messages)),
            str(stated_number),
        )
        gnumed_error_writer.write_error(error_message)
        exit(gnumed_error_writer.EXIT_CODE_MISMATCH)

    # Write HL7 messages to  an HL7 file
    try:
        with open(out_file, "w") as hl7_file:
            for node in messages:
                hl7_file.write(node.text)
    except IOError:
        error_message = "Could not open file %s" % out_file
        gnumed_error_writer.write_error(error_message)
        exit(gnumed_error_writer.EXIT_FILE_OPERATION_ERROR)
def handle_processed_file(file_path, processed_file_dir=None):
    if processed_file_dir != None:
        try:
            move_processed_file(file_path, processed_file_dir)
        except IOError:
            #Attempt to rename the file so it won't be scanned later
            try:
                rename_processed_file(file_path)
            except IOError:
                error_message = "File %s could not be moved or renamed" % (file_path)
                gnumed_error_writer.write_error(error_message)
                exit(gnumed_error_writer.EXIT_FILE_OPERATION_ERROR)
            else:
                error_message = "File %s not moved. File renamed in its current directory with %s extension" % (
                file_path, PROCESSED_FILE_EXTENSION)
                gnumed_error_writer.write_error(error_message)
                exit(gnumed_error_writer.EXIT_FILE_OPERATION_ERROR)
    else:
        try:
            rename_processed_file(file_path)
        except IOError:
            error_message = "Could not rename file % with extension %s" % (file_path, PROCESSED_FILE_EXTENSION)