@Test @Transactional public void testSaveAndGet() throws Exception { Order order = new Order(); order.getItems().add(new Item()); entityManager.persist(order); entityManager.flush(); // Otherwise the query returns the existing order (and we didn't set the // parent in the item)... entityManager.clear(); Order other = (Order) entityManager.find(Order.class, order.getId()); assertEquals(1, other.getItems().size()); assertEquals(other, other.getItems().iterator().next().getOrder()); }
public Order getOrder(Integer orderId) throws Exception { LOGGER.info("Getting Order with no: " + orderId); Order corder = _orders.get(orderId); if (corder == null) { throw new RuntimeException("Order " + orderId + " not found!"); } LOGGER.info("Updating descriptions: " + corder.getItems()); // Get the updated descriptions of items for (OrderItem orderItem : corder.getItems()) { Item item = _warehouse.getItem(orderItem.getItem().getItemId()); orderItem.setItem(item); } return corder; }
@Test @Transactional public void testSaveAndGet() throws Exception { Session session = sessionFactory.getCurrentSession(); Order order = new Order(); order.getItems().add(new Item()); session.save(order); session.flush(); // Otherwise the query returns the existing order (and we didn't set the // parent in the item)... session.clear(); Order other = (Order) session.get(Order.class, order.getId()); assertEquals(1, other.getItems().size()); assertEquals(other, other.getItems().iterator().next().getOrder()); }
/** * Cancels an order. Note that cancel requests for orders will be rejected if they are older than * one hour, or don't target the given customer. * * @param alias the customer alias * @param passwordHash the customer password-hash * @param orderIdentity the order identity * @throws IllegalStateException if the login data is invalid, if the order is too old, or if it * is not targeting the given customer * @throws SQLException if there is a problem with the underlying JDBC connection */ public void deleteOrder(final String alias, final byte[] passwordHash, final long orderIdentity) throws SQLException { final Customer customer = this.queryCustomer(alias, passwordHash); final Order order = this.queryOrder(alias, passwordHash, orderIdentity); if (order.getCustomerIdentity() != customer.getIdentity()) throw new IllegalStateException("purchase not created by given customer."); if (System.currentTimeMillis() > order.getCreationTimestamp() + TimeUnit.HOURS.toMillis(1)) throw new IllegalStateException("purchase too old."); synchronized (this.connection) { try (PreparedStatement statement = this.connection.prepareStatement(SQL_DELETE_PURCHASE)) { statement.setLong(1, orderIdentity); statement.executeUpdate(); } } for (final OrderItem item : order.getItems()) { synchronized (this.connection) { try (PreparedStatement statement = this.connection.prepareStatement(SQL_RELEASE_ARTICLE_UNITS)) { statement.setLong(1, item.getCount()); statement.setLong(2, item.getArticleIdentity()); statement.executeUpdate(); } } } }
public String addItems(Order order) throws Exception { LOGGER.info("Adding items to order: " + order.getOrderId()); Order corder = getOrder(order.getOrderId()); for (OrderItem orderItem : order.getItems()) { LOGGER.info("Adding item: " + orderItem.getItem().getItemId()); Item item = _warehouse.getItem(orderItem.getItem().getItemId()); if (item == null) { throw new RuntimeException("Item " + orderItem.getItem().getItemId() + " not found!"); } else { orderItem.setItem(item); corder.getItems().add(orderItem); } } LOGGER.info("Order after adding items: " + corder); return SUCCESS; }
@Test @Transactional public void testSaveOrderWithItems() throws Exception { Order order = new Order(); order.getItems().add(new Item()); entityManager.persist(order); entityManager.flush(); assertNotNull(order.getId()); }
public String removeItem(String orderItemId) throws Exception { // Not so elegant, but what to do? String[] ids = orderItemId.split(":"); if (ids.length != 2) { throw new RuntimeException("Missing either order id or item id!"); } Integer orderId = Integer.parseInt(ids[0]); Integer itemId = Integer.parseInt(ids[1]); Order corder = getOrder(orderId); for (OrderItem orderItem : corder.getItems()) { if (orderItem.getItem().getItemId() == itemId) { corder.getItems().remove(orderItem); break; } } LOGGER.info("Order after deleting item: " + corder); return SUCCESS; }
/* * @GRASP: Polimorfismo * */ @Override double orderPayment(Order order) { double total = 0; for (OrderItem item : order.getItems()) { total += item.calculatePriceItem(order.getShipping()); } total *= 0.5; return total; }
@Test @Transactional public void testSaveOrderWithItems() throws Exception { Session session = sessionFactory.getCurrentSession(); Order order = new Order(); order.getItems().add(new Item()); session.save(order); session.flush(); assertNotNull(order.getId()); }
public Double process(Order order) { Double totalAmount = 0d; List<OrderItem> orderItems = order.getItems(); for (int i = 0; i < orderItems.size(); i++) { OrderItem item = orderItems.get(i); // static method double productPrice = productPrice(item); totalAmount += item.getSum() * productPrice; } return totalAmount; }
@Override public byte[] toBinary(Object o) { Order order = (Order) o; OrderFormat orderFormat = new OrderFormat(); orderFormat.id = order.getId(); orderFormat.items = new ArrayList<>(order.getItems().reverse().toCollection()); orderFormat.cancelled = order.isCancelled(); return SerializationExtension.get(system).serialize(orderFormat).get(); }
@Transient public Long getOrderMoney() { Long money = 0L; for (Order order : orders) { for (OrderItem item : order.getItems()) { money += item.getMoney(); } } return money; }
@Test @Transactional public void testSaveAndFind() throws Exception { Order order = new Order(); Item item = new Item(); item.setProduct("foo"); order.getItems().add(item); entityManager.persist(order); entityManager.flush(); // Otherwise the query returns the existing order (and we didn't set the // parent in the item)... entityManager.clear(); Order other = (Order) entityManager .createQuery("select o from Order o join o.items i where i.product=:product") .setParameter("product", "foo") .getSingleResult(); assertEquals(1, other.getItems().size()); assertEquals(other, other.getItems().iterator().next().getOrder()); }
@Test @Transactional public void testSaveAndFind() throws Exception { Session session = sessionFactory.getCurrentSession(); Order order = new Order(); Item item = new Item(); item.setProduct("foo"); order.getItems().add(item); session.save(order); session.flush(); // Otherwise the query returns the existing order (and we didn't set the // parent in the item)... session.clear(); Order other = (Order) session .createQuery("select o from Order o join o.items i where i.product=:product") .setString("product", "foo") .uniqueResult(); assertEquals(1, other.getItems().size()); assertEquals(other, other.getItems().iterator().next().getOrder()); }
public static void addOrderToDB(Order o) { Customer customer = CustomerServiceImpl.getCustomer(o.getCustomerID()); customer.setOrders(OrderServiceImpl.retrieveUnpaidOrders(customer)); customer.addOrder(o); if (customer.getOrders().contains(o)) { orderDAO.addOrder(o); List<OrderItem> items = o.getItems(); Inventory inventory = new Inventory(InventoryServiceImpl.getAllAvailableProductsInDB()); for (OrderItem i : items) { Product product = ProductServiceImpl.getProduct(i.getItemSKUNumber()); InventoryItem inventoryitem = InventoryServiceImpl.getInventoryItem(inventory, product); InventoryServiceImpl.deductFromInventory(inventory, inventoryitem, i.getQuantity()); } } }
@Override public double calculateShippingCost(Order order) { List<OrderItem> items = order.getItems(); double totalWeight = 0; for (OrderItem item : items) { if (item.getWeight() < 1) { totalWeight += 1; } else { totalWeight += item.getWeight(); } } return totalWeight * GROUND_COST_PER_POUND; }
/** * Queries the items for a given order. * * @param order the order to be filled with it's items * @throws SQLException if there is a problem with the underlying JDBC connection */ private void populateOrderItems(final Order order) throws SQLException { synchronized (this.connection) { try (PreparedStatement statement = this.connection.prepareStatement(SQL_SELECT_PURCHASE_ITEMS)) { statement.setLong(1, order.getIdentity()); try (ResultSet resultSet = statement.executeQuery()) { while (resultSet.next()) { final OrderItem item = new OrderItem(); item.setIdentity(resultSet.getLong("identity")); item.setArticleIdentity(resultSet.getLong("articleIdentity")); item.setArticleGrossPrice(resultSet.getLong("articlePrice")); item.setCount(resultSet.getInt("count")); order.getItems().add(item); } } } } }
@Test public void createOrder() { order = new Order(561, "customer"); final OrderItem orderItem = new OrderItem(order, "Prod", "Some product", 3, new BigDecimal("5.20")); order.getItems().add(orderItem); new Expectations() { { Database.executeInsertUpdateOrDelete( withPrefix("insert into order "), order.getNumber(), order.getCustomerId()); Database.executeInsertUpdateOrDelete( withPrefix("insert into order_item "), order.getNumber(), orderItem.getProductId(), orderItem.getProductDescription(), orderItem.getQuantity(), orderItem.getUnitPrice()); } }; new OrderRepository().create(order); }
public static void deleteOrderItem(Order order, int itemIndex) { List<OrderItem> items = order.getItems(); OrderItem itemToBeRemoved = items.get(itemIndex); order.deleteItem(itemToBeRemoved); }