@Override public List<Reference<Category>> getAncestors() { return delegate.getAncestors(); }
@Override public List<Reference<Category>> getAncestors() { return delegate.getAncestors(); }
@Override public List<Reference<Category>> getAncestors() { return delegate.getAncestors(); }
@Override public List<Reference<Category>> getAncestors() { return delegate.getAncestors(); }
@Override public List<Reference<Category>> getAncestors() { return delegate.getAncestors(); }
@Override public List<Reference<Category>> getAncestors() { return delegate.getAncestors(); }
@Override public Category getRootAncestor(final Identifiable<Category> category) { requireNonNull(category); final Category theCategory = CategoryTreeUtils.getCategoryOrThrow(category, this); return theCategory .getAncestors().stream().findFirst() .flatMap(root -> findById(root.getId())) .orElse(theCategory); }
@Override public Category getRootAncestor(final Identifiable<Category> category) { requireNonNull(category); final Category theCategory = CategoryTreeUtils.getCategoryOrThrow(category, this); return theCategory .getAncestors().stream().findFirst() .flatMap(root -> findById(root.getId())) .orElse(theCategory); }
public CategoryTree createSubtree(final CategoryTree categoryTree, final Collection<? extends Identifiable<Category>> subtreeRoots) { final List<Category> subtreeRootsCategories = subtreeRoots.stream() .map(identifiable -> getCategoryOrThrow(identifiable, categoryTree)) .collect(toList()); final List<String> subtreeRootIds = subtreeRoots.parallelStream().map(Identifiable::getId).collect(toList()); subtreeRootsCategories.forEach(subtreeRoot -> { final Optional<String> rootAncestorOptional = subtreeRoot.getAncestors() .stream() .map(x -> x.getId()) .filter(ancestorId -> subtreeRootIds.contains(ancestorId)) .findFirst(); if (rootAncestorOptional.isPresent()) { throw new IllegalArgumentException(String.format("category of ID [%s] cannot be subtree root and descendant of [%s]", subtreeRoot.getId(), rootAncestorOptional.get())); } }); final List<Category> includedCategories = new LinkedList<>(); subtreeRootsCategories.forEach(parentCategory -> { includedCategories.add(parentCategory); final List<Category> children = categoryTree.findChildren(parentCategory); includedCategories.addAll(getSubtreeAsFlatList(categoryTree, children)); }); final List<Category> roots = findRoots(includedCategories); return create(includedCategories, roots, subtreeRootsCategories); } }
static String toString(final Category category) { return new ToStringBuilder(category, SdkDefaults.TO_STRING_STYLE) .append("id", category.getId()) .append("version", category.getVersion()) .append("createdAt", category.getCreatedAt()) .append("lastModifiedAt", category.getLastModifiedAt()) .append("name", category.getName()) .append("slug", category.getSlug()) .append("description", category.getDescription()) .append("ancestors", join(category.getAncestors())) .append("parent", category.getParent()) .append("orderHint", category.getOrderHint()) .toString(); }
public static String toString(final Category category) { final List<String> pathInTreeIds = category.getPathInTree().stream().map(Category::getId).collect(toList()); return new ToStringBuilder(category). append("id", category.getId()). append("version", category.getVersion()). append("createdAt", category.getCreatedAt()). append("lastModifiedAt", category.getLastModifiedAt()). append("name", category.getName()). append("slug", category.getSlug()). append("description", category.getDescription()). append("ancestors", join(category.getAncestors())). append("parent", category.getParent()). append("orderHint", category.getOrderHint()). append("children", category.getChildren()). append("pathInTree", join(pathInTreeIds)).toString(); } }
public static String toString(final Category category) { final List<String> pathInTreeIds = category.getPathInTree().stream().map(Category::getId).collect(toList()); return new ToStringBuilder(category, Base.TO_STRING_STYLE). append("id", category.getId()). append("version", category.getVersion()). append("createdAt", category.getCreatedAt()). append("lastModifiedAt", category.getLastModifiedAt()). append("name", category.getName()). append("slug", category.getSlug()). append("description", category.getDescription()). append("ancestors", join(category.getAncestors())). append("parent", category.getParent()). append("orderHint", category.getOrderHint()). append("children", category.getChildren()). append("pathInTree", join(pathInTreeIds)).toString(); } }
public static String toString(final Category category) { final List<String> pathInTreeIds = category.getPathInTree().stream().map(Category::getId).collect(toList()); return new ToStringBuilder(category). append("id", category.getId()). append("version", category.getVersion()). append("createdAt", category.getCreatedAt()). append("lastModifiedAt", category.getLastModifiedAt()). append("name", category.getName()). append("slug", category.getSlug()). append("description", category.getDescription()). append("ancestors", join(category.getAncestors())). append("parent", category.getParent()). append("orderHint", category.getOrderHint()). append("children", category.getChildren()). append("pathInTree", join(pathInTreeIds)).toString(); } }
public static void demoForRendering(final CategoryTree categoryTree) { final Category currentCategory = categoryTree.findBySlug(ENGLISH, "tshirts-men").get(); final List<Reference<Category>> ancestorReferences = currentCategory.getAncestors().stream() .skip(1)//remove top level category .collect(toList()); final StringBuilder stringBuilder = new StringBuilder(); appendToBuilder(ancestorReferences.get(0), stringBuilder, categoryTree, 0, currentCategory); final String actual = stringBuilder.toString(); assertThat(actual).isEqualTo( "1 men\n" + " 3 clothing\n" + " ***7 t-shirts***\n" + " 8 jeans\n" + " 4 shoes\n"); }
@Test public void createAViewForACategoryBreadCrumb() throws Exception { final CategoryTree categoryTree = createCategoryTree(); final Category currentCategory = categoryTree.findBySlug(ENGLISH, "tshirts-men").get(); final List<Reference<Category>> ancestorReferences = currentCategory.getAncestors().stream() .skip(1)//remove top level category .collect(toList()); final Function<Category, String> formatCategory = cat -> defaultString(cat.getExternalId()) + " " + cat.getName().find(ENGLISH).orElse(""); final String ancestorCategoriesString = ancestorReferences.stream() .map(ref -> categoryTree.findById(ref.getId()).get()) .map(formatCategory) .collect(joining(" > ")); final String actual = ancestorCategoriesString + " > " + formatCategory.apply(currentCategory); assertThat(actual).isEqualTo("1 men > 3 clothing > 7 t-shirts"); }
private static void appendToBuilder(final Identifiable<Category> categoryReference, final StringBuilder stringBuilder, final CategoryTree categoryTree, final int level, final Category selectedCategory) { final Category category = categoryTree.findById(categoryReference.getId()).get(); final String name = category.getName().get(ENGLISH); final String externalId = category.getExternalId(); final String offset = StringUtils.repeat(' ', level * 4); stringBuilder.append(offset); if (categoryReference.getId().equals(selectedCategory.getId())) { stringBuilder.append("***"); } stringBuilder.append(externalId).append(" ").append(name); if (categoryReference.getId().equals(selectedCategory.getId())) { stringBuilder.append("***"); } stringBuilder.append("\n"); final Predicate<Category> isAncestor = cat -> selectedCategory.getAncestors().stream().anyMatch(anc -> anc.getId().equals(cat.getId())); final Predicate<Category> isOnHigherLevelThanCurrent = cat -> cat.getAncestors().size() < selectedCategory.getAncestors().size(); final Predicate<Category> isSibling = cat -> cat.getAncestors().equals(selectedCategory.getAncestors()); final List<Category> children = categoryTree.findChildren(category); children.stream() .filter(isAncestor.or(isOnHigherLevelThanCurrent).or(isSibling)) .sorted(EXTERNALID_COMPARATOR) .forEach(child -> appendToBuilder(child, stringBuilder, categoryTree, level + 1, selectedCategory)); } }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()). orderHint(category.getOrderHint()).children(category.getChildren()). pathInTree(category.getPathInTree()); }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()). orderHint(category.getOrderHint()).externalId(category.getExternalId()).children(category.getChildren()). pathInTree(category.getPathInTree()); }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()). orderHint(category.getOrderHint()).externalId(category.getExternalId()).children(category.getChildren()). pathInTree(category.getPathInTree()); }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()).assets(category.getAssets()). orderHint(category.getOrderHint()).externalId(category.getExternalId()); }