+ # get name and organization info for each individual
+ $name = $organization_class_data{$index}{'Preferred Name'};
+ $fullname = $organization_class_data{$index}{'Full Name'};
+ $org_class = $organization_class_data{$index}{'Organization Class'};
+
+ if ($org_class =~ m/Elder/i) {
+ #print " $name: Elder\n";
+ $sth = $dbh->prepare("update tc_individual set steward='Elder' where name=\"$name\" COLLATE latin1_general_cs AND fullname=\"$fullname\" COLLATE latin1_general_cs");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ }
+ if ($org_class =~ m/High Priest/i) {
+ #print " $name: High Priest\n";
+ $sth = $dbh->prepare("update tc_individual set steward='High Priest' where name=\"$name\" COLLATE latin1_general_cs AND fullname=\"$fullname\" COLLATE latin1_general_cs");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ }
+ }
+}
+
+sub update_tc_scheduling_priority_table
+{
+ print "\n-> Updating scheduling priority table\n";
+
+ # individuals
+ $sth = $dbh->prepare("select * from tc_individual where steward='$default_stewardship' and valid=1");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ while($sqlhashref = $sth->fetchrow_hashref) {
+ $individual = $sqlhashref->{individual};
+ $scheduling_priority = $sqlhashref->{scheduling_priority};
+ if ($scheduling_priority == 'NULL')
+ {
+ $sth2 = $dbh->prepare("insert into tc_scheduling_priority values (NULL, 30, '')");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ $scheduling_priority = $dbh->last_insert_id(NULL,NULL,'tc_scheduling_priority',NULL);
+ $sth2 = $dbh->prepare("update tc_individual set scheduling_priority=$scheduling_priority where individual=$individual");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ }
+ }
+ &remove_obsolete_scheduling_priority("tc_individual");
+
+ # families
+ $sth = $dbh->prepare("select tf.scheduling_priority, tf.family from tc_family AS tf JOIN tc_individual AS ti ON tf.individual=ti.individual and tf.companionship!='0' and tf.valid=1");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ while($sqlhashref = $sth->fetchrow_hashref) {
+ $family = $sqlhashref->{family};
+ $scheduling_priority = $sqlhashref->{scheduling_priority};
+ #print "$family $scheduling_priority\n";
+ if ($scheduling_priority == 'NULL')
+ {
+ $sth2 = $dbh->prepare("insert into tc_scheduling_priority values (NULL, 30, '')");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ $scheduling_priority = $dbh->last_insert_id(NULL,NULL,'tc_scheduling_priority',NULL);
+ $sth2 = $dbh->prepare("update tc_family set scheduling_priority=$scheduling_priority where family=$family");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ }
+ }
+ &remove_obsolete_scheduling_priority("tc_family");
+
+ # companions
+ $sth = $dbh->prepare("select tc.individual, tc.scheduling_priority from tc_companion AS tc JOIN tc_individual AS ti ON tc.individual=ti.individual and (ti.steward='$default_stewardship' or ti.steward='') and tc.valid=1");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ while($sqlhashref = $sth->fetchrow_hashref) {
+ $individual = $sqlhashref->{individual};
+ $scheduling_priority = $sqlhashref->{scheduling_priority};
+ #print "$individual $scheduling_priority\n";
+ if ($scheduling_priority == 'NULL')
+ {
+ $sth2 = $dbh->prepare("insert into tc_scheduling_priority values (NULL, 30, '')");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ $scheduling_priority = $dbh->last_insert_id(NULL,NULL,'tc_scheduling_priority',NULL);
+ #print "update tc_companion set scheduling_priority=$scheduling_priority where individual=$individual\n";
+ $sth2 = $dbh->prepare("update tc_companion set scheduling_priority=$scheduling_priority where individual=$individual");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ }
+ }
+ &remove_obsolete_scheduling_priority("tc_companion");
+}
+
+sub remove_obsolete_scheduling_priority
+{
+ my $table_name = $_[0];
+
+ #print "\n-> Cleaning $table_name\n";
+
+ $sth = $dbh->prepare("SELECT * FROM $table_name WHERE valid=0");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ while($sqlhashref = $sth->fetchrow_hashref) {
+ $scheduling_priority = $sqlhashref->{scheduling_priority};
+ $individual = $sqlhashref->{individual};
+ if ($scheduling_priority != "NULL") {
+ #print "SELECT * FROM $table_name WHERE scheduling_priority=$scheduling_priority AND valid=1\n";
+ $sth2 = $dbh->prepare("SELECT * FROM $table_name WHERE scheduling_priority=$scheduling_priority AND valid=1");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ if (!($sqlhashref2 = $sth2->fetchrow_hashref)) {
+ #print "$name\n";
+ # set scheduling_priority to NULL
+ #print "UPDATE $table_name SET scheduling_priority=NULL WHERE individual=$individual\n";
+ $sth3 = $dbh->prepare("UPDATE $table_name SET scheduling_priority=NULL WHERE individual=$individual");
+ $sth3->execute or die "-E- DB error: $DBI::errstr\n";
+
+ # remove entry from tc_scheduling_priority
+ #print "DELETE FROM tc_scheduling_priority WHERE scheduling_priority=$scheduling_priority\n";
+ $sth3 = $dbh->prepare("DELETE FROM tc_scheduling_priority WHERE scheduling_priority=$scheduling_priority");
+ $sth3->execute or die "-E- DB error: $DBI::errstr\n";
+ $sth3->finish();
+ }
+ $sth2->finish();
+ }