Index: trunk/DataStoreServer/scripts/dsprodindex
===================================================================
--- trunk/DataStoreServer/scripts/dsprodindex	(revision 17381)
+++ trunk/DataStoreServer/scripts/dsprodindex	(revision 23876)
@@ -27,43 +27,30 @@
 }
 
-# build the header line
-my $header = "# filesetID|time registered     |type     |";
+# first get the column names
 
-# now add the product specific columns that are defined
-# not particularly elegant. I guess I should have fetched the row as an array
-# note: we don't do any formatting for these columns. The width of the string
-# in the database should be padded if a nice width is desired
+# we have at least 3 columns
+my @header_col = ("# filesetID", "time registered", "type");
+my $numCols = 3;
+
+# now add any product specific columns that are defined. Note if there is no header value
+# then any values in the rows will be ignored
 my $last_prod_col = -1;
-if (defined $prod->{prod_col_0}) {
-    $last_prod_col = 0;
-    $header .= "$prod->{prod_col_0}|";
-    if (defined $prod->{prod_col_1}) {
-        $last_prod_col = 1;
-        $header .= "$prod->{prod_col_1}|";
-        if (defined $prod->{prod_col_2}) {
-            $last_prod_col = 2;
-            $header .= "$prod->{prod_col_2}|";
-            if (defined $prod->{prod_col_3}) {
-                $last_prod_col = 3;
-                $header .= "$prod->{prod_col_3}|";
-                if (defined $prod->{prod_col_4}) {
-                    $last_prod_col = 4;
-                    $header .= "$prod->{prod_col_4}|";
-                    if (defined $prod->{prod_col_5}) {
-                        $last_prod_col = 5;
-                        $header .= "$prod->{prod_col_5}|";
-                        if (defined $prod->{prod_col_6}) {
-                            $last_prod_col = 6;
-                            $header .= "$prod->{prod_col_6}|";
-                            if (defined $prod->{prod_col_7}) {
-                                $last_prod_col = 7;
-                                $header .= "$prod->{prod_col_7}|";
-                            }
-                        }
-                    }
-                }
-            }
-        }
-    }
+for (my $i = 0; $i < 8; $i++) {
+    my $col_key = "prod_col_$i";
+    my $col_name = $prod->{$col_key};
+
+    last if ! defined $col_name;
+
+    $header_col[$numCols++] = $col_name;
+    $last_prod_col = $i;
+}
+
+# now set up arrays for the values for each column. 
+my @column;
+for (my $i = 0; $i < $numCols; $i++) {
+
+    my @col = ($header_col[$i]);
+
+    $column[$i] = \@col;
 }
     
@@ -89,5 +76,5 @@
         # XXX: the spec doesn't say what should happen in this case.
         # Here we follow the conductor implementation and return the whole list.
-        # This may not be the right thing to do.
+        # This may not be the right thing to do. It might be better to throw an error
     }
 }
@@ -96,9 +83,9 @@
 $stmt->execute();
 
-# XXX: dsproductls expects a header even if there are no matching filesets
-print "$header\n";
-
+# we at least have the header row to output
+my $numRows = 1;
 while( my $row = $stmt->fetchrow_hashref()) {
 
+    $numRows++;
     my $daytime = $row->{reg_time};
     (my $date, my $time) = split " ", $daytime;
@@ -108,31 +95,50 @@
     my $line = sprintf "%-11s|%-20s|%-9s|", $row->{fileset_name}, $reg_time, $row->{type};
 
-    # now add the product specific columns that are defined
-    # again using an array would have made this look nicer
-    if ($last_prod_col >= 0) {
-        $line .= "$row->{prod_col_0}|";
-        if ($last_prod_col >= 1) {
-            $line .= "$row->{prod_col_1}|";
-            if ($last_prod_col >= 2) {
-                $line .= "$row->{prod_col_2}|";
-                if ($last_prod_col >= 3) {
-                    $line .= "$row->{prod_col_3}|";
-                    if ($last_prod_col >= 4) {
-                        $line .= "$row->{prod_col_4}|";
-                        if ($last_prod_col >= 5) {
-                            $line .= "$row->{prod_col_5}|";
-                            if ($last_prod_col >= 6) {
-                                $line .= "$row->{prod_col_6}|";
-                                if ($last_prod_col >= 7) {
-                                    $line .= "$row->{prod_col_7}|";
-                                }
-                            }
-                        }
-                    }
-                }
-            }
+    # this probably would have been simpler had I used fetchrow array
+    push @{$column[0]}, $row->{fileset_name};
+    push @{$column[1]}, $reg_time;
+    push @{$column[2]}, $row->{type};
+    
+    for (my $i = 0; $i <= $last_prod_col; $i++) {
+        my $col_key = "prod_col_$i";
+        my $col_val = $row->{$col_key};
+
+        if (!defined($col_val)) {
+            $col_val = "null";
+        }
+        push @{$column[$i+3]}, $col_val;
+    }
+}
+
+# find the largest value width in each column
+my @col_widths;
+for (my $c = 0; $c < $numCols; $c++) {
+    $col_widths[$c] = 0;
+    my $col = $column[$c];
+    for (my $i = 0; $i < $numRows; $i++) {
+        my $val = $col->[$i];
+        if (!$val) {
+            die "value for column $c in row $i is null";
+            next;
+        }
+        my $len = length($val);
+        if ($len > $col_widths[$c]) {
+            $col_widths[$c] = $len;
         }
     }
+}
 
-    print "$line\n";
+# print out the results
+for (my $r = 0; $r < $numRows; $r++) {
+    for (my $c = 0; $c < $numCols; $c++) {
+        my $val = $column[$c]->[$r];
+        my $width = $col_widths[$c];
+        if ($val) {
+            printf "%-*s|", $width, $val;
+        } else {
+            die "value for column $c in row $r is null";
+        }
+    }
+    print "\n";
 }
+    
