Timer.Context context = _insertTimer.time();
try {
final long docId = nextId("sec_security_seq");
final long docOid = (document.getUniqueId() != null ? extractOid(document.getUniqueId()) : docId);
// the arguments for inserting into the security table
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.getSecurity().getName())
.addValue("sec_type", document.getSecurity().getSecurityType());
if (document.getSecurity() instanceof RawSecurity) {
docArgs.addValue("detail_type", "R");
} else if (document.getSecurity().getClass() == ManageableSecurity.class) {
docArgs.addValue("detail_type", "M");
} else {
docArgs.addValue("detail_type", "D");
}
// 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 : document.getSecurity().getExternalIdBundle()) {
final DbMapSqlParameterSource assocArgs = new DbMapSqlParameterSource()
.addValue("doc_id", docId)
.addValue("key_scheme", id.getScheme().getName())
.addValue("key_value", id.getValue());
assocList.add(assocArgs);
if (getJdbcTemplate().queryForList(sqlSelectIdKey, assocArgs).isEmpty()) {
// select avoids creating unnecessary id, but id may still not be used
final long idKeyId = nextId("sec_idkey_seq");
final DbMapSqlParameterSource idkeyArgs = new DbMapSqlParameterSource()
.addValue("idkey_id", idKeyId)
.addValue("key_scheme", id.getScheme().getName())
.addValue("key_value", id.getValue());
idKeyList.add(idkeyArgs);
}
}
final String sqlDoc = getElSqlBundle().getSql("Insert", docArgs);
final String sqlIdKey = getElSqlBundle().getSql("InsertIdKey");
final String sqlDoc2IdKey = getElSqlBundle().getSql("InsertDoc2IdKey");
getJdbcTemplate().update(sqlDoc, docArgs);
getJdbcTemplate().batchUpdate(sqlIdKey, idKeyList.toArray(new DbMapSqlParameterSource[idKeyList.size()]));
getJdbcTemplate().batchUpdate(sqlDoc2IdKey, assocList.toArray(new DbMapSqlParameterSource[assocList.size()]));
// set the uniqueId
final UniqueId uniqueId = createUniqueId(docOid, docId);
document.getSecurity().setUniqueId(uniqueId);
document.setUniqueId(uniqueId);
// store the detail
if (document.getSecurity() instanceof RawSecurity) {
storeRawSecurityDetail((RawSecurity) document.getSecurity());
} else {
final SecurityMasterDetailProvider detailProvider = getDetailProvider();
if (detailProvider != null) {
detailProvider.storeSecurityDetail(document.getSecurity());
}
}
// store attributes
Map<String, String> attributes = new HashMap<String, String>(document.getSecurity().getAttributes());
final List<DbMapSqlParameterSource> securityAttributeList = Lists.newArrayList();
for (Map.Entry<String, String> entry : attributes.entrySet()) {
final long securityAttrId = nextId("sec_security_attr_seq");
final DbMapSqlParameterSource attributeArgs = new DbMapSqlParameterSource()
.addValue("attr_id", securityAttrId)
.addValue("security_id", docId)
.addValue("security_oid", docOid)
.addValue("key", entry.getKey())
.addValue("value", entry.getValue());