final long positionOid = (document.getUniqueId() != null ? extractOid(document.getUniqueId()) : positionId);
final UniqueId positionUid = createUniqueId(positionOid, positionId);
final ManageablePosition position = document.getPosition();
// the arguments for inserting into the position table
final DbMapSqlParameterSource docArgs = new DbMapSqlParameterSource().addValue("position_id", positionId).addValue("position_oid", positionOid)
.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("quantity", position.getQuantity(), Types.DECIMAL)
.addValue("provider_scheme",
position.getProviderId() != null ? position.getProviderId().getScheme().getName() : null, Types.VARCHAR)
.addValue("provider_value",
position.getProviderId() != null ? position.getProviderId().getValue() : null, Types.VARCHAR);
// the arguments for inserting into the pos_attribute table
final List<DbMapSqlParameterSource> posAttrList = Lists.newArrayList();
for (final Entry<String, String> entry : position.getAttributes().entrySet()) {
final long posAttrId = nextId("pos_trade_attr_seq");
final DbMapSqlParameterSource posAttrArgs = new DbMapSqlParameterSource().addValue("attr_id", posAttrId)
.addValue("pos_id", positionId)
.addValue("pos_oid", positionOid)
.addValue("key", entry.getKey())
.addValue("value", entry.getValue());
posAttrList.add(posAttrArgs);
}
// the arguments for inserting into the idkey tables
final List<DbMapSqlParameterSource> posAssocList = new ArrayList<DbMapSqlParameterSource>();
final Set<Pair<String, String>> schemeValueSet = Sets.newHashSet();
for (final ExternalId id : position.getSecurityLink().getAllExternalIds()) {
final DbMapSqlParameterSource assocArgs = new DbMapSqlParameterSource().addValue("position_id", positionId)
.addValue("key_scheme", id.getScheme().getName())
.addValue("key_value", id.getValue());
posAssocList.add(assocArgs);
schemeValueSet.add(Pair.of(id.getScheme().getName(), id.getValue()));
}
// the arguments for inserting into the trade table
final List<DbMapSqlParameterSource> tradeList = Lists.newArrayList();
final List<DbMapSqlParameterSource> tradeAssocList = Lists.newArrayList();
final List<DbMapSqlParameterSource> tradeAttributeList = Lists.newArrayList();
for (final ManageableTrade trade : position.getTrades()) {
final long tradeId = nextId("pos_master_seq");
final long tradeOid = (trade.getUniqueId() != null ? extractOid(trade.getUniqueId()) : tradeId);
final ExternalId counterpartyId = trade.getCounterpartyExternalId();
final DbMapSqlParameterSource tradeArgs = new DbMapSqlParameterSource().addValue("trade_id", tradeId)
.addValue("trade_oid", tradeOid)
.addValue("position_id", positionId)
.addValue("position_oid", positionOid)
.addValue("quantity", trade.getQuantity())
.addDate("trade_date", trade.getTradeDate())
.addTimeAllowNull("trade_time", trade.getTradeTime() != null ? trade.getTradeTime().toLocalTime() : null)
.addValue("zone_offset",
trade.getTradeTime() != null ? trade.getTradeTime().getOffset().getTotalSeconds() : null, Types.INTEGER)
.addValue("cparty_scheme", counterpartyId.getScheme().getName())
.addValue("cparty_value", counterpartyId.getValue())
.addValue("provider_scheme",
position.getProviderId() != null ? position.getProviderId().getScheme().getName() : null, Types.VARCHAR)
.addValue("provider_value",
position.getProviderId() != null ? position.getProviderId().getValue() : null, Types.VARCHAR)
.addValue("premium_value", trade.getPremium(), Types.DOUBLE)
.addValue("premium_currency",
trade.getPremiumCurrency() != null ? trade.getPremiumCurrency().getCode() : null, Types.VARCHAR)
.addDateAllowNull("premium_date", trade.getPremiumDate())
.addTimeAllowNull("premium_time", (trade.getPremiumTime() != null ? trade.getPremiumTime().toLocalTime() : null))
.addValue("premium_zone_offset",
trade.getPremiumTime() != null ? trade.getPremiumTime().getOffset().getTotalSeconds() : null, Types.INTEGER);
tradeList.add(tradeArgs);
// trade attributes
final Map<String, String> attributes = new HashMap<String, String>(trade.getAttributes());
for (final Entry<String, String> entry : attributes.entrySet()) {
final long tradeAttrId = nextId("pos_trade_attr_seq");
final DbMapSqlParameterSource tradeAttributeArgs = new DbMapSqlParameterSource().addValue("attr_id", tradeAttrId)
.addValue("trade_id", tradeId)
.addValue("trade_oid", tradeOid)
.addValue("key", entry.getKey())
.addValue("value", entry.getValue());
tradeAttributeList.add(tradeAttributeArgs);
}
// set the trade uniqueId
final UniqueId tradeUid = createUniqueId(tradeOid, tradeId);
IdUtils.setInto(trade, tradeUid);
trade.setParentPositionId(positionUid);
for (final ExternalId id : trade.getSecurityLink().getAllExternalIds()) {
final DbMapSqlParameterSource assocArgs = new DbMapSqlParameterSource().addValue("trade_id", tradeId)
.addValue("key_scheme", id.getScheme().getName())
.addValue("key_value", id.getValue());
tradeAssocList.add(assocArgs);
schemeValueSet.add(Pair.of(id.getScheme().getName(), id.getValue()));
}
}
final List<DbMapSqlParameterSource> idKeyList = new ArrayList<DbMapSqlParameterSource>();
final String sqlSelectIdKey = getElSqlBundle().getSql("SelectIdKey");
for (final Pair<String, String> pair : schemeValueSet) {
final DbMapSqlParameterSource idkeyArgs = new DbMapSqlParameterSource().addValue("key_scheme", pair.getFirst())
.addValue("key_value", pair.getSecond());
if (getJdbcTemplate().queryForList(sqlSelectIdKey, idkeyArgs).isEmpty()) {
// select avoids creating unecessary id, but id may still not be used
final long idKeyId = nextId("pos_idkey_seq");
idkeyArgs.addValue("idkey_id", idKeyId);
idKeyList.add(idkeyArgs);
}
}
final String sqlDoc = getElSqlBundle().getSql("Insert", docArgs);