Project

General

Profile

metacat / lib / style / skins / first / assessmentQuery.jsp @ 4271

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

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

    
13
if (docids != null && docids.length > 0) {
14

    
15
	List assessmentDocIds = Arrays.asList(docids);
16
	
17
	//the "columns" to extract from the metadata document (will be param soon)
18
	Map attributeMap = new OrderedMap();
19
	attributeMap.put("id", "//@packageId");
20
	attributeMap.put("title", "//assessment/@title");
21
	attributeMap.put("duration", "//assessment/duration");
22
	attributeMap.put("badColumn", "//does/not/exist");
23
	attributeMap.put("item", "//assessmentItems/assessmentItem/assessmentItemId");
24
	
25
	//make a title
26
	String tableTitle = "Showing data for ";
27
	String fileName = "results-";
28
	for (int i=0; i<docids.length; i++) {
29
		tableTitle += docids[i];
30
		tableTitle +=", ";
31
		
32
		fileName += docids[i];
33
	}
34
	tableTitle = tableTitle.substring(0, tableTitle.length()-2);
35
	fileName += ".csv";
36
	
37
	//AssessmentQuery.testEcogrid("edml.4.5");
38
	ResultSet rs = null;
39
	if (questionId != null) {
40
		rs = AssessmentQuery.selectResponseData(assessmentDocIds, questionId, "=", new Integer(1));
41
	}
42
	else {
43
		rs = AssessmentQuery.selectMergedResponseData(assessmentDocIds, attributeMap);
44
	}
45
	
46
	if (rs == null) {
47
		return;
48
	}
49
	
50
	//get the results as csv file
51
	if (qformat != null && qformat.equalsIgnoreCase("csv")) {
52
		response.setContentType("text/csv");
53
		//response.setContentType("application/csv");
54
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
55
        
56
		Writer writer = new OutputStreamWriter(response.getOutputStream());
57
		CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
58
		csv.writeAll(rs, true);
59
		csv.flush();
60
		
61
		response.flushBuffer();
62
		
63
		rs.close();
64
		return;
65
	}
66
	
67
	int numColumns = rs.getMetaData().getColumnCount();
68
%>
69

    
70
<!-- <h3><%=tableTitle %></h3> -->
71

    
72
<table>
73
<!-- 
74
<tr>
75
	<th colspan="<%=numColumns %>"><%=tableTitle %></th>
76
</tr>
77
-->
78
<tr>
79
<% for (int i=1; i<=numColumns; i++) { %>
80
	<th><%=rs.getMetaData().getColumnName(i) %></th>
81
<% } %>
82
</tr>
83
<%
84
while (rs.next()) {
85
	//System.out.println("rs:" + rs.getInt(1));
86
%>
87
	<tr>
88
<% for (int i=1; i<=numColumns; i++) { %>
89
		<td><%=rs.getString(i) %></td>
90
<% } %>		
91
	</tr> 
92
<% 
93
} 
94
%>
95
</table>
96
<%
97
	//clean up
98
	if (rs != null) {
99
		rs.close();
100
	}	
101
}//end if docids
102
else {
103
%>
104
No items selected
105
<%}%>