From 6e373d0fe4be1cbe847035e62d6280e2b11aa2fa Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Mon, 16 Feb 2015 14:07:57 -0500 Subject: [PATCH] LP#1410369: Create user messages when configured to do so When an A/T definition has both message_usr_path and message_template defined, we gather the user, sending lib, template, and title and push those into the environment. After the user-defined reactor has successfully run, we pass the environment off to a special-purpose handler that generates output for actor.usr_message.message and, upon success of that, we insert the user message into the database. Signed-off-by: Mike Rylander Signed-off-by: Galen Charlton Signed-off-by: Kathy Lussier --- .../lib/OpenILS/Application/Trigger/Event.pm | 46 +++++++++++++++++-- .../OpenILS/Application/Trigger/Reactor.pm | 21 +++++++++ .../Trigger/Reactor/ProcessMessage.pm | 22 +++++++++ 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/ProcessMessage.pm diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm index e8d2f7eaa6..99e7e3e5f3 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm @@ -171,12 +171,34 @@ sub react { } else { $self->update_state( 'reacting') || die 'Unable to update event state'; try { - $self->reacted( - OpenILS::Application::Trigger::ModRunner::Reactor - ->new( $self->event->event_def->reactor, $env ) - ->run - ->final_result + my $reactor = OpenILS::Application::Trigger::ModRunner::Reactor->new( + $self->event->event_def->reactor, + $env ); + + $self->reacted( $reactor->run->final_result); + + if ($env->{usr_message}{usr} && $env->{usr_message}{template}) { + my $message_template_output = + $reactor->pass('ProcessMessage')->run->final_result; + + if ($message_template_output) { + my $usr_message = Fieldmapper::actor::usr_message->new; + $usr_message->title( $self->event->event_def->message_title || $self->event->event_def->name ); + $usr_message->message( $message_template_output ); + $usr_message->usr( $env->{usr_message}{usr}->id ); + $usr_message->sending_lib( $env->{usr_message}{sending_lib}->id ); + + if ($self->editor->xact_begin) { + if ($self->editor->create_actor_usr_message( $usr_message )) { + $self->editor->xact_commit; + } else { + $self->editor->xact_rollback; + } + } + } + } + } otherwise { $log->error("Event reacting failed with ". shift() ); $self->update_state( 'error' ) || die 'Unable to update event state'; @@ -444,6 +466,8 @@ sub build_environment { $self->environment->{target} = $self->target; $self->environment->{event} = $self->event; $self->environment->{template} = $self->event->event_def->template; + $self->environment->{usr_message}{template} = $self->event->event_def->message_template; + $self->environment->{usr_message}{title} = $self->event->event_def->message_title; $self->environment->{user_data} = $self->user_data; $current_environment = $self->environment; @@ -463,6 +487,18 @@ sub build_environment { pop(@group_path); # the last part is a field, should not get fleshed my $group_object = $self->_object_by_path( $self->target, undef, [], \@group_path ) if (@group_path); } + + if ($self->event->event_def->message_usr_path and $self->environment->{usr_message}{template}) { + my @usr_path = split(/\./, $self->event->event_def->message_usr_path); + $self->_object_by_path( $self->target, undef, [qw/usr_message usr/], \@usr_path ); + + if ($self->event->event_def->message_library_path) { + my @library_path = split(/\./, $self->event->event_def->message_library_path); + $self->_object_by_path( $self->target, undef, [qw/usr_message sending_lib/], \@library_path ); + } else { + $self->_object_by_path( $self->event->event_def, undef, [qw/usr_message sending_lib/], ['owner'] ); + } + } $self->environment->{complete} = 1; } otherwise { diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm index 7c9127aa0f..dd23448dbc 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm @@ -463,5 +463,26 @@ sub run_TT { return $output; } +# processes message templates. Returns template output on success, undef on error +sub run_message_TT { + my $self = shift; + my $env = shift; + return undef unless $env->{usr_message}{template}; + + my $error; + my $output = ''; + my $tt = Template->new; + # my $tt = Template->new(ENCODING => 'utf8'); # ?? + $env->{helpers} = $_TT_helpers; + + unless( $tt->process(\$env->{usr_message}{template}, $env, \$output) ) { + $output = undef; + ($error = $tt->error) =~ s/\n/ /og; + $logger->error("Error processing Trigger message template: $error"); + } + + return $output; +} + 1; diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/ProcessMessage.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/ProcessMessage.pm new file mode 100644 index 0000000000..47b51e997a --- /dev/null +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/ProcessMessage.pm @@ -0,0 +1,22 @@ +package OpenILS::Application::Trigger::Reactor::ProcessMessage; +use base 'OpenILS::Application::Trigger::Reactor'; +use strict; use warnings; +use OpenSRF::Utils::Logger qw/:logger/; + +sub ABOUT { + return <run_message_TT($env); +} + +1; + -- 2.43.2