Skip to content

Commit

Permalink
feat: update asset url method added
Browse files Browse the repository at this point in the history
  • Loading branch information
reeshika-h committed May 15, 2024
1 parent e5f7a66 commit 958502b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
76 changes: 43 additions & 33 deletions src/main/java/com/contentstack/sdk/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> keys = embeddedItems.keys();
Map<String, String> 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<String, String> assetUrls) {
Iterator<String> 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());
}
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/contentstack/sdk/TestStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,14 @@ 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
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());
}
});
}
Expand Down

0 comments on commit 958502b

Please sign in to comment.