]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/import_clean_marc.pl
eet leevs
[Evergreen.git] / Open-ILS / src / extras / import / import_clean_marc.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use lib '../../perlmods/';
4 use lib '../../../../OpenSRF/src/perlmods/';
5 use OpenSRF::EX qw/:try/;
6 use OpenSRF::System;
7 use OpenSRF::Utils::SettingsClient;
8 use OpenILS::Utils::FlatXML;
9 use OpenILS::Utils::Fieldmapper;
10 use Time::HiRes;
11 use Getopt::Long;
12 use Data::Dumper;
13
14 my ($config, $userid, $sourceid, $wormize) = ('/pines/conf/bootstrap.conf', 1, 2);
15
16 GetOptions (    
17         "file=s"        => \$config,
18         "wormize"       => \$wormize,
19         "sourceid"      => \$sourceid,
20         "userid=i"      => \$userid,
21 );
22
23 OpenSRF::System->bootstrap_client( config_file => $config );
24 my $st_server = OpenSRF::AppSession->create( 'open-ils.storage' );
25 my $worm_server = OpenSRF::AppSession->create( 'open-ils.worm' ) if ($wormize);
26
27 try {
28
29         throw OpenSRF::EX::PANIC ("I can't connect to the storage server!")
30                 if (!$st_server->connect);
31
32         throw OpenSRF::EX::PANIC ("I can't connect to the worm server!")
33                 if ($wormize && !$worm_server->connect);
34
35 } catch Error with {
36         die shift;
37 };
38
39
40 while ( my $xml = <> ) {
41         chomp $xml;
42
43         my $new_id;
44
45         my $ns = OpenILS::Utils::FlatXML->new( xml => $xml );
46
47         next unless ($ns->xml);
48
49         my $doc = $ns->xml_to_doc;
50         my $tcn = $doc->documentElement->findvalue( '/*/*[@tag="035"]' );
51
52         $tcn =~ s/^.*?(\w+)$/$1/go;
53
54         warn "Adding record for TCN $tcn\n";
55
56         #$ns->xml_to_nodeset;
57         #next;
58
59         warn "  ==> Starting transaction...\n";
60
61         my $xact = $st_server->request( 'open-ils.storage.transaction.begin' );
62         $xact->wait_complete;
63
64         my $r = $xact->recv;
65         die $r unless (UNIVERSAL::can($r, 'content'));
66         die "Couldn't start transaction!" unless ($r);
67         
68         warn "  ==> Transaction ".$xact->session->session_id." started\n";
69
70         try {
71                 my $fe = new Fieldmapper::biblio::record_entry;
72                 $fe->editor( $userid );
73                 $fe->creator( $userid );
74                 $fe->source( $sourceid );
75                 $fe->tcn_value( $tcn );
76
77                 my $req = $st_server->request( 'open-ils.storage.biblio.record_entry.create' => $fe );
78
79                 $req->wait_complete;
80
81                 my $resp = $req->recv;
82                 unless( $resp && $resp->can('content') ) {
83                         throw OpenSRF::EX::ERROR ("Failed to create record for TCN [$tcn].  Got an exception!! -- ".$resp->toString);
84                 }
85
86                 $new_id = $resp->content;
87                 warn "    (new record_entry id is $new_id)\n";
88
89                 $req->finish;
90
91                 if ($new_id) {
92
93                         #$ns->xml_to_nodeset;
94                         #my $nodeset = $ns->nodeset;
95                         #$_->owner_doc( $new_id ) for (@$nodeset);
96
97                         my $rec = new Fieldmapper::biblio::record_marc;
98                         $rec->id( $new_id );
99                         $rec->marc( $xml );
100
101                         $req = $st_server->request( 'open-ils.storage.biblio.record_marc.create', $rec );
102
103                         $req->wait_complete;
104
105                         $resp = $req->recv;
106                         unless( $resp && $resp->can('content') ) {
107                                 throw OpenSRF::EX::ERROR ("Failed to create record_nodes for TCN [$tcn].  Got an exception!! -- $resp");
108                         }
109
110
111                         $req->finish;
112                 } else {
113                         throw OpenSRF::EX::ERROR ("Failed to create record for TCN [$tcn].  Got no new ID !! -- ".$resp->toString);
114                 }
115         } catch Error with {
116                 warn "  !!> Rolling back transaction\n".shift();
117                 $xact = $st_server->request( 'open-ils.storage.transaction.rollback' );
118                 $xact->wait_complete;
119
120                 die $r unless (UNIVERSAL::can($r, 'content'));
121                 die "Couldn't rollback transaction!" unless ($r->content);
122
123                 $xact = undef;
124         };
125
126         if ($xact) {
127                 warn "  ==>Commiting addition of $tcn\n";
128                 $xact = $st_server->request( 'open-ils.storage.transaction.commit' );
129                 $xact->wait_complete;
130
131                 my $r = $xact->recv;
132                 die $r unless (UNIVERSAL::can($r, 'content'));
133                 die "Couldn't commit transaction!" unless ($r->content);
134
135                 if ($wormize) {
136                         $worm_server->request( 'open-ils.worm.wormize', $new_id,);
137                 }
138
139         }
140 }
141
142
143
144
145