Skip to content

Commit

Permalink
SUPPORT-28804 refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran committed Nov 3, 2024
1 parent 712be23 commit 68640fd
Showing 1 changed file with 120 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,59 +36,7 @@ public Attribute deserialize(JsonParser p, DeserializationContext ctxt) throws I
case STRING:
return TextAttributeBuilder.of().name(node.get("name").asText()).value(valueNode.asText()).build();
case OBJECT:
if (valueNode.has("key") && valueNode.has("label")) {
JsonNode label = valueNode.get("label");
if (label.getNodeType() == OBJECT) {
return LocalizableEnumAttributeBuilder.of()
.name(node.get("name").asText())
.value(valueNode.get("key").textValue())
.build();
}
return EnumAttributeBuilder.of()
.name(node.get("name").asText())
.value(valueNode.get("key").textValue())
.build();
}
if (valueNode.has("currencyCode")) {
return MoneyAttributeBuilder.of()
.name(node.get("name").asText())
.value(typedMoneyBuilder -> typedMoneyBuilder.centPrecisionBuilder()
.currencyCode(valueNode.get("currencyCode").asText())
.centAmount(valueNode.get("centAmount").asLong())
.fractionDigits(valueNode.get("fractionDigits").asInt()))
.build();
}
if (valueNode.has("typeId")) {
return ReferenceAttributeBuilder.of()
.name(node.get("name").asText())
// this is not working because reference contains ID and KeyReference requires ID
// .value((KeyReference) p.getCodec().treeAsTokens(valueNode).readValueAs(getTypeRef(valueNode.get("typeId").asText())))
.value(new KeyReference() {
@Override
public String getKey() {
return valueNode.get("id").asText();
}

@Override
public ReferenceType getTypeId() {
return getReferenceType(valueNode.get("typeId").asText());
}

@Override
public void setKey(String key) {
}
})
.build();
}
return LocalizableTextAttributeBuilder.of()
.name(node.get("name").asText())
.value(localizedStringBuilder -> {
valueNode.fields()
.forEachRemaining(nodeEntry -> localizedStringBuilder.addValue(nodeEntry.getKey(),
nodeEntry.getValue().asText()));
return localizedStringBuilder;
})
.build();
return createAttributeFromObject(node);
case ARRAY:
JsonNode firstElement = valueNode.get(0);
JsonNodeType firstElementNodeType = firstElement.getNodeType();
Expand All @@ -112,72 +60,7 @@ public void setKey(String key) {
.value(booleanValues)
.build();
case OBJECT:
if (firstElement.has("key") && firstElement.has("label")) {
List<String> keys = new ArrayList<>();
valueNode.iterator()
.forEachRemaining(jsonNode -> jsonNode.fields().forEachRemaining(nodeEntry -> {
String key = nodeEntry.getKey();
if (key.equals("key")) {
keys.add(nodeEntry.getValue().asText());
}
}));
JsonNode label = firstElement.get("label");
if (label.getNodeType() == OBJECT) {
return LocalizableEnumSetAttributeBuilder.of()
.name(node.get("name").asText())
.value(keys)
.build();
}
return EnumSetAttributeBuilder.of().name(node.get("name").asText()).value(keys).build();
}
if (firstElement.has("typeId")) {
List<KeyReference> keyReferences = new ArrayList<>();
valueNode.iterator().forEachRemaining(nodeEntry -> {
keyReferences.add(new KeyReference() {
@Override
public String getKey() {
return nodeEntry.get("id").asText();
}

@Override
public ReferenceType getTypeId() {
return getReferenceType(nodeEntry.get("typeId").asText());
}

@Override
public void setKey(String key) {
}
});
});
return ReferenceSetAttributeBuilder.of()
.name(node.get("name").asText())
.value(keyReferences)
.build();
}
if (firstElement.has("currencyCode")) {
List<TypedMoney> values = new ArrayList<>();
valueNode.iterator().forEachRemaining(nodeEntry -> {
values.add(MoneyBuilder.of()
.currencyCode(nodeEntry.get("currencyCode").asText())
.centAmount(nodeEntry.get("centAmount").asLong())
.fractionDigits(nodeEntry.get("fractionDigits").asInt())
.build());
});
return MoneySetAttributeBuilder.of().name(node.get("name").asText()).value(values).build();
}

List<LocalizedString> values = new ArrayList<>();
valueNode.iterator().forEachRemaining(jsonNode -> {
LocalizedStringBuilder localizedStringBuilder = LocalizedStringBuilder.of();
jsonNode.fields()
.forEachRemaining(nodeEntry -> localizedStringBuilder.addValue(nodeEntry.getKey(),
nodeEntry.getValue().asText()));
values.add(localizedStringBuilder.build());
});
return LocalizableTextSetAttributeBuilder.of()
.name(node.get("name").asText())
.value(values)
.build();
return createSetAttributeFromObject(node);
}
default:
AttributeImpl attribute = new AttributeImpl();
Expand All @@ -192,4 +75,122 @@ private ReferenceType getReferenceType(String typeId) {
.findFirst()
.orElse(null);
}

private Attribute createAttributeFromObject(JsonNode node) {
JsonNode valueNode = node.get("value");
if (valueNode.has("key") && valueNode.has("label")) {
JsonNode label = valueNode.get("label");
if (label.getNodeType() == OBJECT) {
return LocalizableEnumAttributeBuilder.of()
.name(node.get("name").asText())
.value(valueNode.get("key").textValue())
.build();
}
return EnumAttributeBuilder.of()
.name(node.get("name").asText())
.value(valueNode.get("key").textValue())
.build();
}
if (valueNode.has("currencyCode")) {
return MoneyAttributeBuilder.of()
.name(node.get("name").asText())
.value(typedMoneyBuilder -> typedMoneyBuilder.centPrecisionBuilder()
.currencyCode(valueNode.get("currencyCode").asText())
.centAmount(valueNode.get("centAmount").asLong())
.fractionDigits(valueNode.get("fractionDigits").asInt()))
.build();
}
if (valueNode.has("typeId")) {
return ReferenceAttributeBuilder.of()
.name(node.get("name").asText())
// this is not working because reference contains ID and KeyReference requires ID
// .value((KeyReference) p.getCodec().treeAsTokens(valueNode).readValueAs(getTypeRef(valueNode.get("typeId").asText())))
.value(createKeyReference(valueNode))
.build();
}
return LocalizableTextAttributeBuilder.of()
.name(node.get("name").asText())
.value(localizedStringBuilder -> {
valueNode.fields()
.forEachRemaining(nodeEntry -> localizedStringBuilder.addValue(nodeEntry.getKey(),
nodeEntry.getValue().asText()));
return localizedStringBuilder;
})
.build();
}

private Attribute createSetAttributeFromObject(JsonNode node) {
JsonNode valueNode = node.get("value");
JsonNode firstElement = valueNode.get(0);
if (firstElement.has("key") && firstElement.has("label")) {
List<String> keys = new ArrayList<>();
valueNode.iterator()
.forEachRemaining(jsonNode -> jsonNode.fields().forEachRemaining(nodeEntry -> {
String key = nodeEntry.getKey();
if (key.equals("key")) {
keys.add(nodeEntry.getValue().asText());
}
}));
JsonNode label = firstElement.get("label");
if (label.getNodeType() == OBJECT) {
return LocalizableEnumSetAttributeBuilder.of()
.name(node.get("name").asText())
.value(keys)
.build();
}
return EnumSetAttributeBuilder.of().name(node.get("name").asText()).value(keys).build();
}
if (firstElement.has("typeId")) {
List<KeyReference> keyReferences = new ArrayList<>();
valueNode.iterator().forEachRemaining(nodeEntry -> {
keyReferences.add(createKeyReference(nodeEntry));
});
return ReferenceSetAttributeBuilder.of()
.name(node.get("name").asText())
.value(keyReferences)
.build();
}
if (firstElement.has("currencyCode")) {
List<TypedMoney> values = new ArrayList<>();
valueNode.iterator().forEachRemaining(nodeEntry -> {
values.add(MoneyBuilder.of()
.currencyCode(nodeEntry.get("currencyCode").asText())
.centAmount(nodeEntry.get("centAmount").asLong())
.fractionDigits(nodeEntry.get("fractionDigits").asInt())
.build());
});
return MoneySetAttributeBuilder.of().name(node.get("name").asText()).value(values).build();
}

List<LocalizedString> values = new ArrayList<>();
valueNode.iterator().forEachRemaining(jsonNode -> {
LocalizedStringBuilder localizedStringBuilder = LocalizedStringBuilder.of();
jsonNode.fields()
.forEachRemaining(nodeEntry -> localizedStringBuilder.addValue(nodeEntry.getKey(),
nodeEntry.getValue().asText()));
values.add(localizedStringBuilder.build());
});
return LocalizableTextSetAttributeBuilder.of()
.name(node.get("name").asText())
.value(values)
.build();
}

private KeyReference createKeyReference(JsonNode node) {
return new KeyReference() {
@Override
public String getKey() {
return node.get("id").asText();
}

@Override
public ReferenceType getTypeId() {
return getReferenceType(node.get("typeId").asText());
}

@Override
public void setKey(String key) {
}
};
}
}

0 comments on commit 68640fd

Please sign in to comment.