1 # The OpenILS::Utils::LooseEDI classes are an intentiaonally simplistic way to
2 # represent EDI interchanges and the messages contained therein (which are in
3 # turn made up of segment groups, segments, and smaller data structures).
5 # There is virtually no validation against EDIFACT or Editeur rules. All we're
6 # doing here is the minimum data munging against incoming JEDI that will let us
7 # access segments by name without looping and searching for them (much), when
8 # they're where they should be.
10 # Segment groups are hereinafter just "groups." Groups can belong to other
11 # groups, and segments can belong to groups, but groups cannot belong to
14 # Groups and segments at a given level always appear in
15 # arrays in case there are any repeats of the the same thing at the same level.
16 # Anything "less" than a segment is just copied as-is from the JEDI.
18 # The class you want to instantiate is OpenILS::Utils::LooseEDI::Interchange.
19 # The only argument you need to give new() is the JEDI data (in string form
22 package OpenILS::Utils::LooseEDI::Segment; # so simple it does nothing.
28 my ($class, $data) = @_;
30 my $self = bless $data, $class; # data is already hashref
37 package OpenILS::Utils::LooseEDI::Group;
42 use OpenSRF::Utils::Logger qw/:logger/;
45 my ($class, $data) = @_;
59 foreach (@{$self->{data}}) {
60 $logger->warn("bad element in data for " . __PACKAGE__) unless
63 my ($left, $right) = @$_;
64 $self->{$left} ||= [];
65 push @{$self->{$left}}, $self->load_children($right);
72 my ($self, $thing) = @_;
74 if (ref $thing eq 'ARRAY') {
75 return new OpenILS::Utils::LooseEDI::Group($thing);
76 } elsif (ref $thing eq 'HASH') {
77 return new OpenILS::Utils::LooseEDI::Segment($thing);
79 $logger->warn("unexpected data, neither array nor hashref");
85 package OpenILS::Utils::LooseEDI::Message;
90 # In our unsophisticated implementation, a message is just like a segment group.
91 use base 'OpenILS::Utils::LooseEDI::Group';
96 return $self->{UNH}[0]{S009}{'0065'};
101 package OpenILS::Utils::LooseEDI::Interchange;
106 use OpenSRF::EX qw/:try/;
107 use OpenSRF::Utils::JSON;
108 use OpenSRF::Utils::Logger qw/:logger/;
111 my ($class, $data) = @_;
113 $data = OpenSRF::Utils::JSON->JSON2perl($data) unless ref $data;
115 if (ref $data eq 'HASH') {
117 throw new OpenSRF::EX::Error("Interchange lacks body") unless
119 throw new OpenSRF::EX::Error("Interchange has empty body") unless
120 ref $data->{body} eq 'ARRAY' and @{ $data->{body} };
122 my $self = bless {}, $class;
124 foreach my $part (@{ $data->{body} }) {
125 foreach my $msgname (grep /^[A-Z]/, keys %$part) {
126 $self->{$msgname} ||= [];
128 new OpenILS::Utils::LooseEDI::Message($part->{$msgname});
129 if ($msgname ne $message->message_name) {
131 "Found message thought to be named $msgname, " .
132 "but it says " . $message->message_name
135 push @{$self->{$msgname}}, $message;
140 $logger->error(__PACKAGE__ . " given bad data");