1
|
<%@page
|
2
|
import="java.util.Iterator"%><%@page
|
3
|
import="java.util.ArrayList"%><%@page
|
4
|
import="edu.ucsb.nceas.utilities.OrderedMap"%><%@page
|
5
|
import="java.util.List"%><%@page
|
6
|
import="java.util.Enumeration"%><%@page
|
7
|
import="java.sql.SQLException"%><%@page
|
8
|
import="org.ecoinformatics.datamanager.transpose.DataTranspose"%><%@page
|
9
|
import="au.com.bytecode.opencsv.CSVWriter"%><%@page
|
10
|
import="java.io.OutputStreamWriter"%><%@page
|
11
|
import="java.io.Writer"%><%@page
|
12
|
import="java.sql.ResultSet"%><%@page
|
13
|
import="edu.ucsb.nceas.metacat.dataquery.DataQuery"%><%@page
|
14
|
import="java.io.IOException"%><%@page
|
15
|
import="edu.ucsb.nceas.utilities.PropertyNotFoundException"%><%@page
|
16
|
import="java.util.Hashtable"%><%@ page
|
17
|
language="java" %><%
|
18
|
/**
|
19
|
*
|
20
|
* '$RCSfile$'
|
21
|
* Copyright: 2008 Regents of the University of California and the
|
22
|
* National Center for Ecological Analysis and Synthesis
|
23
|
* '$Author: leinfelder $'
|
24
|
* '$Date: 2008-08-22 16:48:56 -0700 (Fri, 22 Aug 2008) $'
|
25
|
* '$Revision: 4305 $'
|
26
|
*
|
27
|
* This program is free software; you can redistribute it and/or modify
|
28
|
* it under the terms of the GNU General Public License as published by
|
29
|
* the Free Software Foundation; either version 2 of the License, or
|
30
|
* (at your option) any later version.
|
31
|
*
|
32
|
* This program is distributed in the hope that it will be useful,
|
33
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
34
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
35
|
* GNU General Public License for more details.
|
36
|
|
37
|
* You should have received a copy of the GNU General Public License
|
38
|
* along with this program; if not, write to the Free Software
|
39
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
40
|
*/
|
41
|
%><%
|
42
|
Hashtable params = getParams(request);
|
43
|
handleDataquery(params,response,request.getSession().getId());
|
44
|
%><%!
|
45
|
private Hashtable getParams(HttpServletRequest request) {
|
46
|
Hashtable params = new Hashtable();
|
47
|
Enumeration<String> paramlist = (Enumeration<String>)request.getParameterNames();
|
48
|
while (paramlist.hasMoreElements()) {
|
49
|
|
50
|
String name = paramlist.nextElement();
|
51
|
String[] value = request.getParameterValues(name);
|
52
|
params.put(name, value);
|
53
|
}
|
54
|
|
55
|
return params;
|
56
|
}
|
57
|
%><%!
|
58
|
private List transpose(ResultSet rs, int idCol, int pivotCol, List pivotAttributes, boolean omitIdValues) throws SQLException {
|
59
|
//map keyed by id column - data
|
60
|
OrderedMap table = new OrderedMap();
|
61
|
//track all the data columns
|
62
|
OrderedMap widestRow = new OrderedMap();
|
63
|
//map keyed by the pivot column - metadata
|
64
|
OrderedMap headerRows = new OrderedMap();
|
65
|
|
66
|
int colCount = rs.getMetaData().getColumnCount();
|
67
|
String idColName = rs.getMetaData().getColumnName(idCol);
|
68
|
|
69
|
while (rs.next()) {
|
70
|
String id = rs.getString(idCol);
|
71
|
String pivotValue = rs.getString(pivotCol);
|
72
|
|
73
|
//look up the data row we are working on
|
74
|
OrderedMap row = (OrderedMap) table.get(id);
|
75
|
if (row == null) {
|
76
|
row = new OrderedMap();
|
77
|
}
|
78
|
//look up the metadata row we are working on
|
79
|
OrderedMap metadataRow = (OrderedMap) table.get(pivotValue);
|
80
|
if (metadataRow == null) {
|
81
|
metadataRow = new OrderedMap();
|
82
|
}
|
83
|
|
84
|
//get the values for this pivot
|
85
|
for (int i = 1; i <= colCount; i++) {
|
86
|
if (i != pivotCol) {
|
87
|
String colName = rs.getMetaData().getColumnName(i);
|
88
|
String value = rs.getString(i);
|
89
|
//clean up the value
|
90
|
value = value.replaceAll("\n", " ");
|
91
|
value = value.replaceAll("<html>", " ");
|
92
|
value = value.replaceAll("</html>", " ");
|
93
|
value = value.replaceAll("<head>", " ");
|
94
|
value = value.replaceAll("</head>", " ");
|
95
|
value = value.replaceAll("<body>", " ");
|
96
|
value = value.replaceAll("</body>", " ");
|
97
|
|
98
|
//do we include this column in the pivot?
|
99
|
if (pivotAttributes.contains(colName)) {
|
100
|
//annotate the column name with the pivot column value if not the id column
|
101
|
if (i != idCol) {
|
102
|
colName = pivotValue + "_" + colName;
|
103
|
}
|
104
|
row.put(colName, value);
|
105
|
}
|
106
|
else {
|
107
|
metadataRow.put(colName, value);
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
//track the data columns - the values are junk
|
112
|
widestRow.putAll(row);
|
113
|
|
114
|
//put the row back (or maybe it's the first time)
|
115
|
table.put(id, row);
|
116
|
|
117
|
//put the metadata header back
|
118
|
headerRows.put(pivotValue, metadataRow);
|
119
|
|
120
|
}
|
121
|
|
122
|
//now make it into a list
|
123
|
List retTable = new ArrayList();
|
124
|
|
125
|
//map keyed by metadata labels
|
126
|
OrderedMap metadataHeaders = new OrderedMap();
|
127
|
|
128
|
//do the data header - drives the other columns - based on widest entry
|
129
|
List header = new ArrayList(widestRow.keySet());
|
130
|
|
131
|
//do the metadata header rows (basically rotate them around)
|
132
|
Iterator headerIter = header.iterator();
|
133
|
while (headerIter.hasNext()) {
|
134
|
String column = (String) headerIter.next();
|
135
|
//get the pivotValue part of column name
|
136
|
String pivotValue = column;
|
137
|
try {
|
138
|
pivotValue = column.substring(0, column.indexOf("_"));
|
139
|
}
|
140
|
catch (Exception e) {}
|
141
|
//look up the row from the metadata - keyed by pivot value
|
142
|
OrderedMap metadataRow = (OrderedMap) headerRows.get(pivotValue);
|
143
|
if (metadataRow != null) {
|
144
|
//go through the values
|
145
|
Iterator metadataIter = metadataRow.keySet().iterator();
|
146
|
while (metadataIter.hasNext()) {
|
147
|
String key = (String) metadataIter.next();
|
148
|
String value = (String) metadataRow.get(key);
|
149
|
OrderedMap newMetadataRow = (OrderedMap) metadataHeaders.get(key);
|
150
|
if (newMetadataRow == null) {
|
151
|
newMetadataRow = new OrderedMap();
|
152
|
}
|
153
|
newMetadataRow.put(column, value);
|
154
|
metadataHeaders.put(key, newMetadataRow);
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
|
159
|
//make metadata rows as list/arrays on the reteurn table
|
160
|
Iterator metadataLabelIter = metadataHeaders.keySet().iterator();
|
161
|
while (metadataLabelIter.hasNext()) {
|
162
|
String label = (String) metadataLabelIter.next();
|
163
|
OrderedMap row = (OrderedMap) metadataHeaders.get(label);
|
164
|
List rowValues = new ArrayList(row.values());
|
165
|
rowValues.add(0, label);
|
166
|
retTable.add(rowValues.toArray(new String[0]));
|
167
|
}
|
168
|
|
169
|
//put the data header row on the table
|
170
|
retTable.add(header.toArray(new String[0]));
|
171
|
|
172
|
//now the value rows in the table
|
173
|
Iterator rowIter = table.values().iterator();
|
174
|
int rowCount = 1;
|
175
|
while (rowIter.hasNext()) {
|
176
|
OrderedMap rowMap = (OrderedMap) rowIter.next();
|
177
|
List row = new ArrayList();
|
178
|
//iterate over the widest row's columns
|
179
|
Iterator columnIter = widestRow.keySet().iterator();
|
180
|
while (columnIter.hasNext()) {
|
181
|
Object key = columnIter.next();
|
182
|
Object value = rowMap.get(key);
|
183
|
//hide the value used for Ids - just increment row
|
184
|
if (key.equals(idColName) && omitIdValues) {
|
185
|
value = String.valueOf(rowCount);
|
186
|
}
|
187
|
row.add(value);
|
188
|
}
|
189
|
rowCount++;
|
190
|
retTable.add(row.toArray(new String[0]));
|
191
|
}
|
192
|
|
193
|
return retTable;
|
194
|
}
|
195
|
%><%!
|
196
|
private void handleDataquery(
|
197
|
Hashtable<String, String[]> params,
|
198
|
HttpServletResponse response,
|
199
|
String sessionId) throws PropertyNotFoundException, IOException {
|
200
|
|
201
|
DataQuery dq = null;
|
202
|
if (sessionId != null) {
|
203
|
dq = new DataQuery(sessionId);
|
204
|
}
|
205
|
else {
|
206
|
dq = new DataQuery();
|
207
|
}
|
208
|
|
209
|
String dataqueryXML = (params.get("dataquery"))[0];
|
210
|
|
211
|
ResultSet rs = null;
|
212
|
try {
|
213
|
rs = dq.executeQuery(dataqueryXML);
|
214
|
} catch (Exception e) {
|
215
|
//probably need to do something here
|
216
|
e.printStackTrace();
|
217
|
return;
|
218
|
}
|
219
|
|
220
|
//process the result set
|
221
|
String qformat = "csv";
|
222
|
String[] temp = params.get("qformat");
|
223
|
if (temp != null && temp.length > 0) {
|
224
|
qformat = temp[0];
|
225
|
}
|
226
|
String fileName = "query-results." + qformat;
|
227
|
|
228
|
boolean transpose = false;
|
229
|
temp = params.get("transpose");
|
230
|
if (temp != null && temp.length > 0) {
|
231
|
transpose = Boolean.parseBoolean(temp[0]);
|
232
|
}
|
233
|
int observation = 0;
|
234
|
temp = params.get("observation");
|
235
|
if (temp != null && temp.length > 0) {
|
236
|
observation = Integer.parseInt(temp[0]);
|
237
|
}
|
238
|
int pivot = 0;
|
239
|
temp = params.get("pivot");
|
240
|
if (temp != null && temp.length > 0) {
|
241
|
pivot = Integer.parseInt(temp[0]);
|
242
|
}
|
243
|
|
244
|
//get the results as csv file
|
245
|
if (qformat != null && qformat.equalsIgnoreCase("csv")) {
|
246
|
response.setContentType("text/csv");
|
247
|
//response.setContentType("application/csv");
|
248
|
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
249
|
|
250
|
Writer writer = new OutputStreamWriter(response.getOutputStream());
|
251
|
//CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
|
252
|
CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER);
|
253
|
try {
|
254
|
if (transpose) {
|
255
|
List pivotCols = new ArrayList();
|
256
|
pivotCols.add("studentid");
|
257
|
pivotCols.add("score");
|
258
|
pivotCols.add("response");
|
259
|
List transposedTable = transpose(rs, observation, pivot, pivotCols, true);
|
260
|
csv.writeAll(transposedTable);
|
261
|
} else {
|
262
|
csv.writeAll(rs, true);
|
263
|
}
|
264
|
|
265
|
csv.flush();
|
266
|
response.flushBuffer();
|
267
|
|
268
|
rs.close();
|
269
|
|
270
|
} catch (SQLException e) {
|
271
|
e.printStackTrace();
|
272
|
}
|
273
|
|
274
|
return;
|
275
|
}
|
276
|
|
277
|
}
|
278
|
%>
|