Project

General

Profile

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.*"%><%
7

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

    
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
	String fileName = "results-";
19
	for (int i=0; i<docids.length; i++) {
20
		tableTitle += docids[i];
21
		tableTitle +=", ";
22
		
23
		fileName += docids[i];
24
	}
25
	tableTitle = tableTitle.substring(0, tableTitle.length()-2);
26
	fileName += ".csv";
27
	
28
	//AssessmentQuery.testEcogrid("edml.4.5");
29
	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
	
37
	if (rs == null) {
38
		return;
39
	}
40
	
41
	//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
		csv.flush();
51
		
52
		response.flushBuffer();
53
		
54
		rs.close();
55
		return;
56
	}
57
	
58
	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
		<td><%=rs.getString(i) %></td>
81
<% } %>		
82
	</tr> 
83
<% 
84
} 
85
%>
86
</table>
87
<%
88
	//clean up
89
	if (rs != null) {
90
		rs.close();
91
	}	
92
}//end if docids
93
else {
94
%>
95
No items selected
96
<%}%>
(1-1/10)