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 4034 leinfelder
	ResultSet rs = null;
30
	if (questionId != null) {
31
		rs = AssessmentQuery.selectResponseData(assessmentDocIds, questionId, "=", new Integer(1));
32
	}
33
	else {
34
		rs = AssessmentQuery.selectMergedResponseData(assessmentDocIds);
35
	}
36 3551 leinfelder
37 3563 leinfelder
	if (rs == null) {
38
		return;
39
	}
40
41 3551 leinfelder
	//get the results as csv file
42
	if (qformat != null && qformat.equalsIgnoreCase("csv")) {
43
		response.setContentType("text/csv");
44
		//response.setContentType("application/csv");
45
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
46
47
		Writer writer = new OutputStreamWriter(response.getOutputStream());
48
		CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
49
		csv.writeAll(rs, true);
50 3669 leinfelder
		csv.flush();
51 3551 leinfelder
52
		response.flushBuffer();
53
54
		rs.close();
55
		return;
56
	}
57
58 3542 leinfelder
	int numColumns = rs.getMetaData().getColumnCount();
59
%>
60
61
<!-- <h3><%=tableTitle %></h3> -->
62
63
<table>
64
<!--
65
<tr>
66
	<th colspan="<%=numColumns %>"><%=tableTitle %></th>
67
</tr>
68
-->
69
<tr>
70
<% for (int i=1; i<=numColumns; i++) { %>
71
	<th><%=rs.getMetaData().getColumnName(i) %></th>
72
<% } %>
73
</tr>
74
<%
75
while (rs.next()) {
76
	//System.out.println("rs:" + rs.getInt(1));
77
%>
78
	<tr>
79
<% for (int i=1; i<=numColumns; i++) { %>
80 3563 leinfelder
		<td><%=rs.getString(i) %></td>
81 3542 leinfelder
<% } %>
82
	</tr>
83
<%
84
}
85
%>
86
</table>
87 3551 leinfelder
<%
88
	//clean up
89
	if (rs != null) {
90
		rs.close();
91
	}
92
}//end if docids
93
else {
94
%>
95
No items selected
96
<%}%>