diff --git a/CHANGELOG.md b/CHANGELOG.md index e7f8657d..7e9e080a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## v1.14.1 + +### Date: 17-May-2024 + +-Enhancement: Update Asset url method added + ## v1.14.0 ### Date: 13-May-2024 diff --git a/pom.xml b/pom.xml index 42eaa863..753dc3de 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.contentstack.sdk java - 1.14.0 + 1.14.1 jar contentstack-java Java SDK for Contentstack Content Delivery API diff --git a/src/main/java/com/contentstack/sdk/Stack.java b/src/main/java/com/contentstack/sdk/Stack.java index 8e54ff0a..2f27565d 100644 --- a/src/main/java/com/contentstack/sdk/Stack.java +++ b/src/main/java/com/contentstack/sdk/Stack.java @@ -4,6 +4,7 @@ import okhttp3.OkHttpClient; import okhttp3.ResponseBody; import org.jetbrains.annotations.NotNull; +import org.json.JSONArray; import org.json.JSONObject; import retrofit2.Response; import retrofit2.Retrofit; @@ -552,5 +553,57 @@ public enum PublishType { ENTRY_PUBLISHED, ENTRY_UNPUBLISHED } + public void updateAssetUrl(Entry entry) { + JSONObject entryJson = entry.toJSON(); + // Check if entry consists of _embedded_items object + if (!entryJson.has("_embedded_items")) { + throw new IllegalArgumentException("_embedded_items not present in entry. Call includeEmbeddedItems() before fetching entry."); + } + // Get _embedded_items as a JSONObject + JSONObject embeddedItems = entryJson.getJSONObject("_embedded_items"); + Iterator keys = embeddedItems.keys(); + Map assetUrls = new HashMap<>(); + while (keys.hasNext()) { + String key = keys.next(); + Object embeddedItem = embeddedItems.get(key); + if (embeddedItem instanceof JSONArray) { + JSONArray itemList = (JSONArray) embeddedItem; + for (int i = 0; i < itemList.length(); i++) { + JSONObject item = itemList.getJSONObject(i); + if ("sys_assets".equals(item.getString("_content_type_uid")) && item.has("filename")) { + String url = item.getString("url"); + String uid = item.getString("uid"); + assetUrls.put(uid,url); + } + } + } + } + updateChildObjects(entryJson, assetUrls); + } + private void updateChildObjects(JSONObject entryJson, Map assetUrls) { + Iterator mainKeys = entryJson.keys(); + while (mainKeys.hasNext()) { + String key = mainKeys.next(); + Object childObj = entryJson.get(key); + if(childObj instanceof JSONObject) + { JSONObject mainKey = (JSONObject) childObj; + if (mainKey.has("children")) { + JSONArray mainList = mainKey.getJSONArray("children"); + for (int i = 0; i < mainList.length(); i++) { + JSONObject list = mainList.getJSONObject(i); + if (list.has("attrs") ) { + JSONObject childList = list.getJSONObject("attrs"); + if(childList.has("asset-uid") && childList.has("asset-link")){ + String assetUid = childList.getString("asset-uid"); + if (assetUrls.containsKey(assetUid)) { + childList.put("asset-link", assetUrls.get(assetUid)); + } + } + } + } + } + } + } + } } diff --git a/src/test/java/com/contentstack/sdk/TestStack.java b/src/test/java/com/contentstack/sdk/TestStack.java index b59ad734..ccf89b55 100644 --- a/src/test/java/com/contentstack/sdk/TestStack.java +++ b/src/test/java/com/contentstack/sdk/TestStack.java @@ -18,6 +18,8 @@ class TestStack { Stack stack = Credentials.getStack(); protected String paginationToken; private final Logger logger = Logger.getLogger(TestStack.class.getName()); + private String entryUid = Credentials.ENTRY_UID; + private String CONTENT_TYPE = Credentials.CONTENT_TYPE; @Test @@ -384,5 +386,19 @@ public void onCompletion(SyncStack response, Error error) { } }); } + @Test + @Disabled + @Order(43) + void testAsseturlupdate() throws IllegalAccessException { + Entry entry = stack.contentType(CONTENT_TYPE).entry(entryUid).includeEmbeddedItems(); + entry.fetch(new EntryResultCallBack() { + @Override + public void onCompletion(ResponseType responseType, Error error) { + stack.updateAssetUrl(entry); + Assertions.assertEquals(entryUid, entry.getUid()); + Assertions.assertTrue(entry.params.has("include_embedded_items[]")); + } + }); + } }