11package com .contentstack .utils ;
2+
23import com .contentstack .utils .helper .Metadata ;
34import com .contentstack .utils .render .DefaultOptions ;
45import com .contentstack .utils .render .Options ;
78import org .jsoup .Jsoup ;
89import org .jsoup .nodes .Document ;
910import org .jsoup .select .Elements ;
10- import java .util .Optional ;
11+
12+ import java .util .*;
1113import java .util .logging .Logger ;
1214import java .util .stream .StreamSupport ;
1315
@@ -23,20 +25,34 @@ private interface MetadataCallback { void embeddedObject(Metadata metadata); }
2325 * @param keyPath keyPath
2426 * @param renderObject renderObject
2527 */
26- public void render (JSONObject entryObj , String [] keyPath , Options renderObject ){
27-
28- for (int i = 0 ; i < keyPath .length ; i ++) {
29- String path = keyPath [i ];
30- // Check path is available in the entryObj
31- // if available , return the entry as entryPath to renderContent's second param
32- entryObj = findEntryByPath (entryObj , path );
33- // Pass entryObj to the renderContent Second parameter
34- renderContent (path , entryObj , new Options () {
35- @ Override
36- public String renderOptions (JSONObject embeddedObject , Metadata attributes ) {
37- return null ;
28+ public static void render (JSONObject entryObj , String [] keyPath , Options renderObject ){
29+
30+ ContentCallback callback = content -> {
31+ if (content instanceof JSONArray ) {
32+ JSONArray contentArray = (JSONArray ) content ;
33+ return renderContents (contentArray , entryObj , renderObject );
34+ }else if (content instanceof String ){
35+ String contentString = (String ) content ;
36+ return renderContent (contentString , entryObj , renderObject );
37+ }
38+ return null ;
39+ };
40+
41+ if (entryObj !=null && entryObj .has ("_embedded_items" )){
42+ if (keyPath !=null ){
43+ for (String path : keyPath ) {
44+ findContent (entryObj , path , callback );
45+ }
46+ }else {
47+ // if keyPath is not available
48+ JSONObject embedKeys = entryObj .getJSONObject ("_embedded_items" );
49+ ArrayList <String > pathKeys = new ArrayList <>(embedKeys .keySet ());
50+ for (int idx = 0 ; idx < pathKeys .size (); idx ++) {
51+ String path = pathKeys .get (idx );
52+ findContent (entryObj , path , callback );
53+ logger .info ("" );
3854 }
39- });
55+ }
4056 }
4157 }
4258
@@ -74,19 +90,14 @@ public static String renderContent(String rteStringify, JSONObject embedObject,
7490
7591 Optional <JSONObject > filteredContent = Optional .empty ();
7692 // Find the type of _embedded object
77- if (metadata .getItemType ().equalsIgnoreCase ("entry" )) {
78- boolean available = embedObject .has ("_embedded_entries" );
79- if (available ) {
80- JSONArray jsonArray = embedObject .optJSONArray ("_embedded_entries" );
81- filteredContent = findEmbeddedEntry (jsonArray , metadata );
82- }
83- } else if (metadata .getItemType ().equalsIgnoreCase ("asset" )) {
84- boolean available = embedObject .has ("_embedded_assets" );
93+ //if (metadata.getItemType().equalsIgnoreCase("entry")) {
94+ boolean available = embedObject .has ("_embedded_items" );
8595 if (available ) {
86- JSONArray jsonArray = embedObject .optJSONArray ("_embedded_assets" );
87- filteredContent = findEmbeddedAsset (jsonArray , metadata );
96+ JSONObject jsonArray = embedObject .optJSONObject ("_embedded_items" );
97+ filteredContent = findEmbeddedItems (jsonArray , metadata );
98+ logger .info ("so,." );
8899 }
89- }
100+ // }
90101 // check if filteredContent is not null
91102 if (filteredContent .isPresent ()) {
92103 JSONObject contentToPass = filteredContent .get ();
@@ -107,7 +118,6 @@ public static String renderContent(String rteStringify, JSONObject embedObject,
107118 * @return String of rte with replaced tag
108119 */
109120 public static JSONArray renderContents (JSONArray rteArray , JSONObject entryObject , Options options ) {
110-
111121 JSONArray jsonArrayRTEContent = new JSONArray ();
112122 for (Object RTE : rteArray ) {
113123 String stringify = (String ) RTE ;
@@ -142,12 +152,22 @@ private static Optional<JSONObject> findEmbeddedEntry(JSONArray jsonArray, Metad
142152 * @param metadata EmbeddedObject: contains the model class information
143153 * @return Optional<JSONObject>
144154 */
145- private static Optional <JSONObject > findEmbeddedAsset (JSONArray jsonArray , Metadata metadata ) {
146- Optional <JSONObject > filteredContent = StreamSupport .stream (jsonArray .spliterator (), false )
147- .map (val -> (JSONObject ) val )
148- .filter (val -> val .optString ("uid" ).equalsIgnoreCase (metadata .getItemUid ()))
149- .findFirst ();
150- return filteredContent ;
155+ private static Optional <JSONObject > findEmbeddedItems (JSONObject jsonObject , Metadata metadata ) {
156+ Set <String > allKeys = jsonObject .keySet ();
157+ for (String key : allKeys ) {
158+ logger .info ("keys---------->" +key );
159+ JSONArray jsonArray = jsonObject .optJSONArray (key );
160+ Optional <JSONObject > filteredContent = StreamSupport .stream (jsonArray .spliterator (), false )
161+ .map (val -> (JSONObject ) val )
162+ .filter (val -> {
163+ logger .info (val .optString ("uid" )+" ----metadata---" +metadata .getItemUid () );
164+ return val .optString ("uid" ).equalsIgnoreCase (metadata .getItemUid ());
165+ }).findFirst ();
166+ if (filteredContent .isPresent ()){
167+ return filteredContent ;
168+ }
169+ }
170+ return null ;
151171 }
152172
153173 private static String getStringOption (Options options , Metadata metadata , JSONObject contentToPass ) {
0 commit comments