From 85e73bc2c8caa94b14c78c44866411ca192b2c82 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Tue, 7 Feb 2017 12:25:01 -0500 Subject: [PATCH] LP#1596595 AOUS lookup batch by org id Org unit setting value lookup for batches of org units, instead of the traditional batches by setting name. Perl live test included. Signed-off-by: Bill Erickson Signed-off-by: Chris Sharp Signed-off-by: Kathy Lussier --- .../perlmods/lib/OpenILS/Application/AppUtils.pm | 38 +++++++++++++++++++ .../src/perlmods/live_t/21-batch-org-settings.t | 43 ++++++++++++++++++++++ Open-ILS/src/sql/Pg/020.schema.functions.sql | 20 ++++++++++ .../upgrade/YYYY.schema.batch_settings_by_org.sql | 24 ++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 Open-ILS/src/perlmods/live_t/21-batch-org-settings.t create mode 100644 Open-ILS/src/sql/Pg/upgrade/YYYY.schema.batch_settings_by_org.sql diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm index dca778db9d..a8c6c9ff63 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm @@ -1333,6 +1333,44 @@ sub ou_ancestor_setting_batch_insecure { return %result; } +# Returns a hash of hashes like so: +# { +# $lookup_org_id => {org => $context_org, value => $setting_value}, +# $lookup_org_id2 => {org => $context_org2, value => $setting_value2}, +# $lookup_org_id3 => {} # example of no setting value exists +# ... +# } +sub ou_ancestor_setting_batch_by_org_insecure { + my ($self, $org_ids, $name, $e) = @_; + + $e ||= OpenILS::Utils::CStoreEditor->new(); + my %result = map { $_ => {value => undef} } @$org_ids; + + my $query = { + from => [ + 'actor.org_unit_ancestor_setting_batch_by_org', + $name, '{' . join(',', @$org_ids) . '}' + ] + }; + + # DB func returns an array of settings matching the order of the + # list of org unit IDs. If the setting does not contain a valid + # ->id value, then no setting value exists for that org unit. + my $settings = $e->json_query($query); + for my $idx (0 .. $#$org_ids) { + my $setting = $settings->[$idx]; + my $org_id = $org_ids->[$idx]; + + next unless $setting->{id}; # null ID means no value is present. + + $result{$org_id}->{org} = $setting->{org_unit}; + $result{$org_id}->{value} = + OpenSRF::Utils::JSON->JSON2perl($setting->{value}); + } + + return %result; +} + # returns the ISO8601 string representation of the requested epoch in GMT sub epoch2ISO8601 { my( $self, $epoch ) = @_; diff --git a/Open-ILS/src/perlmods/live_t/21-batch-org-settings.t b/Open-ILS/src/perlmods/live_t/21-batch-org-settings.t new file mode 100644 index 0000000000..27d88b5e02 --- /dev/null +++ b/Open-ILS/src/perlmods/live_t/21-batch-org-settings.t @@ -0,0 +1,43 @@ +#!perl +use strict; use warnings; +use Test::More tests => 180; # 15 orgs * 12 settings +use OpenILS::Utils::TestUtils; +use OpenILS::Application::AppUtils; +my $U = 'OpenILS::Application::AppUtils'; + +diag("Tests batch org setting retrieval"); + +my $script = OpenILS::Utils::TestUtils->new(); +$script->bootstrap; + +my $org_ids = [1 .. 15]; +# All settings at time of writing. None of these have view perms. +my @settings = qw/ + circ.patron_search.diacritic_insensitive + circ.checkout_auto_renew_age + cat.label.font.weight + cat.spine.line.height + circ.grace.extend + cat.label.font.size + circ.booking_reservation.default_elbow_room + cat.spine.line.width + lib.info_url + circ.hold_go_home_interval + cat.label.font.family + cat.spine.line.margin +/; + +# compare the values returned from the batch-by-org setting to the +# traditional setting value lookup call. +for my $setting (@settings) { + my %batch_settings = + $U->ou_ancestor_setting_batch_by_org_insecure($org_ids, $setting); + + for my $org_id (@$org_ids) { + my $value = $U->ou_ancestor_setting_value($org_id, $setting); + is($value, $batch_settings{$org_id}->{value}, + "Value matches for setting $setting and org $org_id"); + } +} + + diff --git a/Open-ILS/src/sql/Pg/020.schema.functions.sql b/Open-ILS/src/sql/Pg/020.schema.functions.sql index 7cc5be0c6c..b2560877e8 100644 --- a/Open-ILS/src/sql/Pg/020.schema.functions.sql +++ b/Open-ILS/src/sql/Pg/020.schema.functions.sql @@ -321,6 +321,26 @@ For each setting name passed, search "up" the org_unit tree until we find the first occurrence of an org_unit_setting with the given name. $$; +CREATE OR REPLACE FUNCTION actor.org_unit_ancestor_setting_batch_by_org( + setting_name TEXT, org_ids INTEGER[]) + RETURNS SETOF actor.org_unit_setting AS +$FUNK$ +DECLARE + setting RECORD; + org_id INTEGER; +BEGIN + /* Returns one actor.org_unit_setting row per org unit ID provided. + When no setting exists for a given org unit, the setting row + will contain all empty values. */ + FOREACH org_id IN ARRAY org_ids LOOP + SELECT INTO setting * FROM + actor.org_unit_ancestor_setting(setting_name, org_id); + RETURN NEXT setting; + END LOOP; + RETURN; +END; +$FUNK$ LANGUAGE plpgsql STABLE; + CREATE OR REPLACE FUNCTION evergreen.get_barcodes(select_ou INT, type TEXT, in_barcode TEXT) RETURNS SETOF evergreen.barcode_set AS $$ DECLARE cur_barcode TEXT; diff --git a/Open-ILS/src/sql/Pg/upgrade/YYYY.schema.batch_settings_by_org.sql b/Open-ILS/src/sql/Pg/upgrade/YYYY.schema.batch_settings_by_org.sql new file mode 100644 index 0000000000..567183f307 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/YYYY.schema.batch_settings_by_org.sql @@ -0,0 +1,24 @@ +BEGIN; + +CREATE OR REPLACE FUNCTION actor.org_unit_ancestor_setting_batch_by_org( + setting_name TEXT, org_ids INTEGER[]) + RETURNS SETOF actor.org_unit_setting AS +$FUNK$ +DECLARE + setting RECORD; + org_id INTEGER; +BEGIN + /* Returns one actor.org_unit_setting row per org unit ID provided. + When no setting exists for a given org unit, the setting row + will contain all empty values. */ + FOREACH org_id IN ARRAY org_ids LOOP + SELECT INTO setting * FROM + actor.org_unit_ancestor_setting(setting_name, org_id); + RETURN NEXT setting; + END LOOP; + RETURN; +END; +$FUNK$ LANGUAGE plpgsql STABLE; + +COMMIT; + -- 2.11.0