def _translated_summary(self): """ Find corresponding translation of English summary Parameters: EnglishSummary - required CDR ID for the document for which we want to find the Spanish translation Return: XML document node with the following structure: ReportBody ReportName TranslatedSummary """ english_id = self.__opts.get("EnglishSummary") if not english_id: raise Exception("Missing required 'EnglishSummary' parameter") doc = Doc(self.session, id=english_id) query = Query("query_term", "doc_id") query.where("path = '/Summary/TranslationOf/@cdr:ref'") query.where(query.Condition("int_val", doc.id)) row = query.execute(self.cursor).fetchone() if not row: message = "No translated summary found for {}".format(english_id) raise Exception(message) body = self.__start_body() spanish_id = Doc.normalize_id(row.doc_id) etree.SubElement(body, "TranslatedSummary").text = spanish_id return body
def _glossary_term_names(self): """ Find GlossaryTermName documents linked to a GlossaryTermConcept doc Parameters: ConceptID - required CDR ID for the master concept document Return: XML document node with the following structure: ReportBody ReportName GlossaryTermName* @ref """ concept_id = self.__opts.get("ConceptId") if not concept_id: raise Exception("Missing required 'ConceptId' parameter") path = "/GlossaryTermName/GlossaryTermConcept/@cdr:ref" doc = Doc(self.session, id=concept_id) query = Query("document d", "d.id", "d.title") query.join("query_term n", "n.doc_id = d.id") query.where(query.Condition("n.path", path)) query.where(query.Condition("n.int_val", doc.id)) body = self.__start_body() for id, title in query.execute(self.cursor).fetchall(): cdr_id = Doc.normalize_id(id) etree.SubElement(body, "GlossaryTermName", ref=cdr_id).text = title return body
def _patient_summary(self): """ Find corresponding patient version for HP summary Parameters: HPSummary - required CDR ID for the Health Professional for which we want to find the patient verion Return: XML document node with the following structure: ReportBody ReportName PatientSummary """ hp_id = self.__opts.get("HPSummary") if not hp_id: raise Exception("Missing required 'HPSummary' parameter") doc = Doc(self.session, id=hp_id) query = Query("query_term", "doc_id") query.where("path = '/Summary/PatientVersionOf/@cdr:ref'") query.where(query.Condition("int_val", doc.id)) row = query.execute(self.cursor).fetchone() if not row: message = "No patient summary found for {}".format(hp_id) raise Exception(message) body = self.__start_body() patient_id = Doc.normalize_id(row.doc_id) etree.SubElement(body, "PatientSummary").text = patient_id return body
def _genetics_syndromes(self): """ Find Term documents used to represent genetics syndromes Parameters: TitlePattern - optional string narrowing report to syndrome names which match the pattern Return: XML document node with the following structure: ReportBody ReportName ReportRow* DocId DocTitle """ pattern = self.__opts.get("TitlePattern") query = Query("document d", "d.id", "d.title") query.unique().order("d.title") query.join("query_term t", "t.doc_id = d.id") query.join("query_term s", "s.doc_id = t.doc_id", "LEFT(s.node_loc, 8) = LEFT(t.node_loc, 8)") query.where("t.path = '/Term/MenuInformation/MenuItem/MenuType'") query.where("s.path = '/Term/MenuInformation/MenuItem/MenuStatus'") query.where("s.value = 'Online'") query.where("t.value = 'Genetics Professionals--GeneticSyndrome'") if pattern: query.where(query.Condition("d.title", pattern, "LIKE")) body = self.__start_body() for id, title in query.execute(self.cursor).fetchall(): wrapper = etree.SubElement(body, "ReportRow") etree.SubElement(wrapper, "DocId").text = Doc.normalize_id(id) etree.SubElement(wrapper, "DocTitle").text = title return body
def _dated_actions(self): """ Find the to-do list for documents of a particular type Parameters: DocType - required string naming the document type for which documents should be included Return: XML document node with the following structure: ReportBody ReportName ReportRow* DocId DocTitle """ doctype = self.__opts.get("DocType") if not doctype: raise Exception("Missing required 'DocType' parameter") path = "/{}/DatedAction/ActionDate".format(doctype) query = Query("document d", "d.id", "d.title").unique() query.join("query_term a", "a.doc_id = d.id") query.where(query.Condition("a.path", path)) rows = query.execute(self.cursor).fetchall() body = self.__start_body() for id, title in rows: wrapper = etree.SubElement(body, "ReportRow") etree.SubElement(wrapper, "DocId").text = Doc.normalize_id(id) etree.SubElement(wrapper, "DocTitle").text = title result = Doc(self.session, id=id).filter("name:Dated Actions") wrapper.append(result.result_tree.getroot()) return body
def _board_member(self): """ Find corresponding person document linked from board member document Parameters: PersonID - required CDR ID for the Person document for which we want to find the corresponding PDQBoardMemeberInfo document Return: XML document node with the following structure: ReportBody ReportName BoardMember """ person_id = self.__opts.get("PersonId") if not person_id: raise Exception("Missing required 'PersonId' parameter") doc = Doc(self.session, id=person_id) query = Query("query_term", "doc_id") query.where("path = '/PDQBoardMemberInfo/BoardMemberName/@cdr:ref'") query.where(query.Condition("int_val", doc.id)) row = query.execute(self.cursor).fetchone() if not row: message = "No board member found for {}".format(person_id) raise Exception(message) body = self.__start_body() member_id = Doc.normalize_id(row.doc_id) etree.SubElement(body, "BoardMember").text = member_id return body
def _inactive_checked_out_documents(self): """ Report on locked documents which haven't seen any action recently Parameters: InactivityLength - required string for how long a document can be active before showing up on the report in the form YYYY-MM-DD Return: XML document node with the following structure: ReportBody ReportName ReportRow* DocId DocType CheckedOutTo WhenCheckedOut LastActivity ActionType ActionWhen """ deltas = self.__opts.get("InactivityLength") if not deltas: raise Exception("Missing required 'InactivityLength' parameter") try: years, months, days = [int(digits) for digits in deltas.split("-")] except: message = "InactivityLength parameter must be in YYYY-MM-DD format" raise Exception(message) delta = relativedelta(years=years, months=months, days=days) cutoff = (datetime.now() - delta).replace(microsecond=0) subquery = Query("audit_trail", "MAX(dt)") subquery.where("document = a.document") fields = "d.id", "t.name", "c.dt_out", "u.name", "w.name", "a.dt" query = Query("document d", *fields) query.join("doc_type t", "t.id = d.doc_type") query.join("checkout c", "c.id = d.id") query.join("open_usr u", "u.id = c.usr") query.join("audit_trail a", "a.document = d.id") query.join("action w", "w.id = a.action") query.where("c.dt_in IS NULL") query.where(query.Condition("c.dt_out", cutoff, "<")) query.where(query.Condition("a.dt", cutoff, "<")) query.where(query.Condition("a.dt", subquery)) rows = query.order("c.id").execute(self.cursor).fetchall() body = self.__start_body() for id, doctype, locked, locker, what, when in rows: wrapper = etree.SubElement(body, "ReportRow") etree.SubElement(wrapper, "DocId").text = Doc.normalize_id(id) etree.SubElement(wrapper, "DocType").text = doctype etree.SubElement(wrapper, "CheckedOutTo").text = locker etree.SubElement(wrapper, "WhenCheckedOut").text = str(locked) last_activity = etree.SubElement(wrapper, "LastActivity") etree.SubElement(last_activity, "ActionType").text = what etree.SubElement(last_activity, "ActionWhen").text = str(when) return body
def _locked_documents(self): """ Report on documents locked by specified user Parameters: UserId - required string identifying a user account; this is the value often referred to as the 'name' rather than the integer primary key for the row in the usr table Return: XML document node with the following structure: ReportBody ReportName ReportRow* DocId DocType WhenCheckedOut """ user_name = self.__opts.get("UserId") if not user_name: raise Exception("Missing required 'UserId' parameter") query = Query("document d", "d.id", "t.name", "c.dt_out") query.join("doc_type t", "t.id = d.doc_type") query.join("checkout c", "c.id = d.id") query.join("open_usr u", "u.id = c.usr") query.where("c.dt_in IS NULL") query.where(query.Condition("u.name", user_name)) rows = query.order("c.id").execute(self.cursor).fetchall() body = self.__start_body() for id, doctype, locked in rows: wrapper = etree.SubElement(body, "ReportRow") etree.SubElement(wrapper, "DocId").text = Doc.normalize_id(id) etree.SubElement(wrapper, "DocType").text = doctype etree.SubElement(wrapper, "WhenCheckedOut").text = str(locked) return body