@Override
protected ExchangeDocument insert(final ExchangeDocument document) {
ArgumentChecker.notNull(document.getExchange(), "document.exchange");
ArgumentChecker.notNull(document.getName(), "document.name");
final ManageableExchange exchange = document.getExchange();
final long docId = nextId("exg_exchange_seq");
final long docOid = (document.getUniqueId() != null ? extractOid(document.getUniqueId()) : docId);
// set the uniqueId (needs to go in Fudge message)
final UniqueId uniqueId = createUniqueId(docOid, docId);
exchange.setUniqueId(uniqueId);
document.setUniqueId(uniqueId);
// the arguments for inserting into the exchange table
FudgeMsgEnvelope env = FUDGE_CONTEXT.toFudgeMsg(exchange);
byte[] bytes = FUDGE_CONTEXT.toByteArray(env.getMessage());
final DbMapSqlParameterSource docArgs = new DbMapSqlParameterSource()
.addValue("doc_id", docId)
.addValue("doc_oid", docOid)
.addTimestamp("ver_from_instant", document.getVersionFromInstant())
.addTimestampNullFuture("ver_to_instant", document.getVersionToInstant())
.addTimestamp("corr_from_instant", document.getCorrectionFromInstant())
.addTimestampNullFuture("corr_to_instant", document.getCorrectionToInstant())
.addValue("name", document.getName())
.addValue("time_zone", exchange.getTimeZone() != null ? exchange.getTimeZone().getId() : null, Types.VARCHAR)
.addValue("detail", new SqlLobValue(bytes, getDialect().getLobHandler()), Types.BLOB);
// the arguments for inserting into the idkey tables
final List<DbMapSqlParameterSource> assocList = new ArrayList<DbMapSqlParameterSource>();
final List<DbMapSqlParameterSource> idKeyList = new ArrayList<DbMapSqlParameterSource>();
final String sqlSelectIdKey = getElSqlBundle().getSql("SelectIdKey");
for (ExternalId id : exchange.getExternalIdBundle()) {
final DbMapSqlParameterSource assocArgs = new DbMapSqlParameterSource()
.addValue("doc_id", docId)
.addValue("key_scheme", id.getScheme().getName())
.addValue("key_value", id.getValue());
assocList.add(assocArgs);