|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.json.JSONObject; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.LinkedHashMap; |
| 9 | +import java.util.logging.Logger; |
| 10 | + |
| 11 | +/** |
| 12 | + * This call returns information of a specific global field. It returns the |
| 13 | + * global field schema. |
| 14 | + * |
| 15 | + */ |
| 16 | +public class GlobalField { |
| 17 | + |
| 18 | + protected static final Logger logger = Logger.getLogger(GlobalField.class.getSimpleName()); |
| 19 | + protected String globalFieldUid; |
| 20 | + protected Stack stackInstance = null; |
| 21 | + protected JSONObject params = new JSONObject(); |
| 22 | + protected LinkedHashMap<String, Object> headers = null; |
| 23 | + |
| 24 | + protected GlobalField() { |
| 25 | + this.headers = new LinkedHashMap<>(); |
| 26 | + } |
| 27 | + |
| 28 | + protected GlobalField(@NotNull String globalFieldUid) { |
| 29 | + this.globalFieldUid = globalFieldUid; |
| 30 | + this.headers = new LinkedHashMap<>(); |
| 31 | + } |
| 32 | + |
| 33 | + protected void setStackInstance(Stack stack) { |
| 34 | + this.stackInstance = stack; |
| 35 | + this.headers = stack.headers; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Sets header on {@link Stack}. |
| 40 | + * |
| 41 | + * @param headerKey |
| 42 | + * the header key |
| 43 | + * @param headerValue |
| 44 | + * the header value |
| 45 | + */ |
| 46 | + public void setHeader(String headerKey, String headerValue) { |
| 47 | + if (!headerKey.isEmpty() && !headerValue.isEmpty()) { |
| 48 | + this.headers.put(headerKey, headerValue); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Remove header from {@link Stack} |
| 54 | + * |
| 55 | + * @param headerKey |
| 56 | + * the header key |
| 57 | + */ |
| 58 | + public void removeHeader(String headerKey) { |
| 59 | + if (!headerKey.isEmpty()) { |
| 60 | + this.headers.remove(headerKey); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Fetch. |
| 66 | + * |
| 67 | + * @param params |
| 68 | + * the params |
| 69 | + * @param callback |
| 70 | + * the callback |
| 71 | + * @throws IllegalAccessException |
| 72 | + * illegal access exception |
| 73 | + */ |
| 74 | + |
| 75 | + public GlobalField includeBranch() { |
| 76 | + this.params.put("include_branch", true); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + public GlobalField includeGlobalFieldSchema() { |
| 81 | + this.params.put("include_global_field_schema", true); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public void fetch(final GlobalFieldsCallback callback) throws IllegalAccessException { |
| 86 | + String urlString = "global_fields/" + globalFieldUid; |
| 87 | + if (globalFieldUid == null || globalFieldUid.isEmpty()) { |
| 88 | + throw new IllegalAccessException("globalFieldUid is required"); |
| 89 | + } |
| 90 | + fetchGlobalFields(urlString, this.params, this.headers, callback); |
| 91 | + } |
| 92 | + |
| 93 | + public void findAll(final GlobalFieldsCallback callback) { |
| 94 | + String urlString = "global_fields"; |
| 95 | + fetchGlobalFields(urlString, this.params, this.headers, callback); |
| 96 | + } |
| 97 | + |
| 98 | + private void fetchGlobalFields(String urlString, JSONObject params, HashMap<String, Object> headers, |
| 99 | + GlobalFieldsCallback callback) { |
| 100 | + if (callback != null) { |
| 101 | + HashMap<String, Object> urlParams = getUrlParams(params); |
| 102 | + new CSBackgroundTask(this, stackInstance, Constants.FETCHGLOBALFIELDS, urlString, headers, urlParams, |
| 103 | + Constants.REQUEST_CONTROLLER.GLOBALFIELDS.toString(), callback); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private HashMap<String, Object> getUrlParams(JSONObject urlQueriesJSON) { |
| 108 | + HashMap<String, Object> hashMap = new HashMap<>(); |
| 109 | + if (urlQueriesJSON != null && urlQueriesJSON.length() > 0) { |
| 110 | + Iterator<String> itStr = urlQueriesJSON.keys(); |
| 111 | + while (itStr.hasNext()) { |
| 112 | + String key = itStr.next(); |
| 113 | + Object value = urlQueriesJSON.opt(key); |
| 114 | + hashMap.put(key, value); |
| 115 | + } |
| 116 | + } |
| 117 | + return hashMap; |
| 118 | + } |
| 119 | +} |
0 commit comments