FungibleTradeBuilder extends AbstractTradeBuilder { UniqueId updateTrade(BeanDataSource tradeData) { ManageableTrade trade = buildTrade(tradeData); ManageableTrade previousTrade = getPositionMaster().getTrade(trade.getUniqueId()); ManageablePosition position = getPositionMaster().get(previousTrade.getParentPositionId()).getPosition(); if (!trade.getSecurityLink().equals(previousTrade.getSecurityLink())) { throw new IllegalArgumentException("Cannot update a trade's security. new version " + trade + ", previous version: " + previousTrade); } List<ManageableTrade> trades = Lists.newArrayList(); for (ManageableTrade existingTrade : position.getTrades()) { if (existingTrade.getUniqueId().equals(trade.getUniqueId())) { trades.add(trade); position.setQuantity(position.getQuantity().subtract(existingTrade.getQuantity()).add(trade.getQuantity())); } else { trades.add(existingTrade); } } position.setTrades(trades); ManageablePosition savedPosition = getPositionMaster().update(new PositionDocument(position)).getPosition(); ManageableTrade savedTrade = savedPosition.getTrade(trade.getUniqueId().getObjectId()); if (savedTrade == null) { throw new DataNotFoundException("Failed to save trade " + trade + " to position " + savedPosition); } else { return savedTrade.getUniqueId(); } } FungibleTradeBuilder(PositionMaster positionMaster,
                                     PortfolioMaster portfolioMaster,
                                     SecurityMaster securityMaster,
                                     Set<MetaBean> metaBeans,
                                     StringConvert stringConvert);   }
@Test public void updateTrade() { ManageableTrade previousTrade = _savedPosition.getTrades().get(0); BeanDataSource tradeData = createTradeData("AAPL US Equity", previousTrade.getUniqueId().toString()); UniqueId updatedId = _tradeBuilder.updateTrade(tradeData); ManageableTrade updatedTrade = _positionMaster.getTrade(updatedId); assertEquals(LocalDate.of(2012, 12, 21), updatedTrade.getTradeDate()); assertEquals(OffsetTime.of(LocalTime.of(14, 25), ZoneOffset.UTC), updatedTrade.getTradeTime()); assertEquals(APPLE_BUNDLE, updatedTrade.getSecurityLink().getExternalId()); assertEquals(1234d, updatedTrade.getPremium()); assertEquals(Currency.USD, updatedTrade.getPremiumCurrency()); assertEquals(LocalDate.of(2012, 12, 22), updatedTrade.getPremiumDate()); assertEquals(OffsetTime.of(LocalTime.of(13, 30), ZoneOffset.UTC), updatedTrade.getPremiumTime()); assertEquals(BigDecimal.valueOf(30), updatedTrade.getQuantity()); assertEquals(ExternalId.of(AbstractTradeBuilder.CPTY_SCHEME, "cptyName"), updatedTrade.getCounterpartyExternalId()); assertTrue(updatedTrade.getAttributes().isEmpty()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void updateTradeChangeSecurity() { ManageableTrade previousTrade = _savedPosition.getTrades().get(0); BeanDataSource tradeData = createTradeData("INTC US Equity", previousTrade.getUniqueId().toString()); _tradeBuilder.updateTrade(tradeData); }