1 package OpenILS::Application::Trigger::Reactor;
2 use strict; use warnings;
3 use Encode qw/ encode /;
6 use DateTime::Format::ISO8601;
7 use Unicode::Normalize;
9 use OpenSRF::Utils qw/:datetime/;
10 use OpenSRF::Utils::Logger qw(:logger);
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Utils::CStoreEditor qw/:funcs/;
13 my $U = 'OpenILS::Application::AppUtils';
15 sub fourty_two { return 42 }
16 sub NOOP_True { return 1 }
17 sub NOOP_False { return 0 }
21 # helper functions inserted into the TT environment
24 # turns a date into something TT can understand
27 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
29 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
39 # escapes a string for inclusion in an XML document. escapes &, <, and > characters
42 $str =~ s/&/&/sog;
50 $str =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%0.4x',ord($1))/sgoe;
54 # encode email headers in UTF-8, per RFC2231
55 escape_email_header => sub {
57 $str = encode("MIME-Header", $str);
61 # strip non-ASCII characters after splitting base characters and diacritics
62 # least common denominator for EDIFACT messages using the UNOB character set
63 force_jedi_unob => sub {
66 $str =~ s/[\x{0080}-\x{fffd}]//g;
70 # returns the calculated user locale
71 get_user_locale => sub {
73 return $U->get_user_locale($user_id);
76 # returns the calculated copy price
77 get_copy_price => sub {
79 return $U->get_copy_price(new_editor(xact=>1), $copy_id);
82 # given a copy, returns the title and author in a hash
83 get_copy_bib_basics => sub {
85 my $copy = new_editor(xact=>1)->retrieve_asset_copy([
90 acp => ['call_number'],
95 if($copy->call_number->id == -1) {
97 title => $copy->dummy_title,
98 author => $copy->dummy_author,
101 my $mvr = $U->record_to_mvr($copy->call_number->record);
103 title => $mvr->title,
104 author => $mvr->author
109 # returns the org unit setting value
110 get_org_setting => sub {
111 my($org_id, $setting) = @_;
112 return $U->ou_ancestor_setting_value($org_id, $setting);
115 # This basically greps/maps out ths isbn string values, but also promotes the first isbn-13 to the
116 # front of the line (so that the EDI translator takes it as primary) if there is one.
117 get_li_isbns => sub {
122 $_->attr_name eq 'isbn' or next;
123 my $val = $_->attr_value;
124 if (! $primary and length($val) == 13) {
130 $primary and unshift @isbns, $primary;
131 $logger->debug("get_li_isbns returning isbns: " . join(', ', @isbns));
135 # helpers.get_li_attr('isbn_13', li.attributes)
136 # returns matching line item attribute, or undef
138 my $name = shift or return; # the first arg is always the name
139 my ($type, $attr) = (scalar(@_) == 1) ? (undef, $_[0]) : @_;
140 # if the next is the last, it's the attributes, otherwise type
141 # use Data::Dumper; $logger->warn("get_li_attr: " . Dumper($attr));
142 ($name and @$attr) or return;
144 $name =~ s/^(\D+)_(\d+)$/$1/ and $length = $2;
146 $_->attr_name eq $name or next;
147 next if $length and $length != length($_->attr_value);
148 return $_->attr_value if (! $type) or $type eq $_->attr_type;
153 get_queued_bib_attr => sub {
154 my $name = shift or return; # the first arg is always the name
156 # use Data::Dumper; $logger->warn("get_queued_bib_attr: " . Dumper($attr));
157 ($name and @$attr) or return;
160 select => {'vqbrad' => ['id']},
162 where => {code => $name}
165 my $def_ids = new_editor()->json_query($query);
169 $name =~ s/^(\D+)_(\d+)$/$1/ and $length = $2;
171 $_->field eq @{$def_ids}[0]->{id} or next;
172 next if $length and $length != length($_->attr_value);
173 return $_->attr_value;
178 get_queued_auth_attr => sub {
179 my $name = shift or return; # the first arg is always the name
181 # use Data::Dumper; $logger->warn("get_queued_auth_attr: " . Dumper($attr));
182 ($name and @$attr) or return;
185 select => {'vqarad' => ['id']},
187 where => {code => $name}
190 my $def_ids = new_editor()->json_query($query);
194 $name =~ s/^(\D+)_(\d+)$/$1/ and $length = $2;
196 $_->field eq @{$def_ids}[0]->{id} or next;
197 next if $length and $length != length($_->attr_value);
198 return $_->attr_value;
206 if ($str =~ /\,/ || $str =~ /"/) {
208 $str = '"' . $str . '"';
216 return $str ? (new XML::LibXML)->parse_string($str) : undef;
222 # processes templates. Returns template output on success, undef on error
227 return undef unless $env->{template};
231 my $tt = Template->new;
232 # my $tt = Template->new(ENCODING => 'utf8'); # ??
233 $env->{helpers} = $_TT_helpers;
235 unless( $tt->process(\$env->{template}, $env, \$output) ) {
237 ($error = $tt->error) =~ s/\n/ /og;
238 $logger->error("Error processing Trigger template: $error");
241 if ( $error or (!$nostore && $output) ) {
242 my $t_o = Fieldmapper::action_trigger::event_output->new;
243 $t_o->data( ($error) ? $error : $output );
244 $t_o->is_error( ($error) ? 't' : 'f' );
245 $logger->info("trigger: writing " . length($t_o->data) . " bytes to template output");
247 $env->{EventProcessor}->editor->xact_begin;
248 $t_o = $env->{EventProcessor}->editor->create_action_trigger_event_output( $t_o );
250 my $state = (ref $$env{event} eq 'ARRAY') ? $$env{event}->[0]->state : $env->{event}->state;
251 my $key = ($error) ? 'error_output' : 'template_output';
252 $env->{EventProcessor}->update_state( $state, { $key => $t_o->id } );