@Test public void shouldBatchUpdateStockMovements() throws LMISException, ParseException { StockCard stockCard = saveStockCardWithOneMovement(stockRepository, productRepository); StockMovementItem stockMovementItem = new StockMovementItem(); stockMovementItem.setMovementQuantity(100); stockMovementItem.setMovementType(MovementReasonManager.MovementType.POSITIVE_ADJUST); stockMovementItem.setMovementDate(DateUtil.parseString("2016-03-11", DateUtil.DB_DATE_FORMAT)); stockMovementItem.setStockCard(stockCard); stockMovementItem.setSynced(false); Lot lot1 = new Lot(); lot1.setProduct(product); lot1.setExpirationDate(DateUtil.parseString("2017-12-31", DateUtil.DB_DATE_FORMAT)); lot1.setLotNumber("AAA"); Lot lot2 = new Lot(); lot2.setProduct(product); lot2.setExpirationDate(DateUtil.parseString("2017-12-31", DateUtil.DB_DATE_FORMAT)); lot2.setLotNumber("BBB"); Lot lot3 = new Lot(); lot3.setProduct(product); lot3.setExpirationDate(DateUtil.parseString("2017-12-31", DateUtil.DB_DATE_FORMAT)); lot3.setLotNumber("CCC"); LotMovementItem lotMovementItem = new LotMovementItemBuilder() .setStockMovementItem(stockMovementItem) .setLot(lot1) .setMovementQuantity(2L).build(); LotMovementItem lotMovementItem2 = new LotMovementItemBuilder() .setStockMovementItem(stockMovementItem) .setLot(lot2) .setMovementQuantity(3L).build(); LotMovementItem lotMovementItem3 = new LotMovementItemBuilder() .setStockMovementItem(stockMovementItem) .setLot(lot3) .setMovementQuantity(5L).build(); stockMovementItem.setLotMovementItemListWrapper(Lists.newArrayList(lotMovementItem, lotMovementItem2, lotMovementItem3)); stockMovementItem.setStockOnHand(stockCard.getStockOnHand() + 100); stockCard.setStockOnHand(stockMovementItem.getStockOnHand()); stockMovementRepository.batchCreateOrUpdateStockMovementsAndLotInfo(Lists.newArrayList(stockMovementItem)); stockRepository.refresh(stockCard); StockMovementItem stockMovementItem1 = stockCard.getStockMovementItemsWrapper().get(1); stockMovementItem1.getLotMovementItemListWrapper(); stockMovementItem1.setForeignLotMovementItems(null); assertEquals(3, stockMovementItem.getLotMovementItemListWrapper().size()); assertEquals(stockMovementItem.getStockOnHand(), stockMovementItem1.getStockOnHand()); assertEquals(stockMovementItem.getMovementDate(), stockMovementItem1.getMovementDate()); stockMovementItem.setSynced(true); stockMovementRepository.batchCreateOrUpdateStockMovementsAndLotInfo(Lists.newArrayList(stockMovementItem)); stockRepository.refresh(stockCard); stockCard.setStockMovementItemsWrapper(null); assertTrue(stockCard.getStockMovementItemsWrapper().get(1).isSynced()); }
protected void batchCreateOrUpdateStockMovementsAndLotInfo(final List<StockMovementItem> stockMovementItems) throws LMISException { dbUtil.withDaoAsBatch(StockMovementItem.class, (DbUtil.Operation<StockMovementItem, Void>) dao -> { for (StockMovementItem stockMovementItem : stockMovementItems) { updateDateTimeIfEmpty(stockMovementItem); dao.createOrUpdate(stockMovementItem); lotRepository.batchCreateLotsAndLotMovements(stockMovementItem.getLotMovementItemListWrapper()); } return null; }); }
StockMovementRepository { protected void batchCreateOrUpdateStockMovementsAndLotInfo(final List<StockMovementItem> stockMovementItems) throws LMISException { dbUtil.withDaoAsBatch(StockMovementItem.class, (DbUtil.Operation<StockMovementItem, Void>) dao -> { for (StockMovementItem stockMovementItem : stockMovementItems) { updateDateTimeIfEmpty(stockMovementItem); dao.createOrUpdate(stockMovementItem); lotRepository.batchCreateLotsAndLotMovements(stockMovementItem.getLotMovementItemListWrapper()); } return null; }); } }
StockMovementRepository { protected void batchCreateOrUpdateStockMovementsAndLotInfo(final List<StockMovementItem> stockMovementItems) throws LMISException { dbUtil.withDaoAsBatch(StockMovementItem.class, (DbUtil.Operation<StockMovementItem, Void>) dao -> { for (StockMovementItem stockMovementItem : stockMovementItems) { updateDateTimeIfEmpty(stockMovementItem); dao.createOrUpdate(stockMovementItem); lotRepository.batchCreateLotsAndLotMovements(stockMovementItem.getLotMovementItemListWrapper()); } return null; }); } @Inject  StockMovementRepository(Context context); }
StockMovementRepository { protected void batchCreateOrUpdateStockMovementsAndLotInfo(final List<StockMovementItem> stockMovementItems) throws LMISException { dbUtil.withDaoAsBatch(StockMovementItem.class, (DbUtil.Operation<StockMovementItem, Void>) dao -> { for (StockMovementItem stockMovementItem : stockMovementItems) { updateDateTimeIfEmpty(stockMovementItem); dao.createOrUpdate(stockMovementItem); lotRepository.batchCreateLotsAndLotMovements(stockMovementItem.getLotMovementItemListWrapper()); } return null; }); } @Inject  StockMovementRepository(Context context); List<StockMovementItem> listUnSynced(); void batchCreateStockMovementItemAndLotItems(final StockMovementItem stockMovementItem); void batchCreateOrUpdateStockMovementsAndLotMovements(final List<StockMovementItem> stockMovementItems); StockMovementItem getFirstStockMovement(); List<String> queryStockMovementDatesByProgram(final String programCode); Date queryEarliestStockMovementDateByProgram(final String programCode); StockMovementItem queryFirstStockMovementByStockCardId(final long stockCardId); List<StockMovementItem> queryStockMovementHistory(final long stockCardId, final long startIndex, final long maxRows); List<StockMovementItem> queryStockItemsByCreatedDate(final long stockCardId, final Date periodBeginDate, final Date periodEndDate); List<StockMovementItem> queryStockMovementsByMovementDate(final long stockCardId, final Date startDate, final Date endDate); List<StockMovementItem> listLastFiveStockMovements(final long stockCardId); List<StockMovementItem> listLastTwoStockMovements(final long stockCardId); List<StockMovementItem> queryMovementByStockCardId(final long stockCardId); List<StockMovementItem> queryEachStockCardNewestMovement(); void deleteStockMovementItems(final StockCard stockCard); }
StockMovementRepository { protected void batchCreateOrUpdateStockMovementsAndLotInfo(final List<StockMovementItem> stockMovementItems) throws LMISException { dbUtil.withDaoAsBatch(StockMovementItem.class, (DbUtil.Operation<StockMovementItem, Void>) dao -> { for (StockMovementItem stockMovementItem : stockMovementItems) { updateDateTimeIfEmpty(stockMovementItem); dao.createOrUpdate(stockMovementItem); lotRepository.batchCreateLotsAndLotMovements(stockMovementItem.getLotMovementItemListWrapper()); } return null; }); } @Inject  StockMovementRepository(Context context); List<StockMovementItem> listUnSynced(); void batchCreateStockMovementItemAndLotItems(final StockMovementItem stockMovementItem); void batchCreateOrUpdateStockMovementsAndLotMovements(final List<StockMovementItem> stockMovementItems); StockMovementItem getFirstStockMovement(); List<String> queryStockMovementDatesByProgram(final String programCode); Date queryEarliestStockMovementDateByProgram(final String programCode); StockMovementItem queryFirstStockMovementByStockCardId(final long stockCardId); List<StockMovementItem> queryStockMovementHistory(final long stockCardId, final long startIndex, final long maxRows); List<StockMovementItem> queryStockItemsByCreatedDate(final long stockCardId, final Date periodBeginDate, final Date periodEndDate); List<StockMovementItem> queryStockMovementsByMovementDate(final long stockCardId, final Date startDate, final Date endDate); List<StockMovementItem> listLastFiveStockMovements(final long stockCardId); List<StockMovementItem> listLastTwoStockMovements(final long stockCardId); List<StockMovementItem> queryMovementByStockCardId(final long stockCardId); List<StockMovementItem> queryEachStockCardNewestMovement(); void deleteStockMovementItems(final StockCard stockCard);  }