###########################################################################
#
# CICPlug.pm
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2005 New Zealand Digital Library Project
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

package CICPlug;


use BasPlug;
use DBI;
use strict;
no strict 'refs';


sub BEGIN {
    @CICPlug::ISA = ('BasPlug');
}


my $arguments =
  [ 
    { 'name' => "images_directory",
      'type' => "string",
      'deft' => "",
      'reqd' => "yes" },
    { 'name' => "cache_directory",
      'type' => "string",
      'deft' => &util::filename_cat($ENV{'GSDLHOME'}, "tmp"),
      'reqd' => "no" },
    { 'name' => "large_image_options",
      'type' => "string",
      'deft' => "",
      'reqd' => "no" },
    { 'name' => "large_image_type",
      'type' => "string",
      'deft' => "jpg",
      'reqd' => "no" },
    { 'name' => "large_image_width",
      'type' => "string",
      'deft' => "1250",
      'reqd' => "no" },
    { 'name' => "medium_image_options",
      'type' => "string",
      'deft' => "",
      'reqd' => "no" },
    { 'name' => "medium_image_type",
      'type' => "string",
      'deft' => "jpg",
      'reqd' => "no" },
    { 'name' => "medium_image_width",
      'type' => "string",
      'deft' => "375",
      'reqd' => "no" },
    { 'name' => "small_image_options",
      'type' => "string",
      'deft' => "",
      'reqd' => "no" },
    { 'name' => "small_image_type",
      'type' => "string",
      'deft' => "jpg",
      'reqd' => "no" },
    { 'name' => "small_image_width",
      'type' => "string",
      'deft' => "125",
      'reqd' => "no" }
  ];

my $options = { 'name'     => "CICPlug",
		'desc'     => "{CICPlug.desc}",
		'abstract' => "no",
		'inherits' => "yes" };


sub get_default_process_exp
{
    return q^(?i)\.mdb$^;
}


sub new
{
    my ($class) = shift (@_);
    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
    push(@$pluginlist, $class);

    if (defined $arguments) { push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments}); }
    if (defined $options) { push(@{$hashArgOptLists->{"OptList"}}, $options); }

    my $self = (defined $hashArgOptLists) ? new BasPlug($pluginlist,$inputargs,$hashArgOptLists) : new BasPlug($pluginlist,$inputargs);

    return bless $self, $class;
}


my $state_abbr_to_name_mapping = {
    "AL" => "Alabama",
    "AK" => "Alaska",
    "AZ" => "Arizona",
    "AR" => "Arkansas",
    "CA" => "California",
    "CO" => "Colorado",
    "CT" => "Connecticut",
    "DE" => "Delaware",
    "FL" => "Florida",
    "GA" => "Georgia",
    "HI" => "Hawaii",
    "ID" => "Idaho",
    "IL" => "Illinois",
    "IN" => "Indiana",
    "IA" => "Iowa",
    "KS" => "Kansas",
    "KY" => "Kentucky",
    "LA" => "Louisiana",
    "ME" => "Maine",
    "MD" => "Maryland",
    "MA" => "Massachusetts",
    "MI" => "Michigan",
    "MN" => "Minnesota",
    "MS" => "Mississippi",
    "MO" => "Missouri",
    "MT" => "Montana",
    "NE" => "Nebraska",
    "NV" => "Nevada",
    "NH" => "New Hampshire",
    "NJ" => "New Jersey",
    "NM" => "New Mexico",
    "NY" => "New York",
    "NC" => "North Carolina",
    "ND" => "North Dakota",
    "OH" => "Ohio",
    "OK" => "Oklahoma",
    "OR" => "Oregon",
    "PA" => "Pennsylvania",
    "RI" => "Rhode Island",
    "SC" => "South Carolina",
    "SD" => "South Dakota",
    "TN" => "Tennessee",
    "TX" => "Texas",
    "UT" => "Utah",
    "VT" => "Vermont",
    "VA" => "Virginia",
    "WA" => "Washington",
    "WV" => "West Virginia",
    "WI" => "Wisconsin",
    "WY" => "Wyoming"
    };


my $place_type_id_to_name_mapping = {
    "1" => "Individual building",
    "2" => "Landscape site",
    "3" => "Campus arrangement",
    "4" => "Building group",
};


sub read
{
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;

    $self->{'filename'} = &util::filename_cat($base_dir, $file);
    if ($self->{'filename'} !~ /$self->{'process_exp'}/ || !-f $self->{'filename'}) {
	return undef;
    }
    $self->{'processor'} = $processor;
    $self->{'gli'} = $gli;

    # Open connection to Access database
    my $dbh = DBI->connect('dbi:ODBC:CIC-HCAP');

    $self->process_institutions($dbh);
    $self->process_places($dbh);
    $self->process_designers($dbh);

    return 1;
}


sub process_institutions
{
    my $self = shift(@_);
    my $dbh = shift(@_);
    my $fail_log_handle = $self->{'failhandle'};

    # Prepare SQL statement for getting everything from the Institution table
    my $institution_sql_statement = "SELECT * FROM tblInstitution";  # WHERE Institution_ID<200";
    my $institution_sql_handle = $dbh->prepare($institution_sql_statement);
    $institution_sql_handle->{LongReadLen} = 65536;
    $institution_sql_handle->execute() or die "Could not execute SQL statement.";

    # Prepare SQL statement for getting the Institution places
    my $institution_places_sql_statement = "SELECT Entry_ID,Current_name FROM tblPlace WHERE PlaceType>0 AND Institution_ID=?";
    my $institution_places_sql_handle = $dbh->prepare($institution_places_sql_statement);
    $institution_places_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Institution best place image location
    my $institution_best_place_image_location_sql_statement = "SELECT Location FROM tblImages WHERE FileType=1 AND FileName=?";
    my $institution_best_place_image_location_sql_handle = $dbh->prepare($institution_best_place_image_location_sql_statement);
    $institution_best_place_image_location_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Institution places images
    my $institution_places_images_sql_statement = "SELECT FileName FROM tblImages,tblPlace WHERE tblImages.FileType=1 AND tblImages.Entry_ID=tblPlace.Entry_ID AND tblPlace.Institution_ID=?";
    my $institution_places_images_sql_handle = $dbh->prepare($institution_places_images_sql_statement);
    $institution_places_images_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Institution campus plans
    my $institution_campus_plans_sql_statement = "SELECT * FROM tblCampusMaps WHERE Electronic=1 AND Institution_ID=?";
    my $institution_campus_plans_sql_handle = $dbh->prepare($institution_campus_plans_sql_statement);
    $institution_campus_plans_sql_handle->{LongReadLen} = 65536;

    # Create a document object for each institution
    my %institution_id_to_name_mapping;
    my %institution_state_to_ids_mapping;
    while (my $row_hashref = $institution_sql_handle->fetchrow_hashref) {
	# Skip any institutions that didn't respond
	next if !defined($row_hashref->{"City"});

	my $institution_id = $row_hashref->{"Institution_ID"};
	# print STDERR "       Institution $institution_id\n";
	my $institution_doc_obj = new doc($self->{'filename'} . "-", "indexed_doc");
	$institution_doc_obj->set_OID("i$institution_id");
	&new_metadata_entry($institution_doc_obj, "DocumentType", "Institution");

	# For some reason the hyphen seems to be lost from the Zip field, so add it back in
	my $institution_zip = $row_hashref->{"Zip"};
	if ($institution_zip =~ /^(\d\d\d\d\d)(\d\d\d\d)$/) {
	    $row_hashref->{"Zip"} = $1 . "-" . $2;
	}

	# Map state to full name
	$row_hashref->{"State"} = $state_abbr_to_name_mapping->{$row_hashref->{"State"}};

	# Get the places in this institution
	my $institution_random_place_id;
	my $institution_places_list_html = "";
	$institution_places_sql_handle->execute($institution_id) or die "Could not execute SQL statement.";
	while (my $institution_places_match_hashref = $institution_places_sql_handle->fetchrow_hashref) {
	    my $institution_place_id = $institution_places_match_hashref->{"Entry_ID"};
	    my $institution_place_name = $institution_places_match_hashref->{"Current_name"};
	    $institution_places_list_html .= "<span class=\"cictext\"><a href=\"_gwcgi_?a=d&d=p$institution_place_id\">$institution_place_name</a></span><br />\n";
	}
	&new_metadata_entry($institution_doc_obj, "InstitutionPlacesListHTML", $institution_places_list_html);

	# Get the best place image for this institution
	my $institution_best_place_image_name = $row_hashref->{"Best_image"};
	if (!defined($institution_best_place_image_name) || $institution_best_place_image_name eq "") {
	    # Some institutions have no electronic images, and thus have no best image
	    $institution_places_images_sql_handle->execute($institution_id) or die "Could not execute SQL statement.";
	    if (defined($institution_places_images_sql_handle->fetchrow_hashref())) {
		print STDERR "<ProcessingError n='Institution $institution_id' p='CICPlug' r='No best image'>\n" if ($self->{'gli'});
		print STDERR "Error: Institution $institution_id -- No best image.\n";
		print $fail_log_handle "Error: Institution $institution_id -- No best image.\n";
		$self->{'num_not_processed'}++;
		next;
	    }
	    &new_metadata_entry($institution_doc_obj, "InstitutionBestPlaceImageHTML", "");
	}
	else {
	    # Get the file location of the best place image for this institution
	    $institution_best_place_image_location_sql_handle->execute($institution_best_place_image_name) or die "Could not execute SQL statement.";
	    my $institution_best_place_image_location = $institution_best_place_image_location_sql_handle->fetchrow();
	    if (!defined($institution_best_place_image_location) || $institution_best_place_image_location eq "") {
		print STDERR "<ProcessingError n='Institution $institution_id' p='CICPlug' r='Could not match best image $institution_best_place_image_name to a file'>\n" if ($self->{'gli'});
		print STDERR "Error: Institution $institution_id -- Could not match best image $institution_best_place_image_name to a file.\n";
		print $fail_log_handle "Error: Institution $institution_id -- Could not match best image $institution_best_place_image_name to a file.\n";
		$self->{'num_not_processed'}++;
		next;
	    }

	    # PDFs are not allowed for institution best place images
	    if ($institution_best_place_image_location =~ /.pdf$/i) {
		print STDERR "<ProcessingError n='Institution $institution_id' p='CICPlug' r='PDF not allowed for best image'>\n" if ($self->{'gli'});
		print STDERR "Error: Institution $institution_id -- PDF not allowed for best image.\n";
		print $fail_log_handle "Error: Institution $institution_id -- PDF not allowed for best image.\n";
		$self->{'num_not_processed'}++;
		next;
	    }

	    my $institution_best_place_image_medium_file_href = $self->generate_place_image_variant($institution_doc_obj, $institution_best_place_image_location, "medium");
	    my $institution_best_place_image_large_file_href = $self->generate_place_image_variant($institution_doc_obj, $institution_best_place_image_location, "large");
	    &new_metadata_entry($institution_doc_obj, "InstitutionBestPlaceImageHTML", "<a href=\"$institution_best_place_image_large_file_href\"><img src=\"$institution_best_place_image_medium_file_href\"/><br /><span class=\"cictext\">$institution_best_place_image_name</span></a>");
	}

	# Get institution campus plans
	my $institution_campus_plans_list_html = "";
	$institution_campus_plans_sql_handle->execute($institution_id) or die "Could not execute SQL statement.";
	while (my $institution_campus_plans_match_hashref = $institution_campus_plans_sql_handle->fetchrow_hashref) {
	    my $institution_campus_plan_name = $institution_campus_plans_match_hashref->{"NameAndFormat"};
	    my $institution_campus_plan_image_location = $institution_campus_plans_match_hashref->{"Location_electronic"};

	    # Deal with PDF files separately: don't convert, just associate
	    if ($institution_campus_plan_image_location =~ /\.pdf$/i) {
		# Convert the server location of the file into the local location of the file
		my $institution_campus_plan_pdf_file_path = $institution_campus_plan_image_location;
		$institution_campus_plan_pdf_file_path =~ s/^[A-Z]:/$self->{'images_directory'}/;

		if (-f $institution_campus_plan_pdf_file_path) {
		    my $institution_campus_plan_pdf_file_name = $institution_campus_plan_name . ".pdf";
		    $institution_campus_plan_pdf_file_name =~ s/ /%20/g;
		    my $institution_campus_plan_pdf_file_href = "_httpcollection_/index/assoc/[assocfilepath]/$institution_campus_plan_pdf_file_name";
		    $institution_campus_plans_list_html .= "<a href=\"$institution_campus_plan_pdf_file_href\"><span class=\"cictext\">$institution_campus_plan_name (PDF)</span></a><br />";

		    $institution_doc_obj->associate_file($institution_campus_plan_pdf_file_path, $institution_campus_plan_name . ".pdf", undef, $institution_doc_obj->get_top_section());
		}
		else {
		    print STDERR "<ProcessingError n='$institution_campus_plan_pdf_file_path' p='CICPlug' r='Does not exist'>\n" if ($self->{'gli'});
		    print STDERR "Error: File $institution_campus_plan_pdf_file_path does not exist.\n";
		    print $fail_log_handle "Error: File $institution_campus_plan_pdf_file_path does not exist.\n";
		}
	    }
	    else {
		my $institution_campus_plan_image_large_file_href = $self->generate_place_image_variant($institution_doc_obj, $institution_campus_plan_image_location, "large");
		$institution_campus_plans_list_html .= "<a href=\"$institution_campus_plan_image_large_file_href\"><span class=\"cictext\">$institution_campus_plan_name</span></a><br />";
	    }
	}
	&new_metadata_entry($institution_doc_obj, "InstitutionCampusPlansListHTML", $institution_campus_plans_list_html);

	# Add each field from the table as metadata
	foreach my $key (keys(%$row_hashref)) {
	    my $value = $row_hashref->{$key};
	    if (defined($value)) {
		&new_metadata_entry($institution_doc_obj, $key, $value);
	    }
	}

	$institution_doc_obj->add_utf8_text($institution_doc_obj->get_top_section(), "Some dummy text.");
	$self->{'processor'}->process($institution_doc_obj);
	$self->{'num_processed'}++;

	# Build mappings for creating the static macrofiles
	my $institution_name = $row_hashref->{"Institution_Name"};
	$institution_id_to_name_mapping{$institution_doc_obj->get_OID()} = $institution_name;
	my $institution_state = $row_hashref->{"State"};
	push(@{$institution_state_to_ids_mapping{$institution_state}}, $institution_doc_obj->get_OID());
    }

    # Write the institutions.dm macrofile
    &write_static_browser_macrofile("institutions", \%institution_id_to_name_mapping);

    # Write the states.dm macrofile
    &write_state_browser_macrofile("states", \%institution_state_to_ids_mapping, \%institution_id_to_name_mapping);
}


sub process_places
{
    my $self = shift(@_);
    my $dbh = shift(@_);
    my $fail_log_handle = $self->{'failhandle'};

    # Prepare SQL statement for getting everything from the Place table
    my $place_sql_statement = "SELECT * FROM tblPlace";
    my $place_sql_handle = $dbh->prepare($place_sql_statement);
    $place_sql_handle->{LongReadLen} = 65536;
    $place_sql_handle->execute() or die "Could not execute SQL statement.";

    # Prepare SQL statement for getting the Place institution
    my $place_institution_sql_statement = "SELECT Institution_Name FROM tblInstitution,tblPlace WHERE tblInstitution.Institution_ID=tblPlace.Institution_ID and tblPlace.Entry_ID=?";
    my $place_institution_sql_handle = $dbh->prepare($place_institution_sql_statement);

    # Prepare SQL statement for getting the Place "date of construction"
    my $place_construction_date_sql_statement = "SELECT Date FROM tblConstruction_and_Dates WHERE Entry_ID=?";
    my $place_construction_date_sql_handle = $dbh->prepare($place_construction_date_sql_statement);

    # Prepare SQL statement for getting the Place images
    my $place_images_sql_statement = "SELECT FileName,Location FROM tblImages WHERE FileType=1 AND Entry_ID=?";
    my $place_images_sql_handle = $dbh->prepare($place_images_sql_statement);
    $place_images_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Place materials
    my $place_materials_sql_statement = "SELECT * FROM tblDescription_building WHERE Entry_ID=?";
    my $place_materials_sql_handle = $dbh->prepare($place_materials_sql_statement);
    $place_materials_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Place building styles
    my $place_styles_sql_statement = "SELECT Architectural_Classification FROM tblArchTypes,jnxtblArchPlace WHERE tblArchTypes.ArchType_ID=jnxtblArchPlace.ArchType_ID AND Entry_ID=?";
    my $place_styles_sql_handle = $dbh->prepare($place_styles_sql_statement);
    $place_styles_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Place functions
    my $place_functions_sql_statement = "SELECT Function,Year,Prefix FROM tblFunction_and_dates WHERE Entry_ID=?";
    my $place_functions_sql_handle = $dbh->prepare($place_functions_sql_statement);
    $place_functions_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Place significance
    my $place_significance_sql_statement = "SELECT SigType FROM tblSigTypes,SigPlace WHERE tblSigTypes.SigTypes_ID=SigPlace.SigType_ID+1 AND SigPlace.Entry_ID=?";
    my $place_significance_sql_handle = $dbh->prepare($place_significance_sql_statement);
    $place_significance_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Place references
    my $place_references_sql_statement = "SELECT Bibliography FROM tblReferences WHERE Entry_ID=?";
    my $place_references_sql_handle = $dbh->prepare($place_references_sql_statement);
    $place_references_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Place narrative
    my $place_narrative_sql_statement = "SELECT Narrative FROM tblSignificance_Narrative WHERE Entry_ID=?";
    my $place_narrative_sql_handle = $dbh->prepare($place_narrative_sql_statement);
    $place_narrative_sql_handle->{LongReadLen} = 65536;

    # Prepare SQL statement for getting the Place state
    my $place_state_sql_statement = "SELECT State FROM tblInstitution,tblPlace WHERE tblInstitution.Institution_ID=tblPlace.Institution_ID AND Entry_ID=?";
    my $place_state_sql_handle = $dbh->prepare($place_state_sql_statement);
    $place_state_sql_handle->{LongReadLen} = 65536;

    # Create a document object for each place
    my %place_type_to_ids_mapping;
    my %place_style_to_ids_mapping;
    my %place_date_to_ids_mapping;
    my %place_function_to_ids_mapping;
    my %place_id_to_name_mapping;
    my %place_id_to_institution_name_mapping;
    while (my $row_hashref = $place_sql_handle->fetchrow_hashref) {
	my $place_id = $row_hashref->{"Entry_ID"};
	# print STDERR "       Place $place_id\n";
	my $place_doc_obj = new doc($self->{'filename'} . "-", "indexed_doc");
	$place_doc_obj->set_OID("p$place_id");
	&new_metadata_entry($place_doc_obj, "DocumentType", "Place");

	# Convert the place type ID into a name
	$row_hashref->{"PlaceType"} = $place_type_id_to_name_mapping->{$row_hashref->{"PlaceType"}};

	# Add each field from the table as metadata
	foreach my $key (keys(%$row_hashref)) {
	    my $value = $row_hashref->{$key};
	    if (defined($value)) {
		&new_metadata_entry($place_doc_obj, $key, $value);
	    }
	}

	# Get place name
	my $place_name = $row_hashref->{"Current_name"};
	if (!defined($place_name)) {
	    print STDERR "<ProcessingError n='Place $place_id' p='CICPlug' r='Missing place name'>\n" if ($self->{'gli'});
	    print STDERR "Error: Place $place_id -- Missing place name.\n";
	    print $fail_log_handle "Error: Place $place_id -- Missing place name.\n";
	    $self->{'num_not_processed'}++;
	    next;
	}

	# Get place type
	my $place_type = $row_hashref->{"PlaceType"};
	if (!defined($place_type)) {
	    print STDERR "<ProcessingError n='Place $place_id' p='CICPlug' r='Missing place type'>\n" if ($self->{'gli'});
	    print STDERR "Error: Place $place_id -- Missing place type.\n";
	    print $fail_log_handle "Error: Place $place_id -- Missing place type.\n";
	    $self->{'num_not_processed'}++;
	    next;
	}

	# Get place date of construction, except for landscape sites which have no date
	$place_construction_date_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	my $place_construction_date_value = $place_construction_date_sql_handle->fetchrow();
	if (!defined($place_construction_date_value) && $place_type eq "Landscape site") {
	    # Landscape sites may not have a construction date
	    $place_construction_date_value = "";
	}
	if (!defined($place_construction_date_value)) {
	    print STDERR "<ProcessingError n='Place $place_id' p='CICPlug' r='Missing construction date'>\n" if ($self->{'gli'});
	    print STDERR "Error: Place $place_id -- Missing construction date.\n";
	    print $fail_log_handle "Error: Place $place_id -- Missing construction date.\n";
	    $self->{'num_not_processed'}++;
	    next;
	}
	&new_metadata_entry($place_doc_obj, "Construction_date", $place_construction_date_value);

	# Create place styles mapping
	$place_styles_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	while (my $place_styles_match_hashref = $place_styles_sql_handle->fetchrow_hashref()) {
	    my $place_style = $place_styles_match_hashref->{"Architectural_Classification"};
	    push(@{$place_style_to_ids_mapping{$place_style}}, $place_doc_obj->get_OID());
	    &new_metadata_entry($place_doc_obj, "Style", $place_style);
	}

	# Get place institution
	$place_institution_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	my $place_institution = $place_institution_sql_handle->fetchrow();
	&new_metadata_entry($place_doc_obj, "Institution_name", $place_institution);
	$place_id_to_institution_name_mapping{$place_doc_obj->get_OID()} = ", " . $place_institution;

	# Get place state (for searching)
	$place_state_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	my $place_state_abbr = $place_state_sql_handle->fetchrow();
	&new_metadata_entry($place_doc_obj, "State", $place_state_abbr . " " . $state_abbr_to_name_mapping->{$place_state_abbr});

	# Create place dates mapping
	my $place_time_period = $place_construction_date_value;
	if ($place_construction_date_value =~ /^(\d\d\d\d).*$/) {
	    $place_construction_date_value =~ s/^(\d\d\d\d).*$/$1/;
	    if ($place_construction_date_value < 1800) { $place_time_period = "pre-1800"; }
	    elsif ($place_construction_date_value < 1850) { $place_time_period = "1800-1850"; }
	    elsif ($place_construction_date_value < 1900) { $place_time_period = "1850-1900"; }
	    elsif ($place_construction_date_value < 1945) { $place_time_period = "1900-1945"; }
	    elsif ($place_construction_date_value < 1995) { $place_time_period = "1945-1995"; }
	    else { $place_time_period = "post-1995"; }
	    push(@{$place_date_to_ids_mapping{$place_time_period}}, $place_doc_obj->get_OID());
	}
	&new_metadata_entry($place_doc_obj, "Time_period", $place_time_period);

	# Get place materials (individual buildings only)
	if ($row_hashref->{"PlaceType"} eq "Individual building") {
	    $place_materials_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	    my $place_materials_match_hashref = $place_materials_sql_handle->fetchrow_hashref();
	    &new_metadata_entry($place_doc_obj, "MaterialFoundation", $place_materials_match_hashref->{"foundation"} || "");
	    &new_metadata_entry($place_doc_obj, "MaterialRoof", $place_materials_match_hashref->{"roof"} || "");
	    &new_metadata_entry($place_doc_obj, "MaterialWalls", $place_materials_match_hashref->{"walls"} || "");
	}

	# Get place functions
	my $place_functions = "";
	my $place_functions_table_html = "";
	$place_functions_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	while (my $place_functions_match_hashref = $place_functions_sql_handle->fetchrow_hashref()) {
	    my $place_function = $place_functions_match_hashref->{"Function"};
	    if (!defined($place_function)) {
		print STDERR "<ProcessingError n='Place $place_id' p='CICPlug' r='Missing function'>\n" if ($self->{'gli'});
		print STDERR "Error: Place $place_id -- Missing function.\n";
		print $fail_log_handle "Error: Place $place_id -- Missing function.\n";
		next;
	    }
	    # Check for multiline values (these are errors)
	    if ($place_function =~ /\n/) {
		print STDERR "<ProcessingError n='Place $place_id' p='CICPlug' r='Multiline function'>\n" if ($self->{'gli'});
		print STDERR "Error: Place $place_id -- Multiline function.\n";
		print $fail_log_handle "Error: Place $place_id -- Multiline function.\n";
		next;
	    }
	    my $place_year = $place_functions_match_hashref->{"Year"};
	    if (!defined($place_year)) {
		print STDERR "<ProcessingError n='Place $place_id' p='CICPlug' r='Missing function year'>\n" if ($self->{'gli'});
		print STDERR "Error: Place $place_id -- Missing function year.\n";
		print $fail_log_handle "Error: Place $place_id -- Missing function year.\n";
		next;
	    }
	    my $place_year_prefix = $place_functions_match_hashref->{"Prefix"} || "";
	    $place_functions .= "$place_function ";
	    $place_functions_table_html .= "<tr><td valign=\"top\"><nobr>$place_year_prefix $place_year</nobr>&nbsp;</td><td valign=\"top\">$place_function</td></tr>";

	    # Create place functions mapping
	    $place_function =~ s/\(.*\)//g;
	    $place_function =~ s/^\s*//;
	    $place_function =~ s/\s*$//;
	    next if ($place_function eq "");
	    push(@{$place_function_to_ids_mapping{$place_function}}, $place_doc_obj->get_OID());
	}
	&new_metadata_entry($place_doc_obj, "Functions", $place_functions);
	&new_metadata_entry($place_doc_obj, "PlaceFunctionsTableHTML", "<table cellpadding=\"0\" cellspacing=\"0\">" . $place_functions_table_html . "</table>");

	# Get place significance
	$place_significance_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	while (my $place_significance_match_hashref = $place_significance_sql_handle->fetchrow_hashref()) {
	    my $place_significance = $place_significance_match_hashref->{"SigType"};
	    &new_metadata_entry($place_doc_obj, "Significance", lc($place_significance));
	}

	# Get place references
	$place_references_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	my $place_references = $place_references_sql_handle->fetchrow();
	if (defined($place_references)) {
	    &new_metadata_entry($place_doc_obj, "References", &rtf_to_html($place_references));
	}

	# Get place narrative
	$place_narrative_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	my $place_narrative = $place_narrative_sql_handle->fetchrow();
	if (defined($place_narrative)) {
	    $place_narrative = &rtf_to_html($place_narrative);
	    $place_narrative =~ s/<br \/>(\s|\n)*$//;  # Remove any trailing <br /> tags
	    &new_metadata_entry($place_doc_obj, "Narrative", $place_narrative);
	}

	# Get place images
	my $place_images_html = "";
	$place_images_sql_handle->execute($place_id) or die "Could not execute SQL statement.";
	while (my $place_images_match_hashref = $place_images_sql_handle->fetchrow_hashref) {
	    my $place_image_location = $place_images_match_hashref->{"Location"};
	    my $place_image_name = $place_images_match_hashref->{"FileName"};

	    # Deal with PDF files separately: don't convert, just associate
	    if ($place_image_location =~ /\.pdf$/i) {
		# Convert the server location of the PDF file into the local location of the file
		my $place_pdf_file_path = $place_image_location;
		$place_pdf_file_path =~ s/^[A-Z]:/$self->{'images_directory'}/;
 
		if (-f $place_pdf_file_path) {
		    my ($place_pdf_file_name) = ($place_pdf_file_path =~ /^.+\\(.+)$/);
		    $place_doc_obj->associate_file($place_pdf_file_path, $place_pdf_file_name, undef, $place_doc_obj->get_top_section());

		    $place_pdf_file_name =~ s/ /%20/g;
		    my $place_pdf_file_href = "_httpcollection_/index/assoc/[assocfilepath]/$place_pdf_file_name";
		    $place_images_html .= "<tr><td align=\"right\" valign=\"top\"><a href=\"$place_pdf_file_href\">_iconpdf_</a></td><td valign=\"top\"><a href=\"$place_pdf_file_href\"><span class=\"cictext\">$place_image_name (PDF)</span></a></td></tr>\n";
		}
		else {
		    print STDERR "<ProcessingError n='$place_pdf_file_path' p='CICPlug' r='Does not exist'>\n" if ($self->{'gli'});
		    print STDERR "Error: File $place_pdf_file_path does not exist.\n";
		    print $fail_log_handle "Error: File $place_pdf_file_path does not exist.\n";
		}
	    }
	    else {
		my $place_image_small_file_href = $self->generate_place_image_variant($place_doc_obj, $place_image_location, "small");
		my $place_image_large_file_href = $self->generate_place_image_variant($place_doc_obj, $place_image_location, "large");

		if (defined($place_image_name)) {
		    $place_images_html .= "<tr><td valign=\"top\"><a href=\"$place_image_large_file_href\"><img src=\"$place_image_small_file_href\"/></a></td><td valign=\"top\"><a href=\"$place_image_large_file_href\"><span class=\"cictext\">$place_image_name</span></a></td></tr>\n";
		}
		else {
		    $place_images_html .= "<tr><td valign=\"top\"><img src=\"_httpcollection_/images/no_image-small.jpg\"/></td><td valign=\"top\"><span class=\"cictext\">&nbsp;</span></td></tr>\n";
		}
	    }
	}

	&new_metadata_entry($place_doc_obj, "PlaceImagesHTML", "<table>" . $place_images_html . "</table>");

	$place_doc_obj->add_utf8_text($place_doc_obj->get_top_section(), "Some dummy text.");
	$self->{'processor'}->process($place_doc_obj);
	$self->{'num_processed'}++;

	# Build mappings for creating the static macrofiles
	$place_id_to_name_mapping{$place_doc_obj->get_OID()} = $place_name;
	push(@{$place_type_to_ids_mapping{$place_type}}, $place_doc_obj->get_OID());
    }

    &write_bilevel_static_browser_macrofile("types", \%place_type_to_ids_mapping, \%place_id_to_name_mapping, \%place_id_to_institution_name_mapping);
    &write_bilevel_static_browser_macrofile("styles", \%place_style_to_ids_mapping, \%place_id_to_name_mapping, \%place_id_to_institution_name_mapping);
    &write_bilevel_static_browser_macrofile("dates", \%place_date_to_ids_mapping, \%place_id_to_name_mapping, \%place_id_to_institution_name_mapping);
    &write_bilevel_static_browser_macrofile("functions", \%place_function_to_ids_mapping, \%place_id_to_name_mapping, \%place_id_to_institution_name_mapping);
}


sub process_designers
{
    my $self = shift(@_);
    my $dbh = shift(@_);
    my $fail_log_handle = $self->{'failhandle'};

    # Prepare SQL statement for getting all the Architects from the Place table
    my $designer_sql_statement = "SELECT Architect_Name,Entry_ID FROM tblConstruction_and_dates";
    my $designer_sql_handle = $dbh->prepare($designer_sql_statement);
    $designer_sql_handle->{LongReadLen} = 65536;
    $designer_sql_handle->execute() or die "Could not execute SQL statement.";

    # Prepare SQL statement for getting the Place name
    my $place_name_sql_statement = "SELECT Current_name FROM tblPlace WHERE Entry_ID=?";
    my $place_name_sql_handle = $dbh->prepare($place_name_sql_statement);

    # Prepare SQL statement for getting the Place institution
    my $place_institution_sql_statement = "SELECT Institution_Name FROM tblInstitution,tblPlace WHERE tblInstitution.Institution_ID=tblPlace.Institution_ID and tblPlace.Entry_ID=?";
    my $place_institution_sql_handle = $dbh->prepare($place_institution_sql_statement);

    # Prepare SQL statement for getting the Place "date of construction"
    my $place_construction_date_sql_statement = "SELECT Date FROM tblConstruction_and_Dates WHERE Entry_ID=?";
    my $place_construction_date_sql_handle = $dbh->prepare($place_construction_date_sql_statement);

    # Prepare SQL statement for getting the Place non-PDF images
    my $place_images_sql_statement = "SELECT Location FROM tblImages WHERE FileType=1 AND Location NOT LIKE '%.pdf' AND Entry_ID=?";
    my $place_images_sql_handle = $dbh->prepare($place_images_sql_statement);
    $place_images_sql_handle->{LongReadLen} = 65536;

    # Get a list of all the designers, and the places they worked on
    my %designer_name_to_place_ids_mapping;
    my %designer_name_to_id_mapping;
    my %designer_id_to_name_mapping;
    my $designer_count = 0;
    while (my $designer_hashref = $designer_sql_handle->fetchrow_hashref) {
	my $designer_names_string = $designer_hashref->{"Architect_Name"};
	next if !defined($designer_names_string);
	foreach my $designer_name (split(/;/, $designer_names_string)) {
	    $designer_name =~ s/\(.*?\)//g;
	    $designer_name =~ s/^\s*//;
	    $designer_name =~ s/\s*$//;

	    if (!defined($designer_name_to_id_mapping{$designer_name})) {
		$designer_count++;
		$designer_name_to_id_mapping{$designer_name} = "d" . $designer_count;
	    }

	    $designer_id_to_name_mapping{$designer_name_to_id_mapping{$designer_name}} = $designer_name;
	    push(@{$designer_name_to_place_ids_mapping{$designer_name}}, $designer_hashref->{"Entry_ID"});
	}
    }

    # Create a document object for each designer
    foreach my $designer_name (keys %designer_name_to_place_ids_mapping) {
  	my $designer_id = $designer_name_to_id_mapping{$designer_name};
  	# print STDERR "       Designer $designer_id\n";
  	my $designer_doc_obj = new doc($self->{'filename'} . "-", "indexed_doc");
  	$designer_doc_obj->set_OID($designer_id);
  	&new_metadata_entry($designer_doc_obj, "DocumentType", "Designer");

  	&new_metadata_entry($designer_doc_obj, "Designer_name", $designer_name);

  	my $designer_places_list_html = "";
  	foreach my $designer_place_id (@{$designer_name_to_place_ids_mapping{$designer_name}}) {
	    # Get place name
	    $place_name_sql_handle->execute($designer_place_id) or die "Could not execute SQL statement.";
	    my $designer_place_name = $place_name_sql_handle->fetchrow();

	    # Get place institution name
	    $place_institution_sql_handle->execute($designer_place_id) or die "Could not execute SQL statement.";
	    my $designer_place_institution_name = $place_institution_sql_handle->fetchrow();

	    # Get place date of construction
	    $place_construction_date_sql_handle->execute($designer_place_id) or die "Could not execute SQL statement.";
	    my $designer_place_construction_date_value = $place_construction_date_sql_handle->fetchrow() || "";

	    # Get the first non-PDF image for this place
	    my $designer_place_image_small_file_href;
    	    $place_images_sql_handle->execute($designer_place_id) or die "Could not execute SQL statement.";
	    my $designer_place_image_location = $place_images_sql_handle->fetchrow();
	    if (defined($designer_place_image_location)) {
  		$designer_place_image_small_file_href = $self->generate_place_image_variant($designer_doc_obj, $designer_place_image_location, "small");
	    }
	    else {
		# There is no non-PDF image for this place
		$designer_place_image_small_file_href = "_httpcollection_/images/no_image-small.jpg";
	    }

	    $designer_places_list_html .= "<tr><td valign=\"top\"><a href=\"_gwcgi_?a=d&d=p$designer_place_id\"><img src=\"$designer_place_image_small_file_href\"/></a></td><td valign=\"top\"><a href=\"_gwcgi_?a=d&d=p$designer_place_id\">$designer_place_name</a>, $designer_place_institution_name<br /><b>Date of construction:</b> $designer_place_construction_date_value</td></tr>\n";
	}

  	&new_metadata_entry($designer_doc_obj, "DesignerPlacesListHTML", "<table>" . $designer_places_list_html . "</table>");

  	$designer_doc_obj->add_utf8_text($designer_doc_obj->get_top_section(), "Some dummy text.");
  	$self->{'processor'}->process($designer_doc_obj);
	$self->{'num_processed'}++;
    }

    # Write the designers.dm macrofile
    &write_static_browser_macrofile("designers", \%designer_id_to_name_mapping);
}


sub new_metadata_entry
{
    my ($doc_obj, $metadata_name, $metadata_value) = (@_);

    # Don't bother with empty metadata
    return if ($metadata_value eq "");

    # Spaces aren't allowed in metadata names
    $metadata_name =~ s/ /_/g;

    # Anything from the database is ISO 8859-1 encoded, so convert to UTF-8
    $metadata_value = &unicode::ascii2utf8(\$metadata_value);

    # Remove '#' characters around links
    if ($metadata_value =~ /\#(.*?)\#/) {
	$metadata_value = $1;
    }

    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), $metadata_name, $metadata_value);
}


sub rtf_to_html
{
    my $rtf_string = shift(@_);
    $rtf_string =~ s/\{(.*?)\}//g;
    $rtf_string =~ s/\\rquote /'/g;  # ' # (for Emacs)
    $rtf_string =~ s/\\pard//g;
    $rtf_string =~ s/\\par/<br \/>/g;
    $rtf_string =~ s/\\i0 /<\/i>/g;
    $rtf_string =~ s/\\i /<i>/g;
    $rtf_string =~ s/\\~/ /g;
    $rtf_string =~ s/\\([A-Za-z0-9]+)//g;
    $rtf_string =~ s/\}//g;

    # Assume non-ASCII is ISO 8859-1, and convert into HTML entities
    while ($rtf_string =~ /\\'([a-z0-9][a-z0-9])/) {
        my $dec = hex($1);
	$rtf_string =~ s/\\'$1/&#$dec\;/;
    }

    return $rtf_string;
}


sub get_place_image_dimensions
{
    my $self = shift(@_);
    my $place_image_file_path = shift(@_);
    my $fail_log_handle = $self->{'failhandle'};

    # Make sure the place image file actually exists
    if (!-f $place_image_file_path) {
	print STDERR "<ProcessingError n='$place_image_file_path' p='CICPlug' r='Does not exist'>\n" if ($self->{'gli'});
	print STDERR "Error: Image $place_image_file_path does not exist.\n";
	print $fail_log_handle "Error: Image $place_image_file_path does not exist.\n";
	return;
    }
    my $place_image_file_date = (stat($place_image_file_path))[9];

    # Check if this place image has already been identified by looking for a ".info" file in the same directory
    my $place_image_info_file_path = $place_image_file_path . ".info";
    if (-f $place_image_info_file_path) {
	# A ".info" file exists, so read the cached place image information from it
	open(PLACE_IMAGE_INFO_FILE, "<$place_image_info_file_path");
	my @place_image_info = <PLACE_IMAGE_INFO_FILE>;
	close(PLACE_IMAGE_INFO_FILE);

  	# Read the cached place image file date and check that it matches
  	my $cached_place_image_file_date = $place_image_info[0];
  	$cached_place_image_file_date =~ s/\n$//;
  	if ($cached_place_image_file_date == $place_image_file_date) {
	    # It does match, so use the cached information from the ".info" file instead of re-identifying the file
  	    my $place_image_width = $place_image_info[1];
  	    $place_image_width =~ s/\n$//;
  	    my $place_image_height = $place_image_info[2];
  	    $place_image_height =~ s/\n$//;
	    return ($place_image_width, $place_image_height);
	}
    }

    # We haven't already identified the place image, so do it now
    print STDERR "Identifying place image $place_image_file_path...\n";
    my $identify_command = "identify -format \"%w %h\" \"$place_image_file_path\"";
    my $identify_result = `$identify_command`;
    print "Identify result: $identify_result\n" if ($self->{'verbosity'} > 2);

    # Check that the output is what we're expecting
    if ($identify_result !~ /(\d+) (\d+)/) {
	print STDERR "<ProcessingError n='$place_image_file_path' p='CICPlug' r='Could not identify'>\n" if ($self->{'gli'});
	print STDERR "Error: Place image $place_image_file_path could not be identified.\n";
	print $fail_log_handle "Error: Place image $place_image_file_path could not be identified.\n";
	return;
    }

    # Parse the place image width and height from the output
    my $place_image_width = $1;
    my $place_image_height = $2;

    # Write the place image info file so we don't have to identify this exact image again in the future
    open(PLACE_IMAGE_INFO_FILE, ">$place_image_info_file_path");
    print PLACE_IMAGE_INFO_FILE "$place_image_file_date\n";
    print PLACE_IMAGE_INFO_FILE "$place_image_width\n";
    print PLACE_IMAGE_INFO_FILE "$place_image_height\n";
    close(PLACE_IMAGE_INFO_FILE);
    return ($place_image_width, $place_image_height);
}


sub generate_place_image_variant
{
    my $self = shift(@_);
    my ($doc_obj, $place_image_location, $place_image_variant_size) = (@_);
    my $fail_log_handle = $self->{'failhandle'};

    # Convert the server location of the file into the local location of the file
    my $place_image_file_path = $place_image_location;
    $place_image_file_path =~ s/^[A-Z]:/$self->{'images_directory'}/;

    # Get the width and height of the place image
    my ($place_image_width, $place_image_height) = $self->get_place_image_dimensions($place_image_file_path);
    if (!defined($place_image_width) || !defined($place_image_height)) {
	# An error has occurred (error message generated by get_place_image_dimensions())
	return;
    }
    my $place_image_file_date = (stat($place_image_file_path))[9];

    # Generate the path of the place image variant (in the cache directory)
    my $place_image_variant_file_suffix = "-$place_image_variant_size." . $self->{$place_image_variant_size . '_image_type'};
    my $place_image_variant_file_path = $place_image_location;
    $place_image_variant_file_path =~ s/^[A-Z]:/$self->{'cache_directory'}/;
    $place_image_variant_file_path =~ s/^(.+)(\..*)/$1$place_image_variant_file_suffix/;
    my ($place_image_variant_file_name) = ($place_image_variant_file_path =~ /^.+\\(.+)$/);

    # Only scale down the place image if it is bigger than the desired width
    my $place_image_variant_desired_width = $self->{$place_image_variant_size . '_image_width'};
    if ($place_image_width > $place_image_variant_desired_width) {
	# Only generate the place image variant if it doesn't already exist, or if the place image is newer
	if (!-f $place_image_variant_file_path || $place_image_file_date > (stat($place_image_variant_file_path))[9]) {
	    print STDERR "Generating place image variant $place_image_variant_file_path...\n";
	    my ($place_image_variant_directory) = ($place_image_variant_file_path =~ /^(.+)\\.+$/);
	    &util::mk_all_dir($place_image_variant_directory);
	    my $place_image_variant_options = "-scale $place_image_variant_desired_width " . $self->{$place_image_variant_size . '_image_options'};
	    my $convert_command = "convert $place_image_variant_options \"$place_image_file_path\" \"$place_image_variant_file_path\"";
	    my $convert_result = `$convert_command`;
	}
    }
    else {
	# The desired width is bigger than the place image, so we just use the original
	$place_image_variant_file_path = $place_image_file_path;
    }

    my ($place_image_variant_width, $place_image_variant_height) = $self->get_place_image_dimensions($place_image_variant_file_path);
    if (!defined($place_image_variant_width) || !defined($place_image_variant_height)) {
	# An error has occurred (error message generated by get_place_image_dimensions())
	return;
    }

    # Create a new section for each place image variant (!! TO DO: Check if this is really necessary)
    my $place_image_variant_section = $doc_obj->insert_section($doc_obj->get_end_child($doc_obj->get_top_section()));
    $doc_obj->add_utf8_text($place_image_variant_section, "Some dummy text.");
    $doc_obj->add_utf8_metadata($place_image_variant_section, "Title", "1");

    # Associate the place image variant file
    $doc_obj->associate_file($place_image_variant_file_path, $place_image_variant_file_name, undef, $place_image_variant_section);

    # Add various bits of metadata for the place image variant
    my $place_image_variant_href = "_httpcollection_/index/assoc/[assocfilepath]/" . $place_image_variant_file_name;
    $place_image_variant_href =~ s/ /%20/g;
    return $place_image_variant_href;
}


sub write_static_browser_macrofile
{
    my $static_browser_package_name = shift(@_);
    my $id_to_name_mapping = shift(@_);

    my $static_browser_macrofile_path = "$ENV{'GSDLHOME'}\\collect\\cic-hcap\\macros\\$static_browser_package_name.dm";
    print STDERR "Writing $static_browser_macrofile_path...\n";
    open(BROWSER_MACROFILE, ">$static_browser_macrofile_path") or die "Error: Could not write to $static_browser_macrofile_path.\n";
    &write_static_browser_macros($static_browser_package_name, $id_to_name_mapping);
    close(BROWSER_MACROFILE);
}


sub write_static_browser_macros
{
    my $static_browser_package_name = shift(@_);
    my $id_to_name_mapping = shift(@_);
    my $id_to_extra_mapping = shift(@_);

    print BROWSER_MACROFILE "package $static_browser_package_name\n\n";
    print BROWSER_MACROFILE "_cicstaticbrowser_ {\n";

    my %letter_to_ids_mapping;
    foreach my $id (keys %$id_to_name_mapping) {
	my $name = $id_to_name_mapping->{$id};
	my ($letter) = ($name =~ /([A-Za-z0-9])/);
	push(@{$letter_to_ids_mapping{$letter}}, $id);
    }

    print BROWSER_MACROFILE "<center><b>";
    foreach my $letter (split(//, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
	if (defined($letter_to_ids_mapping{$letter})) {
	    print BROWSER_MACROFILE "<a href=\"#$letter\">$letter</a>&nbsp;";
	}
	else {
	    print BROWSER_MACROFILE "$letter&nbsp;";
	}
    }
    print BROWSER_MACROFILE "</b></center>\n";

    print BROWSER_MACROFILE "<table cellpadding=\"0\" cellspacing=\"0\" width=\"_pagewidth_\">\n";
    foreach my $letter (sort(keys %letter_to_ids_mapping)) {
	my @letter_ids = @{$letter_to_ids_mapping{$letter}};
	&write_static_browser_macros_chunk($letter, \@letter_ids, $id_to_name_mapping, $id_to_extra_mapping);
    }
    print BROWSER_MACROFILE "</table>\n";

    print BROWSER_MACROFILE "}\n";
}


sub write_static_browser_macros_chunk
{
    my $chunk_title = shift(@_);
    my $chunk_ids_ref = shift(@_);
    my $id_to_name_mapping = shift(@_);
    my $id_to_extra_mapping = shift(@_);

    print BROWSER_MACROFILE "<tr><td width=\"50%\"><br /><a name=\"$chunk_title\"/><span style=\"color: black;\"><b>$chunk_title</b></span></td><td width=\"50%\"></td></tr>\n";

    my %full_name_to_id_mapping;
    foreach my $id (@{$chunk_ids_ref}) {
	my $full_name = $id_to_name_mapping->{$id};
	if (defined($id_to_extra_mapping)) {
	    $full_name .= " " . $id_to_extra_mapping->{$id};
	}
	$full_name_to_id_mapping{$full_name} = $id;
    }
 
    my @full_names = sort(keys(%full_name_to_id_mapping));
    my $half_point = ((scalar(@full_names) % 2 == 0) ? scalar(@full_names) / 2 : (scalar(@full_names) + 1) / 2);
    for (my $i = 0; $i < $half_point; $i++) {
	print BROWSER_MACROFILE "<tr>";

	my $id = $full_name_to_id_mapping{$full_names[$i]};
	my $name = $id_to_name_mapping->{$id};
	my $extra = $id_to_extra_mapping->{$id} || "";
	print BROWSER_MACROFILE "<td valign=\"top\"><a href=\"_gwcgi_?a=d&d=$id\">$name</a>$extra</td>";

	print BROWSER_MACROFILE "<td valign=\"top\">";
	if (defined($full_names[$i+$half_point])) {
	    $id = $full_name_to_id_mapping{$full_names[$i+$half_point]};
	    $name = $id_to_name_mapping->{$id};
	    $extra = $id_to_extra_mapping->{$id} || "";
	    print BROWSER_MACROFILE "<a href=\"_gwcgi_?a=d&d=$id\">$name</a>$extra";
	}
	print BROWSER_MACROFILE "</td>";

	print BROWSER_MACROFILE "</tr>\n";
    }
}


sub write_bilevel_static_browser_macrofile
{
    my $static_browser_package_root = shift(@_);
    my $category_to_ids_mapping = shift(@_);
    my $id_to_name_mapping = shift(@_);
    my $id_to_extra_mapping = shift(@_);

    my $static_browser_macrofile_path = "$ENV{'GSDLHOME'}\\collect\\cic-hcap\\macros\\$static_browser_package_root.dm";
    print STDERR "Writing $static_browser_macrofile_path...\n";
    open(BROWSER_MACROFILE, ">$static_browser_macrofile_path") or die "Error: Could not write to $static_browser_macrofile_path.\n";

    foreach my $category (keys(%{$category_to_ids_mapping})) {
	my $static_browser_package_name = $static_browser_package_root . $category;
	$static_browser_package_name =~ s/\W//g;

	my %id_to_name_mapping_for_category = ();
	foreach my $id (@{$category_to_ids_mapping->{$category}}) {
	    $id_to_name_mapping_for_category{$id} = $id_to_name_mapping->{$id};
	}
	&write_static_browser_macros($static_browser_package_name, \%id_to_name_mapping_for_category, $id_to_extra_mapping);
    }

    close(BROWSER_MACROFILE);
}


sub write_state_browser_macrofile
{
    my $static_browser_package_name = shift(@_);
    my $state_to_ids_mapping = shift(@_);
    my $id_to_name_mapping = shift(@_);

    my $static_browser_macrofile_path = "$ENV{'GSDLHOME'}\\collect\\cic-hcap\\macros\\$static_browser_package_name.dm";
    print STDERR "Writing $static_browser_macrofile_path...\n";
    open(BROWSER_MACROFILE, ">$static_browser_macrofile_path") or die "Error: Could not write to $static_browser_macrofile_path.\n";
    print BROWSER_MACROFILE "package $static_browser_package_name\n\n";
    print BROWSER_MACROFILE "_cicstaticbrowser_ {\n";

    print BROWSER_MACROFILE "<table cellpadding=\"0\" cellspacing=\"0\" width=\"_pagewidth_\">\n";
    foreach my $state (sort(keys(%{$state_to_ids_mapping}))) {
	my @state_ids = @{$state_to_ids_mapping->{$state}};
	&write_static_browser_macros_chunk($state, \@state_ids, $id_to_name_mapping, undef);
    }
    print BROWSER_MACROFILE "</table>\n";

    print BROWSER_MACROFILE "}\n";
    close(BROWSER_MACROFILE);
}


1;
