]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/import_pines_holdings.pl
arg! cdbi is getting complex... need to restructure
[Evergreen.git] / Open-ILS / src / extras / import / import_pines_holdings.pl
1 #!/usr/bin/perl
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 OpenILS::Utils::Fieldmapper;
8 use XML::LibXML;
9 use Time::HiRes;
10 use Getopt::Long;
11 use Data::Dumper;
12
13 my ($config, $userid, $skip) = ('/pines/conf/bootstrap.conf', 1, 0);
14
15 GetOptions (    
16         "file=s"        => \$config,
17         "userid=i"      => \$userid,
18         "skip=s"        => \$skip,
19 );
20
21 OpenSRF::System->bootstrap_client( config_file => $config );
22 my $st_server = OpenSRF::AppSession->create( 'open-ils.storage' );
23
24 try {
25
26         throw OpenSRF::EX::PANIC ("I can't connect to the storage server!")
27                 if (!$st_server->connect);
28
29 } catch Error with {
30         die shift;
31 };
32
33 my $go = 1;
34 $go = 0 if ($skip);
35
36 while ( my $xml = <> ) {
37         chomp $xml;
38
39         my $new_id;
40
41         unless ($xml) {
42                 warn "!!!!!!!!!!!!!!! NO XML !!!!!!!!!!!!!!!\n";
43                 next;
44         }
45
46         my $doc = XML::LibXML->new()->parse_string($xml);
47         my $tcn = $doc->documentElement->findvalue( '/*/*[@tag="035"]' );
48
49         $tcn =~ s/^.*?(\w+)$/$1/go;
50
51         if (!$go) {
52                 if ($tcn ne $skip) {
53                         warn " Skipping $tcn...\n";
54                         next;
55                 }
56                 $go++;
57         }
58
59         warn "Adding holdings for TCN $tcn\n";
60
61         warn "  ==> Starting transaction...\n";
62
63         my $xact = $st_server->request( 'open-ils.storage.transaction.begin' );
64         $xact->wait_complete;
65
66         my $r = $xact->recv;
67         die "Couldn't start transaction!" unless ($r);
68         die $r unless (UNIVERSAL::can($r, 'content'));
69         
70         warn "  ==> Transaction ".$xact->session->session_id." started\n";
71
72         my $req = $st_server->request( 'open-ils.storage.biblio.record_entry.search.tcn_value' => $tcn );
73
74         $req->wait_complete;
75
76         my $resp = $req->recv;
77         $req->finish;
78
79         if( $resp && !$resp->can('content') ) {
80                 warn ("Failed to retrieve record for TCN [$tcn].  Got an exception!! -- ".$resp->toString."\n");
81                 next;
82         } elsif (!$resp) {
83                 warn ("!!-> Failed to retrieve record for TCN [$tcn].\n");
84                 next;
85         }
86
87         my $rec = $resp->content;
88         unless (@$rec) {
89                 warn ("Failed to retrieve record for TCN [$tcn].\n");
90                 next;
91         }
92         $rec = $rec->[0];
93
94         my @cps;
95         try {
96                 for my $node ($doc->documentElement->findnodes('/*/*[@tag="999"]')) {
97                         unless ($rec->id > $skip) {
98                                 warn("Skipping record ".$rec->id."\n");
99                                 last;
100                         }
101
102                         my $barcode = $node->findvalue( '*[@code="i"]' );
103                         my $cp_req = $st_server->request( 'open-ils.storage.asset.copy.search.barcode' => $barcode );
104
105                         $cp_req->wait_complete;
106         
107                         my $cp_resp = $cp_req->recv;
108                         $cp_req->finish;
109
110                         if ($cp_resp && !$cp_resp->isa('Error') && @{ $cp_resp->content }) {
111                                 warn "Skipping barcode $barcode\n";
112                                 next;
113                         }
114
115         
116                         warn "    (record_entry id is ".$rec->id.")\n";
117         
118                         my $label = $node->findvalue( '*[@code="a"]' );
119                         my $owning_lib = $node->findvalue( '*[@code="m"]' );
120         
121                         my $cn_req = $st_server->request( 'open-ils.storage.asset.call_number.search' =>
122                                                                 {       owning_lib      => $owning_lib,
123                                                                         label           => $label,
124                                                                         record          => $rec->id,
125                                                                 }
126                         );
127
128                         $cn_req->wait_complete;
129         
130                         my $cn_resp = $cn_req->recv;
131                         $cn_req->finish;
132         
133                         my $cn;
134
135                         if ( $cn_resp && !$cn_resp->can('content') ) {
136                                 warn ("Failed to retrieve call_number for $owning_lib:$label.  Got an exception!! -- ".$cn_resp->toString."\n");
137                                 next;
138                         } elsif (!$cn_resp) {
139                                 $cn = new Fieldmapper::asset::call_number;
140                                 $cn->editor( $userid );
141                                 $cn->creator( $userid );
142                                 $cn->record( $rec->id );
143                                 $cn->label( $label );
144                                 $cn->owning_lib( $owning_lib );
145
146                                 $cn_req = $st_server->request( 'open-ils.storage.asset.call_number.create' => $cn );
147                                 $cn_req->wait_complete;
148
149                                 $cn_resp = $cn_req->recv;
150                                 unless( $cn_resp && $cn_resp->can('content') && $cn_resp->content ) {
151                                         warn ("Failed to create call_number for $owning_lib:$label.  Got an exception!! -- ".$cn_resp->toString."\n");
152                                         next;
153                                 }
154                                 $cn->id($cn_resp->content);
155
156                         } else {
157                                 $cn = $cn_resp->content;
158                                 #warn ("Skipping call_number ".$cn->id."\n");
159                                 #next;
160                         }
161
162                         warn "    (call_number id is ".$cn->id.")\n";
163                         
164                         my $barcode = $node->findvalue( '*[@code="i"]' );
165                         my $cp_req = $st_server->request( 'open-ils.storage.asset.copy.search.barcode' => $barcode );
166
167                         $cp_req->wait_complete;
168         
169                         my $cp_resp = $cp_req->recv;
170                         $cp_req->finish;
171
172                         if ($cp_resp && !$cp_resp->isa('Error') && $cp_resp->content) {
173                                 warn "Skipping barcode $barcode\n";
174                         }
175
176                         my $cp = new Fieldmapper::asset::copy;
177                         $cp->editor( $userid );
178                         $cp->creator( $userid );
179                         $cp->call_number( $cn->id );
180
181                         my $price = $node->findvalue( '*[@code="p"]' );
182                         my $genre = $node->findvalue( '*[@code="x"]' );
183                         my $audience = $node->findvalue( '*[@code="z"]' );
184                         my $home_lib = $node->findvalue( '*[@code="m"]' );
185                         my $status = $node->findvalue( '*[@code="k"]' );
186                         my $copy_number = $node->findvalue( '*[@code="c"]' );
187
188                         $cp->barcode( $barcode );
189                         $cp->price( $price );
190                         $cp->genre( $genre );
191                         $cp->audience( $audience );
192                         $cp->home_lib( $home_lib );
193                         $cp->status( $status );
194                         $cp->copy_number( $copy_number );
195
196                         $cp->loan_duration( 2 );
197                         $cp->fine_level( 2 );
198                         $cp->ref( 0 );
199                         $cp->circulate( 1 );
200                         $cp->deposit( '0.00' );
201                         $cp->opac_visible( 1 );
202                         $cp->shelving_loc( 'stacks');
203
204                         warn "    (copy_number is ".$cp->copy_number.")\n";
205                         my $cp_req = $st_server->request( 'open-ils.storage.asset.copy.create' => $cp );
206                         $cp_req->wait_complete;
207
208                         my $cp_resp = $cp_req->recv;
209                         unless( $cp_resp && $cp_resp->can('content') && $cp_resp->content ) {
210                                 throw OpenSRF::EX::ERROR ("Failed to create copy for $barcode.  Got an exception!! -- ".$cp_resp->toString);
211                         }
212
213                 }
214         } catch Error with {
215                 warn "  !!> Rolling back transaction\n".shift();
216                 $xact = $st_server->request( 'open-ils.storage.transaction.rollback' );
217                 $xact->wait_complete;
218
219                 die $r unless (UNIVERSAL::can($r, 'content'));
220                 die "Couldn't rollback transaction!" unless ($r->content);
221
222                 $xact = undef;
223         };
224
225
226 #       if (@cps) {
227 #               my $cp_req = $st_server->request('open-ils.storage.asset.copy.batch.create',@cps);
228 #               $cp_req->wait_complete;
229 #
230 #               @cps = ();
231 #
232                 $xact = $st_server->request( 'open-ils.storage.transaction.commit' );
233                 $xact->wait_complete;
234                 my $r = $xact->recv;
235                 die $r unless (UNIVERSAL::can($r, 'content'));
236                 die "Couldn't commit transaction!" unless ($r->content);
237                 warn "  ==> Commited any changes\n";
238
239 #       }
240
241 }
242
243
244
245
246