From 89332d93c1ca292ef54ce635aba7dad1bbc65cab Mon Sep 17 00:00:00 2001 From: Bryant Austin Date: Thu, 26 Jan 2023 12:44:38 -0700 Subject: [PATCH] =?UTF-8?q?fixed=20testDataDateRoller=20to=20handle=20new?= =?UTF-8?q?=20format=20of=20prefetch=20-=20no=20respon=E2=80=A6=20(#409)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixed testDataDateRoller to handle new format of prefetch - no response in items --- .../dateroller/HookDataDateRoller.java | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/tooling/src/main/java/org/opencds/cqf/tooling/dateroller/HookDataDateRoller.java b/tooling/src/main/java/org/opencds/cqf/tooling/dateroller/HookDataDateRoller.java index 05eb76a9c..f04e462ee 100644 --- a/tooling/src/main/java/org/opencds/cqf/tooling/dateroller/HookDataDateRoller.java +++ b/tooling/src/main/java/org/opencds/cqf/tooling/dateroller/HookDataDateRoller.java @@ -63,18 +63,30 @@ public void rollContextDates(JsonObject context) { public void rollPrefetchItemsDates(JsonObject prefetch) { JsonObject item; for (int i = 1; i <= prefetch.size(); i++) { + String itemName = "item" + i; try { - item = prefetch.getAsJsonObject("item" + i); + item = prefetch.getAsJsonObject(itemName); } catch (Exception ex) { + // the following is simply to preserve the original order - the item is present, but null + prefetch.remove(itemName); + prefetch.add(itemName, null); continue; } if(null == item){ continue; } - IBaseResource resource = resourceParser.parseResource(item.getAsJsonObject("resource").toString()); - if (null == resource) { + IBaseResource resource = null; + String resourceTypeName = "resourceType"; + if(item.getAsJsonPrimitive("resourceType") != null){ + resource = resourceParser.parseResource(item.toString()); + } + if(resource == null && item.getAsJsonObject("resource").toString()!= null){ + resource = resourceParser.parseResource(item.getAsJsonObject("resource").toString()); + resourceTypeName = "resource"; + } + if(resource == null){ logger.info("This hook did not contain prefetch items"); - continue; + return; } if (resource.fhirType().equalsIgnoreCase("bundle")) { ResourceDataDateRoller.rollBundleDates(fhirContext, resource); @@ -82,18 +94,26 @@ public void rollPrefetchItemsDates(JsonObject prefetch) { ResourceDataDateRoller.rollResourceDates(fhirContext, resource); } JsonObject response = item.getAsJsonObject("response"); - item.add("response", response); - addUpdatedJsonObject(resource, item, "resource"); -// addUpdatedJsonObject(resource, prefetch, "item" + i); + if(response != null){ + item.add("response", response); + } + item = addUpdatedJsonObject(resource, item, resourceTypeName); + prefetch.remove(itemName); + prefetch.add(itemName, item); } } - public void addUpdatedJsonObject(IBaseResource resource, JsonObject objectToAddTo, String objectName){ + public JsonObject addUpdatedJsonObject(IBaseResource resource, JsonObject objectToAddTo, String objectName){ JsonObject objectToAdd; - objectToAddTo.remove(objectName); JsonElement newItem = JsonParser.parseString(resourceParser.setPrettyPrint(true).encodeResourceToString(resource)); + // handle the new format - no response in items + if(objectName.equalsIgnoreCase("resourceType")){ + return newItem.getAsJsonObject(); + } + // handle the old format - a response followed by the resource + objectToAddTo.remove(objectName); objectToAdd = newItem.getAsJsonObject(); objectToAddTo.add(objectName, objectToAdd); - + return objectToAddTo; } }