blib/lib/Net/HL7/Message.pm untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 31 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 271 SCAR 58.3
CC
18
Cov
0%
_init
72.0
_resetCtrl
20.0
insertSegment
12.0
CRAP 342.0
1 ################################################################################
2 #
3 # File : Message.pm
4 # Author : Duco Dokter
5 # Created : Mon Nov 11 17:37:11 2002
6 # Version : $Id: Message.pm,v 1.21 2015/01/29 15:30:11 wyldebeast Exp $
7 # Copyright : D.A.Dokter, Wyldebeast & Wunderliebe
8 #
9 ################################################################################
10  
11 package Net::HL7::Message;
12  
13 use 5.004;
14 use strict;
15 use warnings;
16 use Net::HL7::Segment;
17 use Net::HL7;
18  
19 =pod
20  
21 =head1 NAME
22  
23 Net::HL7::Message
24  
25 =head1 SYNOPSIS
26  
27 my $request = new Net::HL7::Request();
28 my $conn = new Net::HL7::Connection('localhost', 8089);
29  
30 my $msh = new Net::HL7::Segments::MSH();
31  
32 my $seg1 = new Net::HL7::Segment("PID");
33  
34 $seg1->setField(1, "foo");
35  
36 $request->addSegment($msh);
37 $request->addSegment($seg1);
38  
39 my $response = $conn->send($request);
40  
41  
42 =head1 DESCRIPTION
43  
44 In general one needn't create an instance of the Net::HL7::Message
45 class directly, but use the L<Net::HL7::Request|Net::HL7::Request>
46 class. When adding segments, note that the segment index starts at 0,
47 so to get the first segment, segment, do
48 C<$msg-E<gt>getSegmentByIndex(0)>.
49  
50 The segment separator defaults to \015. To change this, set the
51 variable $Net::HL7::SEGMENT_SEPARATOR.
52  
53  
54 =head1 METHODS
55  
56 =over 4
57  
58 =item B<$msg = new Net::HL7::Message([$msg])>
59  
60 The constructor takes an optional string argument that is a string
61 representation of a HL7 message. If the string representation is not a
62 valid HL7 message. according to the specifications, undef is returned
63 instead of a new instance. This means that segments should be
64 separated within the message with the segment separator (defaults to
65 \015) or a newline, and segments should be syntactically correct.
66 When using the string argument constructor, make sure that you have
67 escaped any characters that would have special meaning in Perl. For
68 instance (using a different subcomponent separator):
69  
70 C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x@y@z^z\r");>
71  
72 would actually mean
73  
74 C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x^z\r");>
75  
76 since '@y@z' would be interpreted as two empty arrays, so do:
77  
78 C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x\@y\@z^z\r");>
79  
80 instead.
81  
82 The control characters and field separator will take the values from
83 the MSH segment, if set. Otherwise defaults will be used. Changing the
84 MSH fields specifying the field separator and control characters after
85 the MSH has been added to the message will result in setting these
86 values for the message.
87  
88 If the message couldn't be created, for example due to a erroneous HL7
89 message string, undef is returned.
90  
91 =cut
92  
93 sub new {
94
95 my $class = shift;
96 bless my $self = {}, $class;
97
98 $self->_init(@_) || return undef;
99
100 return $self;
101 }
102  
103  
104 sub _init {
105  
106 my ($self, $hl7str) = @_;
107  
108 # Array holding the segments
109 #
110 $self->{SEGMENTS} = [];
111  
112 # Control characters and other HL7 properties
113 #
114 $self->{SEGMENT_SEPARATOR} = $Net::HL7::SEGMENT_SEPARATOR;
115 $self->{FIELD_SEPARATOR} = $Net::HL7::FIELD_SEPARATOR;
116 $self->{COMPONENT_SEPARATOR} = $Net::HL7::COMPONENT_SEPARATOR;
117 $self->{SUBCOMPONENT_SEPARATOR} = $Net::HL7::SUBCOMPONENT_SEPARATOR;
118 $self->{REPETITION_SEPARATOR} = $Net::HL7::REPETITION_SEPARATOR;
119 $self->{ESCAPE_CHARACTER} = $Net::HL7::ESCAPE_CHARACTER;
120 $self->{HL7_VERSION} = $Net::HL7::HL7_VERSION;
121  
122 # If an HL7 string is given to the constructor, parse it.
123 if ($hl7str) {
124  
125 my @segments = split("[\n\\" . $self->{SEGMENT_SEPARATOR} . "]", $hl7str);
126  
127 # the first segment should be the control segment
128 #
129 $segments[0] =~ /^([A-Z0-9]{3})(.)(.)(.)(.)(.)(.)/;
130  
131 my ($hdr, $fldSep, $compSep, $repSep, $esc, $subCompSep, $fldSepCtrl) =
132 ($1, $2, $3, $4, $5, $6, $7);
133  
134 # Check whether field separator is repeated after 4 control characters
135  
136 if ($fldSep ne $fldSepCtrl) {
137  
138 return undef;
139 }
140  
141 # Set field separator based on control segment
142 $self->{FIELD_SEPARATOR} = $fldSep;
143
144 # Set other separators
145 $self->{COMPONENT_SEPARATOR} = $compSep;
146 $self->{SUBCOMPONENT_SEPARATOR} = $subCompSep;
147 $self->{ESCAPE_CHARACTER} = $esc;
148 $self->{REPETITION_SEPARATOR} = $repSep;
149
150 # Do all segments
151 #
152 for (my $i = 0; $i < @segments; $i++) {
153
154 my @fields = split('\\' . $self->{FIELD_SEPARATOR}, $segments[$i]);
155  
156 my $name = shift(@fields);
157  
158 # Now decompose fields if necessary, into refs to arrays
159 #
160 for (my $j = 0; $j < @fields; $j++) {
161  
162 # Skip control field
163 if ($i == 0 && $j == 0) {
164
165 next;
166 }
167
168 my @comps = split('\\' . $self->{COMPONENT_SEPARATOR}, $fields[$j]);
169
170 for (my $k = 0; $k < @comps; $k++) {
171  
172 my @subComps = split('\\' . $self->{SUBCOMPONENT_SEPARATOR}, $comps[$k]);
173
174 # Make it a ref or just the value
175 if (@subComps <= 1) {
176 $comps[$k] = $subComps[0];
177 }
178 else {
179 $comps[$k] = \@subComps;
180 }
181  
182 }
183  
184 if (@comps <= 1) {
185 $fields[$j] = $comps[0];
186 }
187 else {
188 $fields[$j] = \@comps;
189 }
190 }
191  
192 my $seg;
193  
194 # untaint
195 my $segClass = "";
196  
197 if ($name =~ /^[A-Z][A-Z0-9]{2}$/) {
198 $segClass = "Net::HL7::Segments::$name";
199 $segClass =~ /^(.*)$/;
200 $segClass = $1;
201 }
202  
203 # Let's see whether it's a special segment
204 #
205 if ( $segClass && eval("require $segClass;") ) {
206 unshift(@fields, $self->{FIELD_SEPARATOR});
207 $seg = eval{ "$segClass"->new(\@fields); };
208 }
209 else {
210 $seg = new Net::HL7::Segment($name, \@fields);
211 }
212
213 $seg || return undef;
214  
215 $self->addSegment($seg);
216 }
217 }
218  
219 return 1;
220 }
221  
222  
223 =pod
224  
225 =item B<addSegment($segment)>
226  
227 Add the segment. to the end of the message. The segment should be an
228 instance of L<Net::HL7::Segment|Net::HL7::Segment>.
229  
230 =cut
231  
232 sub addSegment {
233  
234 my ($self, $segment) = @_;
235  
236 if (@{ $self->{SEGMENTS} } == 0) {
237 $self->_resetCtrl($segment);
238 }
239  
240 push( @{ $self->{SEGMENTS} }, $segment);
241 }
242  
243  
244 =pod
245  
246 =item B<insertSegment($segment, $idx)>
247  
248 Insert the segment. The segment should be an instance of
249 L<Net::HL7::Segment|Net::HL7::Segment>. If the index is not given,
250 nothing happens.
251  
252 =cut
253  
254 sub insertSegment {
255  
256 my ($self, $segment, $idx) = @_;
257  
258 (! defined $idx) && return;
259 ($idx > @{ $self->{SEGMENTS} }) && return;
260  
261 if ($idx == 0) {
262  
263 $self->_resetCtrl($segment);
264 unshift(@{ $self->{SEGMENTS} }, $segment);
265 }
266 elsif ($idx == @{ $self->{SEGMENTS} }) {
267  
268 push(@{ $self->{SEGMENTS} }, $segment);
269 }
270 else {
271 @{ $self->{SEGMENTS} } =
272 (@{ $self->{SEGMENTS} }[0..$idx-1],
273 $segment,
274 @{ $self->{SEGMENTS} }[$idx..@{ $self->{SEGMENTS} } -1]
275 );
276 }
277 }
278  
279  
280 =pod
281  
282 =item B<getSegmentByIndex($index)>
283  
284 Return the segment specified by $index. Segment count within the
285 message starts at 0.
286  
287 =cut
288  
289 sub getSegmentByIndex {
290  
291 my ($self, $index) = @_;
292  
293 return $self->{SEGMENTS}->[$index];
294 }
295  
296  
297 =pod
298  
299 =item B<getSegmentsByName($name)>
300  
301 Return an array of all segments with the given name
302  
303 =cut
304  
305 sub getSegmentsByName {
306  
307 my ($self, $name) = @_;
308  
309 my @segments = ();
310  
311 foreach (@{ $self->{SEGMENTS} }) {
312 ($_->getName() eq $name) && push(@segments, $_);
313 }
314  
315 return @segments;
316 }
317  
318  
319 =pod
320  
321 =item B<removeSegmentByIndex($index)>
322  
323 Remove the segment indexed by $index. If it doesn't exist, nothing
324 happens, if it does, all segments after this one will be moved one
325 index up.
326  
327 =cut
328  
329 sub removeSegmentByIndex {
330  
331 my ($self, $index) = @_;
332  
333 ($index < @{ $self->{SEGMENTS} }) && splice( @{ $self->{SEGMENTS} }, $index, 1);
334 }
335  
336  
337 =pod
338  
339 =item B<setSegment($seg, $index)>
340  
341 Set the segment on index. If index is out of range, or not provided,
342 do nothing. Setting MSH on index 0 will revalidate field separator,
343 control characters and hl7 version, based on MSH(1), MSH(2) and
344 MSH(12).
345  
346 =cut
347  
348 sub setSegment {
349  
350 my ($self, $segment, $idx) = @_;
351  
352 (! defined $idx) && return;
353 ($idx > @{ $self->{SEGMENTS} }) && return;
354  
355 if ($segment->getName() eq "MSH" && $idx == 0) {
356  
357 $self->_resetCtrl($segment);
358 }
359
360 @{ $self->{SEGMENTS} }[$idx] = $segment;
361 }
362  
363  
364 # After change of MSH, reset control fields
365 #
366 sub _resetCtrl {
367  
368 my ($self, $segment) = @_;
369  
370 if ($segment->getField(1)) {
371 $self->{FIELD_SEPARATOR} = $segment->getField(1);
372 }
373
374 if ($segment->getField(2) =~ /(.)(.)(.)(.)/) {
375
376 $self->{COMPONENT_SEPARATOR} = $1;
377 $self->{REPETITION_SEPARATOR} = $2;
378 $self->{ESCAPE_CHARACTER} = $3;
379 $self->{SUBCOMPONENT_SEPARATOR} = $4;
380 }
381
382 if ($segment->getField(12)) {
383 $self->{HL7_VERSION} = $segment->getField(12);
384 }
385 }
386  
387  
388 =pod
389  
390 =item B<getSegments()>
391  
392 Return an array containing all segments in the right order.
393  
394 =cut
395  
396 sub getSegments {
397  
398 my $self = shift;
399  
400 return @{ $self->{SEGMENTS} };
401 }
402  
403  
404 =pod
405  
406 =item B<toString([$pretty])>
407  
408 Return a string representation of this message. This can be used to
409 send the message over a socket to an HL7 server. To print to other
410 output, use the $pretty argument as some true value. This will not use
411 the default segment separator, but '\n' instead.
412  
413 =cut
414  
415 sub toString {
416
417 my ($self, $pretty) = @_;
418 my $msg = "";
419  
420 # Make sure MSH(1) and MSH(2) are ok, even if someone has changed
421 # these values
422 #
423 my $msh = $self->{SEGMENTS}->[0];
424  
425 $self->_resetCtrl($msh);
426  
427 for (my $i = 0; $i < @{ $self->{SEGMENTS} }; $i++) {
428
429 $msg .= $self->getSegmentAsString($i);
430  
431 $pretty ? ($msg .= "\n") : ($msg .= $self->{SEGMENT_SEPARATOR});
432 }
433
434 return $msg;
435 }
436  
437  
438 =pod
439  
440 =item B<getSegmentAsString($index)>
441  
442 Get the string representation of the segment, in the context of this
443 message. That means the string representation will use the message's
444 separators.
445  
446 =cut
447  
448 sub getSegmentAsString {
449  
450 my ($self, $index) = @_;
451  
452 my $seg = $self->getSegmentByIndex($index);
453  
454 $seg || return undef;
455  
456 my $segStr = $seg->getName() . $self->{FIELD_SEPARATOR};
457
458 my $start = $seg->getName() eq "MSH" ? 2 : 1;
459  
460 {
461 no warnings;
462
463 foreach ($start..$seg->size()) {
464
465 $segStr .= $self->getSegmentFieldAsString($index, $_);
466 $segStr .= $self->{FIELD_SEPARATOR};
467 }
468 }
469
470 return $segStr;
471 }
472  
473  
474 =pod
475  
476 =item B<getSegmentFieldAsString($segmentIndex, $fieldIndex)>
477  
478  
479 =cut
480  
481 sub getSegmentFieldAsString {
482
483 my ($self, $segIndex, $fldIndex) = @_;
484  
485 my $seg = $self->getSegmentByIndex($segIndex);
486  
487 $seg || return undef;
488  
489 return $seg->getFieldAsString($fldIndex);
490 }
491  
492  
493 =pod
494  
495 =item B<removeSegmentByName($name)>
496  
497 Remove the segment indexed by $name. If it doesn't exist, nothing
498 happens, if it does, all segments after this one will be moved one
499 index up.
500  
501 =back
502  
503 =cut
504  
505 sub removeSegmentByName {
506  
507 my ($self, $name) = @_;
508 my $i = 0;
509  
510 foreach (@{ $self->{SEGMENTS} }) {
511 if ($_->getName() eq $name) {
512 splice( @{ $self->{SEGMENTS} }, $i, 1);
513 }
514 else {
515 $i++;
516 }
517 }
518 }
519  
520  
521 1;
522  
523 =pod
524  
525 =head1 AUTHOR
526  
527 D.A.Dokter <dokter@wyldebeast-wunderliebe.com>
528  
529 =head1 LICENSE
530  
531 Copyright (c) 2002 D.A.Dokter. All rights reserved. This program is
532 free software; you can redistribute it and/or modify it under the same
533 terms as Perl itself.
534  
535 =cut