Class: MessageFactory

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

Constant Summary

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) ⇒ MessageFactory

attr_accessor :std; :version; :event; :version_store; :loadFactor;



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ez7gen/message_factory.rb', line 14

def initialize(args)
  @attributes_hash = args
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
  @loadFactor ||= nil
  # lookup for a template by name if client speified using a template
  @templatePath = (args[:use_template])? self.class.lookup_template_for_event(@std, @use_template):nil
  # @templatePath = self.class.lookup_template_for_event(@std, @event)

end

Instance Attribute Details

#templatePathObject

TODO: refactor



11
12
13
# File 'lib/ez7gen/message_factory.rb', line 11

def templatePath
  @templatePath
end

Class Method Details

.lookup_template_for_event(std, template) ⇒ Object

look up for message template file for an event and standard: 2.4, ADT_A60



27
28
29
30
31
32
33
34
35
36
# File 'lib/ez7gen/message_factory.rb', line 27

def self.lookup_template_for_event(std, template)
  # properties_file = File.expand_path('../resources/properties.yml', __FILE__)
  # yml = YAML.load_file properties_file
  # path = yml['web.install.dir']
  path = File.expand_path('../', __FILE__)
  path = File.join(path, "config/templates/#{std}/#{template}")
  # path = File.join(path, "config/templates/#{std}/*#{event}*")
  # Dir.glob(path, File::FNM_CASEFOLD).sort.last

end

.to_arr(hl7Msg) ⇒ Object



111
112
113
114
115
# File 'lib/ez7gen/message_factory.rb', line 111

def self.to_arr(hl7Msg)
  arr = []
  hl7Msg.each{|it| arr << it.to_s.gsub("\r","\n")}
  return arr
end

Instance Method Details

#find_Groups(segments) ⇒ Object

find ranges of groups before segments collection containing groups can be flattened



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ez7gen/message_factory.rb', line 124

def find_Groups(segments)
  offset = 0
  # groups = segments.map.with_index { |it, idx| (it.instance_of? Array) ? ( groupLn=it.size-1;((offset+idx) .. (idx + offset=offset + groupLn)) ) : nil }.compact

  groups = segments.map.with_index { |it, idx|
    # groups of sequential segments stored as arrays of segments
    if (it.instance_of? Array)
      # calculate group range based on offset that affected flattening arrays of segments
      groupLen = it.size-1
      groupStart = offset + idx
      groupEnd = idx + offset = offset + groupLen # inline var assignment, just cause I can..
      (groupStart..groupEnd) # group range
    end
  }
  # get rid of nils
  groups.compact()
end

#generate(useExVal = false) ⇒ Object

main factory method



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ez7gen/message_factory.rb', line 39

def generate(useExVal=false)
  parsers = {}
  # get message structure from the schema file for message type and version
  parsers[PRIMARY] = ProfileParser.new(@attributes_hash)

  # check if current version is not the base version, find the base version and add use it as a base parser
  if(!is_base?(@version))# version is not standard != '2.4'
    parsers[BASE]= get_base_parser()
  end

  #useExVal can be passed from the client - future feature
  return (@templatePath)? generate_message_from_template(parsers, templatePath, useExVal) : generate_message(parsers)
end

#generate_message(parsers) ⇒ Object

factory method to build message using schema



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
# File 'lib/ez7gen/message_factory.rb', line 54

def generate_message(parsers)

  # get message structure from the schema file for message type and version
  # use primary parser
  structure = parsers[PRIMARY].get_message_definition()

  # brake message structure into segments, handle groups of segments
  structParser = StructureParser.new()
  structParser.process_segments(structure)

  # select segments to build, keep required and z segments, random pick optional segments
  profile = structure.split('~')
  segment_picker = SegmentPicker.new(profile, structParser.encodedSegments, @loadFactor)
  segments = segment_picker.pick_segments_to_build()

  # configure a segment generator
  baseVersion = @std
  segmentGenerator = SegmentGenerator.new(baseVersion, @event, parsers)

  # msh segment configured by hand, due to many requirements that only apply for this segment
  hl7Msg = HL7::Message.new
  hl7Msg << segmentGenerator.init_msh
  # hl7Msg << segmentGenerator.init_msh(is_base?(@version))

  #iterate over selected segments and build the entire message
  segments.each{ |segment|
    segmentGenerator.generate(hl7Msg, segment, parsers)
   }

  return hl7Msg
end

#generate_message_from_template(parsers, templatePath, useExVal) ⇒ Object

factory method to build message using MWB templates



87
88
89
90
91
92
93
94
95
# File 'lib/ez7gen/message_factory.rb', line 87

def generate_message_from_template(parsers, templatePath, useExVal)

  hl7Msg = HL7::Message.new

  templateGenerator = TemplateGenerator.new(templatePath, parsers)

  return templateGenerator.generate(hl7Msg, useExVal)

end

#get_base_parserObject

Add parser for base version of schema



103
104
105
106
107
108
109
# File 'lib/ez7gen/message_factory.rb', line 103

def get_base_parser()
  # find version for base standard version - standard with 'std' attribute
  v_base = @version_store.find { |s| s[:std] == @std }[:profiles].find { |p| p[:std]!=nil }[:doc]
  v_base_hash = @attributes_hash.clone()
  v_base_hash[:version] = v_base
  ProfileParser.new(v_base_hash)
end

#in_group?(groups, idx) ⇒ Boolean

Identify segment as a part of a group

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/ez7gen/message_factory.rb', line 118

def in_group?(groups, idx)
  is_in_group = !groups.select { |group| group.cover?(idx) }.empty?
  # p is_in_group
end

#is_base?(version) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ez7gen/message_factory.rb', line 98

def is_base?(version)
  isBase = @version_store.find { |s| s[:std] == @std }[:profiles].find { |p| p[:doc] == version }[:std]
end