Package Gnumed :: Package business :: Module gmCoding |
[frames] | no frames]

# Source Code for Module Gnumed.business.gmCoding

      1  """GNUmed coding systems handling middleware"""
      2  #============================================================
      3  __license__ = "GPL"
      4  __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
      5
      6
      7  # stdlib
      8  import sys
      9  import logging
     10
     11
     12  # GNUmed modules
     13  if __name__ == '__main__':
     14          sys.path.insert(0, '../../')
     15  from Gnumed.pycommon import gmPG2
     16  from Gnumed.pycommon import gmBusinessDBObject
     17  from Gnumed.pycommon import gmHooks
     18  from Gnumed.pycommon import gmDispatcher
     19
     20
     21  _log = logging.getLogger('gm.coding')
     22
     23  #============================================================
     24 -def _on_code_link_modified():
     25          """Always relates to the active patient."""
     26          gmHooks.run_hook_script(hook = u'after_code_link_modified')
     27
     28  gmDispatcher.connect(_on_code_link_modified, u'clin.episode_code_mod_db')
     29  gmDispatcher.connect(_on_code_link_modified, u'clin.rfe_code_mod_db')
     30  gmDispatcher.connect(_on_code_link_modified, u'clin.aoe_code_mod_db')
     31  gmDispatcher.connect(_on_code_link_modified, u'clin.health_issue_code_mod_db')
     32  gmDispatcher.connect(_on_code_link_modified, u'clin.narrative_code_mod_db')
     33  gmDispatcher.connect(_on_code_link_modified, u'clin.procedure_code_mod_db')
     34
     35  #============================================================
     36  # generic linked code handling
     37  #------------------------------------------------------------
     38  _SQL_get_generic_linked_codes = u"SELECT * FROM clin.v_linked_codes WHERE %s"
     39
     40 -class cGenericLinkedCode(gmBusinessDBObject.cBusinessDBObject):
     41          """Represents a generic linked code.
     42
     43          READ ONLY
     44          """
     45          _cmd_fetch_payload = _SQL_get_generic_linked_codes % u"pk_lnk_code2item = %s"
     46          _cmds_store_payload = []
     47          _updatable_fields = []
     48  #------------------------------------------------------------
     49 -def get_generic_linked_codes(order_by=None):
     50          if order_by is None:
     51                  order_by = u'true'
     52          else:
     53                  order_by = u'true ORDER BY %s' % order_by
     54
     55          cmd = _SQL_get_generic_linked_codes % order_by
     56          rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd}], get_col_idx = True)
     57          return [ cGenericLinkedCode(row = {'data': r, 'idx': idx, 'pk_field': 'pk_lnk_code2item'}) for r in rows ]
     58  #============================================================
     59  # this class represents a generic (non-qualified) code
     60  #------------------------------------------------------------
     61  _SQL_get_generic_code = u"SELECT * FROM ref.v_generic_codes WHERE %s"
     62
     63 -class cGenericCode(gmBusinessDBObject.cBusinessDBObject):
     64          """READ ONLY"""
     65          _cmd_fetch_payload = _SQL_get_generic_code % u"pk_generic_code = %s"
     66          _cmds_store_payload = []
     67          _updatable_fields = []
     68  #------------------------------------------------------------
     69 -def get_generic_codes(order_by=None):
     70          if order_by is None:
     71                  order_by = u'true'
     72          else:
     73                  order_by = u'true ORDER BY %s' % order_by
     74
     75          cmd = _SQL_get_generic_code % order_by
     76          rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd}], get_col_idx = True)
     77          return [ cGenericCode(row = {'data': r, 'idx': idx, 'pk_field': 'pk_generic_code'}) for r in rows ]
     78
     79  #============================================================
     80  # module level functions
     81  #------------------------------------------------------------
     82 -def get_coded_terms(coding_systems=None, languages=None, order_by=None):
     83
     84          where_snippets = []
     85          args = {}
     86
     87          if coding_systems is not None:
     88                  where_snippets.append(u"((coding_system IN %(sys)s) OR (coding_system_long IN %(sys)s)")
     89                  args['sys'] = tuple(coding_systems)
     90
     91          if languages is not None:
     92                  where_snippets.append(u'lang IN %(lang)s')
     93                  args['lang'] = tuple(languages)
     94
     95          cmd = u'select * from ref.v_coded_terms'
     96
     97          if len(where_snippets) > 0:
     98                  cmd += u' WHERE %s' % u' AND '.join(where_snippets)
     99
    100          if order_by is not None:
    101                  cmd += u' ORDER BY %s' % order_by
    102
    103          rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = False)
    104
    105          return rows
    106
    107  #============================================================
    108 -def get_data_sources(order_by=u'name_long, lang, version'):
    109          cmd = u'SELECT * FROM ref.data_source ORDER BY %s' % order_by
    110          rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd}], get_col_idx = False)
    111          return rows
    112
    113  #============================================================
    114 -def create_data_source(long_name=None, short_name=None, version=None, source=None, language=None):
    115
    116                  args = {
    117                          'lname': long_name,
    118                          'sname': short_name,
    119                          'ver': version,
    120                          'src': source,
    121                          'lang': language
    122                  }
    123
    124                  cmd = u"SELECT pk FROM ref.data_source WHERE name_long = %(lname)s AND name_short = %(sname)s AND version = %(ver)s"
    125                  rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}])
    126                  if len(rows) > 0:
    127                          return rows[0]['pk']
    128
    129                  cmd = u"""
    130                          INSERT INTO ref.data_source (name_long, name_short, version, source, lang)
    131                          VALUES (
    132                                  %(lname)s,
    133                                  %(sname)s,
    134                                  %(ver)s,
    135                                  %(src)s,
    136                                  %(lang)s
    137                          )
    138                          RETURNING pk
    139                  """
    140                  rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True)
    141
    142                  return rows[0]['pk']
    143
    144  #============================================================
    145  # main
    146  #------------------------------------------------------------
    147  if __name__ == "__main__":
    148
    149          if len(sys.argv) < 2:
    150                  sys.exit()
    151
    152          if sys.argv[1] != 'test':
    153                  sys.exit()
    154
    155          #----------------------------------------------------
    156 -        def test_get_generic_codes():
    157                  print "generic codes:"
    158                  for code in get_generic_codes():
    159                          print code
    160          #----------------------------------------------------
    161 -        def test_get_coded_terms():
    162                  print "known codes:"
    163                  for term in get_coded_terms():
    164                          print term
    165          #----------------------------------------------------
    166 -        def test_get_generic_linked_codes():
    167                  print "generically linked generic codes:"
    168                  for code in get_generic_linked_codes():
    169                          print code
    170          #----------------------------------------------------
    171          #test_get_coded_terms()
    172          #test_get_generic_codes()
    173          test_get_generic_linked_codes()
    174
    175  #============================================================
    176