FungibleTradeBuilder extends AbstractTradeBuilder { UniqueId addTrade(BeanDataSource tradeData, UniqueId nodeId) { ManageableTrade trade = buildTrade(tradeData); Security security = trade.getSecurityLink().resolve(_securitySource); ManageablePortfolioNode node = _portfolioMaster.getNode(nodeId); ManageablePortfolio portfolio = _portfolioMaster.get(node.getPortfolioId()).getPortfolio(); ManageablePortfolioNode portfolioNode = findNode(portfolio, nodeId); ManageablePosition position = findPosition(portfolioNode, security); if (position == null) { ManageablePosition newPosition = new ManageablePosition(trade.getQuantity(), security.getExternalIdBundle()); newPosition.addTrade(trade); ManageablePosition savedPosition = getPositionMaster().add(new PositionDocument(newPosition)).getPosition(); portfolioNode.addPosition(savedPosition.getUniqueId()); _portfolioMaster.update(new PortfolioDocument(portfolio)); return savedPosition.getTrades().get(0).getUniqueId(); } else { position.addTrade(trade); position.setQuantity(position.getQuantity().add(trade.getQuantity())); ManageablePosition savedPosition = getPositionMaster().update(new PositionDocument(position)).getPosition(); List<ManageableTrade> savedTrades = savedPosition.getTrades(); return savedTrades.get(savedTrades.size() - 1).getUniqueId(); } } FungibleTradeBuilder(PositionMaster positionMaster,
                                     PortfolioMaster portfolioMaster,
                                     SecurityMaster securityMaster,
                                     Set<MetaBean> metaBeans,
                                     StringConvert stringConvert);   }
@Test public void addTradeNoExistingPosition() { UniqueId tradeId = _tradeBuilder.addTrade(createTradeData("INTC US Equity", null), _savedNode.getUniqueId()); ManageableTrade testTrade = _positionMaster.getTrade(tradeId); assertEquals(LocalDate.of(2012, 12, 21), testTrade.getTradeDate()); assertEquals(OffsetTime.of(LocalTime.of(14, 25), ZoneOffset.UTC), testTrade.getTradeTime()); assertEquals(INTEL_BUNDLE, testTrade.getSecurityLink().getExternalId()); assertEquals(1234d, testTrade.getPremium()); assertEquals(Currency.USD, testTrade.getPremiumCurrency()); assertEquals(LocalDate.of(2012, 12, 22), testTrade.getPremiumDate()); assertEquals(OffsetTime.of(LocalTime.of(13, 30), ZoneOffset.UTC), testTrade.getPremiumTime()); assertEquals(BigDecimal.valueOf(30), testTrade.getQuantity()); assertEquals(ExternalId.of(AbstractTradeBuilder.CPTY_SCHEME, "cptyName"), testTrade.getCounterpartyExternalId()); assertTrue(testTrade.getAttributes().isEmpty()); ManageablePosition testPosition = _positionMaster.get(testTrade.getParentPositionId()).getPosition(); assertEquals(INTEL_BUNDLE, testPosition.getSecurityLink().getExternalId()); assertEquals(testTrade.getQuantity(), testPosition.getQuantity()); assertEquals(1, testPosition.getTrades().size()); assertEquals(testTrade, testPosition.getTrades().get(0)); ManageablePortfolio testPortfolio = _portfolioMaster.get(_savedNode.getPortfolioId()).getPortfolio(); ManageablePortfolioNode testNode = testPortfolio.getRootNode().getChildNodes().get(0); assertEquals(2, testNode.getPositionIds().size()); assertEquals(_savedPosition.getUniqueId().getObjectId(), testNode.getPositionIds().get(0)); assertEquals(testPosition.getUniqueId().getObjectId(), testNode.getPositionIds().get(1)); }
@Test public void addTradeToExistingPosition() { UniqueId tradeId = _tradeBuilder.addTrade(createTradeData("AAPL US Equity", null), _savedNode.getUniqueId()); ManageableTrade testTrade = _positionMaster.getTrade(tradeId); assertEquals(LocalDate.of(2012, 12, 21), testTrade.getTradeDate()); assertEquals(OffsetTime.of(LocalTime.of(14, 25), ZoneOffset.UTC), testTrade.getTradeTime()); assertEquals(APPLE_BUNDLE, testTrade.getSecurityLink().getExternalId()); assertEquals(1234d, testTrade.getPremium()); assertEquals(Currency.USD, testTrade.getPremiumCurrency()); assertEquals(LocalDate.of(2012, 12, 22), testTrade.getPremiumDate()); assertEquals(OffsetTime.of(LocalTime.of(13, 30), ZoneOffset.UTC), testTrade.getPremiumTime()); assertEquals(BigDecimal.valueOf(30), testTrade.getQuantity()); assertEquals(ExternalId.of(AbstractTradeBuilder.CPTY_SCHEME, "cptyName"), testTrade.getCounterpartyExternalId()); assertTrue(testTrade.getAttributes().isEmpty()); ManageablePosition testPosition = _positionMaster.get(testTrade.getParentPositionId()).getPosition(); assertEquals(APPLE_BUNDLE, testPosition.getSecurityLink().getExternalId()); assertEquals(BigDecimal.valueOf(50), testPosition.getQuantity()); assertEquals(2, testPosition.getTrades().size()); assertEquals(testTrade, testPosition.getTrades().get(1)); ManageablePortfolio testPortfolio = _portfolioMaster.get(_savedNode.getPortfolioId()).getPortfolio(); ManageablePortfolioNode testNode = testPortfolio.getRootNode().getChildNodes().get(0); assertEquals(1, testNode.getPositionIds().size()); assertEquals(_savedPosition.getUniqueId().getObjectId(), testNode.getPositionIds().get(0)); }