From 655a856c6e2ba474433bf4f6bfc0aafa51d8e429 Mon Sep 17 00:00:00 2001 From: sknirmalkar89 <59080585+sknirmalkar89@users.noreply.github.com> Date: Fri, 28 Feb 2020 17:13:45 +0530 Subject: [PATCH] #OS-251 Fix removing of prefix shard label from complete node (#381) * #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 --- .../registry/middleware/util/JSONUtil.java | 28 +++++++++++++++++++ .../service/impl/RegistryServiceImpl.java | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/java/middleware-commons/src/main/java/io/opensaber/registry/middleware/util/JSONUtil.java b/java/middleware-commons/src/main/java/io/opensaber/registry/middleware/util/JSONUtil.java index bdb82bbdf..b7a0757a8 100644 --- a/java/middleware-commons/src/main/java/io/opensaber/registry/middleware/util/JSONUtil.java +++ b/java/middleware-commons/src/main/java/io/opensaber/registry/middleware/util/JSONUtil.java @@ -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); + } + + }); + } } diff --git a/java/registry/src/main/java/io/opensaber/registry/service/impl/RegistryServiceImpl.java b/java/registry/src/main/java/io/opensaber/registry/service/impl/RegistryServiceImpl.java index d43b72402..af1ac72de 100755 --- a/java/registry/src/main/java/io/opensaber/registry/service/impl/RegistryServiceImpl.java +++ b/java/registry/src/main/java/io/opensaber/registry/service/impl/RegistryServiceImpl.java @@ -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.