|
| 1 | +package com.contentstack.utils; |
| 2 | + |
| 3 | +import com.contentstack.utils.helper.Metadata; |
| 4 | +import com.contentstack.utils.interfaces.ContentCallback; |
| 5 | +import com.contentstack.utils.interfaces.MetaToEmbedCallback; |
| 6 | +import com.contentstack.utils.interfaces.MetadataCallback; |
| 7 | +import com.contentstack.utils.interfaces.Option; |
| 8 | +import com.contentstack.utils.node.NodeToHTML; |
| 9 | +import com.contentstack.utils.render.DefaultOption; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.json.JSONArray; |
| 12 | +import org.json.JSONObject; |
| 13 | +import org.jsoup.nodes.Attributes; |
| 14 | +import org.jsoup.nodes.Document; |
| 15 | +import org.jsoup.select.Elements; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +public class AutomateCommon { |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Find dot separated keys |
| 27 | + * |
| 28 | + * @param entryObj Json Object |
| 29 | + * @param path keyPath |
| 30 | + * @param contentCallback content callback |
| 31 | + */ |
| 32 | + protected static void findContent(JSONObject entryObj, String path, ContentCallback contentCallback) { |
| 33 | + String[] arrayString = path.split("\\."); |
| 34 | + getContent(arrayString, entryObj, contentCallback); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * @param arrayString list of keys available |
| 40 | + * @param entryObj entry object |
| 41 | + * @param contentCallback content callback |
| 42 | + */ |
| 43 | + private static void getContent(String[] arrayString, JSONObject entryObj, ContentCallback contentCallback) { |
| 44 | + if (arrayString != null && arrayString.length != 0) { |
| 45 | + String path = arrayString[0]; |
| 46 | + if (arrayString.length == 1) { |
| 47 | + Object varContent = entryObj.opt(path); |
| 48 | + if (varContent instanceof String || varContent instanceof JSONArray || varContent instanceof JSONObject) { |
| 49 | + entryObj.put(path, contentCallback.contentObject(varContent)); |
| 50 | + } |
| 51 | + } else { |
| 52 | + List<String> list = new ArrayList<>(Arrays.asList(arrayString)); |
| 53 | + list.remove(path); |
| 54 | + String[] newArrayString = list.toArray(new String[0]); |
| 55 | + if (entryObj.opt(path) instanceof JSONObject) { |
| 56 | + getContent(newArrayString, entryObj.optJSONObject(path), contentCallback); |
| 57 | + } else if (entryObj.opt(path) instanceof JSONArray) { |
| 58 | + JSONArray jsonArray = entryObj.optJSONArray(path); |
| 59 | + for (int idx = 0; idx < jsonArray.length(); idx++) { |
| 60 | + getContent(newArrayString, jsonArray.optJSONObject(idx), contentCallback); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + protected static String getStringOption(Option option, Metadata metadata, JSONObject contentToPass) { |
| 69 | + String stringOption = option.renderOptions(contentToPass, metadata); |
| 70 | + if (stringOption == null) { |
| 71 | + DefaultOption defaultOptions = new DefaultOption(); |
| 72 | + stringOption = defaultOptions.renderOptions(contentToPass, metadata); |
| 73 | + } |
| 74 | + return stringOption; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + protected static void getEmbeddedObjects(Document html, MetadataCallback metadataCallback) { |
| 79 | + Elements embeddedEntries = html.body().getElementsByClass("embedded-entry"); |
| 80 | + Elements embeddedAssets = html.body().getElementsByClass("embedded-asset"); |
| 81 | + embeddedEntries.forEach((entry) -> { |
| 82 | + String text = entry.text(); |
| 83 | + String type = entry.attr("type"); |
| 84 | + String uid = entry.attr("data-sys-entry-uid"); |
| 85 | + String contentType = entry.attr("data-sys-content-type-uid"); |
| 86 | + String style = entry.attr("sys-style-type"); |
| 87 | + String outerHTML = entry.outerHtml(); |
| 88 | + Metadata metadata = new Metadata(text, type, uid, contentType, style, outerHTML, entry.attributes()); |
| 89 | + metadataCallback.embeddedObject(metadata); |
| 90 | + }); |
| 91 | + |
| 92 | + embeddedAssets.forEach((asset) -> { |
| 93 | + String text = asset.text(); |
| 94 | + String type = asset.attr("type"); |
| 95 | + String uid = asset.attr("data-sys-asset-uid"); |
| 96 | + String style = asset.attr("sys-style-type"); |
| 97 | + String outerHTML = asset.outerHtml(); |
| 98 | + Metadata metadata = new Metadata(text, type, uid, "asset", style, outerHTML, asset.attributes()); |
| 99 | + metadataCallback.embeddedObject(metadata); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + protected static Object enumerateContents(JSONArray contentArray, Option renderObject, MetaToEmbedCallback item) { |
| 105 | + JSONArray jsonArrayRTEContent = new JSONArray(); |
| 106 | + for (Object RTE : contentArray) { |
| 107 | + JSONObject jsonObject = (JSONObject) RTE; |
| 108 | + String renderContent = enumerateContent(jsonObject, renderObject, item); |
| 109 | + jsonArrayRTEContent.put(renderContent); |
| 110 | + } |
| 111 | + return jsonArrayRTEContent; |
| 112 | + } |
| 113 | + |
| 114 | + protected static String enumerateContent(JSONObject jsonObject, Option renderObject, MetaToEmbedCallback item) { |
| 115 | + if (jsonObject.length() > 0 && jsonObject.has("type") && jsonObject.has("children")) { |
| 116 | + if (jsonObject.opt("type").equals("doc")) { |
| 117 | + return _doRawProcessing(jsonObject.optJSONArray("children"), renderObject, item); |
| 118 | + } |
| 119 | + } |
| 120 | + return ""; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + private static String _doRawProcessing(@NotNull JSONArray children, Option renderObject, MetaToEmbedCallback embedItem) { |
| 125 | + StringBuilder stringBuilder = new StringBuilder(); |
| 126 | + children.forEach(item -> { |
| 127 | + JSONObject child; |
| 128 | + if (item instanceof JSONObject) { |
| 129 | + child = (JSONObject) item; |
| 130 | + stringBuilder.append(_extractKeys(child, renderObject, embedItem)); |
| 131 | + } |
| 132 | + }); |
| 133 | + return stringBuilder.toString(); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + private static String _extractKeys(@NotNull JSONObject jsonNode, Option renderObject, MetaToEmbedCallback embedItem) { |
| 138 | + |
| 139 | + if (!jsonNode.has("type") && jsonNode.has("text")) { |
| 140 | + return NodeToHTML.textNodeToHTML(jsonNode, renderObject); |
| 141 | + } else if (jsonNode.has("type")) { |
| 142 | + String nodeType = jsonNode.optString("type"); |
| 143 | + if (nodeType.equalsIgnoreCase("reference")) { |
| 144 | + JSONObject attrObj = jsonNode.optJSONObject("attrs"); |
| 145 | + String attrType = attrObj.optString("type"); |
| 146 | + com.contentstack.utils.helper.Metadata metadata; |
| 147 | + |
| 148 | + if (attrType.equalsIgnoreCase("asset")) { |
| 149 | + String text = attrObj.optString("text"); |
| 150 | + String uid = attrObj.optString("asset-uid"); |
| 151 | + String style = attrObj.optString("display-type"); |
| 152 | + metadata = new com.contentstack.utils.helper.Metadata(text, attrType, uid, |
| 153 | + "asset", style, "", new Attributes()); |
| 154 | + } else { |
| 155 | + String text = attrObj.optString("text"); |
| 156 | + String uid = attrObj.optString("entry-uid"); |
| 157 | + String contentType = attrObj.optString("content-type-uid"); |
| 158 | + String style = attrObj.optString("display-type"); |
| 159 | + metadata = new com.contentstack.utils.helper.Metadata(text, attrType, uid, |
| 160 | + contentType, style, "", new Attributes()); |
| 161 | + } |
| 162 | + |
| 163 | + Optional<JSONObject> filteredContent = embedItem.toEmbed(metadata); |
| 164 | + if (filteredContent.isPresent()) { |
| 165 | + JSONObject contentToPass = filteredContent.get(); |
| 166 | + return getStringOption(renderObject, metadata, contentToPass); |
| 167 | + } |
| 168 | + |
| 169 | + } else { |
| 170 | + return renderObject.renderNode(nodeType, jsonNode, nodeJsonArray -> _doRawProcessing(nodeJsonArray, renderObject, embedItem)); |
| 171 | + } |
| 172 | + } |
| 173 | + return ""; |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments