Skip to content

Commit

Permalink
fixes after review. replaceLinksInJsonNode moved to utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-zinchenko committed Nov 20, 2024
1 parent 12c9773 commit dbdd938
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.epam.aidial.core.storage.util.UrlUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.mutable.MutableObject;

Expand All @@ -44,6 +42,8 @@
import java.util.stream.Stream;
import javax.annotation.Nullable;

import static com.epam.aidial.core.server.util.CustomApplicationUtils.replaceLinksInJsonNode;

@RequiredArgsConstructor
public class PublicationService {

Expand Down Expand Up @@ -613,28 +613,7 @@ private void replaceCustomAppFiles(Application application, Map<String, String>
application.setCustomProperties(customPropertiesMap);
}

private void replaceLinksInJsonNode(JsonNode node, Map<String, String> replacementLinks, JsonNode parent, String fieldName) {
if (node.isObject()) {
node.fields().forEachRemaining(entry -> replaceLinksInJsonNode(entry.getValue(), replacementLinks, node, entry.getKey()));
} else if (node.isArray()) {
for (int i = 0; i < node.size(); i++) {
JsonNode childNode = node.get(i);
if (childNode.isTextual()) {
String replacement = replacementLinks.get(childNode.textValue());
if (replacement != null) {
((ArrayNode) node).set(i, replacement);
}
} else {
replaceLinksInJsonNode(childNode, replacementLinks, node, String.valueOf(i));
}
}
} else if (node.isTextual()) {
String replacement = replacementLinks.get(node.textValue());
if (replacement != null && parent.isObject()) {
((ObjectNode) parent).put(fieldName, replacement);
}
}
}


private void copyReviewToTargetResources(List<Publication.Resource> resources) {
Map<String, String> replacementLinks = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.epam.aidial.core.storage.service.ResourceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.networknt.schema.CollectorContext;
import com.networknt.schema.InputFormat;
import com.networknt.schema.JsonMetaSchema;
Expand Down Expand Up @@ -160,4 +162,27 @@ public static List<ResourceDescriptor> getFiles(Config config, Application appli
throw new CustomAppValidationException("Failed to obtain list of files attached to the custom app", e);
}
}

public static void replaceLinksInJsonNode(JsonNode node, Map<String, String> replacementLinks, JsonNode parent, String fieldName) {
if (node.isObject()) {
node.fields().forEachRemaining(entry -> replaceLinksInJsonNode(entry.getValue(), replacementLinks, node, entry.getKey()));
} else if (node.isArray()) {
for (int i = 0; i < node.size(); i++) {
JsonNode childNode = node.get(i);
if (childNode.isTextual()) {
String replacement = replacementLinks.get(childNode.textValue());
if (replacement != null) {
((ArrayNode) node).set(i, replacement);
}
} else {
replaceLinksInJsonNode(childNode, replacementLinks, node, String.valueOf(i));
}
}
} else if (node.isTextual()) {
String replacement = replacementLinks.get(node.textValue());
if (replacement != null && parent.isObject()) {
((ObjectNode) parent).put(fieldName, replacement);
}
}
}
}

0 comments on commit dbdd938

Please sign in to comment.