@Override protected String convertLocalizedEnumValue(final LocalizedEnumValue localizedEnumValue, final Attribute attribute, final ProductType productType) { return localizedEnumValue.getLabel().getTranslation(getLocales()); }
static AttributeAccess<LocalizedEnumValue> ofLocalizedEnumValue() { return AttributeAccessImpl.ofEnumLike(LocalizedEnumValue.typeReference(), LocalizedEnumAttributeType.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; }
@Test public void shouldSerializeLocalizedEnumValueCorrectly() { final LocalizedString localizedString = LocalizedString.of(Locale.GERMAN, "german-text"); final LocalizedEnumValue localizedEnumValue = LocalizedEnumValue.of("key", localizedString); final AttributeDraft attributeDraft = AttributeDraft.of(AttributeAccess.ofLocalizedEnumValue().ofName("name"), localizedEnumValue); final JsonNode jsonValue = attributeDraft.getValue(); assertThat(jsonValue.get("key").asText()).isEqualTo(localizedEnumValue.getKey()); final JsonNode jsonLabel = jsonValue.get("label"); assertThat(jsonLabel.get("de").asText()).isEqualTo(localizedEnumValue.getLabel().get(Locale.GERMAN)); } }
private static FieldDefinition localizedEnumFieldDefinition() { final List<LocalizedEnumValue> localizedEnumValues = asList("1", "2").stream() .map(s -> LocalizedEnumValue.of("key" + s, en("label " + s))) .collect(toList()); return fieldDefinition(LocalizedEnumFieldType.of(localizedEnumValues), LOCALIZED_ENUM_FIELD_NAME); }
public static LocalizedEnumValue of(final String key, final LocalizedStrings label) { return new LocalizedEnumValue(key, label); }
@Test public void execution() { final Project project = client().executeBlocking(ProjectGet.of()); final CartClassificationDraft cartClassification = CartClassificationDraftBuilder.of(Collections.singleton(LocalizedEnumValue.of("Small", LocalizedString.of(Locale.ENGLISH, "Small", Locale.GERMAN, "Klein")))).build(); final Project updatedProjectCartClassification = client().executeBlocking(ProjectUpdateCommand.of(project, SetShippingRateInputType.of(cartClassification))); assertThat(updatedProjectCartClassification.getShippingRateInputType()).isNotNull(); assertThat(updatedProjectCartClassification.getShippingRateInputType().getType()).isEqualTo("CartClassification"); assertThat(((CartClassification)updatedProjectCartClassification.getShippingRateInputType()).getValues()).containsOnly( LocalizedEnumValue.of("Small", LocalizedString.of(Locale.ENGLISH, "Small", Locale.GERMAN, "Klein")) ); } }
@Override protected String convertLocalizedEnumValue(final LocalizedEnumValue localizedEnumValue, final Attribute attribute, final ProductType productType) { return localizedEnumValue.getLabel().getTranslation(getLocales()); }
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<LocalizedEnumValue> ofLocalizedEnumValue() { return AttributeAccessImpl.ofEnumLike(LocalizedEnumValue.typeReference(), LocalizedEnumAttributeType.class); }
@Test public void changeLocalizedEnumValueLabel() throws Exception { final String attributeName = randomKey(); final LocalizedString label1 = LocalizedString.ofEnglish("label 1"); final LocalizedString label2 = LocalizedString.ofEnglish("label 2"); final LocalizedString newLabel2 = LocalizedString.ofEnglish("label 2 (updated)"); final AttributeDefinition attributeDefinition = AttributeDefinitionBuilder.of(attributeName, randomSlug(), LocalizedEnumAttributeType.of( LocalizedEnumValue.of("key1", label1), LocalizedEnumValue.of("key2", label2) )).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, ChangeLocalizedEnumValueLabel.of(attributeName, LocalizedEnumValue.of("key2", newLabel2)))); final LocalizedEnumAttributeType updatedAttributeType = (LocalizedEnumAttributeType) updatedProductType.getAttribute(attributeName).getAttributeType(); assertThat(updatedAttributeType.getValues()) .containsExactly(LocalizedEnumValue.of("key1", label1), LocalizedEnumValue.of("key2", newLabel2)); return updatedProductType; }); }
@Override protected String convertLocalizedEnumValue(final LocalizedEnumValue localizedEnumValue, final Attribute attribute, final ProductType productType) { return localizedEnumValue.getLabel().getTranslation(getLocales()); }
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; }
public static AttributeAccess<LocalizedEnumValue> ofLocalizedEnumValue() { return ofEnumLike(LocalizedEnumValue.typeReference(), LocalizedEnumType.class); }
@Test public void addLocalizedEnumValue() throws Exception { withUpdateableProductType(client(), productType -> { final String attributeName = "color"; assertThat(productType.getAttribute(attributeName)).isNotNull(); final LocalizedEnumValue value = LocalizedEnumValue.of("brown", LocalizedString.of(Locale.ENGLISH, "brown").plus(GERMAN, "braun")); final ProductType updatedProductType = client().executeBlocking(ProductTypeUpdateCommand.of(productType, AddLocalizedEnumValue.of(attributeName, value))); assertThat(updatedProductType.getAttribute(attributeName).getAttributeType()) .isInstanceOf(LocalizedEnumAttributeType.class) .matches(type -> ((LocalizedEnumAttributeType) type).getValues().contains(value)); return updatedProductType; }); }
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 facetedSearchOnLocEnumKeySetAttributes() throws Exception { testResultWithTerms(PRODUCT_MODEL.allVariants().attribute().ofLocalizedEnumSet(ATTR_NAME_LOC_ENUM_SET).key().is(LOC_ENUM_THREE.getKey()), ids -> assertThat(ids).containsOnly(product1.getId()), termStats -> assertThat(termStats).containsExactly( TermStats.of(LOC_ENUM_TWO.getKey(), 2L), TermStats.of(LOC_ENUM_THREE.getKey(), 1L))); }
public static AttributeAccess<LocalizedEnumValue> ofLocalizedEnumValue() { return ofEnumLike(LocalizedEnumValue.typeReference(), LocalizedEnumAttributeDefinition.class); }
@Test public void changeEnumValueKey(){ withUpdateableProductType(client(), productType -> { final String attributeName = "color"; final LocalizedEnumAttributeType attributeType = (LocalizedEnumAttributeType) productType.getAttribute(attributeName) .getAttributeType(); assertThat(attributeType.getValues()).contains(LocalizedEnumValue.of("red", LocalizedString.of(ENGLISH,"red",GERMAN,"rot"))); final ProductType updatedProductType = client().executeBlocking(ProductTypeUpdateCommand.of(productType, ChangeEnumKey.of(attributeName, "red","rouge"))); final LocalizedEnumAttributeType updatedAttributeType = (LocalizedEnumAttributeType) updatedProductType.getAttribute(attributeName) .getAttributeType(); assertThat(updatedAttributeType.getValues()).contains(LocalizedEnumValue.of("rouge", LocalizedString.of(ENGLISH,"red",GERMAN,"rot"))); return updatedProductType; }); }
@Test public void facetedSearchOnLocEnumLabelSetAttributes() throws Exception { testResultWithTerms(PRODUCT_MODEL.allVariants().attribute().ofLocalizedEnumSet(ATTR_NAME_LOC_ENUM_SET).label().locale(GERMAN).is(LOC_ENUM_THREE.getLabel().get(GERMAN)), ids -> assertThat(ids).containsOnly(product1.getId()), termStats -> assertThat(termStats).containsExactly( TermStats.of(LOC_ENUM_TWO.getLabel().get(GERMAN), 2L), TermStats.of(LOC_ENUM_THREE.getLabel().get(GERMAN), 1L))); }