Project

General

Profile

« Previous | Next » 

Revision 3551

add CSV download as an option for retrieving datasets.
NOTE: depends on opencsv jar in the /first project...

View differences:

lib/style/skins/first/assessmentQuery.jsp
1
<%@page import="java.sql.ResultSet"%>
2
<%@page import="edu.ucsb.nceas.first.metacat.client.AssessmentQuery"%>
3
<%@page import="java.util.*"%>
4
<%
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.*"%><%
5 7

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

  
8 11
if (docids != null && docids.length > 0) {
9 12

  
......
11 14
	
12 15
	//make a title
13 16
	String tableTitle = "Showing data for ";
17
	String fileName = "results-";
14 18
	for (int i=0; i<docids.length; i++) {
15 19
		tableTitle += docids[i];
16 20
		tableTitle +=", ";
21
		
22
		fileName += docids[i];
17 23
	}
18 24
	tableTitle = tableTitle.substring(0, tableTitle.length()-2);
25
	fileName += ".csv";
19 26
	
20 27
	//AssessmentQuery.testEcogrid("edml.4.5");
21 28
	ResultSet rs = AssessmentQuery.selectResponseData(assessmentDocIds);
29
	
30
	//get the results as csv file
31
	if (qformat != null && qformat.equalsIgnoreCase("csv")) {
32
		response.setContentType("text/csv");
33
		//response.setContentType("application/csv");
34
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
35
        
36
		Writer writer = new OutputStreamWriter(response.getOutputStream());
37
		CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
38
		csv.writeAll(rs, true);
39
		
40
		response.flushBuffer();
41
		
42
		rs.close();
43
		return;
44
	}
45
	
22 46
	int numColumns = rs.getMetaData().getColumnCount();
23 47
%>
24 48

  
......
48 72
} 
49 73
%>
50 74
</table>
51

  
52
<%}//end if %>
75
<%
76
	//clean up
77
	if (rs != null) {
78
		rs.close();
79
	}	
80
}//end if docids
81
else {
82
%>
83
No items selected
84
<%}%>
lib/style/skins/first/first-assessment-resultset.xsl
57 57
        </script>
58 58
        <script type="text/javascript" language="Javascript">
59 59
			<![CDATA[
60
			getResponseData = function(docid, divId) {
60
			//reusable function to get the URL since I know this could change....
61
			getResponseDataURL = function() {
61 62
				var styleURL = "/knb/style/skins/first";
62 63
				styleURL += "/assessmentQuery.jsp";
64
				return styleURL;
65
			}	
66
			getResponseData = function(docid, divId) {
67
				var styleURL = getResponseDataURL();
63 68
																				
64 69
				//alert("calling ajax: " + styleURL);
65 70
				//alert("docid: " + docid);
......
79 84
				//alert("done calling response ajax");
80 85
			}
81 86
			getMergedResponseData = function(formId,divId) {
82
				var styleURL = "/knb/style/skins/first";
83
				styleURL += "/assessmentQuery.jsp";
87
				if (!areItemsSelected('docids')) {
88
					alert("Please select at least one dataset to view");
89
					return false;
90
				}
91
				var styleURL = getResponseDataURL();
84 92
																				
85 93
				//alert("calling ajax: " + styleURL);
86 94
				//alert("docid: " + docid);
......
99 107
				Element.show(divId);					
100 108
				//alert("done calling response ajax");
101 109
			}
110
			downloadMergedResponseData = function(formId) {
111
			
112
				if (!areItemsSelected('docids')) {
113
					alert("Please select at least one dataset to download");
114
					return false;
115
				}
116
				
117
				var styleURL = getResponseDataURL();
118
				
119
				var submitFormObj = document.getElementById(formId);
120
				submitFormObj.qformat.value = 'csv';
121
				submitFormObj.action = styleURL;
122
																				
123
				//alert("downloading response data from: " + submitFormObj.action);
124
				//alert("format=" + submitFormObj.qformat.value);
125
				
126
				submitFormObj.submit();
127
			}
128
			areItemsSelected = function(checkBoxName) {
129
				var checkBoxes = document.getElementsByName(checkBoxName);
130
				
131
				for (var i=0; i< checkBoxes.length; i++) {
132
					if (checkBoxes[i].checked == true) {
133
						return true;
134
					}	
135
				}
136
				//alert("please select a checkbox: " + checkBoxName);
137
				return false;
138
				
139
			}
102 140
			hideDiv = function(divId) {
103 141
				Element.hide(divId);
104 142
			}
......
130 168
					<xsl:text>None</xsl:text>
131 169
				</a>
132 170
				<br/>
133
				Actions:
171
				View:
134 172
				<a>
135
					<xsl:attribute name="href">javascript:getMergedResponseData('assessmentForm','mergedResponseData')</xsl:attribute>
136
					<xsl:text>Merge Selected</xsl:text>
173
					<xsl:attribute name="href">#</xsl:attribute>
174
					<xsl:attribute name="onClick">javascript:getMergedResponseData('assessmentForm','mergedResponseData')</xsl:attribute>
175
					<xsl:text>Selected</xsl:text>
137 176
				</a>
138 177
				/
139 178
				<a>
......
141 180
					<xsl:text>Hide</xsl:text>
142 181
				</a>
143 182
				<br/>
183
				Download Selected as:
184
				<a>
185
					<xsl:attribute name="href">#</xsl:attribute>
186
					<xsl:attribute name="onClick">javascript:downloadMergedResponseData('assessmentForm')</xsl:attribute>
187
					<xsl:text>Datafile (.csv)</xsl:text>
188
				</a>
189
				/
190
				<a>
191
					<xsl:attribute name="href">#doNothing</xsl:attribute>
192
					<xsl:text>Metadata Package (.zip)</xsl:text>
193
				</a>
194
				<br/>
144 195
				<div id="mergedResponseData"/>
145 196
				<br/>
146 197
				

Also available in: Unified diff