]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/src/extras/import/parse_patron_xml.pl
sanity checking
[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, 3, 'User', '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         next if ($patron->nodeType == 3);
82         my $p = new Fieldmapper::actor::user;
83         my $card = new Fieldmapper::actor::card;
84         my $profile_sce = new Fieldmapper::actor::stat_cat_entry_user_map;
85
86         my $old_profile = $patron->findvalue( 'user_profile' );
87
88         my $bc = $patron->findvalue( 'user_id' );
89
90         unless (defined($bc)) {
91                 my $xml = $patron->toString;
92                 warn "!!! no barcode found in UMS data, user number $count, xml => $xml \n";
93                 $count++;
94                 next;
95         }
96
97         my $uid;
98         if (keys %u_map) {
99                 $uid = $u_map{$bc};
100                 unless ($uid) {
101                         $count++;
102                         warn "!!! no uid mapping found for barcode $bc\n";
103                         next;
104                 }
105         } else {
106                 next;
107         }
108
109         unless ($uid > 1) {
110                 $count++;
111                 warn "!!! user id lower than 2\n";
112                 next;
113         }
114         
115         $card->barcode( $bc );
116         $card->usr( $uid );
117         $card->active( 't' );
118
119         $p->id( $uid );
120         $p->usrname( $bc );
121         $p->passwd( $patron->findvalue( 'user_pin' ) );
122
123         my $new_profile = $p_map{$old_profile} || $default_profile;
124
125         $p->profile( $$profiles{$new_profile} );
126         if (!$p->profile) {
127                 $count++;
128                 warn "!!! no new profile found for $old_profile\n";
129                 next;
130         }
131
132         # some defaults
133         $p->standing(1);
134         $p->active('t');
135         $p->deleted('f');
136         $p->master_account('f');
137         $p->super_user('f');
138         $p->usrgroup($uid);
139         $p->claims_returned_count(0);
140         $p->credit_forward_balance(0);
141         $p->last_xact_id('IMPORT-'.$starttime);
142
143         $p->barred('f');
144         $p->barred('t') if ( $patron->findvalue( 'user_status' ) eq 'BARRED' );
145
146         $p->ident_type( $identtypeid );
147         my $id_val = $patron->findvalue( 'user_altid' );
148         $p->ident_value( $id_val ) if ($id_val);
149
150         my ($fname,$mname,$lname) = ($patron->findvalue('first_name'),$patron->findvalue('middle_name'),$patron->findvalue('last_name'));
151
152         $fname =~ s/^\s*//o;
153         $mname =~ s/^\s*//o;
154         $lname =~ s/^\s*//o;
155
156         $fname =~ s/\s*$//o;
157         $mname =~ s/\s*$//o;
158         $lname =~ s/\s*$//o;
159
160         $p->first_given_name( $fname );
161         $p->second_given_name( $mname );
162         $p->family_name( $lname );
163
164         $p->day_phone( $patron->findvalue( 'Address/dayphone' ) );
165         $p->evening_phone( $patron->findvalue( 'Address/homephone' ) );
166         $p->other_phone( $patron->findvalue( 'Address/workphone' ) );
167
168         my $hlib = $$orgs{$patron->findvalue( 'user_library' )};
169         unless ($hlib) {
170                 $count++;
171                 warn "!!! no home library found in patron record\n";
172                 next;
173         }
174         $p->home_ou( $hlib );
175
176         $p->dob( parse_date( $patron->findvalue( 'birthdate' ) ) );
177         $p->create_date( parse_date( $patron->findvalue( 'user_priv_granted' ) ) );
178         $p->expire_date( parse_date( $patron->findvalue( 'user_priv_expires' ) ) );
179
180         $p->alert_message("Legacy Import Message: old profile was FIXME")
181                 if ($old_profile eq 'FIXME');
182
183         my $net_access = 1;
184         $net_access = 2 if ($old_profile =~ /^U.I/o);
185         $net_access = 3 if ($old_profile =~ /^X.I/o);
186
187         $p->net_access_level( $net_access );
188
189         $profile_sce->target_usr( $uid );
190         $profile_sce->stat_cat( $profileid );
191         $profile_sce->stat_cat_entry( $old_profile );
192
193         my @addresses;
194         my $mailing_addr_id = $patron->findvalue( 'user_mailingaddr' );
195
196         my $all_valid = 't';
197         for my $addr ( $patron->findnodes( "Address" ) ) {
198                 if (!$p->email) {
199                         $p->email( $patron->findvalue( 'email' ) );
200                 }
201
202                 my $prefix = 'coa_';
203
204                 my $line1 = $addr->findvalue( "${prefix}line1" );
205                 $prefix = 'std_' if (!$line1);
206
207                 $line1 = $addr->findvalue( "${prefix}line1" );
208                 next unless ($line1);
209
210                 my $a = new Fieldmapper::actor::user_address;
211                 $a->usr( $uid );
212                 $a->street1( $line1 );
213                 $a->street2( $addr->findvalue( "${prefix}line2" ) );
214                 $a->city( $addr->findvalue( "${prefix}city" ) );
215                 $a->state( $addr->findvalue( "${prefix}state" ) );
216                 $a->post_code(
217                         $addr->findvalue( "${prefix}zip" ) .
218                         '-' . $addr->findvalue( "${prefix}zip4" )
219                 );
220                 
221                 $a->valid( 'f' );
222                 $a->valid( 't' ) if ($prefix eq 'std_');
223                 $a->valid( 'f' ) if ($prefix eq 'std_' and $a->findvalue( "${prefix}dpvscore" ) < 3);
224                 
225                 $a->within_city_limits( 'f' );
226                 $a->country('USA');
227
228                 if ($addr->getAttribute('addr_type') == $mailing_addr_id) {
229                         $a->address_type( 'LEGACY MAILING' );
230                 } else {
231                         $a->address_type( 'LEGACY' );
232                 }
233
234                 push @addresses, $a;
235
236                 if ($prefix eq 'coa_') {
237                         $all_valid = 'f';
238                         $prefix = 'std_';
239
240                         $line1 = $addr->findvalue( "${prefix}line1" );
241                         next unless ($line1);
242
243                         $a = new Fieldmapper::actor::user_address;
244                         $a->usr( $uid );
245                         $a->street1( $line1 );
246                         $a->street2( $addr->findvalue( "${prefix}line2" ) );
247                         $a->city( $addr->findvalue( "${prefix}city" ) );
248                         $a->state( $addr->findvalue( "${prefix}state" ) );
249                         $a->post_code(
250                                 $addr->findvalue( "${prefix}zip" ) .
251                                 '-' . $addr->findvalue( "${prefix}zip4" )
252                         );
253                 
254                         $a->valid( 'f' );
255                 
256                         $a->within_city_limits( 'f' );
257                         $a->country('USA');
258
259                         $a->address_type( 'LEGACY' );
260
261                         push @addresses, $a;
262                 }
263         }
264
265         if ($all_valid eq 'f') {
266                 $_->valid('f') for (@addresses);
267         }
268
269         my @notes;
270         for my $note_field ( qw#note comment voter bus_school Address/phone1 Address/phone2# ) {
271                 for my $note ( $patron->findnodes( $note_field) ) {
272                         my $a = new Fieldmapper::actor::usr_note;
273
274                         $a->creator(1);
275                         $a->create_date('now');
276                         $a->usr( $uid );
277                         $a->title( "Legacy ".$note->localName );
278                         $a->value( $note->textContent );
279                         $a->pub( 'f' );
280                         push @notes, $a;
281                 }
282         }
283
284         print STDERR "\r$count     ".$count/(time - $starttime) unless ($count % 100);
285         print JSON->perl2JSON( $_ )."\n" for ($p,$card,$profile_sce,@addresses,@notes);
286
287         $count++;
288 }
289
290 print STDERR "\n";
291
292
293 sub parse_date {
294         my $string = shift;
295         my $group = shift;
296
297         my ($y,$m,$d);
298
299         if ($string eq 'NEVER') {
300                 my (undef,undef,undef,$d,$m,$y) = localtime();
301                 return sprintf('%04d-%02d-%02d', $y + 1920, $m + 1, $d);
302         } elsif (length($string) == 8 && $string =~ /^(\d{4})(\d{2})(\d{2})$/o) {
303                 ($y,$m,$d) = ($1,$2,$3);
304         } elsif ($string =~ /(\d+)\D(\d+)\D(\d+)/o) { #looks like it's parsable
305                 if ( length($3) > 2 )  { # looks like mm.dd.yyyy
306                         if ( $1 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
307                                 if ($1 > 12 && $1 < 31 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
308                                         ($y,$m,$d) = ($3,$2,$1);
309                                 } elsif ($2 > 12 && $2 < 31 && $1 < 13) {
310                                         ($y,$m,$d) = ($3,$1,$2);
311                                 }
312                         }
313                 } elsif ( length($1) > 3 ) { # format probably yyyy.mm.dd
314                         if ( $3 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
315                                 if ($2 > 12 && $2 < 32 && $3 < 13) { # well, actually it looks like yyyy.dd.mm -- why, I don't konw
316                                         ($y,$m,$d) = ($1,$3,$2);
317                                 } elsif ($3 > 12 && $3 < 31 && $2 < 13) {
318                                         ($y,$m,$d) = ($1,$2,$3);
319                                 }
320                         }
321                 } elsif ( $1 < 99 && $2 < 99 && $3 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
322                         if ($3 < 7) { # probably 2000 or greater, mm.dd.yy
323                                 $y = $3 + 2000;
324                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
325                                         ($m,$d) = ($2,$1);
326                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
327                                         ($m,$d) = ($1,$2);
328                                 }
329                         } else { # probably before 2000, mm.dd.yy
330                                 $y = $3 + 1900;
331                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
332                                         ($m,$d) = ($2,$1);
333                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
334                                         ($m,$d) = ($1,$2);
335                                 }
336                         }
337                 }
338         }
339
340         my $date;
341         if ($y && $m && $d) {
342                 try {
343                         $date = sprintf('%04d-%02d-%-2d',$y, $m, $d)
344                                 if (new DateTime ( year => $y, month => $m, day => $d ));
345                 } otherwise {};
346         }
347
348         return $date;
349 }
350