public boolean apply(@Nullable Product input) { return input.getParentId() == null || input.isVirtual(); } };
@Override public void onEvent(Event event, Object source, Object data) { // Update stocks for bought products Order order = (Order) source; List<OrderItem> items = order.getOrderItems(); for (OrderItem item : items) { UUID itemId = item.getPurchasableId(); if (itemId == null) { return; } Long quantity = item.getQuantity(); try { Product product = productStore.get().findById(itemId); if (product.getStock() != null) { productStore.get().updateStock(itemId, -quantity.intValue()); } else if (product.getParentId() != null) { Product parent = productStore.get().findById(product.getParentId()); productStore.get().updateStock(product.getParentId(), -quantity.intValue()); } } catch (EntityDoesNotExistException e) { // Ignore, there is just no stock to update } } } }
Product product = productStore.findById(idAndType.getId()); if (product != null) { if (product.getParentId() != null) { Product parent = productStore.findById(product.getParentId()); if (parent == null) {
public Product create(Product product) throws EntityAlreadyExistsException, InvalidEntityException if (this.dao.findBySlug(product.getSlug(), getTenant(), product.getParentId()) != null) { throw new EntityAlreadyExistsException(); product.setCreationDate(new Date()); if (product.getParentId() != null) { this.dao.createChildEntity(product, PRODUCT_TABLE_NAME, getTenant()); } else { if (product.getParentId() == null) { lastIndex = this.dao.lastPosition(getTenant()); } else {
public void update(Product product) throws EntityDoesNotExistException, InvalidEntityException { this.dao.begin(); Product originalProduct = this.findBySlug(product.getSlug(), product.getParentId()); if (originalProduct == null) { this.dao.commit(); throw new EntityDoesNotExistException(); } if (!product.getAddons().isLoaded()) { product.setAddons(originalProduct.getAddons().get()); } getObservationManager().notify(new EntityUpdatingEvent(), product); product.setId(originalProduct.getId()); Integer updatedRows = this.dao.updateProduct(product); this.dao.createOrUpdateAddons(product); if (product.getLocalizedVersions() != null && !product.getLocalizedVersions().isEmpty()) { Map<Locale, Map<String, Object>> localizedVersions = product.getLocalizedVersions(); for (Locale locale : localizedVersions.keySet()) { this.dao.createOrUpdateTranslation(product.getId(), locale, localizedVersions.get(locale)); } } this.dao.commit(); if (updatedRows <= 0) { throw new StoreException("No rows was updated when updating product"); } getObservationManager().notify(new EntityUpdatedEvent(), product); }