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  __version__ = "$Revision: 1.2 $"
      5  __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
      6
      7
      8  # stdlib
      9  import sys, 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  _log = logging.getLogger('gm.coding')
     21  _log.info(__version__)
     22
     23
     24  #============================================================
     25 -def _on_code_link_modified():
     26          """Always relates to the active patient."""
     27          gmHooks.run_hook_script(hook = u'after_code_link_modified')
     28
     29  gmDispatcher.connect(_on_code_link_modified, u'episode_code_mod_db')
     30  gmDispatcher.connect(_on_code_link_modified, u'rfe_code_mod_db')
     31  gmDispatcher.connect(_on_code_link_modified, u'aoe_code_mod_db')
     32  gmDispatcher.connect(_on_code_link_modified, u'health_issue_code_mod_db')
     33  gmDispatcher.connect(_on_code_link_modified, u'narrative_code_mod_db')
     34  gmDispatcher.connect(_on_code_link_modified, u'procedure_code_mod_db')
     35
     36  #============================================================
     37  # generic linked code handling
     38  #------------------------------------------------------------
     39  _SQL_get_generic_linked_codes = u"SELECT * FROM clin.v_linked_codes WHERE %s"
     40
     41 -class cGenericLinkedCode(gmBusinessDBObject.cBusinessDBObject):
     42          """Represents a generic linked code.
     43
     44          READ ONLY
     45          """
     46          _cmd_fetch_payload = _SQL_get_generic_linked_codes % u"pk_lnk_code2item = %s"
     47          _cmds_store_payload = []
     48          _updatable_fields = []
     49  #------------------------------------------------------------
     50 -def get_generic_linked_codes(order_by=None):
     51          if order_by is None:
     52                  order_by = u'true'
     53          else:
     54                  order_by = u'true ORDER BY %s' % order_by
     55
     56          cmd = _SQL_get_generic_linked_codes % order_by
     57          rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd}], get_col_idx = True)
     58          return [ cGenericLinkedCode(row = {'data': r, 'idx': idx, 'pk_field': 'pk_lnk_code2item'}) for r in rows ]
     59  #============================================================
     60  # this class represents a generic (non-qualified) code
     61  #------------------------------------------------------------
     62  _SQL_get_generic_code = u"SELECT * FROM ref.v_generic_codes WHERE %s"
     63
     64 -class cGenericCode(gmBusinessDBObject.cBusinessDBObject):
     65          """READ ONLY"""
     66          _cmd_fetch_payload = _SQL_get_generic_code % u"pk_generic_code = %s"
     67          _cmds_store_payload = []
     68          _updatable_fields = []
     69  #------------------------------------------------------------
     70 -def get_generic_codes(order_by=None):
     71          if order_by is None:
     72                  order_by = u'true'
     73          else:
     74                  order_by = u'true ORDER BY %s' % order_by
     75
     76          cmd = _SQL_get_generic_code % order_by
     77          rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd}], get_col_idx = True)
     78          return [ cGenericCode(row = {'data': r, 'idx': idx, 'pk_field': 'pk_generic_code'}) for r in rows ]
     79
     80  #============================================================
     81  # module level functions
     82  #------------------------------------------------------------
     83 -def get_coded_terms(coding_systems=None, languages=None, order_by=None):
     84
     85          where_snippets = []
     86          args = {}
     87
     88          if coding_systems is not None:
     89                  where_snippets.append(u"((coding_system IN %(sys)s) OR (coding_system_long IN %(sys)s)")
     90                  args['sys'] = tuple(coding_systems)
     91
     92          if languages is not None:
     93                  where_snippets.append(u'lang IN %(lang)s')
     94                  args['lang'] = tuple(languages)
     95
     96          cmd = u'select * from ref.v_coded_terms'
     97
     98          if len(where_snippets) > 0:
     99                  cmd += u' WHERE %s' % u' AND '.join(where_snippets)
    100
    101          if order_by is not None:
    102                  cmd += u' ORDER BY %s' % order_by
    103
    104          rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = False)
    105
    106          return rows
    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  # main
    115  #------------------------------------------------------------
    116  if __name__ == "__main__":
    117
    118          if len(sys.argv) < 2:
    119                  sys.exit()
    120
    121          if sys.argv[1] != 'test':
    122                  sys.exit()
    123
    124          #----------------------------------------------------
    125 -        def test_get_generic_codes():
    126                  print "generic codes:"
    127                  for code in get_generic_codes():
    128                          print code
    129          #----------------------------------------------------
    130 -        def test_get_coded_terms():
    131                  print "known codes:"
    132                  for term in get_coded_terms():
    133                          print term
    134          #----------------------------------------------------
    135 -        def test_get_generic_linked_codes():
    136                  print "generically linked generic codes:"
    137                  for code in get_generic_linked_codes():
    138                          print code
    139          #----------------------------------------------------
    140          #test_get_coded_terms()
    141          #test_get_generic_codes()
    142          test_get_generic_linked_codes()
    143
    144  #============================================================
    145