making restore_itemtypes consistent with using check_itemtype
[migration-tools.git] / kmig.d / bin / mig-import
index e94e5d0..95b70b0 100755 (executable)
@@ -25,13 +25,16 @@ my $dbh = KMig::db_connect();
 #$dbh->{mysql_enable_utf8mb4} = 1;
 $dbh->do('SET NAMES utf8mb4');
 
-#to do check for array passed and if not present then use tags 
+my $mig_path = abs_path($0);
+$mig_path =~ s|[^/]+$||;
 
 my @taglist = @ARGV;
 my $arg_list_length = scalar @taglist;
-if($arg_list_length < 1) { @taglist = ("authorisedvalues","booksellers","borrowerattributes","budgets","calendar","circrules","itemtypes","libraries","patrontypes","preferences","reports","smsproviders"); } 
+if($arg_list_length < 1) { @taglist = ("authorisedvalues","booksellers","borrowerattributes","budgets","calendar","circrules","itemtypes","letters","libraries","patrontypes","preferences","reports","smsproviders"); } 
 $MIGGITDIR =~ s/\/\//\//;
 
+my @errors;
+
 foreach my $restore (@taglist) {
     my $restorefile;
     if ($restore eq 'authorisedvalues') {
@@ -68,13 +71,19 @@ foreach my $restore (@taglist) {
         my $timestamp = most_recent_single($MIGGITDIR,'circrules');
         if ($timestamp) { $restorefile = $MIGGITDIR . 'circrules' . '.' . $timestamp . '.xml'; }
         print "Restoring from $restorefile ... \n";
-        if ($restorefile) { restore_circrules($dbh,$restorefile); }
+        if ($restorefile) { @errors = restore_circrules($dbh,$restorefile,@errors); }
     }
     if ($restore eq 'itemtypes') {
         my $timestamp = most_recent_single($MIGGITDIR,'itemtypes');
         if ($timestamp) { $restorefile = $MIGGITDIR . 'itemtypes' . '.' . $timestamp . '.xml'; }
         print "Restoring from $restorefile ... \n";
-        if ($restorefile) { restore_itemtypes($dbh,$restorefile); }
+        if ($restorefile) { @errors = restore_itemtypes($dbh,$restorefile,@errors); }
+    }
+    if ($restore eq 'letters') {
+        my $timestamp = most_recent_single($MIGGITDIR,'letters');
+        if ($timestamp) { $restorefile = $MIGGITDIR . 'letters' . '.' . $timestamp . '.xml'; }
+        print "Restoring from $restorefile ... \n";
+        if ($restorefile) { restore_letters($dbh,$restorefile); }
     }
     if ($restore eq 'libraries') {
         my $timestamp = most_recent_single($MIGGITDIR,'libraries');
@@ -105,12 +114,49 @@ foreach my $restore (@taglist) {
         my $timestamp = most_recent_single($MIGGITDIR,'smsproviders');
         if ($timestamp) { $restorefile = $MIGGITDIR . 'smsproviders' . '.' . $timestamp . '.xml'; }
         print "Restoring from $restorefile ... \n";
-        if ($restorefile) { restore_smsproviders($dbh,$restorefile); }
+        if ($restorefile) { restore_smsproviders($dbh,$restorefile,$mig_path); }
     }
 }
 
 print "Done.\n";
 
+foreach my $e (@errors) { print "$e\n"; }
+
+# ---------------------------
+
+sub check_category {
+    my $dbh = shift;
+    my $value = shift;
+       my $query;
+
+    if ($value and $value ne 'NULL') {
+       $query = "SELECT EXISTS(SELECT 1 FROM categories WHERE categorycode = $value)";
+       } else {
+               return 1;
+       }
+       print "$query\n";
+    my $sth = $dbh->prepare($query);
+    $sth->execute();
+    my @sqlresult = $sth->fetchrow_array;
+    my $result = pop @sqlresult;
+    return $result;
+}
+
+sub check_itemtype {
+    my $dbh = shift;
+    my $value = shift;
+       my $query;
+    if ($value and $value ne 'NULL') {
+        $query = "SELECT EXISTS(SELECT 1 FROM itemtypes WHERE itemtype = $value)";
+    } else {
+               return 1;
+    }
+    my $sth = $dbh->prepare($query);
+    $sth->execute();
+    my @sqlresult = $sth->fetchrow_array;
+    my $result = pop @sqlresult;
+    return $result;
+}
 
 sub most_recent_single {
     my $MGIGITDIR = shift;
@@ -138,9 +184,23 @@ sub restore_authorisedvalues {
 
     my $query = "DELETE FROM authorised_values WHERE category != 'ITEMTYPECAT'";
     my $sth = $dbh->prepare($query);
-    $sth->execute();
+    print "$query\n"; 
+   #$sth->execute();
 
-    foreach my $node ($dom->findnodes('//value')) {
+    $query = "DELETE FROM authorised_value_categories WHERE category_name != 'ITEMTYPECAT'";
+    $sth = $dbh->prepare($query);
+    print "$query\n";
+    #$sth->execute();
+
+    foreach my $node ($dom->findnodes('/document/categories/value')) {
+        my $name = sql_str_not_null($node->findvalue('./name'));
+        $query = "INSERT INTO authorised_value_categories (category_name) VALUES ($name)";
+        $sth = $dbh->prepare($query);
+        print "$query\n";
+        #$sth->execute();
+    }
+
+    foreach my $node ($dom->findnodes('/document/authorisedvalues/value')) {
         my $category = sql_str($node->findvalue('./category'));
         my $authorised_value = sql_str($node->findvalue('./authorised_value'));
         my $lib = sql_str($node->findvalue('./lib'));
@@ -148,7 +208,8 @@ sub restore_authorisedvalues {
         my $imageurl = sql_str($node->findvalue('./imageurl'));
         $query = "INSERT INTO authorised_values (category,authorised_value,lib,lib_opac,imageurl) VALUES ($category,$authorised_value,$lib,$lib_opac,$imageurl)";
         $sth = $dbh->prepare($query);
-        $sth->execute();
+         print "$query\n";
+        #$sth->execute();
     }
     return;
 }
@@ -370,9 +431,11 @@ sub restore_calendar {
 sub restore_circrules {
     my $dbh = shift;
     my $restore_file = shift;
+    my @errors = shift;
     my $parser = XML::LibXML->new();
     my $dom = $parser->parse_file($restore_file);
+    my $error;
+       my $check; 
     #order of operations - clean out circ_rules, then issuingrules; restore issuingrules then circ_rules 
 
     my $query = "DELETE FROM circulation_rules WHERE 1 = 1";
@@ -417,6 +480,18 @@ sub restore_circrules {
         my $opacitemholds = sql_str($node->findvalue('./opacitemholds'));
         my $article_requests = sql_str($node->findvalue('./article_requests'));
         my $note = sql_str($node->findvalue('./note'));
+               $check = check_itemtype($dbh,$itemtype);
+        if ($check == 0 ) {
+            $error = "restore_circrules: issing rules rules itemtype $itemtype was not valid";
+            push @errors, $error;
+            next;
+        }
+               $check = check_category($dbh,$categorycode);
+        if ($check == 0 ) {
+            $error = "restore_circrules: issuing rules rules category $categorycode was not valid";
+            push @errors, $error;
+            next;
+        }
         $query = "INSERT INTO issuingrules (categorycode,itemtype,restrictedtype,rentaldiscount,reservecharge,fine,finedays,maxsuspensiondays,suspension_chargeperiod,firstremind,chargeperiod,chargeperiod_charge_at,accountsent,issuelength,lengthunit,hardduedate,hardduedatecompare,renewalsallowed,renewalperiod,norenewalbefore,auto_renew,no_auto_renewal_after,no_auto_renewal_after_hard_limit,reservesallowed,holds_per_record,holds_per_day,branchcode,overduefinescap,cap_fine_to_replacement_price,onshelfholds,opacitemholds,article_requests,note) VALUES ($categorycode,$itemtype,$restrictedtype,$rentaldiscount,$reservecharge,$fine,$finedays,$maxsuspensiondays,$suspension_chargeperiod,$firstremind,$chargeperiod,$chargeperiod_charge_at,$accountsent,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$renewalsallowed,$renewalperiod,$norenewalbefore,$auto_renew,$no_auto_renewal_after,$no_auto_renewal_after_hard_limit,$reservesallowed,$holds_per_record,$holds_per_day,$branchcode,$overduefinescap,$cap_fine_to_replacement_price,$onshelfholds,$opacitemholds,$article_requests,$note)";
         $sth = $dbh->prepare($query);
         $sth->execute();
@@ -428,19 +503,35 @@ sub restore_circrules {
         my $itemtype = sql_str($node->findvalue('./itemtype'));
         my $rule_name = sql_str($node->findvalue('./rule_name'));
         my $rule_value = sql_str($node->findvalue('./rule_value'));
+
+               $check = check_itemtype($dbh,$itemtype);
+        if ($check == 0 ) { 
+            $error = "restore_circrules: circulation rules rules itemtype $itemtype was not valid"; 
+            push @errors, $error;
+            next; 
+        }
+               $check = check_category($dbh,$categorycode);
+        if ($check == 0 ) { 
+            $error = "restore_circrules: circulation rules rules category $categorycode was not valid";
+            push @errors, $error;
+            next; 
+        }
+
         $query = "INSERT INTO circulation_rules (branchcode,categorycode,itemtype,rule_name,rule_value) VALUES ($branchcode,$categorycode,$itemtype,$rule_name,$rule_value)";
         $sth = $dbh->prepare($query);
         $sth->execute();
     }
-
-    return;
+     
+    return @errors;
 }
 
 sub restore_itemtypes {
     my $dbh = shift;
     my $restorefile = shift;
+       my @errors = shift;
     my $parser = XML::LibXML->new();
     my $dom = $parser->parse_file($restorefile);
+       my $check;
 
     my $query = "UPDATE itemtypes SET searchcategory = NULL";
     my $sth = $dbh->prepare($query);
@@ -485,12 +576,9 @@ sub restore_itemtypes {
         my $sip_media_type = sql_str($node->findvalue('./sip_media_type'));
         my $hideinopac = sql_str($node->findvalue('./hideinopac'));
         my $searchcategory = sql_str($node->findvalue('./searchcategory'));
-        $query = "SELECT itemtype FROM itemtypes WHERE itemtype = $itemtype";
-        $sth = $dbh->prepare($query);
-        $sth->execute();
-        my $answer;
-        while (my @row = $sth->fetchrow_array) { $answer = sql_str($row[0]); }
-        if (!defined $answer or $answer ne $itemtype) {
+
+               $check = check_itemtype($dbh,$itemtype);
+        if ($check == 0) {
             $query = "INSERT INTO itemtypes (itemtype,description,rentalcharge,rentalcharge_daily,rentalcharge_hourly,defaultreplacecost,processfee,notforloan,imageurl,summary,checkinmsg,sip_media_type,hideinopac,searchcategory) 
             VALUES ($itemtype,$description,$rentalcharge,$rentalcharge_daily,$rentalcharge_hourly,$defaultreplacecost,$processfee,$notforloan,$imageurl,$summary,$checkinmsg,$sip_media_type,$hideinopac,$searchcategory)";
             $sth = $dbh->prepare($query);
@@ -516,6 +604,34 @@ sub restore_itemtypes {
     return;
 }
 
+sub restore_letters {
+    my $dbh = shift;
+    my $restore_file = shift;
+    my $parser = XML::LibXML->new();
+    my $dom = $parser->parse_file($restore_file);
+
+    my $query = "DELETE FROM letter WHERE 1 = 1";
+    my $sth = $dbh->prepare($query);
+    $sth->execute();
+
+    foreach my $node ($dom->findnodes('/letters/letter')) {
+        my $module = sql_str($node->findvalue('./module'));
+        my $code = sql_str($node->findvalue('./code'));
+        my $branchcode = sql_str_not_null($node->findvalue('./branchcode'));
+        my $name = sql_str($node->findvalue('./name'));
+        my $is_html = sql_num($node->findvalue('./is_html'));
+        my $title = sql_str($node->findvalue('./title'));
+        my $content = sql_str($node->findvalue('./content'));
+        my $message_transport_type = sql_str($node->findvalue('./message_transport_type'));
+        my $lang = sql_str($node->findvalue('./lang'));
+        $query = "INSERT INTO letter (module,code,branchcode,name,is_html,title,content,message_transport_type,lang) VALUES ($module,$code,$branchcode,$name,$is_html,$title,$content,$message_transport_type,$lang)";
+        $sth = $dbh->prepare($query);
+        $sth->execute();
+    }
+
+    return;
+}
+
 sub restore_libraries {
     my $dbh = shift;
     my $restorefile = shift;
@@ -637,8 +753,8 @@ sub restore_preferences {
     my $dom = $parser->parse_file($restore_file);
 
     foreach my $node ($dom->findnodes('//pref')) {
-       my $variable = domain_shift(sql_str($node->findvalue('./variable')));
-        my $value = domain_shift(sql_str($node->findvalue('./value')));
+               my $variable = domain_shift(sql_str_not_null($node->findvalue('./variable')));
+        my $value = domain_shift(sql_str_not_null($node->findvalue('./value')));
         my $query = "UPDATE systempreferences SET value = $value WHERE variable = $variable";
         my $sth = $dbh->prepare($query);
         $sth->execute();
@@ -686,6 +802,7 @@ sub restore_reports {
 sub restore_smsproviders {
     my $dbh = shift;
     my $restore_file = shift;
+    my $migpath = shift;
     my $parser = XML::LibXML->new();
     my $dom = $parser->parse_file($restore_file);
 
@@ -693,6 +810,14 @@ sub restore_smsproviders {
     my $sth = $dbh->prepare($query);
     $sth->execute();
 
+    my $p = 0;
+    foreach my $node ($dom->findnodes('//provider')) { $p++; }
+
+    if ($p == 0) {
+        my $failover_file = find_xml('smsproviders.xml',$migpath);        
+        $dom = $parser->parse_file($failover_file);        
+    }
+
     foreach my $node ($dom->findnodes('//provider')) {
         my $name = sql_str($node->findvalue('./name'));
         my $domain = sql_str($node->findvalue('./domain'));
@@ -737,6 +862,22 @@ sub abort {
     exit 1;
 }
 
+sub find_xml {
+    my $reports_xml = shift;
+    my $mig_path = shift;
+
+    if ($reports_xml =~ m/\//) { return $reports_xml; }
+
+    my $mig_test_file =  $mig_path . '/../xml/' . $reports_xml;
+    my $working_test_dir = getcwd();
+    my $working_test_file = $working_test_dir . '/' . $reports_xml;
+
+    if (-e $mig_test_file) { return $mig_test_file; }
+    if (-e $working_test_file) { return $working_test_file; }
+
+    return undef;
+}
+
 sub print_usage {
     print <<_USAGE_;