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 = AssessmentQuery.selectResponseData(assessmentDocIds, questionId, "=", new Integer(1));
30
	
31
	if (rs == null) {
32
		return;
33
	}
34
	
35
	//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
		csv.flush();
45
		
46
		response.flushBuffer();
47
		
48
		rs.close();
49
		return;
50
	}
51
	
52
	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
		<td><%=rs.getString(i) %></td>
75
<% } %>		
76
	</tr> 
77
<% 
78
} 
79
%>
80
</table>
81
<%
82
	//clean up
83
	if (rs != null) {
84
		rs.close();
85
	}	
86
}//end if docids
87
else {
88
%>
89
No items selected
90
<%}%>
(1-1/10)