Skip to content

Commit bcf4913

Browse files
committed
feat: update asset url method added
1 parent 706dac2 commit bcf4913

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

src/main/java/com/contentstack/sdk/Stack.java

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -555,46 +555,56 @@ public enum PublishType {
555555
}
556556
public void updateAssetUrl(Entry entry) {
557557
JSONObject entryJson = entry.toJSON();
558-
String url = "";
559-
try {
560-
//to find the Latest url present in the embedded field
561-
if (entryJson.has("_embedded_items")) {
562-
JSONObject embeddedItems = entryJson.getJSONObject("_embedded_items");
563-
if (embeddedItems.has("json_rte")) {
564-
JSONArray jrteArray = embeddedItems.getJSONArray("json_rte");
565-
for (int i = 0; i < jrteArray.length(); i++) {
566-
JSONObject jrteObject = jrteArray.getJSONObject(i);
567-
if (jrteObject.has("url")) {
568-
url = jrteObject.getString("url");
569-
}
558+
// Check if entry consists of _embedded_items object
559+
if (!entryJson.has("_embedded_items")) {
560+
throw new IllegalArgumentException("_embedded_items not present in entry. Call includeEmbeddedItems() before fetching entry.");
561+
}
562+
// Get _embedded_items as a JSONObject
563+
JSONObject embeddedItems = entryJson.getJSONObject("_embedded_items");
564+
Iterator<String> keys = embeddedItems.keys();
565+
Map<String, String> assetUrls = new HashMap<>();
566+
while (keys.hasNext()) {
567+
String key = keys.next();
568+
Object embeddedItem = embeddedItems.get(key);
569+
if (embeddedItem instanceof JSONArray) {
570+
JSONArray itemList = (JSONArray) embeddedItem;
571+
for (int i = 0; i < itemList.length(); i++) {
572+
JSONObject item = itemList.getJSONObject(i);
573+
if ("sys_assets".equals(item.getString("_content_type_uid")) && item.has("filename")) {
574+
String url = item.getString("url");
575+
String uid = item.getString("uid");
576+
assetUrls.put(uid,url);
570577
}
571-
} else {
572-
System.out.println("_embedded_items not found in the entry. Pass entry with includeEmbeddedItems() method!");
573578
}
574579
}
575-
// To update the jsonRTE with latest url
576-
if (entryJson.has("json_rte")) {
577-
JSONObject jsonrte = entryJson.getJSONObject("json_rte");
578-
if (jsonrte.has("children")) {
579-
JSONArray childrenArray = jsonrte.getJSONArray("children");
580-
for (int j = 0; j < childrenArray.length(); j++) {
581-
JSONObject childObject = childrenArray.getJSONObject(j);
582-
if (childObject.has("attrs") && childObject.getString("type").equals("reference")) {
583-
JSONObject attrsObject = childObject.getJSONObject("attrs");
584-
if (attrsObject.has("asset-link")) {
585-
attrsObject.put("asset-link", url);
586-
} else {
587-
System.err.println("Child object of type 'reference' missing 'asset_link' field in attrs.");
588-
}
589-
break;
580+
}
581+
updateChildObjects(entryJson, assetUrls);
582+
}
583+
private void updateChildObjects(JSONObject entryJson, Map<String, String> assetUrls) {
584+
Iterator<String> mainKeys = entryJson.keys();
585+
while (mainKeys.hasNext()) {
586+
String key = mainKeys.next();
587+
Object childObj = entryJson.get(key);
588+
if(childObj instanceof JSONObject)
589+
{ JSONObject mainKey = (JSONObject) childObj;
590+
if (mainKey.has("children")) {
591+
JSONArray mainList = mainKey.getJSONArray("children");
592+
for (int i = 0; i < mainList.length(); i++) {
593+
JSONObject list = mainList.getJSONObject(i);
594+
if (list.has("attrs") ) {
595+
JSONObject childList = list.getJSONObject("attrs");
596+
if(childList.has("asset-uid") && childList.has("asset-link")){
597+
String assetUid = childList.getString("asset-uid");
598+
if (assetUrls.containsKey(assetUid)) {
599+
childList.put("asset-link", assetUrls.get(assetUid));
600+
} else {
601+
System.out.println("Asset UID " + assetUid + " not found in assetUrls");
590602
}
591603
}
592-
} else {
593-
System.err.println("URL field not found in jrteObject.");
594604
}
595605
}
596-
} catch (Exception e) {
597-
System.err.println("Error parsing JSON or updating asset_link: " + e.getMessage());
606+
}
607+
}
598608
}
599609
}
600610

src/test/java/com/contentstack/sdk/TestStack.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,15 +389,14 @@ public void onCompletion(SyncStack response, Error error) {
389389
@Test
390390
@Disabled
391391
@Order(43)
392-
void testUpdateAsseturl() throws IllegalAccessException {
392+
void testAsseturlupdate() throws IllegalAccessException {
393393
Entry entry = stack.contentType(CONTENT_TYPE).entry(entryUid).includeEmbeddedItems();
394394
entry.fetch(new EntryResultCallBack() {
395395
@Override
396396
public void onCompletion(ResponseType responseType, Error error) {
397397
stack.updateAssetUrl(entry);
398398
Assertions.assertEquals(entryUid, entry.getUid());
399399
Assertions.assertTrue(entry.params.has("include_embedded_items[]"));
400-
System.out.println("enrty asset"+ entry.toJSON());
401400
}
402401
});
403402
}

0 commit comments

Comments
 (0)