script for regenerating serials summaries on demand, w/ options like rollback regen-serials-summaries
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Tue, 7 May 2013 21:46:57 +0000 (17:46 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Tue, 7 May 2013 21:48:43 +0000 (17:48 -0400)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/support-scripts/regen_serials_summaries.pl [new file with mode: 0755]

diff --git a/Open-ILS/src/support-scripts/regen_serials_summaries.pl b/Open-ILS/src/support-scripts/regen_serials_summaries.pl
new file mode 100755 (executable)
index 0000000..d164e46
--- /dev/null
@@ -0,0 +1,129 @@
+#!/usr/bin/perl
+
+require "/openils/bin/oils_header.pl";
+
+use strict;
+use warnings;
+use OpenSRF::Utils qw/cleanse_ISO8601/;
+use OpenSRF::Utils::Logger qw/$logger/;
+use OpenSRF::Utils::SettingsClient;
+use OpenILS::Application::AppUtils;
+use OpenILS::Application::Serial;
+
+use Getopt::Std;
+use Data::Dumper;
+
+my $U = 'OpenILS::Application::AppUtils';
+my $debug = 0;
+
+sub regenerate_summaries {
+    my ($rollback, $print, @dist_ids) = @_;
+    print "dist IDs ", join(",", @dist_ids), "\n" if $debug;
+
+    my $e = new OpenILS::Utils::CStoreEditor(xact => 1);
+
+    foreach my $sdist_id (@dist_ids) {
+        # get distribution
+        my $sdist = $e->retrieve_serial_distribution($sdist_id)
+            or return $e->die_event;
+
+        foreach my $type (qw/basic index supplement/) {
+            # get issuances
+            my $issuances = $e->search_serial_issuance([
+                {
+                    "+sdist" => {"id"=> $sdist_id},
+                    "+sitem" => {"status" => "Received"},
+                    "+scap" => {"type" => $type}
+                },
+                {
+                    "join" => {
+                        "sitem" => {},
+                        "scap" => {},
+                        "ssub" => {
+                            "join" => {"sdist" =>{}}
+                        }
+                    },
+                    "order_by" => {
+                        "siss" => "date_published"
+                    }
+                }
+            ]) or return $e->die_event;
+
+            next unless @$issuances;
+
+            my $evt = OpenILS::Application::Serial::_prepare_summaries(
+                $e, $issuances, $sdist, $type
+            );
+            if ($U->event_code($evt)) {
+                $e->rollback;
+                return $evt;
+            }
+        }
+    }
+
+    if ($print) {
+        my $summaries =
+            $e->search_serial_any_summary({distribution => \@dist_ids}) or
+                return $e->die_event;
+
+        print(Dumper($_), "\n") for sort {
+            $a->{summary_type} cmp $b->{summary_type} || $a->{id} <=> $b->{id}
+        } map { $_->to_bare_hash } @$summaries;
+    }
+
+    if ($rollback) {
+        $e->rollback;
+    } else {
+        $e->commit;
+    }
+
+    return;
+}
+
+sub usage {
+    print STDERR qq<
+$0: regenerate serials summaries for given distributions
+
+    $0 -h | -d DISTID1,DISTID2,... [-c /openils/conf/opensrf_core.xml]
+        [-p] [-r] [-D]
+
+    -h  this help message
+    -d  comma-separated list of distribution IDs
+    -c  path to opensrf config file telling us where Evergreen is
+    -p  print regenerated summaries
+    -r  rollback transaction instead of committing
+    -D  give a little extra debugging output
+>;
+    exit 1;
+}
+
+#############################################################################
+### main
+
+my @dists;
+my $opts = {};
+
+getopts('Dc:d:hrp', $opts) or usage;
+
+usage if $opts->{h};
+$debug = $opts->{D};
+
+if ($opts->{d}) {
+    @dists = map(int, split ",", $opts->{d});
+    usage if not @dists;
+} else {
+    print STDERR "-d required\n";
+    usage;
+}
+
+osrf_connect($opts->{c} || $ENV{SRF_CORE} || "/openils/conf/opensrf_core.xml");
+
+my $evt = regenerate_summaries($opts->{r}, $opts->{p}, @dists);
+if (defined $evt) {
+    print STDERR "Failure: " . Dumper($evt) . "\n";
+    exit 2;
+} else {
+    print "OK\n" if $debug;
+}
+
+0;