Index: lib/style/skins/first/download.jsp
===================================================================
--- lib/style/skins/first/download.jsp	(nonexistent)
+++ lib/style/skins/first/download.jsp	(revision 4872)
@@ -0,0 +1,201 @@
+<%@page 
+import="java.util.Iterator"%><%@page 
+import="java.util.ArrayList"%><%@page 
+import="edu.ucsb.nceas.utilities.OrderedMap"%><%@page 
+import="java.util.List"%><%@page 
+import="java.util.Enumeration"%><%@page 
+import="java.sql.SQLException"%><%@page 
+import="org.ecoinformatics.datamanager.transpose.DataTranspose"%><%@page 
+import="au.com.bytecode.opencsv.CSVWriter"%><%@page 
+import="java.io.OutputStreamWriter"%><%@page 
+import="java.io.Writer"%><%@page 
+import="java.sql.ResultSet"%><%@page 
+import="edu.ucsb.nceas.metacat.dataquery.DataQuery"%><%@page 
+import="java.io.IOException"%><%@page 
+import="edu.ucsb.nceas.utilities.PropertyNotFoundException"%><%@page 
+import="java.util.Hashtable"%><%@ page 
+language="java" %><%
+/**
+ * 
+ * '$RCSfile$'
+ * Copyright: 2008 Regents of the University of California and the
+ *             National Center for Ecological Analysis and Synthesis
+ *    '$Author: leinfelder $'
+ *      '$Date: 2008-08-22 16:48:56 -0700 (Fri, 22 Aug 2008) $'
+ * '$Revision: 4305 $'
+ * 
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */  
+%><%
+Hashtable params = getParams(request);
+handleDataquery(params,response,request.getSession().getId());
+%><%!
+	private Hashtable getParams(HttpServletRequest request) {
+		Hashtable params = new Hashtable();
+		Enumeration<String> paramlist = (Enumeration<String>)request.getParameterNames();
+		while (paramlist.hasMoreElements()) {
+	
+			String name = paramlist.nextElement();
+			String[] value = request.getParameterValues(name);
+			params.put(name, value);
+		}
+	
+		return params;
+	}
+%><%!
+	private List transpose(ResultSet rs, int idCol, int pivotCol, boolean omitIdValues) throws SQLException {
+		OrderedMap table = new OrderedMap();
+		OrderedMap widestRow = new OrderedMap();
+		int colCount = rs.getMetaData().getColumnCount();
+		String idColName = rs.getMetaData().getColumnName(idCol);
+		
+		while (rs.next()) {
+			String id = rs.getString(idCol);
+			String pivotValue = rs.getString(pivotCol);
+			
+			//look up the row we are working on
+			OrderedMap row = (OrderedMap) table.get(id);
+			if (row == null) {
+				row = new OrderedMap();
+			}
+			
+			//get the values for this pivot
+			for (int i = 1; i <= colCount; i++) {
+				if (i != pivotCol) {
+					String colName = rs.getMetaData().getColumnName(i);
+					//annotate the column name with the pivot column value if not the id column
+					if (i != idCol) {
+						colName = pivotValue + "_" + colName;
+					}
+					String value = rs.getString(i);
+					row.put(colName, value);
+				}
+			}
+			//track the headers - the values are junk
+			widestRow.putAll(row);
+			
+			//put the row back (or maybe it's the first time)
+			table.put(id, row);	
+		}
+		
+		//now make it into a list
+		List retTable = new ArrayList();
+		
+		//do the header, based on widest entry
+		List header = new ArrayList(widestRow.keySet());
+		retTable.add(header.toArray(new String[0]));
+		
+		//now the value rows
+		Iterator rowIter = table.values().iterator();
+		int rowCount = 1;
+		while (rowIter.hasNext()) {
+			OrderedMap rowMap = (OrderedMap) rowIter.next();
+			List row = new ArrayList();
+			//iterate over the widest row's columns
+			Iterator columnIter = widestRow.keySet().iterator();
+			while (columnIter.hasNext()) {
+				Object key = columnIter.next();
+				Object value = rowMap.get(key);
+				//hide the value used for Ids - just increment row
+				if (key.equals(idColName) && omitIdValues) {
+					value = String.valueOf(rowCount);
+				}
+				row.add(value);
+			}
+			rowCount++;
+			retTable.add(row.toArray(new String[0]));
+		}
+		
+		return retTable;
+	}
+%><%!
+	private void handleDataquery(
+			Hashtable<String, String[]> params,
+	        HttpServletResponse response,
+	        String sessionId) throws PropertyNotFoundException, IOException {
+		
+		DataQuery dq = null;
+		if (sessionId != null) {
+			dq = new DataQuery(sessionId);
+		}
+		else {
+			dq = new DataQuery();
+		}
+		
+		String dataqueryXML = (params.get("dataquery"))[0];
+	
+		ResultSet rs = null;
+		try {
+			rs = dq.executeQuery(dataqueryXML);
+		} catch (Exception e) {
+			//probably need to do something here
+			e.printStackTrace();
+			return;
+		}
+		
+		//process the result set
+		String qformat = "csv";
+		String[] temp = params.get("qformat");
+		if (temp != null && temp.length > 0) {
+			qformat = temp[0];
+		}
+		String fileName = "query-results." + qformat;
+		
+		boolean transpose = false;
+		temp = params.get("transpose");
+		if (temp != null && temp.length > 0) {
+			transpose = Boolean.parseBoolean(temp[0]);
+		}
+		int observation = 0;
+		temp = params.get("observation");
+		if (temp != null && temp.length > 0) {
+			observation = Integer.parseInt(temp[0]);
+		}
+		int pivot = 0;
+		temp = params.get("pivot");
+		if (temp != null && temp.length > 0) {
+			pivot = Integer.parseInt(temp[0]);
+		}
+		
+		//get the results as csv file
+		if (qformat != null && qformat.equalsIgnoreCase("csv")) {
+			response.setContentType("text/csv");
+			//response.setContentType("application/csv");
+	        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
+	        
+			Writer writer = new OutputStreamWriter(response.getOutputStream());
+			CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
+			try {
+				if (transpose) {
+					List transposedTable = transpose(rs, observation, pivot, true);
+					csv.writeAll(transposedTable);
+				} else {
+					csv.writeAll(rs, true);
+				}
+				
+				csv.flush();
+				response.flushBuffer();
+				
+				rs.close();
+				
+			} catch (SQLException e) {
+				e.printStackTrace();
+			}
+			
+			return;
+		}
+		
+	}
+%>
\ No newline at end of file
Index: lib/style/skins/first/first-assessment-resultset.xsl
===================================================================
--- lib/style/skins/first/first-assessment-resultset.xsl	(revision 4871)
+++ lib/style/skins/first/first-assessment-resultset.xsl	(revision 4872)
@@ -256,7 +256,7 @@
 			</form>	
 			
 			<!-- for downloading data -->				
-			<form action="{$contextURL}/metacat" method="POST" id="assessmentForm" name="assessmentForm" >
+			<form action="{$contextURL}/style/skins/first/download.jsp" method="POST" id="assessmentForm" name="assessmentForm" >
 				<input type="hidden" name="qformat" value="first" />
 				<input type="hidden" name="sessionid">
 					<xsl:attribute name="value">
