@Override protected String convertEnumValue(final EnumValue enumValue, final Attribute attribute, final ProductType productType) { return enumValue.getLabel(); }
public static List<EnumValue> getValuesAsListOfEnumValue() { return EnumSet.allOf(CreditCardNetwork.class) .stream() .map(p -> EnumValue.of(p.name(), p.getLabel())) .collect(Collectors.toList()); } }
static AttributeAccess<EnumValue> ofEnumValue() { return AttributeAccessImpl.ofEnumLike(EnumValue.typeReference(), EnumAttributeType.class); }
static <T> AttributeDraft of(final String name, final T value) { final AttributeDraft result; if (value instanceof LocalizedEnumValue) { result = of(name, ((LocalizedEnumValue) value).getKey()); } else if (value instanceof EnumValue) { result = of(name, ((EnumValue) value).getKey()); } else if (value instanceof Set) { final Set<?> set = (Set<?>) value; if (!set.isEmpty()) { final Object setValue = set.stream().findAny().get(); if (setValue instanceof LocalizedEnumValue || setValue instanceof EnumValue) { //WithKey is a interface the enum like implement final Set<String> newValues = set.stream().map(x -> ((WithKey) x).getKey()).collect(toSet()); result = of(name, newValues); } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } return result; }
public static <T> AttributeDraft of(final String name, final T value) { final AttributeDraft result; if (value instanceof LocalizedEnumValue) { result = of(name, ((LocalizedEnumValue) value).getKey()); } else if (value instanceof EnumValue) { result = of(name, ((EnumValue) value).getKey()); } else if (value instanceof Set) { final Set<?> set = (Set<?>) value; if (!set.isEmpty()) { final Object setValue = set.stream().findAny().get(); if (setValue instanceof LocalizedEnumValue || setValue instanceof EnumValue) { //WithKey is a interface the enum like implement final Set<String> newValues = set.stream().map(x -> ((WithKey) x).getKey()).collect(toSet()); result = of(name, newValues); } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } return result; }
@Override protected String convertEnumValue(final EnumValue enumValue, final Attribute attribute, final ProductType productType) { return enumValue.getLabel(); }
static <T> AttributeDraft of(final String name, final T value) { final AttributeDraft result; if (value instanceof LocalizedEnumValue) { result = of(name, ((LocalizedEnumValue) value).getKey()); } else if (value instanceof EnumValue) { result = of(name, ((EnumValue) value).getKey()); } else if (value instanceof Set) { final Set<?> set = (Set<?>) value; if (!set.isEmpty()) { final Object setValue = set.stream().findAny().get(); if (setValue instanceof LocalizedEnumValue || setValue instanceof EnumValue) { //WithKey is a interface the enum like implement final Set<String> newValues = set.stream().map(x -> ((WithKey) x).getKey()).collect(toSet()); result = of(name, newValues); } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } return result; }
static AttributeAccess<EnumValue> ofEnumValue() { return AttributeAccessImpl.ofEnumLike(EnumValue.typeReference(), EnumAttributeType.class); }
@Override protected String convertEnumValue(final EnumValue enumValue, final Attribute attribute, final ProductType productType) { return enumValue.getLabel(); }
@Test public void facetedSearchOnEnumKeySetAttributes() throws Exception { testResultWithTerms(PRODUCT_MODEL.allVariants().attribute().ofEnumSet(ATTR_NAME_ENUM_SET).key().is(ENUM_THREE.getKey()), ids -> assertThat(ids).containsOnly(product1.getId()), termStats -> assertThat(termStats).containsExactly( TermStats.of(ENUM_TWO.getKey(), 2L), TermStats.of(ENUM_THREE.getKey(), 1L))); }
private static FieldDefinition stateFieldDefinition() { final List<EnumValue> values = asList( EnumValue.of("published", "the category is publicly visible"), EnumValue.of("draft", "the category should not be displayed in the frontend") ); final boolean required = false; final LocalizedString label = en("state of the category concerning to show it publicly"); final String fieldName = "state"; return FieldDefinition .of(EnumFieldType.of(values), fieldName, label, required); }
static AttributeAccess<EnumValue> ofEnumValue() { return AttributeAccessImpl.ofEnumLike(EnumValue.typeReference(), EnumAttributeType.class); }
public static String attributeValue(final Attribute attribute, final List<Locale> locales, final MetaProductType metaProductType) { final AttributeExtraction<String> attributeExtraction = AttributeExtraction.of(metaProductType, attribute); return attributeExtraction .ifIs(AttributeAccess.ofLocalizedString(), v -> v.find(locales).orElse("")) .ifIs(AttributeAccess.ofLocalizedEnumValue(), v -> v.getLabel().find(locales).orElse("")) .ifIs(AttributeAccess.ofEnumValue(), v -> v.getLabel()) .ifIs(AttributeAccess.ofString(), v -> v) .findValue() .orElse(""); }
@Test public void queryByName() throws Exception { withTShirtProductType(type -> { final ProductType productType = client().executeBlocking(ProductTypeQuery.of().byName("t-shirt")).head().get(); final Optional<AttributeDefinition> sizeAttribute = productType.findAttribute("size"); final List<EnumValue> possibleSizeValues = sizeAttribute. map(attrib -> ((EnumAttributeType) attrib.getAttributeType()).getValues()). orElse(Collections.<EnumValue>emptyList()); final List<EnumValue> expected = asList(EnumValue.of("S", "S"), EnumValue.of("M", "M"), EnumValue.of("X", "X")); assertThat(possibleSizeValues).isEqualTo(expected); }); }
@Test public void facetedSearchOnEnumLabelSetAttributes() throws Exception { testResultWithTerms(PRODUCT_MODEL.allVariants().attribute().ofEnumSet(ATTR_NAME_ENUM_SET).label().is(ENUM_THREE.getLabel()), ids -> assertThat(ids).containsOnly(product1.getId()), termStats -> assertThat(termStats).containsExactly( TermStats.of(ENUM_TWO.getLabel(), 2L), TermStats.of(ENUM_THREE.getLabel(), 1L))); }
@Test public void facetedSearchOnEnumKeyAttributes() throws Exception { testResultWithTerms(PRODUCT_MODEL.allVariants().attribute().ofEnum(ATTR_NAME_ENUM).key().is(ENUM_TWO.getKey()), ids -> assertThat(ids).containsOnly(product1.getId()), termStats -> assertThat(termStats).containsOnly( TermStats.of(ENUM_TWO.getKey(), 1L), TermStats.of(ENUM_THREE.getKey(), 1L))); }
@Test public void changePlainEnumValueLabel() throws Exception { final String attributeName = randomKey(); final AttributeDefinition attributeDefinition = AttributeDefinitionBuilder.of(attributeName, randomSlug(), EnumAttributeType.of( EnumValue.of("key1", "label 1"), EnumValue.of("key2", "label 2") )).build(); final String key = randomKey(); final ProductTypeDraft productTypeDraft = ProductTypeDraft.of(key, key, key, singletonList(attributeDefinition)); withUpdateableProductType(client(), () -> productTypeDraft, productType -> { final ProductType updatedProductType = client().executeBlocking(ProductTypeUpdateCommand.of(productType, ChangePlainEnumValueLabel.of(attributeName, EnumValue.of("key2", "label 2 (updated)")))); final EnumAttributeType updatedAttributeType = (EnumAttributeType) updatedProductType.getAttribute(attributeName).getAttributeType(); assertThat(updatedAttributeType.getValues()) .containsExactly(EnumValue.of("key1", "label 1"), EnumValue.of("key2", "label 2 (updated)")); return updatedProductType; }); }
@Test public void readAttributeGetValueAs() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final String attributeValue = masterVariant.findAttribute(SIZE_ATTR_NAME) .map((Attribute a) -> { final EnumValue enumValue = a.getValueAsEnumValue(); return enumValue.getLabel(); }) .orElse("not found"); assertThat(attributeValue).isEqualTo("S"); }