Index: /main/trunk/greenstone2/perllib/cgiactions/explodeaction.pm
===================================================================
--- /main/trunk/greenstone2/perllib/cgiactions/explodeaction.pm	(revision 23741)
+++ /main/trunk/greenstone2/perllib/cgiactions/explodeaction.pm	(revision 23742)
@@ -33,4 +33,5 @@
 use ghtml;
 
+use File::Basename;
 
 BEGIN {
@@ -88,5 +89,5 @@
 
 
-sub oid_to_import_filenames
+sub docid_to_import_filenames
 {
     my $self = shift @_;
@@ -103,31 +104,36 @@
 					$archive_dir);
 
-    print STDERR "**** arcinfo doc filename = $arcinfo_doc_filename\n";
-
-    my @import_files = ();
+    my %all_import_file_keys = ();
     
     foreach my $docid (@docids) {
 	# Obtain the src and associated files specified docID
 	
-	print STDERR "*** looking up key \"$docid\"\n";
-
 	my $doc_rec
 	    = &dbutil::read_infodb_entry($infodb_type, $arcinfo_doc_filename, 
 					 $docid);
-
-	print STDERR "*** doc_rec = $doc_rec\n";
 	
 	my $src_files = $doc_rec->{'src-file'};
 	my $assoc_files = $doc_rec->{'assoc-file'};
 	
-	push(@import_files,@$src_files) if (defined $src_files);
-	push(@import_files,@$assoc_files) if (defined $assoc_files);
-    }
-
-    return \@import_files;
-}
-
-
-sub import_filenames_to_oids
+	if (defined $src_files) {
+	    foreach my $if (@$src_files) {
+		$all_import_file_keys{$if} = 1;
+	    }
+	}
+
+	if (defined $assoc_files) {
+	    foreach my $if (@$assoc_files) {
+		$all_import_file_keys{$if} = 1;
+	    }
+	}
+    }
+
+    my @all_import_files = keys %all_import_file_keys;
+
+    return \@all_import_files;
+}
+
+
+sub import_filenames_to_docids
 {
     my $self = shift @_;
@@ -140,30 +146,95 @@
     my $archive_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"archives");
 
+    # Obtain the oids for the specified import filenames
+    my $arcinfo_src_filename 
+	= &dbutil::get_infodb_file_path($infodb_type, "archiveinf-src", 
+				 	$archive_dir);
+
+    my %all_oid_keys = ();
+
+    foreach my $if (@$import_filenames) {
+
+	my $src_rec
+	    = &dbutil::read_infodb_entry($infodb_type, $arcinfo_src_filename, 
+					 $if);
+	my $oids = $src_rec->{'oid'};
+
+	foreach my $o (@$oids) {
+	    $all_oid_keys{$o} = 1;
+	}
+    }
+
+    my @all_oids = keys %all_oid_keys;
+
+    return \@all_oids;
+}
+
+
+sub remove_import_filenames
+{
+    my $self = shift @_;
+    my ($expanded_import_filenames) = @_;
+
+    foreach my $f (@$expanded_import_filenames) {
+	# If this document has been exploded before then
+	# its original source files will have already been removed	
+	if (-e $f) {
+	    &util::rm($f);
+	}
+    }
+}
+
+sub move_docoids_to_import
+{
+    my $self = shift @_;
+    my ($docids) = @_;
+
+    my $infodb_type = $self->{'infodbtype'};
+
+    # Derive the archives and import directories
+    my $archive_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"archives");
+    my $import_dir  = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"import");
+
     # Obtain the doc.xml path for the specified docID
     my $arcinfo_doc_filename 
-	= &dbutil::get_infodb_file_path($infodb_type, "archiveinf-src", 
+	= &dbutil::get_infodb_file_path($infodb_type, "archiveinf-doc", 
 				 	$archive_dir);
 
-    my %all_oid_keys = ();
-
-    foreach my $if (@$import_filenames) {
-
-	print STDERR "*** looking if key \"$if\"\n";
-
-	my $src_rec
+    foreach my $docid (@$docids) {
+
+	my $doc_rec
 	    = &dbutil::read_infodb_entry($infodb_type, $arcinfo_doc_filename, 
-					 $if);
-	my $oids = $src_rec->{'oid'};
-
-	foreach my $o (@$oids) {
-	    $all_oid_keys{$o} = 1;
-	}
-    }
-
-    my @all_oids = keys %all_oid_keys;
-
-    return \@all_oids;
-}
-
+					 $docid);
+
+	my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
+
+	# The $doc_xml_file is relative to the archives, so need to do
+	# a bit more work to make sure the right folder containing this
+	# is moved to the right place in the import folder
+
+	my $assoc_path = dirname($doc_xml_file);
+	my $import_assoc_dir = &util::filename_cat($import_dir,$assoc_path);
+	my $archive_assoc_dir = &util::filename_cat($archive_dir,$assoc_path);
+
+	# If assoc_path involves more than one sub directory, then need to make
+	# sure the necessary directories exist in the import area also.
+	# For example, if assoc_path is "a/b/c.dir" then need "import/a/b" to
+	# exists before moving "archives/a/b/c.dir" -> "import/a/b"
+	my $import_target_parent_dir = dirname($import_assoc_dir);
+
+	if (-d $import_assoc_dir) {
+	    # detected version from previous explode => remove it
+	    &util::rm_r($import_assoc_dir);
+	}
+	else {
+	    # First time => make sure parent directory exists to move 
+	    # "c.dir" (see above) into
+	    
+	    &util::mk_all_dir($import_target_parent_dir);
+	}
+
+	&util::cp_r($archive_assoc_dir,$import_target_parent_dir)
+    }
+}
 
 
@@ -196,13 +267,17 @@
     }
 
-    my $orig_import_filenames = $self->oid_to_import_filenames($docid);
-    my $oid_keys = $self->import_filenames_to_oids($orig_import_filenames);
-    my $expanded_import_filenames = $self->oid_to_import_filenames(@$oid_keys);
-
+    my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
+
+    my $orig_import_filenames = $self->docid_to_import_filenames($docid_root);
+    my $docid_keys = $self->import_filenames_to_docids($orig_import_filenames);
+    my $expanded_import_filenames = $self->docid_to_import_filenames(@$docid_keys);
+
+    $self->remove_import_filenames($expanded_import_filenames);
+    $self->move_docoids_to_import($docid_keys);
 
     # Release the lock once it is done
     $self->unlock_collection($username, $collect);
 
-    my $mess = "Base OID: $docid\n-----\n";
+    my $mess = "Base Doc ID: $docid_root\n-----\n";
     $mess .= join("\n",@$expanded_import_filenames);
 
