]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/import_clean_marc.pl
adding full marc21 record support
[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 $ns = OpenILS::Utils::FlatXML->new( xml => $xml );
44
45         next unless ($ns->xml);
46
47         my $doc = $ns->xml_to_doc;
48         my $tcn = $doc->documentElement->findvalue( '/*/*[@tag="035"]' );
49
50         $tcn =~ s/^.*?(\w+)$/$1/go;
51
52         warn "Adding record for TCN $tcn\n";
53
54         #$ns->xml_to_nodeset;
55         #next;
56
57         warn "  ==> Starting transaction...\n";
58
59         my $xact = $st_server->request( 'open-ils.storage.transaction.begin' );
60         $xact->wait_complete;
61
62         my $r = $xact->recv;
63         die $r unless (UNIVERSAL::can($r, 'content'));
64         die "Couldn't start transaction!" unless ($r);
65         
66         warn "  ==> Transaction ".$xact->session->session_id." started\n";
67
68         try {
69                 my $fe = new Fieldmapper::biblio::record_entry;
70                 $fe->editor( $userid );
71                 $fe->creator( $userid );
72                 $fe->source( $sourceid );
73                 $fe->tcn_value( $tcn );
74
75                 my $req = $st_server->request( 'open-ils.storage.biblio.record_entry.create' => $fe );
76
77                 $req->wait_complete;
78
79                 my $resp = $req->recv;
80                 unless( $resp && $resp->can('content') ) {
81                         throw OpenSRF::EX::ERROR ("Failed to create record for TCN [$tcn].  Got an exception!! -- ".$resp->toString);
82                 }
83
84                 my $new_id = $resp->content;
85
86                 $req->finish;
87
88                 if ($new_id) {
89
90                         #$ns->xml_to_nodeset;
91                         #my $nodeset = $ns->nodeset;
92                         #$_->owner_doc( $new_id ) for (@$nodeset);
93
94                         my $rec = new Fieldmapper::biblio::record_marc;
95                         $rec->id( $new_id );
96                         $rec->marc( $ns->xml );
97                 
98                         $req = $st_server->request(
99                                 'open-ils.storage.biblio.record_marc.create',
100                                 $rec,
101                         );
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                         if ($wormize) {
112                                 my $worm_req = $worm_server->request(
113                                         'open-ils.worm.record_data.digest',
114                                         $new_id,
115                                 );
116                         }
117
118                         $req->finish;
119                 } else {
120                         throw OpenSRF::EX::ERROR ("Failed to create record for TCN [$tcn].  Got no new ID !! -- ".$resp->toString);
121                 }
122         } catch Error with {
123                 warn "  !!> Rolling back transaction\n".shift();
124                 $xact = $st_server->request( 'open-ils.storage.transaction.rollback' );
125                 $xact->wait_complete;
126
127                 die $r unless (UNIVERSAL::can($r, 'content'));
128                 die "Couldn't rollback transaction!" unless ($r->content);
129
130                 $xact = undef;
131         };
132
133         if ($xact) {
134                 warn "  ==>Commiting addition of $tcn\n";
135                 $xact = $st_server->request( 'open-ils.storage.transaction.commit' );
136                 $xact->wait_complete;
137
138                 my $r = $xact->recv;
139                 die $r unless (UNIVERSAL::can($r, 'content'));
140                 die "Couldn't commit transaction!" unless ($r->content);
141
142         }
143 }
144
145
146
147
148