@Override public EntityAndParent<Collection> findBySlugs(String... slugsArray) { Stack<String> slugs = new Stack<>(); slugs.addAll(Arrays.asList(slugsArray)); List<org.mayocat.shop.catalog.model.Collection> chain = Lists.newArrayList(); while (slugs.size() > 0) { String slug = slugs.remove(0); Collection collection = findBySlug(slug, chain.size() > 0 ? chain.get(0).getId() : null); if (collection == null) { // If the chain is broken, the collection is not found return null; } chain.add(0, collection); } return getCollectionChain(chain); }
public void removeProduct(Collection collection, Product product) { findPositionedById(collection.getId()).getProducts().remove(product); }
/** * Compute the ltree path of a collection, which is the UUID chain (where dashes are replaced by underscores since * dashes are not allowed in ltree pathes) of parents leading to that collection (included). * * @param collection the collection for which to get the path * @return the computed path */ private String getPath(Collection collection) { Collection parent = collection; String path = collection.getId().toString().replace('-', '_'); while (parent.getParentId() != null) { parent = findById(parent.getParentId()); if (parent == null) { throw new RuntimeException("Broken collection path chain in database"); } path = parent.getId().toString().replace('-', '_') + '.' + path; } return path; }
public void addProduct(Collection collection, Product product) { findPositionedById(collection.getId()).getProducts().add(product); }
public List<Product> findAllForCollection(EntityAndParent<Collection> collection) { String path = ""; EntityAndParent<Collection> parent = collection; while (parent != null) { if (Strings.isNullOrEmpty(path)) { path = parent.getEntity().getId().toString().replace('-', '_'); } else { path = parent.getEntity().getId().toString().replace('-', '_') + "." + path; } parent = parent.getParent(); } return AddonsHelper.withAddons(this.dao.findAllForCollectionPath(path), this.dao); }
@Override public void delete(@Valid Collection entity) throws EntityDoesNotExistException { Integer updatedRows = 0; this.dao.begin(); updatedRows += this.dao.deleteEntityEntityById(COLLECTION_TABLE_NAME, entity.getId()); updatedRows += this.dao.detachChildren(entity.getId()); updatedRows += this.dao.deleteEntityAndChildrenById(entity.getId()); this.dao.commit(); if (updatedRows <= 0) { throw new EntityDoesNotExistException("No rows was updated when trying to delete collection"); } }
Collection retrieved = this.collectionStore.findById(collection.getId()); output.println("- indexing " + tenant.getSlug() + ":" + collection.getSlug()); output.flush();
public void update(Collection collection) throws EntityDoesNotExistException, InvalidEntityException { this.dao.begin(); Collection originalCollection = this.dao.findBySlug(collection.getSlug(), getTenant()); if (originalCollection == null) { this.dao.commit(); throw new EntityDoesNotExistException(); } collection.setId(originalCollection.getId()); Integer updatedRows = this.dao.update(collection); this.dao.createOrUpdateAddons(collection); if (collection.getLocalizedVersions() != null && !collection.getLocalizedVersions().isEmpty()) { Map<Locale, Map<String, Object>> localizedVersions = collection.getLocalizedVersions(); for (Locale locale : localizedVersions.keySet()) { this.dao.createOrUpdateTranslation(collection.getId(), locale, localizedVersions.get(locale)); } } this.dao.commit(); if (updatedRows <= 0) { throw new StoreException("No rows was updated when updating collection"); } getObservationManager().notify(new EntityUpdatedEvent(), collection); }