Search in sources :

Example 1 with OrderLine

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());
}
Also used : Order(dataaccess.ex1.entity.Order) OrderLine(dataaccess.ex1.entity.OrderLine) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with OrderLine

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);
}
Also used : Order(dataaccess.ex1.entity.Order) BigDecimal(java.math.BigDecimal) DataManager(io.jmix.core.DataManager) Id(io.jmix.core.Id) Component(org.springframework.stereotype.Component) OrderLine(dataaccess.ex1.entity.OrderLine) Autowired(org.springframework.beans.factory.annotation.Autowired) EventListener(org.springframework.context.event.EventListener) Order(dataaccess.ex1.entity.Order) EntityChangedEvent(io.jmix.core.event.EntityChangedEvent) OrderLine(dataaccess.ex1.entity.OrderLine) BigDecimal(java.math.BigDecimal) EventListener(org.springframework.context.event.EventListener)

Example 3 with OrderLine

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()));
}
Also used : Order(dataaccess.ex1.entity.Order) OrderLine(dataaccess.ex1.entity.OrderLine) Product(dataaccess.ex1.entity.Product) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with OrderLine

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);
}
Also used : Order(dataaccess.ex1.entity.Order) Customer(dataaccess.ex1.entity.Customer) OrderLine(dataaccess.ex1.entity.OrderLine) Product(dataaccess.ex1.entity.Product) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Order (dataaccess.ex1.entity.Order)4 OrderLine (dataaccess.ex1.entity.OrderLine)4 Product (dataaccess.ex1.entity.Product)2 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Customer (dataaccess.ex1.entity.Customer)1 DataManager (io.jmix.core.DataManager)1 Id (io.jmix.core.Id)1 EntityChangedEvent (io.jmix.core.event.EntityChangedEvent)1 BigDecimal (java.math.BigDecimal)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 EventListener (org.springframework.context.event.EventListener)1 Component (org.springframework.stereotype.Component)1