Index: /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/export-individual-expeditee.pl.in
===================================================================
--- /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/export-individual-expeditee.pl.in	(revision 26693)
+++ /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/export-individual-expeditee.pl.in	(revision 26694)
@@ -4,9 +4,4 @@
 #!C:\\Perl32\\bin\\perl -w
 
-##
-# This is used for exporting individual Greenstone
-# documents to Expeditee. To export entire collections,
-# use html-to-expeditee.pl
-##
 use strict;
 my $iis6_mode = 0;
@@ -27,85 +22,134 @@
 sub display_output
 {
-	my($docID,$c,$assocfilepath,$site,$fn,$found) = @_;
+	my($docID,$c,$assocfilepath,$site) = @_;
 	
 	my $iframeURL = "library/collection/$c/document/$docID"."?p.showAssocFilePath=1";
 	
-	#Render an html page to display & export item.
+	#Render an html page to display and export item.
 	my $html = <<EOT;
 <html>
-	<head>
-		<title>Export Item to Expeditee</title>
-		<base href=".."/>
-		
-		<link type="text/css" href="ext/html-to-expeditee/jquery/css/le-frog/jquery-ui-1.8.16.custom.css" rel="stylesheet"/>
-		<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-1.6.2.min.js"></script>
-		<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
-		<script type="text/javascript" src="ext/html-to-expeditee/js/html-to-expeditee.js"></script>
-		<script type="text/javascript" src="ext/html-to-expeditee/js/gsajax-min.js"></script>
-		
-		<style type="text/css">
+<head>
+	<title>Export Document to Expeditee</title>
+	<base href=".."/>
+
+	<link type="text/css" href="ext/html-to-expeditee/jquery/css/le-frog/jquery-ui-1.8.16.custom.css" rel="stylesheet"/>
+	<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-1.6.2.min.js"></script>
+	<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
+	<script type="text/javascript" src="ext/html-to-expeditee/js/html-to-expeditee.js"></script>
+	<script type="text/javascript" src="ext/html-to-expeditee/js/gsajax-min.js"></script>
+	
+	<style type="text/css">
 		body{
 			font-family: Arial;
 			font-size: 16px;
 			color: #000;
+	}
+	</style>
+	<script type="text/javascript">
+		function load(){
+	
+			var iframe = document.getElementById("iframe");
+			
+			if(iframe.style.display != "block"){
+				iframe.height = "90%";
+				iframe.style.display = "block";
+			}
+			
+			var iframeDoc;
+			
+			if(iframe.contentDocument){
+				/* Firefox and Chrome */
+				iframeDoc = iframe.contentDocument;
+			}else if(iframe.contentWindow){
+				/* IE */
+				iframeDoc = iframe.contentWindow.document;
+			}
+			
+			var gsContent = iframeDoc.getElementById("gs_content");
+			var xmlUrl = iframe.src + "&o=xml";
+			
+			var frameNum = getMetadata(xmlUrl,'frameID');
+			
+			if(frameNum === null){
+				//use -1 as default, to change later.
+				frameNum = -1;
+			}
+			
+			//Check for assocfilepath
+			var assocElem = iframeDoc.getElementById('assocfilepath');
+			var assoc = null;
+						
+			if(assocElem === undefined || assocElem === null){
+				assoc = getMetadata(xmlUrl,'assocfilepath');
+			}
+			
+			var expFrameTree = htmlToExpeditee(gsContent);
+			var expFrame = JSON.stringify(expFrameTree);
+			
+			var site = "$site";
+			var url = "cgi-bin/export-individual-expeditee.pl";
+			var params = "c=" + "$c";
+			
+			if(site.match(/\\w/)){
+				params += "&site=" + site;
+			}
+			
+			params += "&a=generate-frame&fn=" + frameNum;
+			params += "&json=" + escape(expFrame);
+			
+			//Add an assocfilepath param 
+			if(assoc !== null){
+				params += "&assoc=" + assoc;
+			}
+			
+			var request = urlPostSync(url,params);
+			
+			alert("Document has been successfully exported to Expeditee. The corresponding Expeditee frame can be found in the collection's export directory at: " + frameNum + ".exp");
+			
 		}
-		</style>
-	<script type="text/javascript">
-	function load(){
-	
-		var iframe = document.getElementById("iframe");
-		if(iframe.style.display != "block"){
-			iframe.height = "90%";
-			iframe.style.display = "block";
+		
+		function getMetadata(xmlUrl,nameValue){
+	
+			var metadata = null;
+		
+			\$.ajax({
+				type: "GET",
+				async: false,
+				url: xmlUrl,
+				dataType: "xml",
+				success: function(xml){
+								
+				\$(xml).find('metadata').each(function(){
+					
+					var name = \$(this).attr('name');
+					
+					if(name === nameValue){
+						
+						if(metadata === null){
+							metadata = \$(this).text();
+							//console.log(metadata);
+						}
+					
+					}	
+				});
+				
+			}
+			
+		});
+		
+		return metadata;
 		}
-		
-		var iframeDoc;
-		if( iframe.contentDocument){
-			/* Firefox & Chrome */
-			iframeDoc = iframe.contentDocument;
-		}else if(iframe.contentWindow){
-			/* IE */
-			iframeDoc = iframe.contentWindow.document;
-		}
-		
-		var gsContent = iframeDoc.getElementById("gs_content");
-						
-		//hide rightSidebar div
-		var rightSidebar = iframeDoc.getElementById("rightSidebar");
-		if(rightSidebar !== undefined){
-			rightSidebar.style.display = "none";
-		}
-		
-		var expFrameTree = htmlToExpeditee(gsContent);
-		var expFrame = JSON.stringify(expFrameTree);
-		
-		var site = "$site";
-		var url = "cgi-bin/export-individual-expeditee.pl";
-		var params = "c=" + "$c";
-		
-		if(site.match(/\\w/)){
-			params += "&site=" + site;
-		}
-		params += "&a=generate-frame&fn=" + "$fn";
-		params += "&found=" + "$found";
-		params += "&json=" + escape(expFrame);
-		
-		var request = urlPostSync(url,params);
-		
-		alert("Document has been successfully exported to Expeditee. It can be found in the collection's export directory at: $fn.exp");
-	}
 	</script>
-	</head>
+	
 	<body>
 	
-	<p id="msg">Exporting following document to Expeditee... </p>
+		<p id="msg">Exporting following document to Expeditee... </p>
 	
 	<hr style="margin: 10px;"/>
 	
 	<!-- Load up an iframe to display URL in-->
-	<div align="center"><iframe id="iframe" width="1000px" height="90%" src="$iframeURL" onload="load();"></iframe</div>
-	
+	<div align="center"><iframe id="iframe" width="1000px" height="90%" src="$iframeURL" onload="load();"></iframe></div>
 	</body>
-</html>
+	</html>
 EOT
 	print "Content-type:text/html\n\n";
@@ -113,17 +157,4 @@
 }
 
-##For debugging.
-sub check{
-
-	my($check1,$check2,$check3,$check4,$check5) = @_;
-
-	print "Content-type:text/html\n\n";
-	print $check1;
-	print $check2;
-	print $check3;
-	print $check4;
-	print $check5;
-}
-
 sub main
 {
@@ -135,6 +166,5 @@
 	my $gsdlhome = $ENV{'GSDLHOME'};
 	$gsdl_cgi->checked_chdir($gsdlhome);
-	
-	#require cgiactions::HtmlToExpediteeAction;
+
 	require cgiactions::ExpediteeExportAction;
 	$gsdl_cgi->parse_cgi_args();
@@ -143,111 +173,29 @@
 	
 	my $fn = $gsdl_cgi->param("fn");
-
+	
 	if(defined $fn){
 		
 		my $site = $gsdl_cgi->clean_param("site");
 		my $c = $gsdl_cgi->clean_param("c");
-		my $a = $gsdl_cgi-> clean_param("a");
+		my $a = $gsdl_cgi->clean_param("a");	#assocfilepath
+		
 		my $json = $gsdl_cgi->clean_param("json");
-		my $found = $gsdl_cgi->clean_param("found");
-		#check($site,$c,$a,$json,$found);
 		
 		my $action = new ExpediteeExportAction($gsdl_cgi,$iis6_mode);
 		$action->do_action();
 		
-		#my $action = new HtmlToExpediteeAction($gsdl_cgi,$iis6_mode);
-		#$action->do_action();
 	}else{
 		
-		printf("fn not defined\n");
 		my $docID = $gsdl_cgi->clean_param("docID");
 		my $c = $gsdl_cgi->clean_param("c");
 		my $assocfilepath = $gsdl_cgi->clean_param("assocfilepath");
 		my $site = $gsdl_cgi->clean_param("site");
-		
+
 		if(!defined $site){
 			$gsdl_cgi->generate_error("No site specified.");
 		}
 		
-		#Open export directory
-		my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-		my $export_dir = &util::filename_cat($collect_dir,$c,"export");
-		
-		my $frameInf = "$export_dir\\frame.inf";
-		my $fn = "";
-		my @files;
-		
-		#check if export directory exists otherwise create it.
-		if(-d $export_dir){
-			opendir DIR, $export_dir or die "cannot open dir $export_dir: $!";
-			@files = readdir DIR;
-			closedir DIR;
-		}else{
-			print "Reached!\n";
-			&util::mk_dir($export_dir);
-			
-			my $zeroFrame = "0.exp";
-			open(ZERO_FRAME,">>$export_dir\\$zeroFrame") || die "$!";
-			close ZERO_FRAME;
-			
-			opendir DIR, $export_dir or die "can't open dir $export_dir: $!\n";
-			@files = readdir DIR;
-			closedir DIR;
-		}
-		
-		my $found = 0;
-		for my $file(@files){
-			if($file =~ m/\d/){
-				
-				my $fullFile = "$export_dir\\$file";
-				open FILE, $fullFile or die $!;
-				my @lines = <FILE>;
-				
-				for my $line(@lines){
-					if($line eq "T \@assocfilepath $assocfilepath\n"){
-						$fn = $fullFile;
-						$found = 1;
-					}
-				}
-				close(FILE);
-			}
-		}
-		
-		unless($found){
-			#use frame.inf file to get file number
-			if(-e $frameInf){
-				
-				open FRAME_INF, $frameInf or die $!;
-				my @lines = <FRAME_INF>;
-				
-				for my $line(@lines){
-					$fn = $line;
-				}
-			}else{
-				
-				print "$frameInf not found\n";
-
-				$fn = 0;
-			}
-		}
-		
-		#now get frame number out of file name
-		my @getFrameNumArray = split('\\\\',$fn);
-		@getFrameNumArray = reverse(@getFrameNumArray);
-		$fn = $getFrameNumArray[0];
-		
-		$fn =~ s/[^0-9]//g;
-		unless($found){
-			$fn = $fn + 1;
-		}
-		#unless($found){
-		# $fn = $fn + 1;
-		#}
-		
-		print "fn = ".$fn."\n";
-		
-	display_output($docID,$c,$assocfilepath,$site,$fn,$found);
+		display_output($docID,$c,$assocfilepath,$site);
 	}
-	
 }
 
Index: /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/html-to-expeditee.pl.in
===================================================================
--- /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/html-to-expeditee.pl.in	(revision 26693)
+++ /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/html-to-expeditee.pl.in	(revision 26694)
@@ -182,4 +182,5 @@
 	      
 	      //url += "&p.showExpediteeAttributes=1";
+		  url += "&p.showAssocFilePath=1";
 
 		  
@@ -213,8 +214,22 @@
 		    iframeDoc = iframe.contentWindow.document;
 	      }
-
-	      var frameID = (numDocOIDs - docOIDs.length);
-	      var progressPercent = frameID/numDocOIDs * 100;
-
+		  
+		  var xmlUrl = iframe.src + "&o=xml";
+		  
+		  var iter = (numDocOIDs - docOIDs.length);
+	      var progressPercent = iter/numDocOIDs * 100;
+		  
+		  /*Check if a frame-id metadata element already exists, 
+		  otherwise generate a new frame number based on the 
+		  number of documents to process.*/
+		  var frameNum = getMetadata(xmlUrl,'frameID');
+		  
+		  if(frameNum === null){
+			frameNum = iter;
+			console.log("no frameNum found, use iter: " + frameNum);
+		  }else{
+			console.log("frameNum metadata found: " + frameNum);
+		  }
+		  
 	      \$(function() {
 		  \$('#progressbar').progressbar({ value: progressPercent })
@@ -228,9 +243,12 @@
 	      var pxr = gsContentPos.xr;
 	      */
-
-		  //get assocfilepath from xml 
-		  var xmlUrl = iframe.src + "&o=xml";
-		  
-		  var assoc = getAssocFilePath(xmlUrl);
+		  
+		  //Check if an assocfilepath annotation already exists on the page, otherwise obtain it from the xml.
+		  var assocElem = iframeDoc.getElementById('assocfilepath');
+		  var assoc = null;
+		  
+		  if(assocElem === undefined || assocElem === null){
+			assoc = getMetadata(xmlUrl,'assocfilepath');
+		  }
 
 	      var expFrameTree = htmlToExpeditee(gsContent);
@@ -248,10 +266,10 @@
    	      }
 		  
-	      params += "&a=generate-frame&fn=" + frameID;
+	      //params += "&a=generate-frame&fn=" + iter;
+		  params += "&a=generate-frame&fn=" + frameNum;
 	      params += "&json=" + escape(expFrame);
 		  
-		  //add an assocfilepath parameter
-		  
-		  if(assoc !== null && assoc !== undefined){
+		  //add an assocfilepath parameter but only if it is defined.
+		  if(assoc !== null){
 			params += "&assoc=" + assoc;
 		  }
@@ -288,13 +306,14 @@
 
 		  //url += "&p.showExpediteeAttributes=1";
-
-		  iframe.src = url;
+			url += "&p.showAssocFilePath=1";
+		  
+			iframe.src = url;
 	      }
 	      else {
-		  var progressbar = document.getElementById("progressbar");
-		  progressbar.style.display = "none";
-
-		  iframe.style.display = "none";
-		  delete iframe.src;
+			var progressbar = document.getElementById("progressbar");
+			progressbar.style.display = "none";
+
+			iframe.style.display = "none";
+			delete iframe.src;
 	      }
 	  }
@@ -302,8 +321,14 @@
       }
 	  
-	  function getAssocFilePath(xmlUrl){
-	  var assoc = "";
+	/**
+	* Once this is working, remove getAssocFilePath method
+	* This method is used at this stage to retrieve assocfilepath
+	* and frame number metadata values from the document's xml.
+	**/
+	function getMetadata(xmlUrl,nameValue){
 	  
-	  \$.ajax({
+		var metadata = null;
+		
+		 \$.ajax({
 			type: "GET",
 			async: false,
@@ -317,8 +342,8 @@
 					var name = \$(this).attr('name');
 					
-					if(name === 'assocfilepath'){
-						if(assoc === ""){
-							assoc = \$(this).text();
-							//console.log(assoc);
+					if(name === nameValue){
+						if(metadata === null){
+							metadata = \$(this).text();
+							//console.log(metadata);
 						}
 						
@@ -329,6 +354,6 @@
 			
 		  });
-		  
-		  return assoc;
+		
+		return metadata;
 	  }
 
