[MKDoc-commit] File from Sam for loading Ufi centre deatils.

chris at mkdoc.demon.co.uk chris at mkdoc.demon.co.uk
Mon Mar 6 15:00:47 GMT 2006


Log Message:
-----------
File from Sam for loading Ufi centre deatils.

Tags:
----
mkdoc-1-6

Added Files:
-----------
    mkd/tools:
        ufi_load_centre_details.pl

-------------- next part --------------
--- /dev/null
+++ tools/ufi_load_centre_details.pl
@@ -0,0 +1,166 @@
+#!/usr/bin/perl
+
+# ------------------------------------------------------------------
+# ufi_load_centre_details.pl
+# ------------------------------------------------------------------
+# Author : Sam Tregar
+# Copyright : (c) MKDoc Holdings Ltd, 2006
+#
+# This file is part of MKDoc. 
+# 
+# MKDoc is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# MKDoc is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with MKDoc; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+# ------------------------------------------------------------------
+
+# This script loads the Centre_Details table and associated tables
+# with data from the UK Online database, using the UFI::Centres module
+# to load this data.
+#
+# This script should be run periodically from cron to keep the data in
+# the Centre_Details table up-to-date.  It produces no output unless
+# an error is encountered.
+
+use strict;
+use warnings;
+use MKDoc;
+use UFI::Centres;
+
+# make sure SITE_DIR is set since MKDoc->init needs it
+die "SITE_DIR isn't set.  Please source mksetenv.sh from an installed ".
+  "MKDoc site and try again.\n" 
+  unless $ENV{SITE_DIR};
+
+# initialize MKDoc, needed for database connection
+MKDoc->init;
+
+my $dbh = lib::sql::DBH->get();
+
+# insert handle for base centre data
+my $centre_insert_sth = $dbh->prepare(<<END);
+INSERT INTO Centre_Details 
+  (Centre_ID, Centre_Name, Address1, Address2, Address3, 
+   Town, County, Postcode, Status, Telephone, Email, Website, Charges,
+   Sun_OpenHours, Mon_OpenHours, Tue_OpenHours, Wed_OpenHours,
+   Thu_OpenHours, Fri_OpenHours, Sat_OpenHours, 
+   WeekEnds_OpenHours, WeekDays_OpenHours,
+   OpenToPublic, NumWorkstations) 
+VALUES 
+  (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+END
+my @centre_insert_fields = 
+  qw(ID CentreName Address1 Address2 Address3 
+     Town County Postcode Status CentreTelNo CentreEmail CentreWebsite
+     Charges Sun_OpenHours Mon_OpenHours Tue_OpenHours Wed_OpenHours
+     Thu_OpenHours Fri_OpenHours Sat_OpenHours 
+     WeekEnds_OpenHours WeekDays_OpenHours OpenToPublic NumWorkstations);
+
+# insert handle for contacts
+my $contact_insert_sth = $dbh->prepare(<<END);
+INSERT INTO Centre_Contact (Centre_ID,
+                            Title, First_Name, Family_Name, Job_Title,
+                            Telephone, Extension, Fax, Mobile, Email,
+                            PublicContact, Ord)
+VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
+END
+my @contact_insert_fields =
+  qw(ID
+     Contact_1_Title
+     Contact_1_FirstName
+     Contact_1_Surname
+     Contact_1_JobTitle
+     Contact_1_TelNo
+     Contact_1_Ext
+     Contact_1_FaxNo
+     Contact_1_Mobile
+     Contact_1_Email
+     Contact_1_PublicContact);
+
+# setup handles for boolean set tables
+my %boolean_sets = (
+   Facility       => 'Facility',
+   TargetAudience => 'TargetAud',
+   Campaign       => 'Campaign',
+   CentreType     => 'CentreType',
+);
+my %boolean_insert_sth;
+foreach my $name (keys %boolean_sets) {
+    $boolean_insert_sth{$name} = $dbh->prepare(<<END);
+      INSERT INTO Centre_$name (Centre_ID, ${name}_ID) VALUES (?,?)
+END
+}
+
+# handle connect-type, a boolean set with some additional data
+my $connect_type_insert_sth = $dbh->prepare(<<END);
+      INSERT INTO Centre_ConnectType (Centre_ID, ConnectType_ID, NumberOfPCs)
+      VALUES (?,?,?)
+END
+  
+
+# get a list of centre IDs
+my $centres = UFI::Centres->new($ENV{SITE_DIR} . '/UFI-Centres-data.txt');
+$centres->Read;
+
+my $ids = $centres->get_all_ids();
+
+# get a table lock to prevent problems from clients getting a
+# half-full table
+my @tables = qw(Centre_Details Centre_Contact 
+                Centre_Facility Centre_Campaign
+                Centre_ConnectType Centre_CentreType 
+                Centre_TargetAudience);
+$dbh->do('LOCK TABLES ' . join(', ', map { "$_ WRITE" } @tables));
+
+# be sure to unlock at the end, no matter what
+END { 
+    if ($dbh) {
+        $dbh->do('UNLOCK TABLES');
+        $dbh->disconnect;
+    }
+}
+
+# clear the tables
+$dbh->do("DELETE FROM $_") for @tables;
+
+# load each centre into the DB
+foreach my $id (@$ids) {
+    my $data = $centres->get_by_id($id);
+    defined $data->{$_} or $data->{$_} = "" for keys %$data;
+      
+    $centre_insert_sth->execute(map { $data->{$_} } @centre_insert_fields);
+
+    # insert contacts
+    for my $ord (1 .. 4) {
+        next unless $data->{"Contact_${ord}_Surname"}; # skip missing contacts
+        $contact_insert_sth->execute(
+          (map { s!_\d_!_${ord}_!; $data->{$_} } @contact_insert_fields),
+          $ord);
+    }
+
+    # deal with boolean sets
+    foreach my $name (keys %boolean_sets) {
+        my @ids = map { s!\D!!g; $_ } 
+                  grep { /^$boolean_sets{$name}_\d+$/ and $data->{$_} } 
+                  keys %$data;
+        $boolean_insert_sth{$name}->execute($data->{ID}, $_) for @ids;
+    }
+
+    # deal with connect types
+    my @ids = map { s!\D!!g; $_ } 
+              grep { /^ConnectType_\d+$/ and $data->{$_} } 
+              keys %$data;
+    my @pcs = map { $data->{"ConnectType_${_}_NumPCs"} } @ids;
+    $connect_type_insert_sth->execute($data->{ID}, $ids[$_], $pcs[$_])
+      for (0 .. $#ids);
+}


More information about the MKDoc-commit mailing list