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; }
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 <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 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))); }
@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 changeEnumValueOrder() { withUpdateableType(client(), type -> { final String fieldName = TypeFixtures.ENUM_FIELD_NAME; final FieldType fieldType = type.getFieldDefinitionByName(fieldName).getType(); final List<EnumValue> oldEnumValues = ((EnumFieldType) fieldType).getValues(); final List<String> enumKeys = reverse(oldEnumValues.stream() .map(enumValue -> enumValue.getKey()) .collect(Collectors.toList()) ); final Type updatedType = client().executeBlocking(TypeUpdateCommand.of(type, ChangeEnumValueOrder.of(fieldName, enumKeys))); final FieldType updatedFieldType = updatedType.getFieldDefinitionByName(fieldName).getType(); final List<EnumValue> newEnumValues = ((EnumFieldType) updatedFieldType).getValues(); assertThat(newEnumValues).containsAll(oldEnumValues); assertThat(newEnumValues.size()).isEqualTo(oldEnumValues.size()); return updatedType; }); }
@Test public void onEnumKeySetAttributes() throws Exception { testProductIds(PRODUCT_MODEL.allVariants().attribute().ofEnumSet(ATTR_NAME_ENUM_SET).key().is(ENUM_THREE.getKey()), ids -> assertThat(ids).containsOnly(product1.getId())); }
@Test public void onEnumKeyAttributes() throws Exception { testProductIds(PRODUCT_MODEL.allVariants().attribute().ofEnum(ATTR_NAME_ENUM).key().is(ENUM_TWO.getKey()), ids -> assertThat(ids).containsOnly(product1.getId())); }