]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/src/extras/import/parse_patron_xml.pl
importer parts
[Evergreen.git] / Evergreen / src / extras / import / parse_patron_xml.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use lib '/openils/lib/perl5';
6
7 use OpenSRF::System;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::AppSession;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenILS::Utils::Fieldmapper;
12 use Digest::MD5 qw/md5_hex/;
13 use Getopt::Long;
14 use JSON;
15 use DateTime;
16 use Time::HiRes qw/time/;
17 use XML::LibXML;
18
19 my ($file,$config,$profileid,$identtypeid,$default_profile,$profile_map,$usermap) =
20         ('return_file_0623-2.xml', '/openils/conf/bootstrap.conf', 1, 1, 1, 'profile.map');
21
22 GetOptions(
23         'usermap=s'        => \$usermap,
24         'file=s'        => \$file,
25         'config=s'      => \$config,
26         'default_profile=i'      => \$default_profile,
27         'profile_map=s'      => \$profile_map,
28         'profile_statcat_id=i'      => \$profileid,
29         'identtypeid=i'      => \$identtypeid,
30 );
31
32 my %u_map;
33 if ($usermap) {
34         open F, $usermap;
35         while (my $line = <F>) {
36                 chomp($line);
37                 my ($b,$i) = split(/\|/, $line);
38                 $b =~ s/^\s*(\S+)\s*$/$1/o;
39                 $i =~ s/^\s*(\S+)\s*$/$1/o;
40                 $u_map{$b} = $i;
41         }
42         close F;
43 }
44
45 my %p_map;
46 if ($profile_map) {
47         open F, $profile_map;
48         while (my $line = <F>) {
49                 chomp($line);
50                 my ($b,$i) = split(/\|/, $line);
51                 $b =~ s/^\s*(\S+)\s*$/$1/o;
52                 $i =~ s/^\s*(\S+)\s*$/$1/o;
53                 $p_map{$b} = $i;
54         }
55         close F;
56 }
57
58 my $doc = XML::LibXML->new->parse_file($file);
59
60 OpenSRF::System->bootstrap_client( config_file => $config );
61 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
62
63 my $cstore = OpenSRF::AppSession->create( 'open-ils.cstore' );
64
65 my $profiles = $cstore->request(
66                 'open-ils.cstore.direct.permission.grp_tree.search.atomic',
67                 { id => { '!=' => undef } },
68 )->gather(1);
69
70 my $orgs = $cstore->request(
71                 'open-ils.cstore.direct.actor.org_unit.search.atomic',
72                 { id => { '!=' => undef } },
73 )->gather(1);
74
75 $profiles = { map { ($_->name => $_->id) } @$profiles };
76 $orgs = { map { ($_->shortname => $_->id) } @$orgs };
77
78 my $starttime = time;
79 my $count = 1;
80 for my $patron ( $doc->documentElement->childNodes ) {
81         my $p = new Fieldmapper::actor::user;
82         my $card = new Fieldmapper::actor::card;
83         my $profile_sce = new Fieldmapper::actor::stat_cat_entry_user_map;
84
85         my $old_profile = $patron->findvalue( 'user_profile' );
86
87         my $bc = $patron->findvalue( 'user_id' );
88
89         next unless $bc;
90
91         my $uid;
92         if (keys %u_map) {
93                 $uid = $u_map{$bc};
94                 unless ($uid) {
95                         $count++;
96                         next;
97                 }
98         } else {
99                 $uid = $count;
100         }
101
102         unless ($uid > 1) {
103                 $count++;
104                 next;
105         }
106         
107         $card->barcode( $bc );
108         $card->usr( $uid );
109         $card->active( 't' );
110
111         $p->id( $uid );
112         $p->usrname( $bc );
113         $p->passwd( $patron->findvalue( 'user_pin' ) );
114
115         my $new_profile = $p_map{$old_profile};
116         unless ($new_profile) {
117                 $count++;
118                 next;
119         }
120
121         $p->profile( $$profiles{$new_profile} || $default_profile );
122
123         # some defaults
124         $p->standing(1);
125         $p->active('t');
126         $p->master_account('f');
127         $p->super_user('f');
128         $p->usrgroup($uid);
129         $p->claims_returned_count(0);
130         $p->credit_forward_balance(0);
131         $p->last_xact_id('IMPORT-'.$starttime);
132
133         $p->barred('f');
134         $p->barred('t') if ( $patron->findvalue( 'user_status' ) eq 'BARRED' );
135
136         $p->ident_type( $identtypeid );
137         my $id_val = $patron->findvalue( 'user_altid' );
138         $p->ident_value( $id_val ) if ($id_val);
139
140         $p->first_given_name( $patron->findvalue( 'firstname' ) );
141         $p->second_given_name( $patron->findvalue( 'middle_name' ) );
142         $p->family_name( $patron->findvalue( 'last_name' ) );
143
144         $p->day_phone( $patron->findvalue( 'Address/dayphone' ) );
145         $p->evening_phone( $patron->findvalue( 'Address/homephone' ) );
146         $p->other_phone( $patron->findvalue( 'Address/workphone' ) );
147         $p->email( $patron->findvalue( 'email' ) );
148
149         my $hlib = $$orgs{$patron->findvalue( 'user_library' )};
150         unless ($hlib) {
151                 $count++;
152                 next;
153         }
154         $p->home_ou( $hlib );
155
156         $p->dob( parse_date( $patron->findvalue( 'birthdate' ) ) );
157         $p->create_date( parse_date( $patron->findvalue( 'user_priv_granted' ) ) );
158         $p->expire_date( parse_date( $patron->findvalue( 'user_priv_expires' ) ) );
159
160         $p->alert_message("Legacy Import Message: old profile was FIXME")
161                 if ($old_profile eq 'FIXME');
162
163         my $net_access = 1;
164         $net_access = 2 if ($old_profile =~ /^U.I/o);
165         $net_access = 3 if ($old_profile =~ /^X.I/o);
166
167         $p->net_access_level( $net_access );
168
169         $profile_sce->target_usr( $uid );
170         $profile_sce->stat_cat( $profileid );
171         $profile_sce->stat_cat_entry( $old_profile );
172
173         my @addresses;
174         my $mailing_addr_id = $patron->findvalue( 'user_mailingaddr' );
175
176         for my $addr ( $patron->findnodes( "Address" ) ) {
177                 my $prefix = 'coa_';
178
179                 my $line1 = $addr->findvalue( "${prefix}line1" );
180                 $prefix = 'std_' if (!$line1);
181
182                 $line1 = $addr->findvalue( "${prefix}line1" );
183                 next unless ($line1);
184
185                 my $a = new Fieldmapper::actor::user_address;
186                 $a->usr( $uid );
187                 $a->street1( $line1 );
188                 $a->street2( $addr->findvalue( "${prefix}line2" ) );
189                 $a->city( $addr->findvalue( "${prefix}city" ) );
190                 $a->state( $addr->findvalue( "${prefix}state" ) );
191                 $a->post_code(
192                         $addr->findvalue( "${prefix}zip" ) .
193                         '-' . $addr->findvalue( "${prefix}zip4" )
194                 );
195                 
196                 $a->valid( 'f' );
197                 $a->valid( 't' ) if ($prefix eq 'std_');
198                 
199                 $a->within_city_limits( 'f' );
200                 $a->country('USA');
201
202                 if ($addr->getAttribute('addr_type') == $mailing_addr_id) {
203                         $a->address_type( 'LEGACY MAILING' );
204                 } else {
205                         $a->address_type( 'LEGACY' );
206                 }
207
208                 push @addresses, $a;
209
210                 if ($prefix eq 'coa_') {
211                         $prefix = 'std_';
212
213                         $line1 = $addr->findvalue( "${prefix}line1" );
214                         next unless ($line1);
215
216                         $a = new Fieldmapper::actor::user_address;
217                         $a->usr( $uid );
218                         $a->street1( $line1 );
219                         $a->street2( $addr->findvalue( "${prefix}line2" ) );
220                         $a->city( $addr->findvalue( "${prefix}city" ) );
221                         $a->state( $addr->findvalue( "${prefix}state" ) );
222                         $a->post_code(
223                                 $addr->findvalue( "${prefix}zip" ) .
224                                 '-' . $addr->findvalue( "${prefix}zip4" )
225                         );
226                 
227                         $a->valid( 'f' );
228                         $a->valid( 't' ) if ($prefix eq 'std_');
229                 
230                         $a->within_city_limits( 'f' );
231                         $a->country('USA');
232
233                         $a->address_type( 'LEGACY' );
234
235                         push @addresses, $a;
236                 }
237         }
238
239         my @notes;
240         for my $note_field ( qw#note comment voter bus_school Address/phone1 Address/phone2# ) {
241                 for my $note ( $patron->findnodes( $note_field) ) {
242                         my $a = new Fieldmapper::actor::usr_note;
243
244                         $a->creator(1);
245                         $a->usr( $uid );
246                         $a->title( "Legacy ".$note->localName );
247                         $a->value( $note->textContent );
248                         $a->pub( 'f' );
249                         push @notes, $a;
250                 }
251         }
252
253         print STDERR "\r$count     ".$count/(time - $starttime) unless ($count % 100);
254         print JSON->perl2JSON( $_ )."\n" for ($p,$card,$profile_sce,@addresses,@notes);
255
256         $count++;
257 }
258
259 print STDERR "\n";
260
261
262 sub parse_date {
263         my $string = shift;
264         my $group = shift;
265
266         my ($y,$m,$d);
267
268         if ($string eq 'NEVER') {
269                 my (undef,undef,undef,$d,$m,$y) = localtime();
270                 return sprintf('%04d-%02d-%02d', $y + 1920, $m + 1, $d);
271         } elsif (length($string) == 8 && $string =~ /^(\d{4})(\d{2})(\d{2})$/o) {
272                 ($y,$m,$d) = ($1,$2,$3);
273         } elsif ($string =~ /(\d+)\D(\d+)\D(\d+)/o) { #looks like it's parsable
274                 if ( length($3) > 2 )  { # looks like mm.dd.yyyy
275                         if ( $1 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
276                                 if ($1 > 12 && $1 < 31 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
277                                         ($y,$m,$d) = ($3,$2,$1);
278                                 } elsif ($2 > 12 && $2 < 31 && $1 < 13) {
279                                         ($y,$m,$d) = ($3,$1,$2);
280                                 }
281                         }
282                 } elsif ( length($1) > 3 ) { # format probably yyyy.mm.dd
283                         if ( $3 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
284                                 if ($2 > 12 && $2 < 32 && $3 < 13) { # well, actually it looks like yyyy.dd.mm -- why, I don't konw
285                                         ($y,$m,$d) = ($1,$3,$2);
286                                 } elsif ($3 > 12 && $3 < 31 && $2 < 13) {
287                                         ($y,$m,$d) = ($1,$2,$3);
288                                 }
289                         }
290                 } elsif ( $1 < 99 && $2 < 99 && $3 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
291                         if ($3 < 7) { # probably 2000 or greater, mm.dd.yy
292                                 my $y = $3 + 2000;
293                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
294                                         ($m,$d) = ($2,$1);
295                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
296                                         ($m,$d) = ($1,$2);
297                                 }
298                         } else { # probably before 2000, mm.dd.yy
299                                 my $y = $3 + 1900;
300                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
301                                         ($m,$d) = ($2,$1);
302                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
303                                         ($m,$d) = ($1,$2);
304                                 }
305                         }
306                 }
307         }
308
309         my $date;
310         if ($y && $m && $d) {
311                 try {
312                         $date = sprintf('%04d-%02d-%-2d',$y, $m, $d)
313                                 if (new DateTime ( year => $y, month => $m, day => $d ));
314                 } otherwise {};
315         }
316
317         return $date;
318 }
319