Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Entity Property with list #117

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -395,28 +395,41 @@ public void addEntityProperty(String entityName, String entityId, JsonNode input
String parentURIPointer = propertyURIPointer.head().toString();

JsonNode parentNode = getParentNode(entityName, updateNode, parentURIPointer);
JsonNode propertyNode = parentNode.get(propertyName);

createOrUpdateProperty(entityName, inputJson, updateNode, propertyName, (ObjectNode) parentNode, propertyNode);
JsonNode propertyNode = getPropertyNode(propertyName, parentNode);
createOrUpdateProperty(entityName, inputJson, updateNode, propertyName, parentNode, propertyNode);
updateEntityAndState(existingNode, updateNode, "");
}

private JsonNode getPropertyNode(String propertyName, JsonNode parentNode) {
if(parentNode.isArray()){
for (JsonNode item : parentNode) {
if (item.get(uuidPropertyName).toString().replaceAll("\"", "").equals(propertyName)) {
return item;
}
}
}
return parentNode.get(propertyName);
}

public String addAttestationProperty(String entityName, String entityId, String propertyName, JsonNode inputJson, HttpServletRequest request) throws Exception {
String userId = getUserId(request, entityName);
JsonNode existingEntityNode = readEntity(userId, entityName, entityId, false, null, false);
JsonNode nodeToUpdate = existingEntityNode.deepCopy();
JsonNode parentNode = nodeToUpdate.get(entityName);
JsonNode propertyNode = parentNode.get(propertyName);
createOrUpdateProperty(entityName, inputJson, nodeToUpdate, propertyName, (ObjectNode) parentNode, propertyNode);
createOrUpdateProperty(entityName, inputJson, nodeToUpdate, propertyName, parentNode, propertyNode);
return updateEntityAndState(existingEntityNode, nodeToUpdate, userId);
}

private void createOrUpdateProperty(String entityName, JsonNode inputJson, JsonNode updateNode, String propertyName, ObjectNode parentNode, JsonNode propertyNode) throws JsonProcessingException {
private void createOrUpdateProperty(String entityName, JsonNode inputJson, JsonNode updateNode, String propertyName, JsonNode parentNode, JsonNode propertyNode) throws JsonProcessingException {
if (propertyNode != null && !propertyNode.isMissingNode()) {
updateProperty(inputJson, propertyName, parentNode, propertyNode);
if (parentNode.isArray()) {
updateItemInArray(inputJson, propertyNode);
} else {
updateProperty(inputJson, propertyName, (ObjectNode) parentNode, propertyNode);
}
} else {
// if array property
createProperty(entityName, inputJson, updateNode, propertyName, parentNode);
createProperty(entityName, inputJson, updateNode, propertyName, (ObjectNode)parentNode);
}
}

Expand All @@ -443,6 +456,16 @@ private void updateProperty(JsonNode inputJson, String propertyName, ObjectNode
}
}

private void updateItemInArray(JsonNode inputJson, JsonNode propertyNode) {
if (propertyNode.isArray()) {
((ArrayNode) propertyNode).add(inputJson);
} else if (propertyNode.isObject()) {
inputJson.fields().forEachRemaining(f -> {
((ObjectNode) propertyNode).set(f.getKey(), f.getValue());
});
}
}

private JsonNode getParentNode(String entityName, JsonNode jsonNode, String parentURIPointer) throws Exception {
JsonNode parentNode;
if (parentURIPointer.equals("")) {
Expand Down