@@ -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
0 commit comments