def makeHL7Docs(self,mode): self.session = self.db.Session() # This starts a Transaction if settings.DEBUG: print '==== Self:', self # Database traversal: # Step through Exports. For each Export, # Step through Persons. For each person, # Step through ServiceEvent. For each Service Event, begin a new document, then # Step through Entrys # Step through ServiceEventNotes - and all note_text exports = list() # Added by FBY on (11-06-2013): if self.export_id != None: #exports = self.session.query(dbobjects.Export).filter(dbobjects.Export.export_id == self.export_id) # Added by FBY on (11-06-2013): queries of the export table must take into # account the source id nobebuilder operates with which is stored in self.options.configID export_table_join = self.session.query(dbobjects.Export, dbobjects.SourceExportLink, dbobjects.Source). \ filter(dbobjects.Export.id == dbobjects.SourceExportLink.export_index_id). \ filter(dbobjects.SourceExportLink.source_index_id == dbobjects.Source.id). \ filter(dbobjects.Source.source_id == self.options.configID). \ filter(dbobjects.Export.export_id == self.export_id) # Added by FBY on (11-06-2013): from the join, extract only the export classes for row in export_table_join: exports.append(row.Export) else: #exports = self.session.query(dbobjects.Export) # Added by FBY on (11-06-2013): queries of the export table must take into # account the source id nobebuilder operates with which is stored in self.options.configID export_table_join = self.session.query(dbobjects.Export, dbobjects.SourceExportLink, dbobjects.Source). \ filter(dbobjects.Export.id == dbobjects.SourceExportLink.export_index_id). \ filter(dbobjects.SourceExportLink.source_index_id == dbobjects.Source.id). \ filter(dbobjects.Source.source_id == self.options.configID) # Added by FBY on (11-06-2013): from the join, extract only the export classes for row in export_table_join: exports.append(row.Export) hl7_output = [] for oneExport in exports: selink = self.session.query(dbobjects.SourceExportLink).filter( dbobjects.SourceExportLink.export_index_id == oneExport.id).one() #print '==== Selink.id:', selink.id oneSource = self.session.query(dbobjects.Source).filter(dbobjects.Source.id == selink.source_index_id).one() #print '==== Source.id:', source.id #self.configRec = self.session.query(dbobjects.SystemConfiguration).filter(and_(dbobjects.SystemConfiguration.source_id # == source.source_id, dbobjects.SystemConfiguration.processing_mode == settings.MODE)).one() #print '==== sys config.id', self.configRec.id Persons = self.session.query(dbobjects.Person).filter(dbobjects.Person.export_index_id == oneExport.id) # More filtering for options if self.options.reported == True: Persons = Persons.filter(dbobjects.Person.reported == True).with_lockmode('update') elif self.options.unreported == True: Persons = Persons.filter(or_(dbobjects.Person.reported == False, dbobjects.Person.reported == None)).with_lockmode('update') elif self.options.reported == None: pass # Now apply the dates to the result set. if self.options.alldates == None: #Persons = Persons.filter(between(dbobjects.Person.person_id_date_collected, # self.options.startDate, self.options.endDate)).with_lockmode('update') Persons = Persons.filter(between(dbobjects.Export.export_date, self.options.startDate, self.options.endDate)).with_lockmode('update') for onePerson in Persons: # Get the person's latest received phone number from PersonHistorical personHistoricals = self.session.query(dbobjects.PersonHistorical).filter(dbobjects.PersonHistorical.person_index_id == onePerson.id) #Get the most recent not null (unreported already filtered) phone number (that came in from the referral) onePersonHistorical = None for onePersonHistorical in personHistoricals: if onePersonHistorical.person_phone_number: self.phone_number = onePersonHistorical.person_phone_number # FBY New 2016-08-16 : Also check call table for phone number and if present, use it instead. calls = self.session.query(dbobjects.Call).filter(dbobjects.Call.id == onePersonHistorical.call_index_id) for oneCall in calls: if oneCall.caller_home_phone: self.phone_number = oneCall.caller_home_phone ServEvts = self.session.query(dbobjects.ServiceEvent).filter(dbobjects.ServiceEvent.person_index_id == onePerson.id) for oneServEvt in ServEvts: # One document per event??? #print "person is: ", self.person self.processXML(oneExport, onePerson, onePersonHistorical, oneServEvt, oneSource) # Create one document xmlutilities.indent(self.root_element) #self.prettify() # Next wraps self.root_element in an ElementTree and writes it to disk if mode=="disk": xmlutilities.writeOutXML(self, xml_declaration=True, encoding="UTF-8") # JCS - enc. defaults to ASCII w/decl. else: # here return the ccd document along with it's referral ID hl7_output.append((xmlutilities.printOutXML(self, encoding="UTF-8", method="xml"), self.referredToProviderID)) # Since no errors occurred to this point, mark the Person record as being reported self.updateReported(onePerson) self.session.commit() # This is only for updateReported() if mode=="disk": return True # Now nodebuilder.run() will find all output files and validate them. else: return hl7_output
def prettify(self): xmlutilities.indent(self.root_element)