22
33import com .contentstack .utils .callbacks .Content ;
44import com .contentstack .utils .callbacks .Metadata ;
5- import com .contentstack .utils .callbacks .OptionsCallback ;
6- import com .contentstack .utils .presets .Constant ;
5+ import com .contentstack .utils .callbacks .Options ;
76import com .contentstack .utils .render .DefaultOption ;
87import org .json .JSONArray ;
98import org .json .JSONObject ;
1716public class Utils {
1817
1918 /**
20- * @param entryObj: Objects that contains RTE with embedded objects
21- * @param keyPath keyPath
22- * @param renderObject renderObject
19+ * @param entryObj:
20+ * Objects that contains RTE with embedded objects
21+ * @param keyPath
22+ * keyPath
23+ * @param renderObject
24+ * renderObject
2325 */
24- public static void render (JSONObject entryObj , String [] keyPath , OptionsCallback renderObject ) {
26+ public static void render (JSONObject entryObj , String [] keyPath , Options renderObject ) {
2527 Content callback = content -> {
2628 if (content instanceof JSONArray ) {
2729 JSONArray contentArray = (JSONArray ) content ;
@@ -33,15 +35,15 @@ public static void render(JSONObject entryObj, String[] keyPath, OptionsCallback
3335 return null ;
3436 };
3537
36- if (entryObj != null && entryObj .has (Constant . EMBEDDED_ITEMS )) {
38+ if (entryObj != null && entryObj .has ("_embedded_items" )) {
3739 if (keyPath != null ) {
3840 for (String path : keyPath ) {
3941 findContent (entryObj , path , callback );
4042 }
4143 } else {
4244 // Case when KeyPath is not given by user,
4345 // Extract all available keyPath from _embedded_items
44- JSONObject embedKeys = entryObj .getJSONObject (Constant . EMBEDDED_ITEMS );
46+ JSONObject embedKeys = entryObj .getJSONObject ("_embedded_items" );
4547 ArrayList <String > pathKeys = new ArrayList <>(embedKeys .keySet ());
4648 for (String path : pathKeys ) {
4749 findContent (entryObj , path , callback );
@@ -52,21 +54,26 @@ public static void render(JSONObject entryObj, String[] keyPath, OptionsCallback
5254
5355 /**
5456 * Find dot separated keys
55- *
56- * @param entryObj Json Object
57- * @param path keyPath
58- * @param content content callback
57+ *
58+ * @param entryObj
59+ * Json Object
60+ * @param path
61+ * keyPath
62+ * @param content
63+ * content callback
5964 */
6065 private static void findContent (JSONObject entryObj , String path , Content content ) {
6166 String [] arrayString = path .split ("\\ ." );
6267 getContent (arrayString , entryObj , content );
6368 }
6469
6570 /**
66- *
67- * @param arrayString list of keys available
68- * @param entryObj entry object
69- * @param content content callback
71+ * @param arrayString
72+ * list of keys available
73+ * @param entryObj
74+ * entry object
75+ * @param content
76+ * content callback
7077 */
7178 private static void getContent (String [] arrayString , JSONObject entryObj , Content content ) {
7279 if (arrayString != null && arrayString .length != 0 ) {
@@ -93,38 +100,41 @@ private static void getContent(String[] arrayString, JSONObject entryObj, Conten
93100 }
94101
95102 /**
96- *
97- * @param jsonArray Objects that contains RTE with embedded objects
98- * @param keyPath String array keyPath
99- * @param renderObject renderObjects
103+ * @param jsonArray
104+ * Objects that contains RTE with embedded objects
105+ * @param keyPath
106+ * String array keyPath
107+ * @param renderObject
108+ * renderObjects
100109 */
101- public void render (JSONArray jsonArray , String [] keyPath , OptionsCallback renderObject ) {
110+ public void render (JSONArray jsonArray , String [] keyPath , Options renderObject ) {
102111 jsonArray .forEach (jsonObj -> render ((JSONObject ) jsonObj , keyPath , renderObject ));
103112 }
104113
105114 /**
106115 * Accepts to render content on the basis of below content
107- *
108- * @param rteStringify String of the rte available for the embedding
109- * @param embedObject JSONObject to get the _embedded_object
110- * (_embedded_entries/_embedded_assets)
111- * @param option Options take takes input as (StyleType type, JSONObject
112- * embeddedObject)
116+ *
117+ * @param rteStringify
118+ * String of the rte available for the embedding
119+ * @param embedObject
120+ * JSONObject to get the _embedded_object (_embedded_entries/_embedded_assets)
121+ * @param options
122+ * Options take takes input as (StyleType type, JSONObject embeddedObject)
113123 * @return String of rte with replaced tag
114124 */
115- public static String renderContent (String rteStringify , JSONObject embedObject , OptionsCallback option ) {
116- final String [] sReplaceRTE = { rteStringify };
125+ public static String renderContent (String rteStringify , JSONObject embedObject , Options options ) {
126+ final String [] sReplaceRTE = {rteStringify };
117127 Document html = Jsoup .parse (rteStringify );
118128 getEmbeddedObjects (html , metadata -> {
119129 Optional <JSONObject > filteredContent = Optional .empty ();
120- boolean available = embedObject .has (Constant . EMBEDDED_ITEMS );
130+ boolean available = embedObject .has ("_embedded_items" );
121131 if (available ) {
122- JSONObject jsonArray = embedObject .optJSONObject (Constant . EMBEDDED_ITEMS );
132+ JSONObject jsonArray = embedObject .optJSONObject ("_embedded_items" );
123133 filteredContent = findEmbeddedItems (jsonArray , metadata );
124134 }
125135 if (filteredContent .isPresent ()) {
126136 JSONObject contentToPass = filteredContent .get ();
127- String stringOption = getStringOption (option , metadata , contentToPass );
137+ String stringOption = getStringOption (options , metadata , contentToPass );
128138 sReplaceRTE [0 ] = html .body ().html ().replace (metadata .getOuterHTML (), stringOption );
129139 }
130140 });
@@ -133,33 +143,36 @@ public static String renderContent(String rteStringify, JSONObject embedObject,
133143
134144 /**
135145 * Take below items to return updated string
136- *
137- * @param rteArray JSONArray of the rte available for the embedding
138- * @param entryObject JSONObject to get the _embedded_object
139- * (_embedded_entries/_embedded_assets)
140- * @param option Options take takes input as (StyleType type, JSONObject
141- * embeddedObject)
146+ *
147+ * @param rteArray
148+ * JSONArray of the rte available for the embedding
149+ * @param entryObject
150+ * JSONObject to get the _embedded_object (_embedded_entries/_embedded_assets)
151+ * @param options
152+ * Options take takes input as (StyleType type, JSONObject embeddedObject)
142153 * @return String of rte with replaced tag
143154 */
144- public static JSONArray renderContents (JSONArray rteArray , JSONObject entryObject , OptionsCallback option ) {
155+ public static JSONArray renderContents (JSONArray rteArray , JSONObject entryObject , Options options ) {
145156 JSONArray jsonArrayRTEContent = new JSONArray ();
146157 for (Object RTE : rteArray ) {
147158 String stringify = (String ) RTE ;
148- String renderContent = renderContent (stringify , entryObject , option );
159+ String renderContent = renderContent (stringify , entryObject , options );
149160 jsonArrayRTEContent .put (renderContent );
150161 }
151162 return jsonArrayRTEContent ;
152163 }
153164
154165 /**
155166 * Matches the uid and _content_type_uid from the
156- *
157- * @param jsonObject JSONObject: jsonObject of the _embedded_assets
158- * @param metadata EmbeddedObject: contains the model class information
167+ *
168+ * @param jsonObject
169+ * JSONObject: jsonObject of the _embedded_assets
170+ * @param metadata
171+ * EmbeddedObject: contains the model class information
159172 * @return Optional<JSONObject>
160173 */
161174 private static Optional <JSONObject > findEmbeddedItems (JSONObject jsonObject ,
162- com .contentstack .utils .helper .Metadata metadata ) {
175+ com .contentstack .utils .helper .Metadata metadata ) {
163176 Set <String > allKeys = jsonObject .keySet ();
164177 for (String key : allKeys ) {
165178 JSONArray jsonArray = jsonObject .optJSONArray (key );
@@ -173,9 +186,9 @@ private static Optional<JSONObject> findEmbeddedItems(JSONObject jsonObject,
173186 return Optional .empty ();
174187 }
175188
176- private static String getStringOption (OptionsCallback option , com .contentstack .utils .helper .Metadata metadata ,
177- JSONObject contentToPass ) {
178- String stringOption = option .renderOptions (contentToPass , metadata );
189+ private static String getStringOption (Options options , com .contentstack .utils .helper .Metadata metadata ,
190+ JSONObject contentToPass ) {
191+ String stringOption = options .renderOptions (contentToPass , metadata );
179192 if (stringOption == null ) {
180193 DefaultOption defaultOptions = new DefaultOption ();
181194 stringOption = defaultOptions .renderOptions (contentToPass , metadata );
0 commit comments