@Override public DTOBestellung createNewOrder( int customerId, int source, List<DTOBestellposition> positions) throws AccessDeniedException { checkRole(UserRole.SYSUSER | UserRole.ADMIN); fTransaction.beginTransaction(); try { // Save Order Bestellung order = new Bestellung(); order.setQuelle(source); order.setPerson1(fPersonRepository.getById(customerId)); order.setPerson2(fSessionAPI.getAuthenticatedUser()); order.setBestelldatum(new Date()); order.setLiefertermin_Ist(new Date()); order.setLiefertermin_Soll(new Date()); fOrderRepository.persistObject(order); // Save positions if (orderProducts(order, positions) == false) { fTransaction.rollbackTransaction(); return null; } // The DeliveryDate could have been changed by orderProducts. fOrderRepository.persistObject(order); DTOBestellung dtoBestellung = new DTOBestellung(fOrderRepository.getById(order.getId())); fTransaction.commitTransaction(); return dtoBestellung; } catch (Exception exception) { fTransaction.rollbackTransaction(); return null; } }
private boolean orderProducts(Bestellung order, List<DTOBestellposition> positions) throws StockException { List<Ticket> tickets = new ArrayList<Ticket>(); try { for (DTOBestellposition position : positions) { if (!fPositionRepository.orderProduct( order.getId(), position.getProdukt(), position.getAnzahl(), position.getStueckpreis(), true)) { // Not enough in storage Produkt produkt = fProductRepository.getById(position.getProdukt()); Ticket reserveItem = fInternalStockAPI.reserveItem( produkt, position.getAnzahl() + produkt.getMinimalMenge()); tickets.add(reserveItem); fPositionRepository.orderProduct( order.getId(), position.getProdukt(), position.getAnzahl(), position.getStueckpreis(), false); } } Date deliveryDate = fInternalStockAPI.finalizeOrder(tickets); order.setLiefertermin_Soll(deliveryDate); order.setLiefertermin_Ist(deliveryDate); } catch (StockException ex) { fInternalStockAPI.cancelReservedTickets(tickets); return false; } return true; }