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