@Nullable protected <X> String convertReference(final Reference<X> reference, final ProductType productType) { return reference.getId(); }
private Map<String, List<Category>> getChildrenByParentIdMap(final List<Category> allCategoriesAsFlatList) { final Map<String, List<Category>> childrenByParentId = new HashMap<>(); allCategoriesAsFlatList.forEach(category -> Optional.ofNullable(category.getParent()).ifPresent(parentReference -> { final String parentId = parentReference.getId(); final List<Category> entries = childrenByParentId.getOrDefault(parentId, new LinkedList<>()); entries.add(category); childrenByParentId.put(parentId, entries); }) ); return childrenByParentId; }
@SuppressWarnings("rawtypes")//at runtime generic type is not determinable @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Reference reference = (Reference) o; if (!getId().equals(reference.getId())) return false; if (!getTypeId().equals(reference.getTypeId())) return false; return true; }
@Override public void serialize(final Reference reference, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeStartObject(); jsonGenerator.writeStringField("id", reference.getId()); jsonGenerator.writeStringField("typeId", reference.getTypeId()); jsonGenerator.writeObjectField("obj", reference.getObj()); jsonGenerator.writeEndObject(); } }
@Override public QueryPredicate<T> isIn(final List<? extends Referenceable<R>> elements) { final List<String> ids = elements.stream().map(elem -> elem.toReference().getId()).collect(toList()); return isInIds(ids); }
@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); }
@Nullable @Override public T convert(final Attribute attribute, final Referenceable<ProductType> productType) { final String productTypeId = productType.toReference().getId(); final Optional<ProductType> productTypeOptional = productTypes.findById(productTypeId); return productTypeOptional.map(pt -> convertWithProductType(attribute, pt)).orElse(null); }
@Override public final QueryPredicate<T> isIn(final Iterable<? extends Referenceable<R>> references) { final List<String> ids = toStream(references).map(r -> r.toReference().getId()).collect(toList()); return id().isIn(ids); }
private static Multimap<String, Category> buildParentMultiMap(List<Category> categoriesWithParents) { final Multimap<String, Category> categoriesByParentId = new Multimap<>(); for (final Category categoryWithParent : categoriesWithParents) { final String parentId = categoryWithParent.getParent().get().getId(); categoriesByParentId.put(parentId, categoryWithParent); } return categoriesByParentId; }
private static Multimap<String, Category> buildParentMultiMap(List<Category> categoriesWithParents) { final Multimap<String, Category> categoriesByParentId = new Multimap<>(); for (final Category categoryWithParent : categoriesWithParents) { final String parentId = categoryWithParent.getParent().get().getId(); categoriesByParentId.put(parentId, categoryWithParent); } return categoriesByParentId; }
private static Multimap<String, Category> buildParentMultiMap(List<Category> categoriesWithParents) { final Multimap<String, Category> categoriesByParentId = new Multimap<>(); for (final Category categoryWithParent : categoriesWithParents) { final String parentId = categoryWithParent.getParent().get().getId(); categoriesByParentId.put(parentId, categoryWithParent); } return categoriesByParentId; }
/** * Serializer to convert the given reference into the identifier value. * @param <R> type of the reference. * @return the serializer for references of the given type. */ public static <R> TypeSerializer<Referenceable<R>> ofReference() { return new TypeSerializer<>(v -> withQuotes(v.toReference().getId())); }
@Override public QueryPredicate<ProductProjection> is(final Referenceable<Product> product) { return id().is(product.toReference().getId()); } }
@Override public QueryPredicate<ProductProjection> is(final Referenceable<Product> product) { return id().is(product.toReference().getId()); } }
@Test public void cartWithRefusedGifts() { final Cart cart = SphereJsonUtils.readObjectFromResource("carts/cart-with-refused-gifts.json", Cart.class); final List<Reference<CartDiscount>> refusedGifts = cart.getRefusedGifts(); assertThat(refusedGifts).hasSize(1); final Reference<CartDiscount> refusedGift = refusedGifts.get(0); assertThat(refusedGift.getId()).isEqualTo("<my-cart-discount-id>"); }
@Test public void deserializeRefusedGifts(){ final Order order = SphereJsonUtils.readObjectFromResource("order/order-with-refused-gift.json", Order.class); assertThat(order.getRefusedGifts()).hasSize(1); assertThat(order.getRefusedGifts().get(0).getId()).isEqualTo("<my-order-discount-id>"); }
@Test public void createFilledReference() throws Exception { final Reference<TestEntity> reference = newFilledReference(); assertThat(reference.getId()).isEqualTo(id); assertThat(reference.getTypeId()).isEqualTo(typeId); assertThat(reference.getObj()).isEqualTo(new TestEntity("value")); }
@Test public void createEmptyReference() throws Exception { final Reference<TestEntity> reference = newEmptyReference(); assertThat(reference.getId()).isEqualTo(id); assertThat(reference.getTypeId()).isEqualTo(typeId); assertThat(reference.getObj()).isNull(); }
@Test public void toReference() { final Category category = getCategory1(); final Reference<Category> categoryReference = category.toReference(); assertThat(category.getId()).isEqualTo(categoryReference.getId()); }
@Test public void cartState() { OrderFixtures.withOrder(client(), order -> { final CartQuery query = CartQuery.of() .plusPredicates(m -> m.cartState().is(CartState.ORDERED)) .plusPredicates(m -> m.is(order.getCart())); final List<Cart> results = client().executeBlocking(query).getResults(); assertThat(results).hasSize(1); assertThat(results.get(0).getId()).isEqualTo(order.getCart().getId()); return order; }); }