Project

General

Profile

1 3551 leinfelder
<%@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.*"%><%
7 3542 leinfelder
8
String[] docids = request.getParameterValues("docids");
9 3669 leinfelder
String questionId = request.getParameter("questionId");
10 3551 leinfelder
String qformat = request.getParameter("qformat");
11 3542 leinfelder
12
if (docids != null && docids.length > 0) {
13
14
	List assessmentDocIds = Arrays.asList(docids);
15
16
	//make a title
17
	String tableTitle = "Showing data for ";
18 3551 leinfelder
	String fileName = "results-";
19 3542 leinfelder
	for (int i=0; i<docids.length; i++) {
20
		tableTitle += docids[i];
21
		tableTitle +=", ";
22 3551 leinfelder
23
		fileName += docids[i];
24 3542 leinfelder
	}
25
	tableTitle = tableTitle.substring(0, tableTitle.length()-2);
26 3551 leinfelder
	fileName += ".csv";
27 3542 leinfelder
28
	//AssessmentQuery.testEcogrid("edml.4.5");
29 3928 leinfelder
	ResultSet rs = AssessmentQuery.selectResponseData(assessmentDocIds, questionId, "=", new Integer(0));
30 3551 leinfelder
31 3563 leinfelder
	if (rs == null) {
32
		return;
33
	}
34
35 3551 leinfelder
	//get the results as csv file
36
	if (qformat != null && qformat.equalsIgnoreCase("csv")) {
37
		response.setContentType("text/csv");
38
		//response.setContentType("application/csv");
39
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
40
41
		Writer writer = new OutputStreamWriter(response.getOutputStream());
42
		CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
43
		csv.writeAll(rs, true);
44 3669 leinfelder
		csv.flush();
45 3551 leinfelder
46
		response.flushBuffer();
47
48
		rs.close();
49
		return;
50
	}
51
52 3542 leinfelder
	int numColumns = rs.getMetaData().getColumnCount();
53
%>
54
55
<!-- <h3><%=tableTitle %></h3> -->
56
57
<table>
58
<!--
59
<tr>
60
	<th colspan="<%=numColumns %>"><%=tableTitle %></th>
61
</tr>
62
-->
63
<tr>
64
<% for (int i=1; i<=numColumns; i++) { %>
65
	<th><%=rs.getMetaData().getColumnName(i) %></th>
66
<% } %>
67
</tr>
68
<%
69
while (rs.next()) {
70
	//System.out.println("rs:" + rs.getInt(1));
71
%>
72
	<tr>
73
<% for (int i=1; i<=numColumns; i++) { %>
74 3563 leinfelder
		<td><%=rs.getString(i) %></td>
75 3542 leinfelder
<% } %>
76
	</tr>
77
<%
78
}
79
%>
80
</table>
81 3551 leinfelder
<%
82
	//clean up
83
	if (rs != null) {
84
		rs.close();
85
	}
86
}//end if docids
87
else {
88
%>
89
No items selected
90
<%}%>