public void updateEntity(Sale sale, SaleEditDto editDto) { if (isNotNull(editDto.getContactId())) { Contact contact = contactsDao.findById(editDto.getContactId()); sale.setContact(contact); } if (isNotNull(editDto.getStoreId())) { Store store = storesDao.findById(editDto.getStoreId()); sale.setStore(store); } if (isNotEmpty(editDto.getLabel())) { sale.setLabel(editDto.getLabel()); } if (isNotNull(editDto.getDateOut())) { sale.setDateOut(editDto.getDateOut()); } if (isNotEmpty(editDto.getProductsToAdd())) { for (Long productToAdd : editDto.getProductsToAdd()) { Product product = productsDao.findById(productToAdd); sale.addProduct(product); } } if (isNotEmpty(editDto.getProductsToRemove())) { for (Long productId : editDto.getProductsToRemove()) { Product productToRemove = productsDao.findById(productId); sale.removeProduct(productToRemove); } } }
public Sale toEntity(SaleAddDto dto) { Sale entity = new Sale(); entity.setDateOut(dto.getDateOut()); entity.setLabel(dto.getLabel()); Contact contact = contactsDao.findById(dto.getContactId()); Store store = storesDao.findById(dto.getStoreId()); entity.setContact(contact); entity.setStore(store); for (Long productId : dto.getProductsIds()) { Product product = productsDao.findById(productId); entity.addProduct(product); } return entity; }
/** * asd. * * @param sale asd. * @param products asd. * @param user asd. * @param store asd. */ @Override @Transactional public final void addNewSale( Sale sale, final List<Product> products, final User user, final Store store) { logger.debug("SaleServiceImpl.addNewSale starts"); getEntityManager().getTransaction().begin(); sale.setCreatedAt(new Date()); sale.setCreator(user); sale.setStore(store); sale = salesDao.save(sale); for (Product p : products) { p.setSale(sale); productsDao.save(p); } getEntityManager().getTransaction().commit(); }