From 66fe10e7edab6b4215691c8f077361dbe2a1d9a0 Mon Sep 17 00:00:00 2001 From: miker Date: Wed, 25 Nov 2009 18:07:13 +0000 Subject: [PATCH] Adding support for an opaque user_data field on Action/Trigger events: * New column on action_trigger.event and fieldmapper field (user_data) to hold json-encoded data blob * New optional param to event creation calls to pass said data blob (related, added a granularity param to targeted event creator -- pass undef in that position to ignore def granularity, as before) * New grouped event environment array, called user_data, carrying the peer user_data fields to target and event * New grouped event method, environment.EventProcessor.findEvent(), for grabbing a specific Event instance (fully fleshed) git-svn-id: svn://svn.open-ils.org/ILS/trunk@15026 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/examples/fm_IDL.xml | 1 + Open-ILS/src/perlmods/OpenILS/Application/Trigger.pm | 9 +++++++++ .../src/perlmods/OpenILS/Application/Trigger/Event.pm | 16 +++++++++++++++- .../perlmods/OpenILS/Application/Trigger/EventGroup.pm | 14 ++++++++++++++ Open-ILS/src/sql/Pg/002.functions.config.sql | 7 +++++++ Open-ILS/src/sql/Pg/002.schema.config.sql | 2 +- Open-ILS/src/sql/Pg/400.schema.action_trigger.sql | 1 + .../0092.schema.action_trigger.event.user_data.sql | 8 ++++++++ 8 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0092.schema.action_trigger.event.user_data.sql diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml index 5f35b32acf..e4a1890c78 100644 --- a/Open-ILS/examples/fm_IDL.xml +++ b/Open-ILS/examples/fm_IDL.xml @@ -730,6 +730,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger.pm index 994f01ea44..7b8a939e6f 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Trigger.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Trigger.pm @@ -4,6 +4,7 @@ use OpenILS::Application; use base qw/OpenILS::Application/; use OpenSRF::EX qw/:try/; +use OpenSRF::Utils::JSON; use OpenSRF::AppSession; use OpenSRF::Utils::SettingsClient; @@ -30,6 +31,8 @@ sub create_active_events_for_object { my $key = shift; my $target = shift; my $location = shift; + my $granularity = shift; + my $user_data = shift; my $ident = $target->Identity; my $ident_value = $target->$ident(); @@ -58,6 +61,7 @@ sub create_active_events_for_object { ); for my $def ( @$defs ) { + next if ($granularity && $def->granularity ne $granularity ); if ($def->usr_field && $def->opt_in_setting) { my $ufield = $def->usr_field; @@ -93,6 +97,7 @@ sub create_active_events_for_object { $event->target( $ident_value ); $event->event_def( $def->id ); $event->run_time( $date->strftime( '%F %T%z' ) ); + $event->user_data( OpenSRF::Utils::JSON->perl2JSON($user_data) ) if (defined($user_data)); $editor->create_action_trigger_event( $event ); @@ -117,6 +122,7 @@ sub create_event_for_object_and_def { my $definitions = shift; my $target = shift; my $location = shift; + my $user_data = shift; my $ident = $target->Identity; my $ident_value = $target->$ident(); @@ -177,6 +183,7 @@ sub create_event_for_object_and_def { $event->target( $ident_value ); $event->event_def( $def->id ); $event->run_time( $date->strftime( '%F %T%z' ) ); + $event->user_data( OpenSRF::Utils::JSON->perl2JSON($user_data) ) if (defined($user_data)); $editor->create_action_trigger_event( $event ); @@ -373,6 +380,7 @@ sub create_batch_events { my $location_field = shift; # where to look for event_def.owner filtering ... circ_lib, for instance, where hook.core_type = circ my $filter = shift || {}; my $granularity = shift; + my $user_data = shift; my $active = ($self->api_name =~ /active/o) ? 1 : 0; if ($active && !keys(%$filter)) { @@ -481,6 +489,7 @@ sub create_batch_events { $event->target( $o_id ); $event->event_def( $def->id ); $event->run_time( $run_time ); + $event->user_data( OpenSRF::Utils::JSON->perl2JSON($user_data) ) if (defined($user_data)); $editor->create_action_trigger_event( $event ); diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Event.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Event.pm index 2f17d2554d..9eaf4a3d33 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Event.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Event.pm @@ -1,6 +1,7 @@ package OpenILS::Application::Trigger::Event; use strict; use warnings; use OpenSRF::EX qw/:try/; +use OpenSRF::Utils::JSON; use OpenSRF::Utils::Logger qw/$logger/; @@ -56,6 +57,9 @@ sub init { ]) ); + $self->user_data(OpenSRF::Utils::JSON->JSON2perl( $self->event->user_data )) + if (defined( $self->event->user_data )); + if ($self->event->state eq 'valid') { $self->valid(1); } elsif ($self->event->state eq 'invalid') { @@ -208,6 +212,15 @@ sub cleanedup { return $self->{cleanedup}; } +sub user_data { + my $self = shift; + return undef unless (ref $self); + + my $r = shift; + $self->{user_data} = $r if (defined $r); + return $self->{user_data}; +} + sub reacted { my $self = shift; return undef unless (ref $self); @@ -368,8 +381,9 @@ sub build_environment { $self->environment->{target} = $self->target; $self->environment->{event} = $self->event; $self->environment->{template} = $self->event->event_def->template; + $self->environment->{user_data} = $self->user_data; - $current_environment = $self->environment; + $current_environment = $self->environment; $self->environment->{params}{ $_->param } = $compartment->reval($_->value) for ( @{$self->event->event_def->params} ); diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/EventGroup.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/EventGroup.pm index 4cd10e1c27..34b942369e 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/EventGroup.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/EventGroup.pm @@ -230,6 +230,17 @@ sub update_state { return $ok || undef; } +sub findEvent { + my $self = shift; + my $member = shift; + + $member = $member->id if (ref($member)); + + my @list = grep { $member == $_->id } @{ $self->events }; + + return shift(@list); +} + sub build_environment { my $self = shift; my $env = $self->environment; @@ -237,6 +248,7 @@ sub build_environment { $$env{EventProcessor} = $self; $$env{target} = []; $$env{event} = []; + $$env{user_data} = []; for my $e ( @{ $self->events } ) { for my $env_part ( keys %{ $e->environment } ) { next if ($env_part eq 'EventProcessor'); @@ -244,6 +256,8 @@ sub build_environment { push @{ $$env{target} }, $e->environment->{target}; } elsif ($env_part eq 'event') { push @{ $$env{event} }, $e->environment->{event}; + } elsif ($env_part eq 'user_data') { + push @{ $$env{user_data} }, $e->environment->{user_data}; } else { $$env{$env_part} = $e->environment->{$env_part}; } diff --git a/Open-ILS/src/sql/Pg/002.functions.config.sql b/Open-ILS/src/sql/Pg/002.functions.config.sql index c5db3d6026..58ed82d4c4 100644 --- a/Open-ILS/src/sql/Pg/002.functions.config.sql +++ b/Open-ILS/src/sql/Pg/002.functions.config.sql @@ -116,5 +116,12 @@ CREATE OR REPLACE FUNCTION oils_i18n_gettext( TEXT, TEXT, TEXT, TEXT ) RETURNS T SELECT $2; $$ LANGUAGE SQL; +CREATE OR REPLACE FUNCTION is_json (TEXT) RETURNS BOOL AS $func$ + use JSON::XS; + my $json = shift(); + eval { decode_json( $json ) }; + return $@ ? 0 : 1; +$func$ LANGUAGE PLPERLU; + COMMIT; diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index 57e25e1954..41918ea92c 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -51,7 +51,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0091'); -- berick +INSERT INTO config.upgrade_log (version) VALUES ('0092'); -- miker CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql b/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql index 37e2971a65..7a6ce57141 100644 --- a/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql +++ b/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql @@ -155,6 +155,7 @@ CREATE TABLE action_trigger.event ( complete_time TIMESTAMPTZ, update_process INT, state TEXT NOT NULL DEFAULT 'pending' CHECK (state IN ('pending','invalid','found','collecting','collected','validating','valid','reacting','reacted','cleaning','complete','error')), + user_data TEXT CHECK (user_data IS NULL OR is_json( user_data )), template_output BIGINT REFERENCES action_trigger.event_output (id), error_output BIGINT REFERENCES action_trigger.event_output (id), async_output BIGINT REFERENCES action_trigger.event_output (id) diff --git a/Open-ILS/src/sql/Pg/upgrade/0092.schema.action_trigger.event.user_data.sql b/Open-ILS/src/sql/Pg/upgrade/0092.schema.action_trigger.event.user_data.sql new file mode 100644 index 0000000000..b99f9f6748 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0092.schema.action_trigger.event.user_data.sql @@ -0,0 +1,8 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0092'); -- miker + +ALTER TABLE action_trigger.event ADD COLUMN user_data TEXT CHECK (user_data IS NULL OR is_json( user_data )); + +COMMIT; + -- 2.11.0