Skip to content

Commit bd9fc3d

Browse files
shaileshmishrashaileshmishra
authored andcommitted
implementation updated
1 parent 948e518 commit bd9fc3d

File tree

9 files changed

+115
-78
lines changed

9 files changed

+115
-78
lines changed
Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,64 @@
11
package com.contentstack.utils;
2-
3-
import com.contentstack.utils.helper.EmbeddedObject;
4-
import com.contentstack.utils.helper.EmbeddedObjectCallback;
2+
import com.contentstack.utils.helper.Metadata;
53
import com.contentstack.utils.render.DefaultOptions;
64
import com.contentstack.utils.render.Options;
7-
85
import org.json.JSONArray;
96
import org.json.JSONObject;
107
import org.jsoup.Jsoup;
118
import org.jsoup.nodes.Document;
129
import org.jsoup.select.Elements;
13-
1410
import java.util.Optional;
1511
import java.util.logging.Logger;
1612
import java.util.stream.StreamSupport;
1713

1814
public class Utils {
1915

16+
// Interface Metadata Callback
17+
private interface MetadataCallback { void embeddedObject(Metadata metadata); }
18+
2019
private static final Logger logger = Logger.getLogger(Utils.class.getName());
2120

21+
/**
22+
* @param entryObj: Objects that contains RTE with embedded objects
23+
* @param keyPath keyPath
24+
* @param renderObject renderObject
25+
*/
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;
38+
}
39+
});
40+
}
41+
}
42+
43+
private JSONObject findEntryByPath(JSONObject entryObj, String path) {
44+
45+
return null;
46+
}
47+
48+
49+
/**
50+
*
51+
* @param jsonArray Objects that contains RTE with embedded objects
52+
* @param keyPath String array keyPath
53+
* @param renderObject renderObjects
54+
*/
55+
public void render(JSONArray jsonArray, String[] keyPath, Options renderObject){
56+
jsonArray.forEach(jsonObj->{
57+
render((JSONObject) jsonObj, keyPath, renderObject);
58+
});
59+
}
60+
61+
2262
/**
2363
* Accepts to render content on the basis of below content
2464
* @param rteStringify String of the rte available for the embedding
@@ -30,31 +70,28 @@ public static String renderContent(String rteStringify, JSONObject embedObject,
3070

3171
final String[] sReplaceRTE = {rteStringify};
3272
Document html = Jsoup.parse(rteStringify);
33-
getEmbeddedObjects(html, embeddedObject -> {
73+
getEmbeddedObjects(html, metadata -> {
3474

3575
Optional<JSONObject> filteredContent = Optional.empty();
3676
// Find the type of _embedded object
37-
if (embeddedObject.getType().equalsIgnoreCase("entry")) {
38-
77+
if (metadata.getItemType().equalsIgnoreCase("entry")) {
3978
boolean available = embedObject.has("_embedded_entries");
4079
if (available) {
4180
JSONArray jsonArray = embedObject.optJSONArray("_embedded_entries");
42-
filteredContent = findEmbeddedEntry(jsonArray, embeddedObject);
81+
filteredContent = findEmbeddedEntry(jsonArray, metadata);
4382
}
44-
} else if (embeddedObject.getType().equalsIgnoreCase("asset")) {
45-
83+
} else if (metadata.getItemType().equalsIgnoreCase("asset")) {
4684
boolean available = embedObject.has("_embedded_assets");
4785
if (available) {
4886
JSONArray jsonArray = embedObject.optJSONArray("_embedded_assets");
49-
filteredContent = findEmbeddedAsset(jsonArray, embeddedObject);
87+
filteredContent = findEmbeddedAsset(jsonArray, metadata);
5088
}
5189
}
52-
5390
// check if filteredContent is not null
5491
if (filteredContent.isPresent()) {
5592
JSONObject contentToPass = filteredContent.get();
56-
String stringOption = getStringOption(options, embeddedObject, contentToPass);
57-
sReplaceRTE[0] = html.body().html().toString().replace(embeddedObject.getOuterHTML(), stringOption);
93+
String stringOption = getStringOption(options, metadata, contentToPass);
94+
sReplaceRTE[0] = html.body().html().replace(metadata.getOuterHTML(), stringOption);
5895
}
5996
});
6097

@@ -64,7 +101,6 @@ public static String renderContent(String rteStringify, JSONObject embedObject,
64101

65102
/**
66103
* Take below items to return updated string
67-
*
68104
* @param rteArray JSONArray of the rte available for the embedding
69105
* @param entryObject JSONObject to get the _embedded_object (_embedded_entries/_embedded_assets)
70106
* @param options Options take takes input as (StyleType type, JSONObject embeddedObject)
@@ -87,14 +123,14 @@ public static JSONArray renderContents(JSONArray rteArray, JSONObject entryObjec
87123
* Matches the uid and _content_type_uid from the
88124
*
89125
* @param jsonArray JSONArray: array of the _embedded_entries
90-
* @param embeddedObject EmbeddedObject: contains the model class information
126+
* @param metadata EmbeddedObject: contains the model class information
91127
* @return Optional<JSONObject>
92128
*/
93-
private static Optional<JSONObject> findEmbeddedEntry(JSONArray jsonArray, EmbeddedObject embeddedObject) {
129+
private static Optional<JSONObject> findEmbeddedEntry(JSONArray jsonArray, Metadata metadata) {
94130
Optional<JSONObject> filteredContent = StreamSupport.stream(jsonArray.spliterator(), false)
95131
.map(val -> (JSONObject) val)
96-
.filter(val -> val.optString("uid").equalsIgnoreCase(embeddedObject.getUid()))
97-
.filter(val -> val.optString("_content_type_uid").equalsIgnoreCase(embeddedObject.getContentTypeUid()))
132+
.filter(val -> val.optString("uid").equalsIgnoreCase(metadata.getItemUid()))
133+
.filter(val -> val.optString("_content_type_uid").equalsIgnoreCase(metadata.getContentTypeUid()))
98134
.findFirst();
99135
return filteredContent;
100136
}
@@ -103,53 +139,59 @@ private static Optional<JSONObject> findEmbeddedEntry(JSONArray jsonArray, Embed
103139
* Matches the uid and _content_type_uid from the
104140
*
105141
* @param jsonArray JSONArray: array of the _embedded_assets
106-
* @param embeddedObject EmbeddedObject: contains the model class information
142+
* @param metadata EmbeddedObject: contains the model class information
107143
* @return Optional<JSONObject>
108144
*/
109-
private static Optional<JSONObject> findEmbeddedAsset(JSONArray jsonArray, EmbeddedObject embeddedObject) {
145+
private static Optional<JSONObject> findEmbeddedAsset(JSONArray jsonArray, Metadata metadata) {
110146
Optional<JSONObject> filteredContent = StreamSupport.stream(jsonArray.spliterator(), false)
111147
.map(val -> (JSONObject) val)
112-
.filter(val -> val.optString("uid").equalsIgnoreCase(embeddedObject.getUid()))
148+
.filter(val -> val.optString("uid").equalsIgnoreCase(metadata.getItemUid()))
113149
.findFirst();
114150
return filteredContent;
115151
}
116152

117-
private static String getStringOption(Options options, EmbeddedObject embeddedObject, JSONObject contentToPass) {
153+
private static String getStringOption(Options options, Metadata metadata, JSONObject contentToPass) {
118154
// TODO: Sending HashMap as HTML Attributes
119-
String stringOption = options.renderOptions(embeddedObject.getSysStyleType(), contentToPass, embeddedObject.getAttributes());
155+
String stringOption = options.renderOptions(
156+
contentToPass, metadata);
120157
if (stringOption == null) {
121158
DefaultOptions defaultOptions = new DefaultOptions();
122-
stringOption = defaultOptions.renderOptions(embeddedObject.getSysStyleType(), contentToPass, embeddedObject.getAttributes());
159+
stringOption = defaultOptions.renderOptions(
160+
contentToPass, metadata);
123161
}
124162
return stringOption;
125163
}
126164

127165

128-
private static void getEmbeddedObjects(Document html, EmbeddedObjectCallback objectCallback) {
166+
private static void getEmbeddedObjects(Document html, MetadataCallback metadataCallback) {
129167

130168
Elements embeddedEntries = html.body().getElementsByClass("embedded-entry");
131169
Elements embeddedAssets = html.body().getElementsByClass("embedded-asset");
132170

133171
embeddedEntries.forEach((entry) -> {
172+
String text = entry.text();
134173
String type = entry.attr("type");
135174
String uid = entry.attr("data-sys-entry-uid");
136175
String contentType = entry.attr("data-sys-content-type-uid");
137176
String style = entry.attr("sys-style-type");
138177
String outerHTML = entry.outerHtml();
139-
EmbeddedObject embeddedEntry = new EmbeddedObject(type, uid, contentType, style, outerHTML, entry.attributes());
140-
logger.info(embeddedEntry.toString());
141-
objectCallback.embeddedObject(embeddedEntry);
178+
Metadata metadata = new Metadata(text, type, uid, contentType, style, outerHTML, entry.attributes());
179+
logger.info(metadata.toString());
180+
metadataCallback.embeddedObject(metadata);
142181
});
143182

144183
embeddedAssets.forEach((asset) -> {
184+
String text = asset.text();
145185
String type = asset.attr("type");
146186
String uid = asset.attr("data-sys-asset-uid");
147187
String style = asset.attr("sys-style-type");
148188
String outerHTML = asset.outerHtml();
149-
EmbeddedObject embeddedAsset = new EmbeddedObject(type, uid, "asset", style, outerHTML, asset.attributes());
150-
objectCallback.embeddedObject(embeddedAsset);
189+
Metadata metadata = new Metadata(text, type, uid, "asset", style, outerHTML, asset.attributes());
190+
metadataCallback.embeddedObject(metadata);
151191
});
152192
}
153193

154194

155195
}
196+
197+

src/main/java/com/contentstack/utils/embedded/StyleType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* There are 3 ways (BLOCK, INLINE, LINKED) of attaching entries inside the RTE field as mentioned in requirements.
55
* And Two Ways (DOWNLOADABLE, DISPLAYABLE) for assets
66
*
7-
* [Example]:
7+
* [`Example`]:
88
*
9-
* For Entry: StyleType.BLOCK, StyleType.INLINE, StyleType.LINKED,
10-
* For Asset: StyleType.DOWNLOADABLE, StyleType.DISPLAYABLE
9+
* For `Entry`: StyleType.BLOCK, StyleType.INLINE, StyleType.LINKED,
10+
* For `Asset`: StyleType.DOWNLOADABLE, StyleType.DISPLAYABLE
1111
*/
1212
public enum StyleType {
13-
BLOCK, INLINE, LINKED, DOWNLOADABLE, DISPLAYABLE
13+
BLOCK, INLINE, LINKED, DISPLAY
1414
}

src/main/java/com/contentstack/utils/helper/EmbeddedObjectCallback.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/com/contentstack/utils/helper/EmbeddedObject.java renamed to src/main/java/com/contentstack/utils/helper/Metadata.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,67 @@
11
package com.contentstack.utils.helper;
22
import com.contentstack.utils.embedded.StyleType;
3+
import com.sun.xml.internal.bind.v2.TODO;
34
import org.jsoup.nodes.Attributes;
45

56
/**
67
* POJO for the Embedded Objects, that helps to carry the objects
78
*/
8-
public class EmbeddedObject {
9+
public class Metadata {
910

11+
String text ;
1012
/* type of embedded object*/
11-
String type;
12-
/* uid of embedded object*/
13-
String uid;
13+
//TODO: itemType Enum type to be taken ( implementation incomplete)
14+
String itemType;
15+
/* itemUid of embedded object*/
16+
String itemUid;
1417
/* contentTypeUid of embedded object*/
1518
String contentTypeUid;
1619
/* styleTypeUid of embedded object*/
17-
StyleType sysStyleType;
20+
StyleType styleType;
1821
/* Outer HTML of embedded object*/
1922
String outerHTML;
20-
23+
/* attributes of embedded object*/
2124
Attributes attributes;
2225

23-
public EmbeddedObject(String type, String uid, String contentTypeUid, String sysStyleType, String outerHTML, Attributes attributes) {
24-
this.type = type;
25-
this.uid = uid;
26+
public Metadata(String text, String itemType, String itemUid, String contentTypeUid, String styleType, String outerHTML, Attributes attributes) {
27+
this.text= text;
28+
this.itemType = itemType;
29+
this.itemUid = itemUid;
2630
this.contentTypeUid = contentTypeUid;
27-
this.sysStyleType = StyleType.valueOf(sysStyleType.toUpperCase());
31+
this.styleType = StyleType.valueOf(styleType.toUpperCase());
2832
this.outerHTML = outerHTML;
2933
this.attributes = attributes;
3034
}
3135

3236
@Override
3337
public String toString() {
3438
return "EmbeddedObject{" +
35-
"type='" + type + '\'' +
36-
", uid='" + uid + '\'' +
39+
"text='" + text + '\'' +
40+
"type='" + itemType + '\'' +
41+
", uid='" + itemUid + '\'' +
3742
", contentTypeUid='" + contentTypeUid + '\'' +
38-
", sysStyleType=" + sysStyleType +
43+
", sysStyleType=" + styleType +
3944
", outerHTML='" + outerHTML + '\'' +
4045
", attributes='" + attributes + '\'' +
4146
'}';
4247
}
4348

49+
public String getText() { return text; }
4450

45-
public String getType() { return type; }
51+
public String getItemType() { return itemType; }
4652

4753
public Attributes getAttributes() { return attributes; }
4854

49-
public String getUid() {
50-
return uid;
55+
public String getItemUid() {
56+
return itemUid;
5157
}
5258

5359
public String getContentTypeUid() {
5460
return contentTypeUid;
5561
}
5662

57-
public StyleType getSysStyleType() {
58-
return sysStyleType;
63+
public StyleType getStyleType() {
64+
return styleType;
5965
}
6066

6167
public String getOuterHTML() {

src/main/java/com/contentstack/utils/render/DefaultOptions.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.contentstack.utils.render;
22
import com.contentstack.utils.embedded.StyleType;
3+
import com.contentstack.utils.helper.Metadata;
34
import org.json.JSONObject;
45
import org.jsoup.nodes.Attributes;
56

@@ -15,18 +16,16 @@ public class DefaultOptions implements Options {
1516
* @return String
1617
*/
1718
@Override
18-
public String renderOptions(StyleType type, JSONObject embeddedObject, Attributes attributes) {
19+
public String renderOptions(JSONObject embeddedObject, Metadata metadata) {
1920

20-
switch (type) {
21+
switch (metadata.getStyleType()) {
2122
case BLOCK:
2223
return "<div><p>"+findTitleOrUid(embeddedObject)+"</p><div><p>Content type: <span>"+embeddedObject.optString("_content_type_uid")+"</span></p></div>";
2324
case INLINE:
2425
return "<span>"+findTitleOrUid(embeddedObject)+"</span>";
2526
case LINKED:
2627
return "<a href=\""+embeddedObject.optString("url")+"\">"+findTitleOrUid(embeddedObject)+"</a>";
27-
//case DOWNLOADABLE:
28-
//return "<a href=\""+embeddedObject.optString("url")+"\">"+findAssetTitle(embeddedObject)+"</a>";
29-
case DISPLAYABLE:
28+
case DISPLAY:
3029
return "<img src=\""+embeddedObject.optString("url")+"\" alt=\""+findAssetTitle(embeddedObject)+"\" />";
3130
default:
3231
return "";
@@ -60,7 +59,6 @@ private String findTitleOrUid(JSONObject embeddedObject) {
6059
* @return String
6160
*/
6261
private String findAssetTitle(JSONObject embeddedObject) {
63-
6462
String _title = "";
6563
if (embeddedObject != null) {
6664
if (embeddedObject.has("title") && !embeddedObject.optString("title").isEmpty()) {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.contentstack.utils.render;
2-
import com.contentstack.utils.embedded.StyleType;
2+
import com.contentstack.utils.helper.Metadata;
33
import org.json.JSONObject;
44
import org.jsoup.nodes.Attributes;
55

6-
7-
86
public interface Options {
9-
String renderOptions(StyleType type, JSONObject embeddedObject, Attributes attributes);
7+
//String renderOptions(StyleType type, JSONObject embeddedObject, Attributes attributes);
8+
9+
String renderOptions( JSONObject embeddedObject, Metadata metadata);
1010
}
1111

src/test/java/DefaultOptionsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void test_embedded_default_displayable() throws IOException {
8787
JSONArray arrayResp = new ReadResource().readJson(ASSET_DISPLAYABLE);
8888
JSONObject entryObject = (JSONObject) arrayResp.get(0);
8989
Attributes attributes = new ReadResource().returnAssetAttributes(entryObject);
90-
String result = defaultOptions.renderOptions(StyleType.DISPLAYABLE, entryObject, attributes);
90+
String result = defaultOptions.renderOptions(StyleType.DISPLAY, entryObject, attributes);
9191
log.info(result);
9292
Assert.assertEquals("<img src=\"/this-is-unique-title\" alt=\"this is unique title\" />", result);
9393
}

src/test/java/EmbeddedModelTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import com.contentstack.utils.helper.EmbeddedObject;
1+
import com.contentstack.utils.helper.Metadata;
22
import org.json.JSONArray;
33
import org.json.JSONObject;
44
import org.jsoup.Jsoup;
@@ -48,8 +48,8 @@ public void test_embedded_object_model() {
4848
String contentType = entry.attr("data-sys-content-type-uid");
4949
String style = entry.attr("sys-style-type");
5050
String outerHTML = entry.outerHtml();
51-
EmbeddedObject embeddedEntry = new EmbeddedObject(type, uid, contentType, style, outerHTML, entry.attributes());
52-
logger.info(embeddedEntry.toString());
51+
Metadata metadata = new Metadata("text", type, uid, contentType, style, outerHTML, entry.attributes());
52+
logger.info(metadata.toString());
5353
Assert.assertEquals("", outerHTML);
5454
});
5555

0 commit comments

Comments
 (0)