ObjectId positionId = getNextPositionId();
if (positionId == null) {
return null;
} else {
ManageablePosition position;
try {
position = _positionMaster.get(positionId, VersionCorrection.LATEST).getPosition();
} catch (Throwable t) {
return new ObjectsPair<ManageablePosition, ManageableSecurity[]>(null, null);
}
// Write the related security(ies)
ManageableSecurityLink sLink = position.getSecurityLink();
Security security = sLink.resolveQuiet(_securitySource);
if ((security != null) && (security instanceof ManageableSecurity)) {
// Find underlying security
// TODO support multiple underlyings; unfortunately the system does not provide a standard way
// to retrieve underlyings
if (((ManageableSecurity) security).propertyNames().contains("underlyingId")) {
ExternalId id = (ExternalId) ((ManageableSecurity) security).property("underlyingId").get();
Security underlying;
try {
underlying = _securitySource.getSingle(id.toBundle());
if (underlying != null) {
return new ObjectsPair<ManageablePosition, ManageableSecurity[]>(
position,
new ManageableSecurity[] {(ManageableSecurity) security, (ManageableSecurity) underlying});
} else {
s_logger.warn("Could not resolve underlying " + id + " for security " + security.getName());
}
} catch (Throwable e) {
// Underlying not found
s_logger.warn("Error trying to resolve underlying " + id + " for security " + security.getName());
}
}
return new ObjectsPair<ManageablePosition, ManageableSecurity[]>(
position,
new ManageableSecurity[] {(ManageableSecurity) security});
} else {
s_logger.warn("Could not resolve security relating to position " + position.getName());
return new ObjectsPair<ManageablePosition, ManageableSecurity[]>(null, null);
}
}
}