Class: HQMF2::Document

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/hqmf-parser/2.0/document.rb

Overview

Class representing an HQMF document

Constant Summary collapse

NAMESPACES =
{'cda' => 'urn:hl7-org:v3', 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#attr_val, attr_val, #to_xml

Methods included from HQMF::Conversion::Utilities

#build_hash, #check_equality, #json_array, #openstruct_to_json

Constructor Details

#initialize(hqmf_contents) ⇒ Document

Create a new HQMF2::Document instance by parsing at file at the supplied path

Parameters:

  • path (String)

    the path to the HQMF document



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/hqmf-parser/2.0/document.rb', line 12

def initialize(hqmf_contents)
  @doc = @entry = Document.parse(hqmf_contents)
  @id = attr_val('cda:QualityMeasureDocument/cda:id/@extension')
  @hqmf_set_id = attr_val('cda:QualityMeasureDocument/cda:setId/@extension')
  @hqmf_version_number = attr_val('cda:QualityMeasureDocument/cda:versionNumber/@value').to_i
  measure_period_def = @doc.at_xpath('cda:QualityMeasureDocument/cda:controlVariable/cda:measurePeriod/cda:value', NAMESPACES)
  if measure_period_def
    @measure_period = EffectiveTime.new(measure_period_def)
  end
  
  # Extract measure attributes
  @attributes = @doc.xpath('/cda:QualityMeasureDocument/cda:subjectOf/cda:measureAttribute', NAMESPACES).collect do |attribute|
    id = attribute.at_xpath('./cda:id/@root', NAMESPACES).try(:value)
    code = attribute.at_xpath('./cda:code/@code', NAMESPACES).try(:value)
    name = attribute.at_xpath('./cda:code/cda:displayName/@value', NAMESPACES).try(:value)
    value = attribute.at_xpath('./cda:value/@value', NAMESPACES).try(:value)

    id_obj = nil
    if attribute.at_xpath('./cda:id', NAMESPACES)
      id_obj = HQMF::Identifier.new(attribute.at_xpath('./cda:id/@xsi:type', NAMESPACES).try(:value), id, attribute.at_xpath('./cda:id/@extension', NAMESPACES).try(:value))
    end

    code_obj = nil;
    if attribute.at_xpath('./cda:code', NAMESPACES)
      nullFlavor = attribute.at_xpath('./cda:code/@nullFlavor', NAMESPACES).try(:value)
      oText = attribute.at_xpath('./cda:code/cda:originalText', NAMESPACES) ? attribute.at_xpath('./cda:code/cda:originalText/@value', NAMESPACES).try(:value) : nil
      code_obj = HQMF::Coded.new(attribute.at_xpath('./cda:code/@xsi:type', NAMESPACES).try(:value) || 'CD',
                             attribute.at_xpath('./cda:code/@codeSystem', NAMESPACES).try(:value),
                             code,
                             attribute.at_xpath('./cda:code/@valueSet', NAMESPACES).try(:value),
                             name,
                             nullFlavor,
                             oText)


      # Mapping for nil values to align with 1.0 parsing
      if code == nil
        code = nullFlavor
      end

      if name == nil
        name = oText
      end

    end

    value_obj = nil
    if attribute.at_xpath('./cda:value', NAMESPACES)
      type = attribute.at_xpath('./cda:value/@xsi:type', NAMESPACES).try(:value)
      case type
      when 'II'
        value_obj = HQMF::Identifier.new(type, attribute.at_xpath('./cda:value/@root', NAMESPACES).try(:value), attribute.at_xpath('./cda:value/@extension', NAMESPACES).try(:value))
        if value == nil
          value = attribute.at_xpath('./cda:value/@extension', NAMESPACES).try(:value)
        end
      when 'ED'
        value_obj = HQMF::ED.new(type, value, attribute.at_xpath('./cda:value/@mediaType', NAMESPACES).try(:value))
      when 'CD'
        value_obj = HQMF::Coded.new('CD', attribute.at_xpath('./cda:value/@codeSystem', NAMESPACES).try(:value), attribute.at_xpath('./cda:value/@code', NAMESPACES).try(:value), attribute.at_xpath('./cda:value/@valueSet', NAMESPACES).try(:value), attribute.at_xpath('./cda:value/cda:displayName/@value', NAMESPACES).try(:value))
      else
        value_obj = value.present? ? HQMF::GenericValueContainer.new(type, value) : HQMF::AnyValue.new(type)
      end
    end

    # Handle the cms_id
    if name == "eMeasure Identifier"
       @cms_id = "CMS#{value}v#{@hqmf_version_number}"
    end

    HQMF::Attribute.new(id, code, value, nil, name, id_obj, code_obj, value_obj)
  end
  
  # Extract the data criteria
  @data_criteria = []
  @source_data_criteria = []
  @doc.xpath('cda:QualityMeasureDocument/cda:component/cda:dataCriteriaSection/cda:entry', NAMESPACES).each do |entry|
    criteria = DataCriteria.new(entry)
    if criteria.is_source_data_criteria
      @source_data_criteria << criteria
    else
      @data_criteria << criteria
    end
  end
  
  # Extract the population criteria and population collections
  @populations = []
  @population_criteria = []
  
  population_counters = {}
  ids_by_hqmf_id = {}
  
  @doc.xpath('cda:QualityMeasureDocument/cda:component/cda:populationCriteriaSection', NAMESPACES).each_with_index do |population_def, population_index|
    population = {}

    stratifier_id_def = population_def.at_xpath('cda:templateId/cda:item[@root="'+HQMF::Document::STRATIFIED_POPULATION_TEMPLATE_ID+'"]/@controlInformationRoot', NAMESPACES)
    population['stratification'] = stratifier_id_def.value if stratifier_id_def

    {
      HQMF::PopulationCriteria::IPP => 'initialPopulationCriteria',
      HQMF::PopulationCriteria::DENOM => 'denominatorCriteria',
      HQMF::PopulationCriteria::NUMER => 'numeratorCriteria',
      HQMF::PopulationCriteria::DENEXCEP => 'denominatorExceptionCriteria',
      HQMF::PopulationCriteria::DENEX => 'denominatorExclusionCriteria',
      HQMF::PopulationCriteria::STRAT => 'stratifierCriteria',
      HQMF::PopulationCriteria::MSRPOPL => 'measurePopulationCriteria'
    }.each_pair do |criteria_id, criteria_element_name|
      criteria_def = population_def.at_xpath("cda:component[cda:#{criteria_element_name}]", NAMESPACES)
      
      if criteria_def

        criteria = PopulationCriteria.new(criteria_def, self)
        
        # check to see if we have an identical population criteria.
        # this can happen since the hqmf 2.0 will export a DENOM, NUMER, etc for each population, even if identical.
        # if we have identical, just re-use it rather than creating DENOM_1, NUMER_1, etc.
        identical = @population_criteria.select {|pc| pc.to_model.base_json.to_json == criteria.to_model.base_json.to_json}
        
        if (identical.empty?)
          # this section constructs a human readable id.  The first IPP will be IPP, the second will be IPP_1, etc.  This allows the populations to be
          # more readable.  The alternative would be to have the hqmf ids in the populations, which would work, but is difficult to read the populations.
          if ids_by_hqmf_id["#{criteria.hqmf_id}-#{population['stratification']}"]
            criteria.create_human_readable_id(ids_by_hqmf_id[criteria.hqmf_id])
          else
            if population_counters[criteria_id]
              population_counters[criteria_id] += 1
              criteria.create_human_readable_id("#{criteria_id}_#{population_counters[criteria_id]}")
            else
              population_counters[criteria_id] = 0
              criteria.create_human_readable_id(criteria_id)
            end
            ids_by_hqmf_id["#{criteria.hqmf_id}-#{population['stratification']}"] = criteria.id
          end
        
        
          @population_criteria << criteria
          population[criteria_id] = criteria.id
        else
          population[criteria_id] = identical.first.id
        end
      end
    end


    id_def = population_def.at_xpath('cda:id/@extension', NAMESPACES)
    population['id'] = id_def ? id_def.value : "Population#{population_index}"
    title_def = population_def.at_xpath('cda:title/@value', NAMESPACES)
    population['title'] = title_def ? title_def.value : "Population #{population_index}"
    observation_section = @doc.xpath('cda:QualityMeasureDocument/cda:component/cda:measureObservationsSection', NAMESPACES)
    if !observation_section.empty?
       population['OBSERV'] = 'OBSERV'
    end
    @populations << population
  end


  #look for observation data in separate section but create a population for it if it exists
  observation_section = @doc.xpath('cda:QualityMeasureDocument/cda:component/cda:measureObservationsSection', NAMESPACES)
  if !observation_section.empty?
    observation_section.xpath("cda:definition",NAMESPACES).each do |criteria_def|
      criteria_id = "OBSERV"
      population = {}
      criteria = PopulationCriteria.new(criteria_def, self)
      criteria.type="OBSERV"
      # this section constructs a human readable id.  The first IPP will be IPP, the second will be IPP_1, etc.  This allows the populations to be
      # more readable.  The alternative would be to have the hqmf ids in the populations, which would work, but is difficult to read the populations.
      if ids_by_hqmf_id["#{criteria.hqmf_id}"]
         criteria.create_human_readable_id(ids_by_hqmf_id[criteria.hqmf_id])
      else
        if population_counters[criteria_id]
          population_counters[criteria_id] += 1
          criteria.create_human_readable_id("#{criteria_id}_#{population_counters[criteria_id]}")
        else
          population_counters[criteria_id] = 0
          criteria.create_human_readable_id(criteria_id)
        end
        ids_by_hqmf_id["#{criteria.hqmf_id}"] = criteria.id
      end
      
      @population_criteria << criteria

      population[criteria_id] = criteria.id
      @populations << population
      end 
  end

end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/hqmf-parser/2.0/document.rb', line 8

def attributes
  @attributes
end

#hqmf_set_idObject (readonly)

Returns the value of attribute hqmf_set_id.



8
9
10
# File 'lib/hqmf-parser/2.0/document.rb', line 8

def hqmf_set_id
  @hqmf_set_id
end

#hqmf_version_numberObject (readonly)

Returns the value of attribute hqmf_version_number.



8
9
10
# File 'lib/hqmf-parser/2.0/document.rb', line 8

def hqmf_version_number
  @hqmf_version_number
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/hqmf-parser/2.0/document.rb', line 8

def id
  @id
end

#measure_periodObject (readonly)

Returns the value of attribute measure_period.



8
9
10
# File 'lib/hqmf-parser/2.0/document.rb', line 8

def measure_period
  @measure_period
end

#populationsObject (readonly)

Returns the value of attribute populations.



8
9
10
# File 'lib/hqmf-parser/2.0/document.rb', line 8

def populations
  @populations
end

#source_data_criteriaObject (readonly)

Returns the value of attribute source_data_criteria.



8
9
10
# File 'lib/hqmf-parser/2.0/document.rb', line 8

def source_data_criteria
  @source_data_criteria
end

Class Method Details

.parse(hqmf_contents) ⇒ Nokogiri::XML::Document

Returns:

  • (Nokogiri::XML::Document)


240
241
242
243
244
# File 'lib/hqmf-parser/2.0/document.rb', line 240

def self.parse(hqmf_contents)
  doc = hqmf_contents.kind_of?(Nokogiri::XML::Document) ? hqmf_contents : Nokogiri::XML(hqmf_contents)
  doc.root.add_namespace_definition('cda', 'urn:hl7-org:v3')
  doc
end

Instance Method Details

#all_data_criteriaArray

Get all the data criteria defined by the measure

Returns:

  • (Array)

    an array of HQMF2::DataCriteria describing the data elements used by the measure



227
228
229
# File 'lib/hqmf-parser/2.0/document.rb', line 227

def all_data_criteria
  @data_criteria
end

#all_population_criteriaArray

Get all the population criteria defined by the measure

Returns:

  • (Array)

    an array of HQMF2::PopulationCriteria



214
215
216
# File 'lib/hqmf-parser/2.0/document.rb', line 214

def all_population_criteria
  @population_criteria
end

#data_criteria(id) ⇒ HQMF2::DataCriteria

Get a specific data criteria by id.

Parameters:

  • id (String)

    the data criteria identifier

Returns:



234
235
236
# File 'lib/hqmf-parser/2.0/document.rb', line 234

def data_criteria(id)
  find(@data_criteria, :id, id)
end

#descriptionString

Get the description of the measure

Returns:

  • (String)

    the description



207
208
209
210
# File 'lib/hqmf-parser/2.0/document.rb', line 207

def description
  description = @doc.at_xpath('cda:QualityMeasureDocument/cda:text/@value', NAMESPACES)
  description==nil ? '' : description.inner_text
end

#population_criteria(id) ⇒ HQMF2::PopulationCriteria

Get a specific population criteria by id.

Parameters:

  • id (String)

    the population identifier

Returns:



221
222
223
# File 'lib/hqmf-parser/2.0/document.rb', line 221

def population_criteria(id)
  find(@population_criteria, :id, id)
end

#titleString

Get the title of the measure

Returns:



201
202
203
# File 'lib/hqmf-parser/2.0/document.rb', line 201

def title
  @doc.at_xpath('cda:QualityMeasureDocument/cda:title/@value', NAMESPACES).inner_text
end

#to_modelObject



246
247
248
249
250
251
252
253
# File 'lib/hqmf-parser/2.0/document.rb', line 246

def to_model

  dcs = all_data_criteria.collect {|dc| dc.to_model}
  pcs = all_population_criteria.collect {|pc| pc.to_model}
  sdc = source_data_criteria.collect{|dc| dc.to_model}
  dcs = update_data_criteria(dcs, sdc)
  HQMF::Document.new(id, id, hqmf_set_id, hqmf_version_number, @cms_id, title, description, pcs, dcs, sdc, attributes, measure_period.to_model, populations)
end

#update_data_criteria(data_criteria, source_data_criteria) ⇒ Object

Update the data criteria to handle variables properly



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/hqmf-parser/2.0/document.rb', line 256

def update_data_criteria(data_criteria, source_data_criteria)
   # step through each criteria and look for groupers (type derived) with one child
   data_criteria.map do |criteria|
     if criteria.type == "derived".to_sym && criteria.children_criteria.length == 1
       source_data_criteria.each do |source_criteria|
         if source_criteria.title == criteria.children_criteria[0]
           criteria.children_criteria = source_criteria.children_criteria
           #if criteria.is_same_type?(source_criteria)
           criteria.update_copy( source_criteria.hard_status, source_criteria.title, source_criteria.description,
                                 source_criteria.derivation_operator, source_criteria.definition )#, occur_letter )
         end
       end
     end
     criteria
   end
end