From 706dac2923f494ea5737ee0f303611bfead6a44a Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 14 May 2024 15:44:34 +0530 Subject: [PATCH 1/3] feat: asset url update method has been added --- CHANGELOG.md | 6 +++ pom.xml | 2 +- src/main/java/com/contentstack/sdk/Stack.java | 45 +++++++++++++++++++ .../java/com/contentstack/sdk/TestStack.java | 17 +++++++ 4 files changed, 69 insertions(+), 1 deletion(-) 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..c6f72fbc 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,49 @@ public enum PublishType { ENTRY_PUBLISHED, ENTRY_UNPUBLISHED } + public void updateAssetUrl(Entry entry) { + JSONObject entryJson = entry.toJSON(); + String url = ""; + try { + //to find the Latest url present in the embedded field + if (entryJson.has("_embedded_items")) { + JSONObject embeddedItems = entryJson.getJSONObject("_embedded_items"); + if (embeddedItems.has("json_rte")) { + JSONArray jrteArray = embeddedItems.getJSONArray("json_rte"); + for (int i = 0; i < jrteArray.length(); i++) { + JSONObject jrteObject = jrteArray.getJSONObject(i); + if (jrteObject.has("url")) { + url = jrteObject.getString("url"); + } + } + } else { + System.out.println("_embedded_items not found in the entry. Pass entry with includeEmbeddedItems() method!"); + } + } +// To update the jsonRTE with latest url + if (entryJson.has("json_rte")) { + JSONObject jsonrte = entryJson.getJSONObject("json_rte"); + if (jsonrte.has("children")) { + JSONArray childrenArray = jsonrte.getJSONArray("children"); + for (int j = 0; j < childrenArray.length(); j++) { + JSONObject childObject = childrenArray.getJSONObject(j); + if (childObject.has("attrs") && childObject.getString("type").equals("reference")) { + JSONObject attrsObject = childObject.getJSONObject("attrs"); + if (attrsObject.has("asset-link")) { + attrsObject.put("asset-link", url); + } else { + System.err.println("Child object of type 'reference' missing 'asset_link' field in attrs."); + } + break; + } + } + } else { + System.err.println("URL field not found in jrteObject."); + } + } + } catch (Exception e) { + System.err.println("Error parsing JSON or updating asset_link: " + e.getMessage()); + } + } } diff --git a/src/test/java/com/contentstack/sdk/TestStack.java b/src/test/java/com/contentstack/sdk/TestStack.java index b59ad734..2e600cbd 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,20 @@ public void onCompletion(SyncStack response, Error error) { } }); } + @Test + @Disabled + @Order(43) + void testUpdateAsseturl() 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[]")); + System.out.println("enrty asset"+ entry.toJSON()); + } + }); + } } From bcf49131318e560d90ed3ba67642f467b5b74ac9 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 15 May 2024 13:50:37 +0530 Subject: [PATCH 2/3] feat: update asset url method added --- src/main/java/com/contentstack/sdk/Stack.java | 76 +++++++++++-------- .../java/com/contentstack/sdk/TestStack.java | 3 +- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/contentstack/sdk/Stack.java b/src/main/java/com/contentstack/sdk/Stack.java index c6f72fbc..58c31d02 100644 --- a/src/main/java/com/contentstack/sdk/Stack.java +++ b/src/main/java/com/contentstack/sdk/Stack.java @@ -555,46 +555,56 @@ public enum PublishType { } public void updateAssetUrl(Entry entry) { JSONObject entryJson = entry.toJSON(); - String url = ""; - try { - //to find the Latest url present in the embedded field - if (entryJson.has("_embedded_items")) { - JSONObject embeddedItems = entryJson.getJSONObject("_embedded_items"); - if (embeddedItems.has("json_rte")) { - JSONArray jrteArray = embeddedItems.getJSONArray("json_rte"); - for (int i = 0; i < jrteArray.length(); i++) { - JSONObject jrteObject = jrteArray.getJSONObject(i); - if (jrteObject.has("url")) { - url = jrteObject.getString("url"); - } + // 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); } - } else { - System.out.println("_embedded_items not found in the entry. Pass entry with includeEmbeddedItems() method!"); } } -// To update the jsonRTE with latest url - if (entryJson.has("json_rte")) { - JSONObject jsonrte = entryJson.getJSONObject("json_rte"); - if (jsonrte.has("children")) { - JSONArray childrenArray = jsonrte.getJSONArray("children"); - for (int j = 0; j < childrenArray.length(); j++) { - JSONObject childObject = childrenArray.getJSONObject(j); - if (childObject.has("attrs") && childObject.getString("type").equals("reference")) { - JSONObject attrsObject = childObject.getJSONObject("attrs"); - if (attrsObject.has("asset-link")) { - attrsObject.put("asset-link", url); - } else { - System.err.println("Child object of type 'reference' missing 'asset_link' field in attrs."); - } - break; + } + 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)); + } else { + System.out.println("Asset UID " + assetUid + " not found in assetUrls"); } } - } else { - System.err.println("URL field not found in jrteObject."); } } - } catch (Exception e) { - System.err.println("Error parsing JSON or updating asset_link: " + e.getMessage()); + } + } } } diff --git a/src/test/java/com/contentstack/sdk/TestStack.java b/src/test/java/com/contentstack/sdk/TestStack.java index 2e600cbd..ccf89b55 100644 --- a/src/test/java/com/contentstack/sdk/TestStack.java +++ b/src/test/java/com/contentstack/sdk/TestStack.java @@ -389,7 +389,7 @@ public void onCompletion(SyncStack response, Error error) { @Test @Disabled @Order(43) - void testUpdateAsseturl() throws IllegalAccessException { + void testAsseturlupdate() throws IllegalAccessException { Entry entry = stack.contentType(CONTENT_TYPE).entry(entryUid).includeEmbeddedItems(); entry.fetch(new EntryResultCallBack() { @Override @@ -397,7 +397,6 @@ public void onCompletion(ResponseType responseType, Error error) { stack.updateAssetUrl(entry); Assertions.assertEquals(entryUid, entry.getUid()); Assertions.assertTrue(entry.params.has("include_embedded_items[]")); - System.out.println("enrty asset"+ entry.toJSON()); } }); } From adc2f3447ebed8221565927812eb6843d04f90d3 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 15 May 2024 14:36:01 +0530 Subject: [PATCH 3/3] changes done --- src/main/java/com/contentstack/sdk/Stack.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/contentstack/sdk/Stack.java b/src/main/java/com/contentstack/sdk/Stack.java index 58c31d02..2f27565d 100644 --- a/src/main/java/com/contentstack/sdk/Stack.java +++ b/src/main/java/com/contentstack/sdk/Stack.java @@ -597,8 +597,6 @@ private void updateChildObjects(JSONObject entryJson, Map assetU String assetUid = childList.getString("asset-uid"); if (assetUrls.containsKey(assetUid)) { childList.put("asset-link", assetUrls.get(assetUid)); - } else { - System.out.println("Asset UID " + assetUid + " not found in assetUrls"); } } }