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