|
11 | 11 |
|
12 | 12 | import javax.validation.constraints.NotNull; |
13 | 13 | import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Iterator; |
| 16 | +import java.util.Map; |
14 | 17 | import java.util.Optional; |
15 | 18 | import java.util.Set; |
16 | 19 | import java.util.stream.StreamSupport; |
@@ -248,7 +251,96 @@ public void render(@NotNull JSONArray jsonArray, @NotNull String[] keyPath, @Not |
248 | 251 | jsonArray.forEach(jsonObj -> render((JSONObject) jsonObj, keyPath, renderObject)); |
249 | 252 | } |
250 | 253 |
|
| 254 | + //update assetURL in json of GQL response |
| 255 | + public static void UpdateAssetURLForGQL(JSONObject entryJson) { |
| 256 | + Map<String, String> assetUrls = new HashMap<>(); |
| 257 | + if (entryJson.has("data")) { |
| 258 | + JSONObject data = entryJson.optJSONObject("data"); |
| 259 | + for (String key : data.keySet()) { |
| 260 | + Object value = data.get(key); |
| 261 | + if (value instanceof JSONObject) { |
| 262 | + JSONObject contentTypeObj = (JSONObject) value; |
| 263 | + Iterator<String> keys = contentTypeObj.keys(); |
| 264 | + while (keys.hasNext()) { |
| 265 | + String mainKey = keys.next(); |
| 266 | + Object embeddedItem = contentTypeObj.get(mainKey); |
| 267 | + if (embeddedItem instanceof JSONObject) { |
| 268 | + JSONObject jsonRteObj = (JSONObject) embeddedItem; |
| 269 | + if (jsonRteObj.has("embedded_itemsConnection")) { |
| 270 | + JSONObject embeddedConnection = jsonRteObj.getJSONObject("embedded_itemsConnection"); |
| 271 | + if (embeddedConnection.has("edges")) { |
| 272 | + JSONArray embeddedItems = embeddedConnection.getJSONArray("edges"); |
| 273 | + for (int i = 0; i < embeddedItems.length(); i++) { |
| 274 | + JSONObject item = embeddedItems.getJSONObject(i); |
| 275 | + if (item.has("node")) { |
| 276 | + JSONObject nodeList = item.getJSONObject("node"); |
| 277 | + if (nodeList.has("url")) { |
| 278 | + String url = nodeList.getString("url"); |
| 279 | + if (nodeList.has("system")) { |
| 280 | + JSONObject systemList = nodeList.getJSONObject("system"); |
| 281 | + if ("sys_assets".equals(systemList.optString("content_type_uid")) && systemList.has("uid")) { |
| 282 | + String uid = systemList.getString("uid"); |
| 283 | + assetUrls.put(uid, url); |
| 284 | + updateChildObjects(entryJson, assetUrls); |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | + else |
| 293 | + { |
| 294 | + throw new IllegalArgumentException("_embedded_items not present in entry. Call includeEmbeddedItems() before fetching entry."); |
| 295 | + } |
| 296 | + } |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + } |
| 301 | + } |
251 | 302 |
|
| 303 | + private static void updateChildObjects(JSONObject entryJson, Map<String, String> assetUrls) { |
| 304 | + if (entryJson.has("data")) { |
| 305 | + JSONObject dataObject = entryJson.getJSONObject("data"); |
| 306 | + for (String key : dataObject.keySet()) { |
| 307 | + Object value = dataObject.get(key); |
| 308 | + if (value instanceof JSONObject) { |
| 309 | + JSONObject contentTypeObj = (JSONObject) value; |
| 310 | + Iterator<String> keys = contentTypeObj.keys(); |
| 311 | + while (keys.hasNext()) { |
| 312 | + String mainKey = keys.next(); |
| 313 | + Object embeddedItem = contentTypeObj.get(mainKey); |
| 314 | + if (embeddedItem instanceof JSONObject) { |
| 315 | + JSONObject jsonRteObj = (JSONObject) embeddedItem; |
| 316 | + if (jsonRteObj.has("json")) { |
| 317 | + JSONObject jsonValue = jsonRteObj.getJSONObject("json"); |
| 318 | + if (jsonValue.has("children")) { |
| 319 | + JSONArray childrenArray = jsonValue.getJSONArray("children"); |
| 320 | + updateChildrenArray(childrenArray, assetUrls); |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + private static void updateChildrenArray(JSONArray childrenArray, Map<String, String> assetUrls) { |
| 331 | + for (int i = 0; i < childrenArray.length(); i++) { |
| 332 | + JSONObject child = childrenArray.getJSONObject(i); |
| 333 | + if (child.has("attrs")) { |
| 334 | + JSONObject attrsObject = child.getJSONObject("attrs"); |
| 335 | + if (attrsObject.has("asset-uid") && attrsObject.has("asset-link")) { |
| 336 | + String assetUid = attrsObject.getString("asset-uid"); |
| 337 | + if (assetUrls.containsKey(assetUid)) { |
| 338 | + attrsObject.put("asset-link", assetUrls.get(assetUid)); |
| 339 | + } |
| 340 | + } |
| 341 | + } |
| 342 | + } |
| 343 | + } |
252 | 344 | } |
253 | 345 |
|
254 | 346 |
|
0 commit comments