]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/src/extras/import/import_holdings.pl
Let's match marc2bre.pl and default to the 001 for the TCN.
[Evergreen.git] / Evergreen / src / extras / import / import_holdings.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use XML::LibXML;
4 use Time::HiRes qw/time/;
5 use Getopt::Long;
6 use Data::Dumper;
7 use Error qw/:try/;
8 use DBI;
9 use open qw/:utf8/;
10
11 #-------------------------------------------------------------------------------
12 #  The keys of this hash should be the string values stored in your legacy
13 #  system that map to the copy statuses in Open-ILS.  If you don't see a
14 #  legacy status here that you need to carry over to your new Open-ILS install
15 #  you can use the "Copy Statuses" bootstrapping CGI to create an entry for it.
16 #  Then simply a key for the legacy status that points to the SysID of the new
17 #  Open-ILS Copy Status.
18 #-------------------------------------------------------------------------------
19 my %status_map = (
20         ''              => 0,
21         CHECKEDOUT      => 1,
22         BINDERY         => 2,
23         LOST            => 3,
24         MISSING         => 4,
25         INPROCESS       => 5,
26         INTRANSIT       => 6,
27         RESHELVING      => 7,
28         'ON HOLDS SHELF'=> 8,
29         'ON-ORDER'      => 9,
30         ILL             => 10,
31         CATALOGING      => 11,
32         RESERVES        => 12,
33         DISCARD         => 13,
34 );
35
36
37 $|=1;
38
39 my ($userid,$cn_id,$cp_id,$cp_file,$cn_file,$lib_map_field,$id_tag, $marc_file) =
40         (1, 1, 1, 'asset_copy.sql','asset_volume.sql','shortname','./controlfield[@tag="001"]');
41
42 my ($holding_tag,$bc,$lbl,$own,$pr,$cpn,$avail) =
43         ('./datafield[@tag="999"]','i','a','m','p','c','k');
44
45 my ($db_driver,$db_host,$db_name,$db_user,$db_pw) =
46         ('Pg','localhost','evergreen','postgres','postgres');
47
48 GetOptions (    
49         "copy_file=s"           => \$cp_file,
50         "volume_file=s"         => \$cn_file,
51         "userid=i"              => \$userid,
52         "first_volume=i"        => \$cn_id,
53         "first_copy=i"          => \$cp_id,
54         "db_driver=s"           => \$db_driver,
55         "db_host=s"             => \$db_host,
56         "db_name=s"             => \$db_name,
57         "db_user=s"             => \$db_user,
58         "db_pw=s"               => \$db_pw,
59         "lib_map_field=s"       => \$lib_map_field,
60         "id_tag_xpath=s"        => \$id_tag,
61         "holding_tag_xpath=s"   => \$holding_tag,
62         "item_barcode=s"        => \$bc,
63         "item_call_number=s"    => \$lbl,
64         "item_owning_lib=s"     => \$own,
65         "item_price=s"          => \$pr,
66         "item_copy_number=s"    => \$cpn,
67         "item_copy_status=s"    => \$avail,
68         "marc_file=s"   => \$marc_file,
69
70 );
71
72 my $dsn = "dbi:$db_driver:host=$db_host;dbname=$db_name";
73 my $dbh = DBI->connect($dsn,$db_user,$db_pw);
74
75 my $t = 'actor_org_unit';
76 if ($db_driver eq 'Pg') {
77         $t = 'actor.org_unit';
78 }
79 my $sth = $dbh->prepare("SELECT $lib_map_field,id FROM $t");
80 $sth->execute;
81
82 my $lib_map = {};
83 while (my $lib = $sth->fetchrow_arrayref) {
84         $$lib_map{$$lib[0]} = $$lib[1];
85 }
86
87 my $tcn_sth = $dbh->prepare("SELECT id FROM biblio.record_entry WHERE tcn_value = ?");
88 my $rec_id;
89
90 open CP, ">$cp_file" or die "Can't open $cp_file!  $!\n";
91 open CN, ">$cn_file" or die "Can't open $cn_file!  $!\n";
92
93
94 print CP <<SQL;
95 SET CLIENT_ENCODING TO 'UNICODE';
96 COPY asset.copy (id,circ_lib,editor,creator,barcode,call_number,copy_number,status,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible) FROM STDIN;
97 SQL
98
99 print CN <<SQL;
100 SET CLIENT_ENCODING TO 'UNICODE';
101 COPY asset.call_number (id,editor,creator,record,label,owning_lib) FROM STDIN;
102 SQL
103
104 my $xact_id = time;
105
106 my $parser = XML::LibXML->new();
107
108 my $cn_map;
109 my $doc;
110
111 $doc = $parser->parse_file( $marc_file );
112 my $xc = XML::LibXML::XPathContext->new($doc);
113 my @records = $xc->findnodes('//record');
114 foreach my $record (@records) {
115
116         my $tcn;
117         my $success = 0;
118         try {
119                 $tcn = $xc->findvalue( $id_tag, $record );
120                 $success = 1;
121         } catch Error with {
122                 my $e = shift;
123                 warn $e;
124         };      
125         next unless $success;
126
127         $tcn =~ s/^\s*(\.+)\s*/$1/o;
128         $tcn =~ s/\s+/_/go;
129         
130         unless ($tcn) {
131                 warn "\nNo TCN found in rec!!\n";
132                 next;
133         }
134
135         $tcn_sth->execute($tcn);
136         $tcn_sth->bind_col(1, \$rec_id);
137         $tcn_sth->fetch;
138
139         unless ($rec_id) {
140                 warn "\n !! TCN $tcn not in the map!\n";
141                 next;
142         }
143
144         for my $node ($xc->findnodes($holding_tag, $record)) {
145                 my $barcode = $xc->findvalue( "./*[\@code=\"$bc\"]", $node );
146                 my $label = $xc->findvalue( "./*[\@code=\"$lbl\"]", $node );
147                 my $owning_lib = $$lib_map{ $xc->findvalue( "./*[\@code=\"$own\"]", $node ) };
148                 my $price = $xc->findvalue( "./*[\@code=\"$pr\"]", $node );
149                 my $copy_number = $xc->findvalue( "./*[\@code=\"$cpn\"]", $node ) || 0;
150                 my $available = $xc->findvalue( "./*[\@code=\"$avail\"]", $node ) || '';
151
152                 my $status = $status_map{$available} || 0;
153
154                 next unless $barcode;
155                 next unless $owning_lib;
156                 next unless $label;
157
158                 $barcode =~ s/\\/\\\\/og;
159                 $label =~ s/\\/\\\\/og;
160                 $price =~ s/\$//og;
161                 if ($price !~ /^\s*\d{1,6}\.\d{2}\s*$/o) {
162                         $price = '0.00';
163                 }
164
165                 unless (exists($$cn_map{"$rec_id/$owning_lib/$label"})) {
166                         $$cn_map{"$rec_id/$owning_lib/$label"} = $cn_id;
167                         print CN join("\t",($cn_id,$userid,$userid,$rec_id,$label,$owning_lib))."\n";
168                         print 'v';
169                         $cn_id++;
170                 }
171
172 # id,editor,creator,barcode,call_number,copy_number,available,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible
173
174                 print CP join("\t", (   $cp_id,$owning_lib,$userid,$userid,$barcode,
175                                         $$cn_map{"$rec_id/$owning_lib/$label"},
176                                         $copy_number,$status,2,2,1,0,'0.00',
177                                         $price,0,1 )
178                          )."\n";
179                 print 'c';
180                 $cp_id++;
181         }
182 }
183
184 print CN "\\.\n";
185 print CN "SELECT setval('asset.call_number_id_seq'::TEXT, $cn_id);\n";
186 print CP "\\.\n";
187 print CP "SELECT setval('asset.copy_id_seq'::TEXT, $cp_id);\n";
188