1 package OpenILS::Application::Trigger::Reactor;
2 use strict; use warnings;
5 use DateTime::Format::ISO8601;
6 use Unicode::Normalize;
7 use OpenSRF::Utils qw/:datetime/;
8 use OpenSRF::Utils::Logger qw(:logger);
9 use OpenILS::Application::AppUtils;
10 use OpenILS::Utils::CStoreEditor qw/:funcs/;
11 my $U = 'OpenILS::Application::AppUtils';
13 sub fourty_two { return 42 }
14 sub NOOP_True { return 1 }
15 sub NOOP_False { return 0 }
19 # helper functions inserted into the TT environment
22 # turns a date into something TT can understand
25 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
27 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
37 # escapes a string for inclusion in an XML document. escapes &, <, and > characters
40 $str =~ s/&/&/sog;
48 $str =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%0.4x',ord($1))/sgoe;
52 # strip non-ASCII characters after splitting base characters and diacritics
53 # least common denominator for EDIFACT messages using the UNOB character set
54 force_jedi_unob => sub {
57 $str =~ s/[\x{0080}-\x{fffd}]//g;
61 # returns the calculated user locale
62 get_user_locale => sub {
64 return $U->get_user_locale($user_id);
67 # returns the calculated copy price
68 get_copy_price => sub {
70 return $U->get_copy_price(new_editor(xact=>1), $copy_id);
73 # given a copy, returns the title and author in a hash
74 get_copy_bib_basics => sub {
76 my $copy = new_editor(xact=>1)->retrieve_asset_copy([
81 acp => ['call_number'],
86 if($copy->call_number->id == -1) {
88 title => $copy->dummy_title,
89 author => $copy->dummy_author,
92 my $mvr = $U->record_to_mvr($copy->call_number->record);
95 author => $mvr->author
100 # returns the org unit setting value
101 get_org_setting => sub {
102 my($org_id, $setting) = @_;
103 return $U->ou_ancestor_setting_value($org_id, $setting);
106 # This basically greps/maps out ths isbn string values, but also promotes the first isbn-13 to the
107 # front of the line (so that the EDI translator takes it as primary) if there is one.
108 get_li_isbns => sub {
113 $_->attr_name eq 'isbn' or next;
114 my $val = $_->attr_value;
115 if (! $primary and length($val) == 13) {
121 $primary and unshift @isbns, $primary;
122 $logger->debug("get_li_isbns returning isbns: " . join(', ', @isbns));
126 # helpers.get_li_attr('isbn_13', li.attributes)
127 # returns matching line item attribute, or undef
129 my $name = shift or return; # the first arg is always the name
130 my ($type, $attr) = (scalar(@_) == 1) ? (undef, $_[0]) : @_;
131 # if the next is the last, it's the attributes, otherwise type
132 # use Data::Dumper; $logger->warn("get_li_attr: " . Dumper($attr));
133 ($name and @$attr) or return;
135 $name =~ s/^(\D+)_(\d+)$/$1/ and $length = $2;
137 $_->attr_name eq $name or next;
138 next if $length and $length != length($_->attr_value);
139 return $_->attr_value if (! $type) or $type eq $_->attr_type;
146 # processes templates. Returns template output on success, undef on error
151 return undef unless $env->{template};
155 my $tt = Template->new;
156 # my $tt = Template->new(ENCODING => 'utf8'); # ??
157 $env->{helpers} = $_TT_helpers;
159 unless( $tt->process(\$env->{template}, $env, \$output) ) {
161 ($error = $tt->error) =~ s/\n/ /og;
162 $logger->error("Error processing Trigger template: $error");
165 if ( $error or (!$nostore && $output) ) {
166 my $t_o = Fieldmapper::action_trigger::event_output->new;
167 $t_o->data( ($error) ? $error : $output );
168 $t_o->is_error( ($error) ? 't' : 'f' );
169 $logger->info("trigger: writing " . length($t_o->data) . " bytes to template output");
171 $env->{EventProcessor}->editor->xact_begin;
172 $t_o = $env->{EventProcessor}->editor->create_action_trigger_event_output( $t_o );
174 my $state = (ref $$env{event} eq 'ARRAY') ? $$env{event}->[0]->state : $env->{event}->state;
175 my $key = ($error) ? 'error_output' : 'template_output';
176 $env->{EventProcessor}->update_state( $state, { $key => $t_o->id } );