Project

General

Profile

1 4872 leinfelder
<%@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 4874 leinfelder
	private List transpose(ResultSet rs, int idCol, int pivotCol, List pivotAttributes, boolean omitIdValues) throws SQLException {
59 4875 leinfelder
		//map keyed by id column - data
60 4872 leinfelder
		OrderedMap table = new OrderedMap();
61 4875 leinfelder
		//track all the data columns
62 4872 leinfelder
		OrderedMap widestRow = new OrderedMap();
63 4875 leinfelder
		//map keyed by the pivot column - metadata
64
		OrderedMap headerRows = new OrderedMap();
65
66 4872 leinfelder
		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 4875 leinfelder
			//look up the data row we are working on
74 4872 leinfelder
			OrderedMap row = (OrderedMap) table.get(id);
75
			if (row == null) {
76
				row = new OrderedMap();
77
			}
78 4875 leinfelder
			//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 4872 leinfelder
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 4875 leinfelder
					String value = rs.getString(i);
89 4876 leinfelder
					//clean up the value
90 4878 leinfelder
					if (value != null) {
91
						value = value.replaceAll("\n", " ");
92
						value = value.replaceAll("<html>", " ");
93
						value = value.replaceAll("</html>", " ");
94
						value = value.replaceAll("<head>", " ");
95
						value = value.replaceAll("</head>", " ");
96
						value = value.replaceAll("<body>", " ");
97
						value = value.replaceAll("</body>", " ");
98
					}
99 4874 leinfelder
					//do we include this column in the pivot?
100
					if (pivotAttributes.contains(colName)) {
101
						//annotate the column name with the pivot column value if not the id column
102
						if (i != idCol) {
103
							colName = pivotValue + "_" + colName;
104
						}
105
						row.put(colName, value);
106 4872 leinfelder
					}
107 4875 leinfelder
					else {
108
						metadataRow.put(colName, value);
109
					}
110 4872 leinfelder
				}
111
			}
112 4875 leinfelder
			//track the data columns - the values are junk
113 4872 leinfelder
			widestRow.putAll(row);
114
115
			//put the row back (or maybe it's the first time)
116 4875 leinfelder
			table.put(id, row);
117
118
			//put the metadata header back
119
			headerRows.put(pivotValue, metadataRow);
120
121 4872 leinfelder
		}
122
123
		//now make it into a list
124
		List retTable = new ArrayList();
125
126 4875 leinfelder
		//map keyed by metadata labels
127
		OrderedMap metadataHeaders = new OrderedMap();
128
129
		//do the data header - drives the other columns - based on widest entry
130 4872 leinfelder
		List header = new ArrayList(widestRow.keySet());
131 4875 leinfelder
132
		//do the metadata header rows (basically rotate them around)
133
		Iterator headerIter = header.iterator();
134
		while (headerIter.hasNext()) {
135
			String column = (String) headerIter.next();
136
			//get the pivotValue part of column name
137
			String pivotValue = column;
138
			try {
139
				pivotValue = column.substring(0, column.indexOf("_"));
140
			}
141
			catch (Exception e) {}
142
			//look up the row from the metadata - keyed by pivot value
143
			OrderedMap metadataRow = (OrderedMap) headerRows.get(pivotValue);
144
			if (metadataRow != null) {
145
				//go through the values
146
				Iterator metadataIter = metadataRow.keySet().iterator();
147
				while (metadataIter.hasNext()) {
148
					String key = (String) metadataIter.next();
149
					String value = (String) metadataRow.get(key);
150
					OrderedMap newMetadataRow = (OrderedMap) metadataHeaders.get(key);
151
					if (newMetadataRow == null) {
152
						newMetadataRow = new OrderedMap();
153
					}
154
					newMetadataRow.put(column, value);
155
					metadataHeaders.put(key, newMetadataRow);
156
				}
157
			}
158
		}
159
160
		//make metadata rows as list/arrays on the reteurn table
161
		Iterator metadataLabelIter = metadataHeaders.keySet().iterator();
162
		while (metadataLabelIter.hasNext()) {
163
			String label = (String) metadataLabelIter.next();
164
			OrderedMap row = (OrderedMap) metadataHeaders.get(label);
165
			List rowValues = new ArrayList(row.values());
166
			rowValues.add(0, label);
167
			retTable.add(rowValues.toArray(new String[0]));
168
		}
169
170
		//put the data header row on the table
171 4872 leinfelder
		retTable.add(header.toArray(new String[0]));
172
173 4875 leinfelder
		//now the value rows in the table
174 4872 leinfelder
		Iterator rowIter = table.values().iterator();
175
		int rowCount = 1;
176
		while (rowIter.hasNext()) {
177
			OrderedMap rowMap = (OrderedMap) rowIter.next();
178
			List row = new ArrayList();
179
			//iterate over the widest row's columns
180
			Iterator columnIter = widestRow.keySet().iterator();
181
			while (columnIter.hasNext()) {
182
				Object key = columnIter.next();
183
				Object value = rowMap.get(key);
184
				//hide the value used for Ids - just increment row
185
				if (key.equals(idColName) && omitIdValues) {
186
					value = String.valueOf(rowCount);
187
				}
188
				row.add(value);
189
			}
190
			rowCount++;
191
			retTable.add(row.toArray(new String[0]));
192
		}
193
194
		return retTable;
195
	}
196
%><%!
197
	private void handleDataquery(
198
			Hashtable<String, String[]> params,
199
	        HttpServletResponse response,
200
	        String sessionId) throws PropertyNotFoundException, IOException {
201
202
		DataQuery dq = null;
203
		if (sessionId != null) {
204
			dq = new DataQuery(sessionId);
205
		}
206
		else {
207
			dq = new DataQuery();
208
		}
209
210
		String dataqueryXML = (params.get("dataquery"))[0];
211
212
		ResultSet rs = null;
213
		try {
214
			rs = dq.executeQuery(dataqueryXML);
215
		} catch (Exception e) {
216
			//probably need to do something here
217
			e.printStackTrace();
218
			return;
219
		}
220
221
		//process the result set
222
		String qformat = "csv";
223
		String[] temp = params.get("qformat");
224
		if (temp != null && temp.length > 0) {
225
			qformat = temp[0];
226
		}
227
		String fileName = "query-results." + qformat;
228
229
		boolean transpose = false;
230
		temp = params.get("transpose");
231
		if (temp != null && temp.length > 0) {
232
			transpose = Boolean.parseBoolean(temp[0]);
233
		}
234
		int observation = 0;
235
		temp = params.get("observation");
236
		if (temp != null && temp.length > 0) {
237
			observation = Integer.parseInt(temp[0]);
238
		}
239
		int pivot = 0;
240
		temp = params.get("pivot");
241
		if (temp != null && temp.length > 0) {
242
			pivot = Integer.parseInt(temp[0]);
243
		}
244
245
		//get the results as csv file
246
		if (qformat != null && qformat.equalsIgnoreCase("csv")) {
247
			response.setContentType("text/csv");
248
			//response.setContentType("application/csv");
249
	        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
250
251
			Writer writer = new OutputStreamWriter(response.getOutputStream());
252 4876 leinfelder
			//CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
253
			CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER);
254 4872 leinfelder
			try {
255
				if (transpose) {
256 4874 leinfelder
					List pivotCols = new ArrayList();
257
					pivotCols.add("studentid");
258
					pivotCols.add("score");
259
					pivotCols.add("response");
260
					List transposedTable = transpose(rs, observation, pivot, pivotCols, true);
261 4872 leinfelder
					csv.writeAll(transposedTable);
262
				} else {
263
					csv.writeAll(rs, true);
264
				}
265
266
				csv.flush();
267
				response.flushBuffer();
268
269
				rs.close();
270
271
			} catch (SQLException e) {
272
				e.printStackTrace();
273
			}
274
275
			return;
276
		}
277
278
	}
279
%>