s_logger.debug("Saving portfolio '{}'", portfolio.getName());
final PortfolioSearchRequest request = new PortfolioSearchRequest();
request.setName(getPortfolioName(portfolio));
request.setVisibility(visibility); // Any existing match needs to be at least as visible
final PortfolioSearchResult result = _portfolios.search(request);
final ManageablePortfolio manageablePortfolio = createManageablePortfolio(portfolio);
PortfolioDocument document;
if (updateMatchingName) {
document = result.getFirstDocument();
// TODO why did this assume document will never be null? is that valid or have I broken something?
if (document != null) {
final ManageablePortfolio resultPortfolio = document.getPortfolio();
if (nodesEqual(manageablePortfolio.getRootNode(), resultPortfolio.getRootNode())) {
s_logger.debug("Found existing match at {}", document.getUniqueId());
return document.getUniqueId();
}
}
} else {
document = null;
for (PortfolioDocument resultDocument : result.getDocuments()) {
final ManageablePortfolio resultPortfolio = resultDocument.getPortfolio();
if (manageablePortfolio.getName().equals(resultPortfolio.getName()) && nodesEqual(manageablePortfolio.getRootNode(), resultPortfolio.getRootNode())) {
s_logger.debug("Found existing match at {}", resultDocument.getUniqueId());
return resultDocument.getUniqueId();
}
}
}