existingPosition.addTrade(trade);
}
if (!_multithread) {
// Save the updated existing position to the position master
PositionDocument addedDoc = _positionMaster.update(new PositionDocument(existingPosition));
s_logger.debug("Updated position {}, delta position {}", addedDoc.getPosition(), position);
// update position map (huh?)
_securityIdToPosition.put(writtenSecurities.get(0).getUniqueId().getObjectId(), addedDoc.getPosition());
// Return the updated position
return new ObjectsPair<>(addedDoc.getPosition(),
securities);
} else {
// update position map
_securityIdToPosition.put(writtenSecurities.get(0).getUniqueId().getObjectId(), existingPosition);
// Return the updated position
return new ObjectsPair<>(existingPosition, securities);
}
}
// Attempt to reuse an existing position from the previous version of the portfolio, and return if an exact match is found
if (!(_originalNode == null) && !_originalNode.getPositionIds().isEmpty()) {
ManageablePosition existingPosition = matchExistingPosition(position, writtenSecurities);
if (existingPosition != null) {
return new ObjectsPair<>(existingPosition,
writtenSecurities.toArray(new ManageableSecurity[writtenSecurities.size()]));
}
}
// If security has no ExternalId, link position to security ObjectId now
if (position.getSecurityLink().getExternalId().isEmpty() && position.getSecurityLink().getObjectId() == null) {
position.setSecurityLink(ManageableSecurityLink.of(writtenSecurities.get(0)));
}
// also check trades within position for a valid securityLink
for (ManageableTrade trade : position.getTrades()) {
if (trade.getSecurityLink().getExternalId().isEmpty() && trade.getSecurityLink().getObjectId() == null) {
trade.setSecurityLink(ManageableSecurityLink.of(writtenSecurities.get(0))); // or reuse link from position?
}
}
// No existing position could be reused/updated: just Add the new position to the position master as a new document
// (can't launch a thread since we need the position id immediately, to be stored with the pos document in the map)
PositionDocument addedDoc;
try {
addedDoc = _positionMaster.add(new PositionDocument(position));
s_logger.debug("Added position {}", position);
} catch (Exception e) {
s_logger.error("Unable to add position " + position.getUniqueId() + ": " + e.getMessage());
return null;
}
// Add the new position to the portfolio
_currentNode.addPosition(addedDoc.getUniqueId());
// Update position map
_securityIdToPosition.put(writtenSecurities.get(0).getUniqueId().getObjectId(), addedDoc.getPosition());
// Return the new position
return new ObjectsPair<>(addedDoc.getPosition(),
writtenSecurities.toArray(new ManageableSecurity[writtenSecurities.size()]));
}