]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/src/extras/import/import_holdings.pl
updating the profile map
[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,$map_file,$lib_map_field,$id_tag) =
40         (1, 1, 1, 'asset_copy.sql','asset_volume.sql','record_id_map.pl','shortname','/*/*/*[@tag="035"][1]');
41
42 my ($holding_tag,$bc,$lbl,$own,$pr,$cpn,$avail) =
43         ('/*/*/*[@tag="999"]','i','a','m','p','c','k');
44
45 my ($db_driver,$db_host,$db_name,$db_user,$db_pw) =
46         ('Pg','localhost','demo-dev','postgres','postgres');
47
48 GetOptions (    
49         "copy_file=s"           => \$cp_file,
50         "volume_file=s"         => \$cn_file,
51         "tcn_map_file=s"        => \$map_file,
52         "userid=i"              => \$userid,
53         "first_volume=i"        => \$cn_id,
54         "first_copy=i"          => \$cp_id,
55         "db_driver=s"           => \$db_driver,
56         "db_host=s"             => \$db_host,
57         "db_name=s"             => \$db_name,
58         "db_user=s"             => \$db_user,
59         "db_pw=s"               => \$db_pw,
60         "lib_map_field=s"       => \$lib_map_field,
61         "id_tag_xpath=s"        => \$id_tag,
62         "holding_tag_xpath=s"   => \$holding_tag,
63         "item_barcode=s"        => \$bc,
64         "item_call_number=s"    => \$lbl,
65         "item_owning_lib=s"     => \$own,
66         "item_price=s"          => \$pr,
67         "item_copy_number=s"    => \$cpn,
68         "item_copy_status=s"    => \$avail,
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_map;
88 eval `cat $map_file`;
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
110 my $xml = '';
111 while ( $xml .= <STDIN> ) {
112         chomp $xml;
113         next unless $xml;
114
115         my $tcn;
116         my $doc;
117         my $success = 0;
118         try {
119                 $doc = $parser->parse_string($xml);;
120                 $tcn = $doc->documentElement->findvalue( '//*[@tag="035"][1]' );
121                 $success = 1;
122         } catch Error with {
123                 my $e = shift;
124                 warn $e;
125                 warn $xml;
126         };      
127         next unless $success;
128
129         $tcn =~ s/^.*?(\w+)\s*$/$1/go;
130         
131         unless ($tcn) {
132                 warn "\nNo TCN found in rec!!\n";
133                 $xml = '';
134                 next;
135         }
136         $tcn = "_$tcn";
137
138         unless (exists($$tcn_map{$tcn})) {
139                 warn "\n !! TCN $tcn not in the map!\n";
140                 $xml = '';
141                 next;
142         }
143
144         my $rec_id = $$tcn_map{$tcn};
145
146         for my $node ($doc->documentElement->findnodes($holding_tag)) {
147                 my $barcode = $node->findvalue( "*[\@code=\"$bc\"]" );
148                 my $label = $node->findvalue( "*[\@code=\"$lbl\"]" );
149                 my $owning_lib = $$lib_map{ $node->findvalue( "*[\@code=\"$own\"]" ) };
150                 my $price = $node->findvalue( "*[\@code=\"$pr\"]" );
151                 my $copy_number = $node->findvalue( "*[\@code=\"$cpn\"]" ) || 0;
152                 my $available = $node->findvalue( "*[\@code=\"$avail\"]" ) || '';
153
154                 my $status = $status_map{$available} || 0;
155
156                 next unless $barcode;
157                 next unless $owning_lib;
158                 next unless $label;
159
160                 $barcode =~ s/\\/\\\\/og;
161                 $label =~ s/\\/\\\\/og;
162                 $price =~ s/\$//og;
163                 if ($price !~ /^\s*\d{1,6}\.\d{2}\s*$/o) {
164                         $price = '0.00';
165                 }
166
167                 unless (exists($$cn_map{"$rec_id/$owning_lib/$label"})) {
168                         $$cn_map{"$rec_id/$owning_lib/$label"} = $cn_id;
169                         print CN join("\t",($cn_id,$userid,$userid,$rec_id,$label,$owning_lib))."\n";
170                         print 'v';
171                         $cn_id++;
172                 }
173
174 # id,editor,creator,barcode,call_number,copy_number,available,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible
175
176                 print CP join("\t", (   $cp_id,$owning_lib,$userid,$userid,$barcode,
177                                         $$cn_map{"$rec_id/$owning_lib/$label"},
178                                         $copy_number,$status,2,2,1,0,'0.00',
179                                         $price,0,1 )
180                          )."\n";
181                 print 'c';
182                 $cp_id++;
183         }
184         $xml = '';
185 }
186
187 print CN "\\.\n";
188 print CN "SELECT setval('asset.call_number_id_seq'::TEXT, $cn_id);\n";
189 print CP "\\.\n";
190 print CP "SELECT setval('asset.copy_id_seq'::TEXT, $cp_id);\n";
191