Project

General

Profile

« Previous | Next » 

Revision 3551

add CSV download as an option for retrieving datasets.
NOTE: depends on opencsv jar in the /first project...

View differences:

assessmentQuery.jsp
1
<%@page import="java.sql.ResultSet"%>
2
<%@page import="edu.ucsb.nceas.first.metacat.client.AssessmentQuery"%>
3
<%@page import="java.util.*"%>
4
<%
1
<%@page 
2
import="java.sql.ResultSet"%><%@page 
3
import="edu.ucsb.nceas.first.metacat.client.AssessmentQuery"%><%@page 
4
import="au.com.bytecode.opencsv.CSVWriter"%><%@page 
5
import="java.util.*"%><%@page 
6
import="java.io.*"%><%
5 7

  
6 8
String[] docids = request.getParameterValues("docids");
9
String qformat = request.getParameter("qformat");
7 10

  
8 11
if (docids != null && docids.length > 0) {
9 12

  
......
11 14
	
12 15
	//make a title
13 16
	String tableTitle = "Showing data for ";
17
	String fileName = "results-";
14 18
	for (int i=0; i<docids.length; i++) {
15 19
		tableTitle += docids[i];
16 20
		tableTitle +=", ";
21
		
22
		fileName += docids[i];
17 23
	}
18 24
	tableTitle = tableTitle.substring(0, tableTitle.length()-2);
25
	fileName += ".csv";
19 26
	
20 27
	//AssessmentQuery.testEcogrid("edml.4.5");
21 28
	ResultSet rs = AssessmentQuery.selectResponseData(assessmentDocIds);
29
	
30
	//get the results as csv file
31
	if (qformat != null && qformat.equalsIgnoreCase("csv")) {
32
		response.setContentType("text/csv");
33
		//response.setContentType("application/csv");
34
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
35
        
36
		Writer writer = new OutputStreamWriter(response.getOutputStream());
37
		CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
38
		csv.writeAll(rs, true);
39
		
40
		response.flushBuffer();
41
		
42
		rs.close();
43
		return;
44
	}
45
	
22 46
	int numColumns = rs.getMetaData().getColumnCount();
23 47
%>
24 48

  
......
48 72
} 
49 73
%>
50 74
</table>
51

  
52
<%}//end if %>
75
<%
76
	//clean up
77
	if (rs != null) {
78
		rs.close();
79
	}	
80
}//end if docids
81
else {
82
%>
83
No items selected
84
<%}%>

Also available in: Unified diff