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
# 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/@extension', 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)
    HQMF::Attribute.new(id, code, value, nil, name)
  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 => 'patientPopulationCriteria',
      HQMF::PopulationCriteria::DENOM => 'denominatorCriteria',
      HQMF::PopulationCriteria::NUMER => 'numeratorCriteria',
      HQMF::PopulationCriteria::DENEXCEP => 'denominatorExceptionCriteria',
      HQMF::PopulationCriteria::DENEX => 'denominatorExclusionCriteria'
    }.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}"
    @populations << population
  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

Parse an XML document at the supplied path

Returns:

  • (Nokogiri::XML::Document)


147
148
149
150
# File 'lib/hqmf-parser/2.0/document.rb', line 147

def self.parse(hqmf_contents)
  doc = Nokogiri::XML(hqmf_contents)
  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



134
135
136
# File 'lib/hqmf-parser/2.0/document.rb', line 134

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



121
122
123
# File 'lib/hqmf-parser/2.0/document.rb', line 121

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:



141
142
143
# File 'lib/hqmf-parser/2.0/document.rb', line 141

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

#descriptionString

Get the description of the measure

Returns:

  • (String)

    the description



114
115
116
117
# File 'lib/hqmf-parser/2.0/document.rb', line 114

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:



128
129
130
# File 'lib/hqmf-parser/2.0/document.rb', line 128

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

#titleString

Get the title of the measure

Returns:

  • (String)

    the title



108
109
110
# File 'lib/hqmf-parser/2.0/document.rb', line 108

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

#to_modelObject



152
153
154
155
156
157
# File 'lib/hqmf-parser/2.0/document.rb', line 152

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}
  HQMF::Document.new(id, id, hqmf_set_id, hqmf_version_number, title, description, pcs, dcs, sdc, attributes, measure_period.to_model, populations)
end