Class: ProfileParser

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/ez7gen/profile_parser.rb

Constant Summary collapse

FILTER_ADM =
{filter: 'ADT_A|QBP_Q2|RSP_K2[1-4]', group: 'Admissions'}
FILTER_FM =
{filter: 'DFT_P03|DFT_P11|DFT_X03', group: 'Financial Management'}
FILTER_GEN =
{filter: 'OSR_Q06|OSQ_Q06|ORG_O20|OMG_O19', group: 'General'}
FILTER_LAB =
{filter: 'ORL_O22|OML_O21|QRY_R02|OUL_R21|ORU_R01', group: 'Laboratory'}
FILTER_MSR =
{filter: 'MFN_M01|MFN_X01|MFN_Y01', group: 'Master Files'}
FILTER_OBS =
{filter: 'OMS_O05', group: 'Order'}
FILTER_PH =
{filter: 'OMP_|ORP_|RDE_|RRE_|RDS_|RRD_|RGV_|RRG_|RAS_|RRA_', group: 'Pharmacy'}
@@FILTER_ALL =

attr_reader :xml;

{filter: '.*', group: 'All'}
@@segment_patern =

attr_accessor :std; :version; :event; :xml; :version_store; @@HL7_VERSIONS = ‘vaz2.4’=>‘vaz2.4/vaz2.4-schema.xml’ class attribute @@segment_patern = /[([^[]]*)]/

/\[([^\[\]]*)\]|\{([^\[\]]*)\}/

Constants included from Utils

Utils::BASE, Utils::BASE_INDICATOR, Utils::DATA_LOOKUP_MIS, Utils::PRIMARY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blank?, #get_name_without_base, #get_segment_name, #get_type_by_name, #has_html_encoded_ch?, #is_number?, #num_to_nil, #safe_len, #sample_index

Constructor Details

#initialize(args) ⇒ ProfileParser

Child class has a wrapper TODO: Refactor def initialize(version=nil, event=nil)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ez7gen/profile_parser.rb', line 31

def initialize(args)
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
  # set to false if it has not been set already
  # @base ||= false

  profile, path = nil
  # if(@version_store)
    profile = @version_store.find{|v| v[:std] == @std}[:profiles].find{|p| p[:doc] == @version }[:path]
    path = @version_store.detect{|v| v[:std] == @std}[:path]
  # else
  #   # path = self.class.get_schema_location
  #   # profile = File.path(path+ @@HL7_VERSIONS[@version])
  # end

  @xml = Ox.parse(IO.read(profile))

  # added = File.path(path+'added.xml')
  begin
    added = File.path(path+'/added/coded-tables.xml')
    @added = Ox.parse(IO.read(added))
  rescue => e
    # puts e.message
    $log.error ("#{self.class.to_s}:#{__method__.to_s}") { e.message }


  end

  # set flag if this is base or custom schema
  @base = (@xml.Export.Document.Category.attributes[:std] == '1')

end

Instance Attribute Details

#baseObject (readonly) Also known as: base?

instance attributes



9
10
11
# File 'lib/ez7gen/profile_parser.rb', line 9

def base
  @base
end

#xmlObject (readonly)

instance attributes



9
10
11
# File 'lib/ez7gen/profile_parser.rb', line 9

def xml
  @xml
end

Class Method Details

.get_schema_locationObject

instance methods



66
67
68
69
70
71
72
73
74
75
# File 'lib/ez7gen/profile_parser.rb', line 66

def self.get_schema_location
  #properties_file = File.expand_path('../resources/properties.yml', __FILE__)
  #yml = YAML.load_file properties_file
  #path = yml['web.install.dir'] # set when run intall gem with argument, example: gem install 'c:/ez7Gen/ez7gen-web/config/resources/'
  path = File.expand_path('../', __FILE__)
  path = File.join(path, 'config/schema/')
  #puts path + ' : self.get_schema_location'
    # path = path<<'config/schema/'
  # path = path<<'config/resources/'
end

.getExclusionFilterRule(std, version) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ez7gen/profile_parser.rb', line 100

def self.getExclusionFilterRule(std, version)
  path = self.get_schema_location
  rules_file = "#{path}#{std}/rules/#{version}.yml"

  if File.exists? (rules_file)
    yml = YAML.load_file rules_file
    all = []
    all += (yml['exclusion.errors'])?yml['exclusion.errors']:[]
    all += (yml['exclusion.blacklist'])?yml['exclusion.blacklist']:[]
  else
    []
  end

end

.getVersionUrlRule(std, version) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ez7gen/profile_parser.rb', line 114

def self.getVersionUrlRule(std, version)
  path = self.get_schema_location
  rules_file = "#{path}#{std}/rules/#{version}.yml"

  if File.exists? (rules_file)
    yml = YAML.load_file rules_file
    (yml['version.url'])?yml['version.url']:nil
  else
    nil
  end

end

.lookup_versionsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ez7gen/profile_parser.rb', line 77

def self.lookup_versions
  path = self.get_schema_location
  puts path + " self.lookup_versions"
  names = Dir.glob("#{path}*").select {|f| File.directory? f}
  versions = names.map{|it| { std: it.sub(path,''), path: it}}
  # for each version
  # look get list of .xml files, except added,own directory for added?
  versions.each{|version|
    profiles = []

    Dir.glob("#{version[:path]}/**").select {|file| !File.directory? file}.each{|path|
      xml = Ox.parse(IO.read(path))
      # for each schema collect metadata
      profile = xml.Export.Document.attributes
      profile[:doc] = profile.delete(:name) # resolve collision with same keys
      profile.merge!(xml.Export.Document.Category.attributes)
      profile[:path] = path
      profiles << profile
    }
    version[:profiles] = profiles
  }
end

Instance Method Details

#build_event_attributes(event, templates, path) ⇒ Object

build all the details for event type including template information



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/ez7gen/profile_parser.rb', line 247

def build_event_attributes(event, templates, path)

  attr = {}
  attr[:name] = event
  #chek if there is a match otherwise use the segment name
  attr[:code] = get_message_event_desc(event)

  # check if this event has matching template files
  # event_templates = templates.collect { |template| template =~/#{event}/ } unless blank?(templates)
  # attr[:templates] = templates unless blank?(event_templates)
  if (!blank?(templates))
    # try to match templates to an event by name
    event_templates = templates.select { |template| template =~/#{event}/i }
    # if found set event attribute with template names
    if(!blank?(event_templates))

       # attr[:templates] = []
       #update event code with template count
      #code = "<b> +#{event_templates.size}" << (event_templates.size == 1) ? "TEMPLATE" : "TEMPLATES" << "</b>"
      # attr[:code] += "<div><b><font color=#337ab7>#{event_templates.size} #{(event_templates.size == 1) ? 'TEMPLATE' : 'TEMPLATES'}</font></b></div>"
      attr[:code] += "<small><b><font color=#337ab7> (#{event_templates.size}#{(event_templates.size == 1) ? 'TEMPLATE' : 'TEMPLATES'})</font></b></small>"
      # if (event_templates.size == 1) then code.chop! end
      # attr[:code] += "#{code}"

      # if (event_templates.size == 1) then attr[:code].chop! end

       attr[:templates] = event_templates.collect{ |tmpl|
         desc = Ox.parse(IO.read("#{path}/#{tmpl}")).HL7v2xConformanceProfile.HL7v2xStaticDef.attributes[:EventDesc]
         # desc = (!blank?(desc)) ? desc : "Custom #{event}"
         desc = (!blank?(desc)) ? desc : tmpl.gsub('_',' ').sub('.xml','')
         {:desc => desc.upcase , :file => tmpl}
       }
    end

  end


 return attr
end

#get_code_table(tableName) ⇒ Object

get hash of attributes for codeTable values



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ez7gen/profile_parser.rb', line 165

def get_code_table(tableName)
  #exclude 361,362 sending/receiving app and facility
  #if(tableName in ['72','88','132','264','269','471','9999']){
  #	println tableName
  #}

  #empty hash if no table name
  return [] if blank?(tableName)

  attributes = lookup_code_table(tableName, @xml)

  if(blank?(attributes))||(attributes.size == 1  && attributes[0][:value] =='...')
    attributes = lookup_code_table(tableName, @added)
  end

  # Per Galina, code table values with special characters. Ensemble validation fails.
  # Filter out codes which have html encoded characters - Ensemble has problem handling it.
  # a bit of awkward logic - if either description or value has html encoded chars, remove the item
  attributes.select!{|a| (has_html_encoded_ch?(a[:description]) || has_html_encoded_ch?(a[:value]))?false:true }

  return attributes
end

#get_message_definitionObject

find message structure by event type



128
129
130
131
132
133
134
# File 'lib/ez7gen/profile_parser.rb', line 128

def get_message_definition
  msg_type = get_message_structure(@event)
  # p msg_type
  $log.info("#{self.class.to_s}:#{__method__.to_s}") { msg_type }
  definition = @xml.Export.Document.Category.locate('MessageStructure').select{|it| it.attributes[:name] == msg_type }.first.attributes[:definition]
  post_process(definition)
end

#get_message_event_desc(event) ⇒ Object

look up for event description



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/ez7gen/profile_parser.rb', line 298

def get_message_event_desc(event)
  desc = nil
  tbl = @xml.Export.Document.Category.locate('CodeTable').select { |it| (it.attributes[:description] == 'Event type') }

  if(!blank?(tbl))
    # get event/message name ex: NO2 for ACK_NO2
    event_name = (event.split('_')).last
    for t in tbl # could be multiple tables, iterate
      desc = (( e = t.locate('Enumerate').select{ |it| it.attributes[:value] == event_name }); e!=[])? (e.first().attributes[:description]) : nil
      if(desc)then break end # brake after first match
    end # for
  end # if

  return desc || event
end

#get_message_structure(event) ⇒ Object



159
160
161
# File 'lib/ez7gen/profile_parser.rb', line 159

def get_message_structure(event)
  msg_type = @xml.Export.Document.Category.locate('MessageType').select { |it| it.attributes[:name] == event }.first.attributes[:structure]
end

#get_segment_structure(segment) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/ez7gen/profile_parser.rb', line 193

def get_segment_structure(segment)
  segmentName = get_segment_name(segment)
  # $log.info (segment)
  $log.info("#{self.class.to_s}:#{__method__.to_s}") { segment }
  # node = export.Document.Category.SegmentStructure.find{ it.@name == segmentName}
  # values = @xml.elements.collect("Export/Document/Category/SegmentStructure[@name ='#{segmentName}']/SegmentSubStructure"){|x| x.attributes}
  @xml.Export.Document.Category.locate('SegmentStructure').select{|it| it.attributes[:name] == segmentName }.first.locate('SegmentSubStructure').map{|it| it.attributes}
  #values.each {|it| puts it}
end

#get_templates(path) ⇒ Object

# look up for message template files in a specified directory



288
289
290
291
292
293
294
295
# File 'lib/ez7gen/profile_parser.rb', line 288

def get_templates(path)
  begin
    Dir.entries(path).select {|f| f =~/.xml/i}.sort
  # rescue => e
  rescue
    [] # handle case when dir is missing
  end
end

#lookup_code_table(tableName, path) ⇒ Object



188
189
190
191
# File 'lib/ez7gen/profile_parser.rb', line 188

def lookup_code_table(tableName, path)
  tbl = path.Export.Document.Category.locate('CodeTable').select { |it| it.attributes[:name] == tableName }
  (!blank?(tbl)) ? tbl.first.locate('Enumerate').map { |it| it.attributes } : [Utils::DATA_LOOKUP_MIS]
end

#lookup_events(params) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/ez7gen/profile_parser.rb', line 224

def lookup_events(params)

  #all events for version
  events = @xml.Export.Document.Category.locate('MessageType').map!{|it| it.attributes[:name]}

  #if there are exclusion rule, remove the exclusions
  if(!blank?(params[:exclusions]))
    events -= params[:exclusions]
  end

  path = params[:templates_path]

  templates = (!blank?(path))? get_templates(path) : []

  # go over the events and build attributes of the array
  events_with_attr = events.map{ |el|
    build_event_attributes(el, templates, path)
  }

  #events_with_attr
end

#lookup_message_groups(groups) ⇒ Object

helper method to look up messages for specific groups of messages



315
316
317
318
319
# File 'lib/ez7gen/profile_parser.rb', line 315

def lookup_message_groups (groups)
  messages = []
  groups.each{ |group| messages += lookup_message_types(group) }
  return messages
end

#lookup_message_types(map = nil, exclusion = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ez7gen/profile_parser.rb', line 203

def lookup_message_types(map=nil, exclusion=nil)
  # match everything if no filter defined
  map ||= @@FILTER_ALL

  filter = map[:filter]
  messageTypeColl = @xml.Export.Document.Category.locate('MessageType').select{|it| it.attributes[:name] =~/#{filter}/}.map!{|it| it.attributes[:name]}
  if(!blank?(exclusion))
    messageTypeColl = messageTypeColl - exclusion
  end
  messages = messageTypeColl.map{ |el|
    event = (el.split('_')).last
     {
        name: el,
        #chek if there is a match otherwise use the segment name
        code: ((e = @xml.Export.Document.Category.locate('MessageEvent').select{|it| it.attributes[:name] == event}); e!=[] )? (e.first().attributes[:description]): el,
        group: map[:group]
    }
  }
  return messages
end

#post_process(definition) ⇒ Object

helper method to handle corner cases



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ez7gen/profile_parser.rb', line 137

def post_process(definition)

  if(@base && (@event == 'OSR_Q06'))
    # 1.If the OSQ_O06 query is about the status of the general messages OMG_O19 General Clinical Order and OML_O21 Lab Order, which only have the OBR segment, then the OSR_O06 should only have the OBR segment in its Order Detail Segment <   >.
    # 2.If the OSQ_O06 query is about the status of the Pharmacy order messages (OMP_O09, RDE_O11) that do not have OBR segment, but have RXO segment, then the OSR_O06 Order Detail Segment <     > should only contain RXO.
    # definition.sub!(/<(.*?)>/,['OBR','RXO'].sample())
    # puts definition
    definition = definition.sub!(/<(.*?)>/,['OBR','RXO'].sample())
    # puts definition
  elsif(@base && (@event == 'ORL_O22'))
    # work around for Ensemble issue where repeating group causes error in validation, remove repeating {} tag
    # MSH~MSA~[~ERR~]~[~{~NTE~}~]~[~[~PID~{~[~SAC~[~{~OBX~}~]~]~[~{~ORC~[~OBR~[~{~SAC~}~]~]~}~]~}~]~]
    # 'MSH~MSA~[~ERR~]~[~{~NTE~}~]~[~PID~{~[~SAC~[~{~OBX~}~]~]~[~{~ORC~[~OBR~[~{~SAC~}~]~]~}~]~}~]' #simplified
    # 'MSH~MSA~[~ERR~]~[~{~NTE~}~]~[~PID~[~[~SAC~[~{~OBX~}~]~]~[~{~ORC~[~OBR~[~{~SAC~}~]~]~}~]~]~]' #changed
    definition = 'MSH~MSA~[~ERR~]~[~{~NTE~}~]~[~PID~[~[~SAC~[~{~OBX~}~]~]~[~{~ORC~[~OBR~[~{~SAC~}~]~]~}~]~]~]'
    # definition.sub('[~{~ORC~[~OBR~[~{~SAC~}~]~]~}~]', '[~ORC~[~OBR~[~{~SAC~}~]~]~]')
  else
    definition
  end
    return definition
end