5 use lib '/openils/lib/perl5';
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/;
16 use Time::HiRes qw/time/;
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');
23 'usermap=s' => \$usermap,
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,
35 while (my $line = <F>) {
37 my ($b,$i) = split(/\|/, $line);
38 $b =~ s/^\s*(\S+)\s*$/$1/o;
39 $i =~ s/^\s*(\S+)\s*$/$1/o;
48 while (my $line = <F>) {
50 my ($b,$i) = split(/\|/, $line);
51 $b =~ s/^\s*(\S+)\s*$/$1/o;
52 $i =~ s/^\s*(\S+)\s*$/$1/o;
58 my $doc = XML::LibXML->new->parse_file($file);
60 OpenSRF::System->bootstrap_client( config_file => $config );
61 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
63 my $cstore = OpenSRF::AppSession->create( 'open-ils.cstore' );
65 my $profiles = $cstore->request(
66 'open-ils.cstore.direct.permission.grp_tree.search.atomic',
67 { id => { '!=' => undef } },
70 my $orgs = $cstore->request(
71 'open-ils.cstore.direct.actor.org_unit.search.atomic',
72 { id => { '!=' => undef } },
75 $profiles = { map { ($_->name => $_->id) } @$profiles };
76 $orgs = { map { ($_->shortname => $_->id) } @$orgs };
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;
85 my $old_profile = $patron->findvalue( 'user_profile' );
87 my $bc = $patron->findvalue( 'user_id' );
107 $card->barcode( $bc );
109 $card->active( 't' );
113 $p->passwd( $patron->findvalue( 'user_pin' ) );
115 my $new_profile = $p_map{$old_profile} || $default_profile;
117 $p->profile( $$profiles{$new_profile} );
127 $p->master_account('f');
130 $p->claims_returned_count(0);
131 $p->credit_forward_balance(0);
132 $p->last_xact_id('IMPORT-'.$starttime);
135 $p->barred('t') if ( $patron->findvalue( 'user_status' ) eq 'BARRED' );
137 $p->ident_type( $identtypeid );
138 my $id_val = $patron->findvalue( 'user_altid' );
139 $p->ident_value( $id_val ) if ($id_val);
141 my ($fname,$mname,$lname) = ($patron->findvalue('first_name'),$patron->findvalue('middle_name'),$patron->findvalue('last_name'));
151 $p->first_given_name( $fname );
152 $p->second_given_name( $mname );
153 $p->family_name( $lname );
155 $p->day_phone( $patron->findvalue( 'Address/dayphone' ) );
156 $p->evening_phone( $patron->findvalue( 'Address/homephone' ) );
157 $p->other_phone( $patron->findvalue( 'Address/workphone' ) );
159 my $hlib = $$orgs{$patron->findvalue( 'user_library' )};
164 $p->home_ou( $hlib );
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' ) ) );
170 $p->alert_message("Legacy Import Message: old profile was FIXME")
171 if ($old_profile eq 'FIXME');
174 $net_access = 2 if ($old_profile =~ /^U.I/o);
175 $net_access = 3 if ($old_profile =~ /^X.I/o);
177 $p->net_access_level( $net_access );
179 $profile_sce->target_usr( $uid );
180 $profile_sce->stat_cat( $profileid );
181 $profile_sce->stat_cat_entry( $old_profile );
184 my $mailing_addr_id = $patron->findvalue( 'user_mailingaddr' );
187 for my $addr ( $patron->findnodes( "Address" ) ) {
189 $p->email( $patron->findvalue( 'email' ) );
194 my $line1 = $addr->findvalue( "${prefix}line1" );
195 $prefix = 'std_' if (!$line1);
197 $line1 = $addr->findvalue( "${prefix}line1" );
198 next unless ($line1);
200 my $a = new Fieldmapper::actor::user_address;
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" ) );
207 $addr->findvalue( "${prefix}zip" ) .
208 '-' . $addr->findvalue( "${prefix}zip4" )
212 $a->valid( 't' ) if ($prefix eq 'std_');
214 $a->within_city_limits( 'f' );
217 if ($addr->getAttribute('addr_type') == $mailing_addr_id) {
218 $a->address_type( 'LEGACY MAILING' );
220 $a->address_type( 'LEGACY' );
225 if ($prefix eq 'coa_') {
229 $line1 = $addr->findvalue( "${prefix}line1" );
230 next unless ($line1);
232 $a = new Fieldmapper::actor::user_address;
234 $a->street1( $line1 );
235 $a->street2( $addr->findvalue( "${prefix}line2" ) );
236 $a->city( $addr->findvalue( "${prefix}city" ) );
237 $a->state( $addr->findvalue( "${prefix}state" ) );
239 $addr->findvalue( "${prefix}zip" ) .
240 '-' . $addr->findvalue( "${prefix}zip4" )
245 $a->within_city_limits( 'f' );
248 $a->address_type( 'LEGACY' );
254 if ($all_valid eq 'f') {
255 $_->valid('f') for (@addresses);
259 for my $note_field ( qw#note comment voter bus_school Address/phone1 Address/phone2# ) {
260 for my $note ( $patron->findnodes( $note_field) ) {
261 my $a = new Fieldmapper::actor::usr_note;
264 $a->create_date('now');
266 $a->title( "Legacy ".$note->localName );
267 $a->value( $note->textContent );
273 print STDERR "\r$count ".$count/(time - $starttime) unless ($count % 100);
274 print JSON->perl2JSON( $_ )."\n" for ($p,$card,$profile_sce,@addresses,@notes);
288 if ($string eq 'NEVER') {
289 my (undef,undef,undef,$d,$m,$y) = localtime();
290 return sprintf('%04d-%02d-%02d', $y + 1920, $m + 1, $d);
291 } elsif (length($string) == 8 && $string =~ /^(\d{4})(\d{2})(\d{2})$/o) {
292 ($y,$m,$d) = ($1,$2,$3);
293 } elsif ($string =~ /(\d+)\D(\d+)\D(\d+)/o) { #looks like it's parsable
294 if ( length($3) > 2 ) { # looks like mm.dd.yyyy
295 if ( $1 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
296 if ($1 > 12 && $1 < 31 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
297 ($y,$m,$d) = ($3,$2,$1);
298 } elsif ($2 > 12 && $2 < 31 && $1 < 13) {
299 ($y,$m,$d) = ($3,$1,$2);
302 } elsif ( length($1) > 3 ) { # format probably yyyy.mm.dd
303 if ( $3 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
304 if ($2 > 12 && $2 < 32 && $3 < 13) { # well, actually it looks like yyyy.dd.mm -- why, I don't konw
305 ($y,$m,$d) = ($1,$3,$2);
306 } elsif ($3 > 12 && $3 < 31 && $2 < 13) {
307 ($y,$m,$d) = ($1,$2,$3);
310 } elsif ( $1 < 99 && $2 < 99 && $3 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
311 if ($3 < 7) { # probably 2000 or greater, mm.dd.yy
313 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
315 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
318 } else { # probably before 2000, mm.dd.yy
320 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
322 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
330 if ($y && $m && $d) {
332 $date = sprintf('%04d-%02d-%-2d',$y, $m, $d)
333 if (new DateTime ( year => $y, month => $m, day => $d ));