UniqueId positionId) {
// need the previous underlying so we don't lose the ID bundle, the data doesn't contain it
ExternalIdBundle previousUnderlyingIdBundle;
if (previousSecurity instanceof FinancialSecurity) {
UnderlyingSecurityVisitor visitor = new UnderlyingSecurityVisitor(VersionCorrection.LATEST, getSecurityMaster());
ManageableSecurity previousUnderlying = ((FinancialSecurity) previousSecurity).accept(visitor);
if (previousUnderlying != null) {
previousUnderlyingIdBundle = previousUnderlying.getExternalIdBundle();
} else {
previousUnderlyingIdBundle = ExternalIdBundle.EMPTY;
}
} else {
previousUnderlyingIdBundle = ExternalIdBundle.EMPTY;
}
ManageableSecurity underlying = buildUnderlying(underlyingData, previousUnderlyingIdBundle);
ManageableSecurity security;
if (underlying == null) {
security = BlotterUtils.buildSecurity(securityData, previousSecurity.getExternalIdBundle());
} else {
// need to set the unique ID to the ID from the previous version, securities aren't allowed to change
// any changes in the security data are interpreted as edits to the security
ManageableSecurity previousUnderlying = getUnderlyingSecurity(previousSecurity, VersionCorrection.LATEST);
validateSecurity(underlying, previousUnderlying);
underlying.setUniqueId(previousUnderlying.getUniqueId());
ManageableSecurity savedUnderlying = getSecurityMaster().update(new SecurityDocument(underlying)).getSecurity();
security = buildSecurity(securityData, savedUnderlying, previousSecurity.getExternalIdBundle());
}
// need to set the unique ID to the ID from the previous version, securities aren't allowed to change
// any changes in the security data are interpreted as edits to the security
validateSecurity(security, previousSecurity);
security.setUniqueId(previousSecurity.getUniqueId());
ManageableSecurity savedSecurity = getSecurityMaster().update(new SecurityDocument(security)).getSecurity();
trade.setSecurityLink(new ManageableSecurityLink(savedSecurity.getUniqueId()));
ManageablePosition position = getPositionMaster().get(positionId).getPosition();
position.setTrades(Lists.newArrayList(trade));
ManageablePosition savedPosition = getPositionMaster().update(new PositionDocument(position)).getPosition();
ManageableTrade savedTrade = savedPosition.getTrades().get(0);
return savedTrade.getUniqueId();