Project

General

Profile

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="edu.ucsb.nceas.metacat.util.SystemUtil"%><%@page 
17
import="java.util.Hashtable"%><%@ page 
18
language="java" %><%
19
/**
20
 * 
21
 * '$RCSfile$'
22
 * Copyright: 2008 Regents of the University of California and the
23
 *             National Center for Ecological Analysis and Synthesis
24
 *    '$Author: leinfelder $'
25
 *      '$Date: 2008-08-22 16:48:56 -0700 (Fri, 22 Aug 2008) $'
26
 * '$Revision: 4305 $'
27
 * 
28
 * This program is free software; you can redistribute it and/or modify
29
 * it under the terms of the GNU General Public License as published by
30
 * the Free Software Foundation; either version 2 of the License, or
31
 * (at your option) any later version.
32
 * 
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
     
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
41
 */  
42
%><%
43
Hashtable params = getParams(request);
44
handleDataquery(params,response,request.getSession().getId());
45
%><%!
46
	private Hashtable getParams(HttpServletRequest request) {
47
		Hashtable params = new Hashtable();
48
		Enumeration<String> paramlist = (Enumeration<String>)request.getParameterNames();
49
		while (paramlist.hasMoreElements()) {
50
	
51
			String name = paramlist.nextElement();
52
			String[] value = request.getParameterValues(name);
53
			params.put(name, value);
54
		}
55
	
56
		return params;
57
	}
58
%><%!
59
	private List processResultsSet(ResultSet rs, int omitColumn, String omitColumnLabel) throws SQLException {
60
		List retTable = new ArrayList();
61
		int colCount = rs.getMetaData().getColumnCount();
62
		List columnHeaders = new ArrayList();
63
		int recordCount = 1;
64
		OrderedMap uniqueIds = new OrderedMap();
65
		
66
		while (rs.next()) {
67
			List row = new ArrayList();
68
			//get the values for this row
69
			for (int i = 1; i <= colCount; i++) {
70

    
71
				String colName = rs.getMetaData().getColumnName(i);
72
				String value = rs.getString(i);
73
					
74
				//clean up the value
75
				value = cleanUp(value);
76
				
77
				//hide the ids
78
				if (i == omitColumn) {
79
					String lookupValue = (String) uniqueIds.get(value);
80
					if (lookupValue == null) {
81
						lookupValue = recordCount + "";
82
					}
83
					uniqueIds.put(value, lookupValue);
84
					value = lookupValue;
85
					colName = omitColumnLabel;
86
				}
87
				if (recordCount == 1) {
88
					columnHeaders.add(colName);
89
				}
90
				
91
				row.add(value);
92
			}
93
			retTable.add(row.toArray(new String[0]));
94
			recordCount++;
95
		}
96
		//make sure there are no "?column?" headers
97
		for (int i = 0; i < columnHeaders.size(); i++) {
98
			String columnName = (String) columnHeaders.get(i);
99
			if (columnName.equals("?column?")) {
100
				String previousColumn = (String) columnHeaders.get(i-1);
101
				String[] previousParts = previousColumn.split("_");
102
				int previousCount = Integer.parseInt(previousParts[1]);
103
				columnName = previousParts[0]  + "_"+ (previousCount + 1);
104
				columnHeaders.set(i, columnName);
105
			}
106
		}
107
		retTable.add(0, columnHeaders.toArray(new String[0]));
108
		return retTable;
109
	}
110
%><%!
111
	private List transpose(ResultSet rs, int idCol, int pivotCol, List pivotAttributes, boolean omitIdValues) throws SQLException {
112
		//map keyed by id column - data
113
		OrderedMap table = new OrderedMap();
114
		//track all the data columns
115
		OrderedMap widestRow = new OrderedMap();
116
		//map keyed by the pivot column - metadata
117
		OrderedMap headerRows = new OrderedMap();
118
		
119
		//vocab columns
120
		String vocabNameCol = "qmetadatavocabulary";
121
		String vocabValueCol = "qmetadatavalue";
122
		//maps for the vocab lists
123
		OrderedMap vocabNames = new OrderedMap();
124
		OrderedMap vocabValues = new OrderedMap();
125
		//all vocab names/values
126
		List allVocabNames = new ArrayList();
127
		List allVocabValues = new ArrayList();
128
		boolean twoColumnMetadata = false;
129
		
130
		int colCount = rs.getMetaData().getColumnCount();
131
		String idColName = rs.getMetaData().getColumnName(idCol);
132
		
133
		while (rs.next()) {
134
			String id = rs.getString(idCol);
135
			String pivotValue = rs.getString(pivotCol);
136
			
137
			//look up the data row we are working on
138
			OrderedMap row = (OrderedMap) table.get(id);
139
			if (row == null) {
140
				row = new OrderedMap();
141
			}
142
			//look up the metadata row we are working on
143
			OrderedMap metadataRow = (OrderedMap) table.get(pivotValue);
144
			if (metadataRow == null) {
145
				metadataRow = new OrderedMap();
146
			}
147
			
148
			//get the values for this pivot
149
			for (int i = 1; i <= colCount; i++) {
150
				if (i != pivotCol) {
151
					String colName = rs.getMetaData().getColumnName(i);
152
					String value = rs.getString(i);
153
					
154
					//clean up the value
155
					value = cleanUp(value);
156
					
157
					//do we include this column in the pivot?
158
					if (pivotAttributes.contains(colName)) {
159
						//annotate the column name with the pivot column value if not the id column
160
						if (i != idCol) {
161
							colName = pivotValue + "_" + colName;
162
						}
163
						row.put(colName, value);
164
					}
165
					else {
166
						if (colName.startsWith(vocabNameCol) || colName.startsWith(vocabValueCol)) {
167
							//don't add it to the normal metadata
168
						}
169
						else {
170
							metadataRow.put(colName, value);
171
						}
172
					}
173
					//names
174
					if (colName.startsWith(vocabNameCol)) {
175
						List list = (List) vocabNames.get(pivotValue);
176
						if (list == null) {
177
							list = new ArrayList();
178
						}
179
						list.add(value);
180
						vocabNames.put(pivotValue, list);
181
						allVocabNames.add(value);
182
					}
183
					//values
184
					if (colName.startsWith(vocabValueCol)) {
185
						List list = (List) vocabValues.get(pivotValue);
186
						if (list == null) {
187
							list = new ArrayList();
188
						}
189
						list.add(value);
190
						vocabValues.put(pivotValue, list);
191
						allVocabValues.add(value);
192
					}
193
				}
194
			}
195
			//track the data columns - the values are junk
196
			widestRow.putAll(row);
197
			
198
			//put the row back (or maybe it's the first time)
199
			table.put(id, row);
200
			
201
			//put the metadata header back
202
			headerRows.put(pivotValue, metadataRow);
203
			
204
		}
205
		
206
		/** Construct the table structure for returning **/
207
		
208
		//now make it into a list
209
		List retTable = new ArrayList();
210
		
211
		//map keyed by metadata labels
212
		OrderedMap metadataHeaders = new OrderedMap();
213
		
214
		//do the data header - drives the other columns - based on widest entry
215
		List header = new ArrayList(widestRow.keySet());
216
		
217
		//do the metadata header rows (basically rotate them around)
218
		Iterator headerIter = header.iterator();
219
		String lastValue = "";
220
		while (headerIter.hasNext()) {
221
			String column = (String) headerIter.next();
222
			//get the pivotValue part of column name
223
			String pivotValue = column;
224
			try {
225
				pivotValue = column.substring(0, column.indexOf("_"));
226
			}
227
			catch (Exception e) {}
228
			//look up the row from the metadata - keyed by pivot value
229
			OrderedMap metadataRow = (OrderedMap) headerRows.get(pivotValue);
230
			if (metadataRow != null) {
231
				//go through the values
232
				Iterator metadataIter = metadataRow.keySet().iterator();
233
				while (metadataIter.hasNext()) {
234
					String key = (String) metadataIter.next();
235
					String value = (String) metadataRow.get(key);
236
					OrderedMap newMetadataRow = (OrderedMap) metadataHeaders.get(key);
237
					if (newMetadataRow == null) {
238
						newMetadataRow = new OrderedMap();
239
					}
240
					//if it's the same as the last one, just use null value
241
					if (lastValue.equals(pivotValue)) {
242
						value = null;
243
					} 
244
					newMetadataRow.put(column, value);
245
					metadataHeaders.put(key, newMetadataRow);
246
				}
247
			}
248
			
249
			lastValue = pivotValue;
250

    
251
		}
252
		
253
		//make metadata rows as list/arrays on the reteurn table
254
		Iterator metadataLabelIter = metadataHeaders.keySet().iterator();
255
		while (metadataLabelIter.hasNext()) {
256
			String label = (String) metadataLabelIter.next();
257
			OrderedMap row = (OrderedMap) metadataHeaders.get(label);
258
			List rowValues = new ArrayList(row.values());
259
			rowValues.add(0, label);
260
			if (twoColumnMetadata) {
261
				//add extra column
262
				rowValues.add(1, null);
263
			}
264
			retTable.add(rowValues.toArray(new String[0]));
265
		}
266
		
267
		//create the special vocab matrix rows
268
		List vocabTable = new ArrayList();
269
		List uniqueVocabs = new ArrayList();
270
		for (int i = 0; i < allVocabNames.size(); i++) {
271
			List vocabRow = new ArrayList();
272
			String vocabName = (String) allVocabNames.get(i);
273
			String vocabValue = (String) allVocabValues.get(i);
274
			String key = vocabName + "/" + vocabValue;
275
			//check if we've processed this already, skip if so
276
			if (uniqueVocabs.contains(key)) {
277
				continue;
278
			}
279
			uniqueVocabs.add(key);			
280
			if (twoColumnMetadata) {
281
				vocabRow.add(vocabName);
282
				vocabRow.add(vocabValue);
283
			}
284
			else {
285
				vocabRow.add(key);
286
			}
287
			//go through the questions now, again
288
			String lastPivotValue = "";
289
			headerIter = header.iterator();
290
			while (headerIter.hasNext()) {
291
				String column = (String) headerIter.next();
292
				//get the pivotValue part of column name if it exists
293
				String pivotValue = null;
294
				try {
295
					pivotValue = column.substring(0, column.indexOf("_"));
296
				}
297
				catch (Exception e) {}
298
				if (pivotValue == null) {
299
					continue;
300
				}
301
				//check to not duplicate values
302
				if (pivotValue.equals(lastPivotValue)) {
303
					vocabRow.add(null);
304
				}
305
				else {
306
					//check to see if this question has that keyword
307
					List names = (List) vocabNames.get(pivotValue);
308
					List values = (List) vocabValues.get(pivotValue);
309
					String containsVocabItem = "false";
310
					if (names != null) {
311
						int vocabNameIndex = names.indexOf(vocabName); 
312
						int vocabValueIndex = values.indexOf(vocabValue);
313
						// contains the vocab and the value _somewhere_
314
						if (vocabNameIndex > -1 && vocabValueIndex > -1) {
315
							containsVocabItem = "true";
316
						}
317
					}
318
					vocabRow.add(containsVocabItem);
319
				}
320
				lastPivotValue = pivotValue;
321
			}
322
			//put the row on
323
			vocabTable.add(vocabRow.toArray(new String[0]));
324
		}
325
		
326
		//put the vocab matrix on the table
327
		retTable.addAll(vocabTable);
328
		
329
		if (twoColumnMetadata) {
330
			//add column to data header row
331
			header.add(1, null);
332
		}
333
		
334
		//replace the "studentId" label
335
		int temp = header.indexOf("studentid");
336
		if (header.remove(temp) != null) {
337
			header.add(temp, "recordNum");
338
		}
339
		
340
		//put the data header row on the table
341
		retTable.add(header.toArray(new String[0]));
342
		
343
		//now the value rows in the table
344
		Iterator rowIter = table.values().iterator();
345
		int rowCount = 1;
346
		while (rowIter.hasNext()) {
347
			OrderedMap rowMap = (OrderedMap) rowIter.next();
348
			List row = new ArrayList();
349
			//iterate over the widest row's columns
350
			Iterator columnIter = widestRow.keySet().iterator();
351
			while (columnIter.hasNext()) {
352
				Object key = columnIter.next();
353
				Object value = rowMap.get(key);
354
				//hide the value used for Ids - just increment row
355
				if (key.equals(idColName) && omitIdValues) {
356
					value = String.valueOf(rowCount);
357
				}
358
				row.add(value);
359
			}
360
			rowCount++;
361
			if (twoColumnMetadata) {
362
				//add extra column
363
				row.add(1, null);
364
			}
365
			retTable.add(row.toArray(new String[0]));
366
		}
367
		
368
		return retTable;
369
	}
370
%><%!
371
	private String cleanUp(String value) {
372
		if (value != null) {
373
			value = value.replaceAll("\n", " ");
374
			value = value.replaceAll("\\s+", " ");
375
			value = value.replaceAll("<html>", " ");
376
			value = value.replaceAll("</html>", " ");
377
			value = value.replaceAll("<head>", " ");
378
			value = value.replaceAll("</head>", " ");
379
			value = value.replaceAll("<body>", " ");
380
			value = value.replaceAll("</body>", " ");
381
			//translate any ecogrid urls
382
			value = convertEcogridURL(value);
383
		}
384
		return value;
385
	}
386
%><%!
387
private String convertEcogridURL(String value) {
388
	if (value != null) {
389
		String prefix = "ecogrid://knb/";
390
		if (value.startsWith(prefix)) {
391
			//String docid = value.substring(prefix.length(), value.length());
392
			// TODO make URL
393
			String contextURL = "";
394
			try {
395
				contextURL = SystemUtil.getContextURL();
396
			}
397
			catch (PropertyNotFoundException pnfe) {
398
				//do nothing
399
			}
400
			value = value.replaceFirst(prefix, contextURL + "/metacat?action=read&docid=");
401
		}
402
	}
403
	return value;
404
}
405
%><%!
406
	private void handleDataquery(
407
			Hashtable<String, String[]> params,
408
	        HttpServletResponse response,
409
	        String sessionId) throws PropertyNotFoundException, IOException {
410
		
411
		DataQuery dq = null;
412
		if (sessionId != null) {
413
			dq = new DataQuery(sessionId);
414
		}
415
		else {
416
			dq = new DataQuery();
417
		}
418
		
419
		String dataqueryXML = (params.get("dataquery"))[0];
420
	
421
		ResultSet rs = null;
422
		try {
423
			rs = dq.executeQuery(dataqueryXML);
424
		} catch (Exception e) {
425
			//probably need to do something here
426
			e.printStackTrace();
427
			return;
428
		}
429
		
430
		//process the result set
431
		String qformat = "csv";
432
		String[] temp = params.get("qformat");
433
		if (temp != null && temp.length > 0) {
434
			qformat = temp[0];
435
		}
436
		String fileName = "query-results." + qformat;
437
		
438
		boolean transpose = false;
439
		temp = params.get("transpose");
440
		if (temp != null && temp.length > 0) {
441
			transpose = Boolean.parseBoolean(temp[0]);
442
		}
443
		int observation = 0;
444
		temp = params.get("observation");
445
		if (temp != null && temp.length > 0) {
446
			observation = Integer.parseInt(temp[0]);
447
		}
448
		int pivot = 0;
449
		temp = params.get("pivot");
450
		if (temp != null && temp.length > 0) {
451
			pivot = Integer.parseInt(temp[0]);
452
		}
453
		String[] pivotColumns = null;
454
		temp = params.get("pivotColumns");
455
		if (temp != null && temp.length > 0) {
456
			pivotColumns = temp;
457
		}
458
		
459
		//get the results as csv file
460
		if (qformat != null && qformat.equalsIgnoreCase("csv")) {
461
			response.setContentType("text/csv");
462
			//response.setContentType("application/csv");
463
	        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
464
	        
465
			Writer writer = new OutputStreamWriter(response.getOutputStream());
466
			//CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
467
			CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER);
468
			try {
469
				if (transpose) {
470
					List pivotCols = new ArrayList();
471
					if (pivotColumns != null) {
472
						for (int i = 0; i < pivotColumns.length; i++) {
473
							pivotCols.add(pivotColumns[i]);
474
						}
475
					} else {
476
						pivotCols.add("studentid");
477
						pivotCols.add("score");
478
						pivotCols.add("response");
479
						pivotCols.add("responsefile");
480
					}
481
					List transposedTable = transpose(rs, observation, pivot, pivotCols, true);
482
					csv.writeAll(transposedTable);
483
				} else {
484
					List processedTable = processResultsSet(rs, 3, "recordNum");
485
					csv.writeAll(processedTable);
486
				}
487
				
488
				csv.flush();
489
				response.flushBuffer();
490
				
491
				rs.close();
492
				
493
			} catch (SQLException e) {
494
				e.printStackTrace();
495
			}
496
			
497
			return;
498
		}
499
		
500
	}
501
%>
(4-4/22)