More filtering of characters that could end up in PO JEDI template output
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Tue, 11 Oct 2011 15:25:51 +0000 (11:25 -0400)
committerBill Erickson <berick@esilibrary.com>
Mon, 14 Nov 2011 14:56:44 +0000 (09:56 -0500)
Backslashes cause problems too.  Let's JSON encode this thing and get it
right once and for all.

Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Signed-off-by: Ben Shum <bshum@biblio.org>
Signed-off-by: Bill Erickson <berick@esilibrary.com>

Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm
Open-ILS/src/support-scripts/edi_pusher.pl

index 5f07972..14a69e7 100644 (file)
@@ -8,6 +8,7 @@ use Unicode::Normalize;
 use XML::LibXML;
 use OpenSRF::Utils qw/:datetime/;
 use OpenSRF::Utils::Logger qw(:logger);
+use OpenSRF::Utils::JSON;
 use OpenILS::Application::AppUtils;
 use OpenILS::Utils::CStoreEditor qw/:funcs/;
 my $U = 'OpenILS::Application::AppUtils';
@@ -154,6 +155,16 @@ my $_TT_helpers = {
     get_li_attr => \&get_li_attr,
 
     get_li_attr_jedi => sub {
+        # This helper has to mangle data in at least three interesting ways.
+        #
+        # 1) We'll be receiving data that may already have some \-escaped
+        # characters.
+        #
+        # 2) We need our output to be valid JSON.
+        #
+        # 3) We need our output to yield valid and unproblematic EDI when
+        # passed through edi4r by the edi_pusher.pl script.
+
         my $value = get_li_attr(@_);
         if ($value) {
             # Here we can add any number of special case transformations to
@@ -165,14 +176,24 @@ my $_TT_helpers = {
                 chop $value;
             }
 
-            # Make sure any double quotation marks are escaped.
-            $value =~ s/"/\\"/g;
+            # Typical vendors dealing with EDIFACT would seem not to want
+            # any unicode characters, so trash them. Yes, they're already
+            # in the data escaped like this at this point even though we
+            # haven't JSON-escaped things yet.
+            $value =~ s/\\u[0-9a-f]{4}//g;
 
             # What the heck, get rid of [ ] too (although I couldn't get them
-            # to cause any problems for me.
+            # to cause any problems for me, problems have been reported. See
+            # LP #812593).
             $value =~ s/[\[\]]//g;
         }
 
+        $value = OpenSRF::Utils::JSON->perl2JSON($value);
+
+        # Existing action/trigger templates expect an unquoted string.
+        $value =~ s/^"//g;
+        chop $value;
+
         return $value;
     },
 
index ea9d63e..80e4e19 100755 (executable)
@@ -155,13 +155,7 @@ foreach my $def (@$defs) {
             printf STDERR "ERROR: No edi_default account found for $logstr.  File will not be sent!\n";
         }
 
-        my $jedi = $event->template_output()->data;
-
-        # Crucial identifiers won't contain unicode characters, and EDIFACT
-        # (or at least our translator) generally can't handle them anyway.
-        $jedi =~ s/\\u[0-9a-f]{4}//g;
-
-        $message->jedi($jedi);
+        $message->jedi($event->template_output()->data);
 
         print "\ntarget->provider->edi_default->id: ", $target->provider->edi_default->id, "\n";
         my $logstr2 = sprintf "event %s, PO %s, template_output %s", $_->{id}, $message->purchase_order, $event->template_output->id;