LP#1410369: Create user messages when configured to do so
authorMike Rylander <mrylander@gmail.com>
Mon, 16 Feb 2015 19:07:57 +0000 (14:07 -0500)
committerBill Erickson <berickxx@gmail.com>
Fri, 20 Feb 2015 21:58:17 +0000 (16:58 -0500)
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 <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/ProcessMessage.pm [new file with mode: 0644]

index e8d2f7e..99e7e3e 100644 (file)
@@ -171,12 +171,34 @@ sub react {
         } else {
             $self->update_state( 'reacting') || die 'Unable to update event state';
             try {
         } 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';
             } 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->{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;
         $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);
         }
             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 {
     
         $self->environment->{complete} = 1;
     } otherwise {
index 7c9127a..dd23448 100644 (file)
@@ -463,5 +463,26 @@ sub run_TT {
     return $output;
 }
 
     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;
 
 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 (file)
index 0000000..47b51e9
--- /dev/null
@@ -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 <<ABOUT;
+
+The ProcessMessage Reactor Module simply processes the configured
+message template.  The output is returned, or undef on error.
+
+ABOUT
+}
+
+sub handler {
+    my $self = shift;
+    my $env = shift;
+    return $self->run_message_TT($env);
+}
+
+1;
+