Commit bff07d26 authored by Matija Obreza's avatar Matija Obreza

Handle BINARY(16) as UUID in Downloads

parent e424f399
package org.genesys2.server.service.impl;
import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.genesys2.util.NumberUtils;
import au.com.bytecode.opencsv.ResultSetHelperService;
/**
* This class extends {@link ResultSetHelperService} by adding support for UUID.
* Copied straigth from superclass.
*/
public class CSVResultSetHelper extends ResultSetHelperService {
public static final int CLOBBUFFERSIZE = 2048;
// note: we want to maintain compatibility with Java 5 VM's
// These types don't exist in Java 5
private static final int NVARCHAR = -9;
private static final int NCHAR = -15;
private static final int LONGNVARCHAR = -16;
private static final int NCLOB = 2011;
/// Copied straight from superclass
@Override
public String[] getColumnValues(ResultSet rs) throws SQLException, IOException {
List<String> values = new ArrayList<String>();
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 0; i < metadata.getColumnCount(); i++) {
values.add(getColumnValue(rs, metadata.getColumnType(i + 1), i + 1));
}
String[] valueArray = new String[values.size()];
return values.toArray(valueArray);
}
private String handleObject(Object obj) {
return obj == null ? "" : String.valueOf(obj);
}
private String handleBigDecimal(BigDecimal decimal) {
return decimal == null ? "" : decimal.toString();
}
private String handleLong(ResultSet rs, int columnIndex) throws SQLException {
long lv = rs.getLong(columnIndex);
return rs.wasNull() ? "" : Long.toString(lv);
}
private String handleInteger(ResultSet rs, int columnIndex) throws SQLException {
int i = rs.getInt(columnIndex);
return rs.wasNull() ? "" : Integer.toString(i);
}
private String handleDate(ResultSet rs, int columnIndex) throws SQLException {
java.sql.Date date = rs.getDate(columnIndex);
String value = null;
if (date != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
value = dateFormat.format(date);
}
return value;
}
private String handleBinary(ResultSet rs, int colIndex) throws SQLException {
byte[] bs = rs.getBytes(colIndex);
if (bs == null) {
return null;
}
if (bs.length == 16) {
// UUID?
UUID uuid = NumberUtils.toUUID(bs);
return uuid.toString();
} else {
// TODO FIXME What to do with other binary?
return "";
}
}
private String handleTime(Time time) {
return time == null ? null : time.toString();
}
private String handleTimestamp(Timestamp timestamp) {
SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
return timestamp == null ? null : timeFormat.format(timestamp);
}
private String getColumnValue(ResultSet rs, int colType, int colIndex) throws SQLException, IOException {
String value = "";
switch (colType) {
case Types.BIT:
case Types.JAVA_OBJECT:
value = handleObject(rs.getObject(colIndex));
break;
case Types.BOOLEAN:
boolean b = rs.getBoolean(colIndex);
value = Boolean.valueOf(b).toString();
break;
case NCLOB: // todo : use rs.getNClob
case Types.CLOB:
Clob c = rs.getClob(colIndex);
if (c != null) {
value = read(c);
}
break;
case Types.BIGINT:
value = handleLong(rs, colIndex);
break;
case Types.DECIMAL:
case Types.DOUBLE:
case Types.FLOAT:
case Types.REAL:
case Types.NUMERIC:
value = handleBigDecimal(rs.getBigDecimal(colIndex));
break;
case Types.INTEGER:
case Types.TINYINT:
case Types.SMALLINT:
value = handleInteger(rs, colIndex);
break;
case Types.DATE:
value = handleDate(rs, colIndex);
break;
case Types.TIME:
value = handleTime(rs.getTime(colIndex));
break;
case Types.TIMESTAMP:
value = handleTimestamp(rs.getTimestamp(colIndex));
break;
case NVARCHAR: // todo : use rs.getNString
case NCHAR: // todo : use rs.getNString
case LONGNVARCHAR: // todo : use rs.getNString
case Types.LONGVARCHAR:
case Types.VARCHAR:
case Types.CHAR:
value = rs.getString(colIndex);
break;
case Types.BINARY:
value = handleBinary(rs, colIndex);
break;
default:
// System.err.println("SQLType not handled " + colType);
value = "";
}
if (value == null) {
value = "";
}
return value;
}
private static String read(Clob c) throws SQLException, IOException {
StringBuilder sb = new StringBuilder((int) c.length());
Reader r = c.getCharacterStream();
char[] cbuf = new char[CLOBBUFFERSIZE];
int n;
while ((n = r.read(cbuf, 0, cbuf.length)) != -1) {
sb.append(cbuf, 0, n);
}
return sb.toString();
}
}
......@@ -1209,7 +1209,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
csv.writeNext(new String[] { "genesysId", "uuid", "instCode", "acceNumb", "genus", "species", "fullTaxa", "orgCty", "acqSrc", "acqDate", "mlsStat",
"available", "historic", "storage", "sampStat", "duplSite", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate" });
final ResultSetHelper csvResultsetHelper = new ResultSetHelperService();
final ResultSetHelper csvResultsetHelper = new CSVResultSetHelper();
// Write accession information
genesysLowlevelRepository.listAccessions(filters, new RowCallbackHandler() {
......
......@@ -16,6 +16,7 @@
package org.genesys2.util;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -96,7 +97,21 @@ public abstract class NumberUtils {
}
v += d;
}
return v;
}
/**
* UUID from byte[]
*/
public static UUID toUUID(byte[] data) {
long msb = 0;
long lsb = 0;
assert data.length == 16 : "data must be 16 bytes in length";
for (int i = 0; i < 8; i++)
msb = (msb << 8) | (data[i] & 0xff);
for (int i = 8; i < 16; i++)
lsb = (lsb << 8) | (data[i] & 0xff);
return new UUID(msb, lsb);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment