// If it doesn't, create it (add)
if (_portfolioDocument == null) {
// Create a new root node
ManageablePortfolioNode rootNode = new ManageablePortfolioNode(portfolioName);
ManageablePortfolio portfolio = new ManageablePortfolio(portfolioName, rootNode);
_portfolioDocument = new PortfolioDocument();
_portfolioDocument.setPortfolio(portfolio);
_portfolioDocument = _portfolioMaster.add(_portfolioDocument);
_originalRoot = null;
_originalNode = null;
// Set current node to the root node
_currentNode = rootNode;
// If it does, create a new version of the existing portfolio (update)
} else {
ManageablePortfolio portfolio = _portfolioDocument.getPortfolio();
_originalRoot = portfolio.getRootNode();
_originalNode = _originalRoot;
if (_keepCurrentPositions) {
// Use the original root node
portfolio.setRootNode(cloneTree(_originalRoot));
_portfolioDocument.setPortfolio(portfolio);
// Set current node to the root node
_currentNode = portfolio.getRootNode();
} else {
// Create a new root node
ManageablePortfolioNode rootNode;
rootNode = JodaBeanUtils.clone(_originalRoot);
rootNode.setChildNodes(new ArrayList<ManageablePortfolioNode>());
rootNode.setPositionIds(new ArrayList<ObjectId>());
portfolio.setRootNode(rootNode);
_portfolioDocument.setPortfolio(portfolio);
// Set current node to the root node
_currentNode = rootNode;
}