Skip to content

Commit

Permalink
#OS-251 Fix removing of prefix shard label from complete node (#381)
Browse files Browse the repository at this point in the history
* #OS-251 Fix removing of prefix shard label from osid

* #OS-251 Adding Json util method to trim prefix based on field name

* #OS-251 removing unnecessary variables

* #OS-251 formatting unnecessary variables

* #OS-251 formatting unnecessary variables

* #OS-251 formatting unnecessary variables
  • Loading branch information
sknirmalkar89 authored Feb 28, 2020
1 parent 50e980d commit 655a856
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,32 @@ public static JsonNode diffJsonNode(JsonNode existingNode, JsonNode latestNode)
JsonNode patchNode = JsonDiff.asJson(existingNode, latestNode);
return patchNode;
}


/**
* Trimming a given prefix if present from each TextNode value corresponding to
* the fieldName in parent's hierarchy (including nested objects).
*
* @param parent
* @param prefix
*/
public static void trimPrefix(ObjectNode parent, String fieldName, String prefix) {

parent.fields().forEachRemaining(entry -> {
JsonNode entryValue = entry.getValue();

if ( entry.getKey().equals(fieldName) && entryValue.isValueNode() && entryValue.toString().contains(prefix)) {
parent.put(entry.getKey(), entry.getValue().asText().replaceFirst(prefix, ""));

} else if (entryValue.isArray()) {
for (int i = 0; i < entryValue.size(); i++) {
if (entry.getValue().get(i).isObject())
trimPrefix((ObjectNode) entry.getValue().get(i), fieldName, prefix);
}
} else if (entryValue.isObject()) {
trimPrefix((ObjectNode) entry.getValue(), fieldName, prefix);
}

});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public void updateEntity(Shard shard, String userId, String id, String jsonStrin
if (!shard.getShardLabel().isEmpty()) {
// Replace osid without shard details
String prefix = shard.getShardLabel() + RecordIdentifier.getSeparator();
JSONUtil.trimPrefix((ObjectNode) inputNode, prefix);
JSONUtil.trimPrefix((ObjectNode)inputNode, uuidPropertyName, prefix);
}

// The entity type is a child and so could be different from parent entity type.
Expand Down

0 comments on commit 655a856

Please sign in to comment.