use of dataaccess.ex1.entity.OrderLine in project jmix-docs by Haulmont.
the class OrderServiceTest method testLazyLoading.
@Test
void testLazyLoading() {
String customerName = orderService.getCustomerName(Id.of(order1));
assertEquals("Alice", customerName);
List<String> productNames = orderService.getProductNames(Id.of(order1));
assertEquals(Collections.singletonList("MacBook Pro"), productNames);
Order order = dataManager.load(Id.of(order1)).one();
OrderLine orderLine = order.getLines().iterator().next();
assertSame(order, orderLine.getOrder());
}
use of dataaccess.ex1.entity.OrderLine in project jmix-docs by Haulmont.
the class OrderLineEventListener method onOrderLineChangedBeforeCommit.
@EventListener
void onOrderLineChangedBeforeCommit(EntityChangedEvent<OrderLine> event) {
Order order;
if (event.getType() == EntityChangedEvent.Type.DELETED) {
// <1>
// <2>
Id<Order> orderId = event.getChanges().getOldReferenceId("order");
order = dataManager.load(orderId).one();
} else {
OrderLine orderLine = dataManager.load(event.getEntityId()).one();
order = orderLine.getOrder();
}
BigDecimal amount = order.getLines().stream().map(line -> line.getProduct().getPrice().multiply(BigDecimal.valueOf(line.getQuantity()))).reduce(BigDecimal.ZERO, BigDecimal::add);
order.setAmount(amount);
dataManager.save(order);
}
use of dataaccess.ex1.entity.OrderLine in project jmix-docs by Haulmont.
the class OrderLineEventListenerTest method test.
@Test
void test() {
Product product1 = dataManager.create(Product.class);
product1.setName("MacBook Pro");
product1.setPrice(BigDecimal.valueOf(2500));
Product product2 = dataManager.create(Product.class);
product2.setName("iPad");
product2.setPrice(BigDecimal.valueOf(1000));
Order order1 = dataManager.create(Order.class);
order1.setDate(LocalDate.now());
dataManager.save(order1, product1, product2);
OrderLine orderLine1 = dataManager.create(OrderLine.class);
orderLine1.setOrder(order1);
orderLine1.setProduct(product1);
orderLine1.setQuantity(2.0);
dataManager.save(orderLine1);
assertEquals(0, BigDecimal.valueOf(5000).compareTo(dataManager.load(Id.of(order1)).one().getAmount()));
OrderLine orderLine2 = dataManager.create(OrderLine.class);
orderLine2.setOrder(order1);
orderLine2.setProduct(product2);
orderLine2.setQuantity(3.0);
dataManager.save(orderLine2);
assertEquals(0, BigDecimal.valueOf(8000).compareTo(dataManager.load(Id.of(order1)).one().getAmount()));
}
use of dataaccess.ex1.entity.OrderLine in project jmix-docs by Haulmont.
the class OrderServiceTest method setUp.
@BeforeEach
void setUp() {
Customer customer1 = dataManager.create(Customer.class);
customer1.setName("Alice");
customer1.setEmail("alice@company.com");
Product product1 = dataManager.create(Product.class);
product1.setName("MacBook Pro");
product1.setPrice(BigDecimal.valueOf(2500));
order1 = dataManager.create(Order.class);
order1.setCustomer(customer1);
order1.setDate(LocalDate.now());
order1.setAmount(BigDecimal.valueOf(5000));
OrderLine orderLine1 = dataManager.create(OrderLine.class);
orderLine1.setOrder(order1);
orderLine1.setProduct(product1);
orderLine1.setQuantity(2.0);
dataManager.save(customer1, order1, orderLine1, product1);
}
Aggregations