private void setDocumentId(
     final PortfolioDocument document,
     final PortfolioDocument clonedDoc,
     final UniqueId uniqueId) {
   document.getPortfolio().setUniqueId(uniqueId);
   clonedDoc.getPortfolio().setUniqueId(uniqueId);
   document.setUniqueId(uniqueId);
   clonedDoc.setUniqueId(uniqueId);
 }
  /**
   * Stores the portfolio.
   *
   * @param portfolio the portfolio, not null
   */
  private void storePortfolio(final ManageablePortfolio portfolio) {
    final PortfolioMaster portfolioMaster = getToolContext().getPortfolioMaster();

    final PortfolioSearchRequest req = new PortfolioSearchRequest();
    req.setName(portfolio.getName());
    final PortfolioSearchResult result = portfolioMaster.search(req);
    switch (result.getDocuments().size()) {
      case 0:
        s_logger.info("Creating new portfolio");
        portfolioMaster.add(new PortfolioDocument(portfolio));
        break;
      case 1:
        final UniqueId previousId = result.getDocuments().get(0).getUniqueId();
        s_logger.info("Updating portfolio {}", previousId);
        portfolio.setUniqueId(previousId);
        final PortfolioDocument document = new PortfolioDocument(portfolio);
        document.setUniqueId(previousId);
        portfolioMaster.update(document);
        break;
      default:
        throw new OpenGammaRuntimeException("Multiple portfolios matching " + req);
    }
  }