final Long portfolioId = nextId("prt_master_seq");
final Long portfolioOid = (document.getUniqueId() != null ? extractOid(document.getUniqueId()) : portfolioId);
final UniqueId portfolioUid = createUniqueId(portfolioOid, portfolioId);
// the arguments for inserting into the portfolio table
final DbMapSqlParameterSource docArgs = new DbMapSqlParameterSource()
.addValue("portfolio_id", portfolioId)
.addValue("portfolio_oid", portfolioOid)
.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", StringUtils.defaultString(document.getPortfolio().getName()))
.addValue("visibility", document.getVisibility().getVisibilityLevel());
// the arguments for inserting into the node table
final List<DbMapSqlParameterSource> nodeList = new ArrayList<DbMapSqlParameterSource>(256);
final List<DbMapSqlParameterSource> posList = new ArrayList<DbMapSqlParameterSource>(256);
insertBuildArgs(portfolioUid, null, document.getPortfolio().getRootNode(), document.getUniqueId() != null,
portfolioId, portfolioOid, null, null,
new AtomicInteger(1), 0, nodeList, posList);
// the arguments for inserting into the portifolio_attribute table
final List<DbMapSqlParameterSource> prtAttrList = Lists.newArrayList();
for (Entry<String, String> entry : document.getPortfolio().getAttributes().entrySet()) {
final long prtAttrId = nextId("prt_portfolio_attr_seq");
final DbMapSqlParameterSource posAttrArgs = new DbMapSqlParameterSource()
.addValue("attr_id", prtAttrId)
.addValue("portfolio_id", portfolioId)
.addValue("portfolio_oid", portfolioOid)
.addValue("key", entry.getKey())
.addValue("value", entry.getValue());