Skip to content

Commit 6e88199

Browse files
committed
feat: implement Global Fields functionality with associated classes and methods
1 parent b084b20 commit 6e88199

File tree

8 files changed

+338
-0
lines changed

8 files changed

+338
-0
lines changed

contentstack/src/main/java/com/contentstack/sdk/CSBackgroundTask.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ public CSBackgroundTask(ContentType contentType, Stack stackInstance, String con
127127
}
128128
}
129129

130+
public CSBackgroundTask(GlobalField globalField, Stack stackInstance, String controller, String url, ArrayMap<String, Object> headers, HashMap<String, Object> urlParams, JSONObject jsonMain, String cacheFilePath, String requestInfo, boolean b, SDKConstant.RequestMethod method, ResultCallBack callback) {
131+
132+
if (SDKConstant.IS_NETWORK_AVAILABLE) {
133+
if (headers != null && headers.size() > 0) {
134+
135+
String URL = stackInstance.PROTOCOL + stackInstance.URL + url;
136+
137+
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(globalField);
138+
csConnectionRequest.setGlobalFieldInstance(globalField);
139+
csConnectionRequest.setURLQueries(urlParams);
140+
csConnectionRequest.setParams(URL, method, controller, jsonMain, headers, cacheFilePath, requestInfo, callback);
141+
142+
} else {
143+
sendErrorForHeader(callback);
144+
}
145+
} else {
146+
sendErrorToUser(callback);
147+
}
148+
}
149+
130150

131151
private void sendErrorToUser(ResultCallBack callbackObject) {
132152
Error error = new Error();

contentstack/src/main/java/com/contentstack/sdk/CSConnectionRequest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class CSConnectionRequest implements IRequestModelHTTP {
4040
private Query queryInstance;
4141
private Asset assetInstance;
4242
private ContentType contentTypeInstance;
43+
44+
private GlobalField globalFieldInstance;
4345
private JSONObject errorJObject;
4446
private Error errorObject = new Error();
4547

@@ -66,6 +68,10 @@ public CSConnectionRequest(ContentType contentType) {
6668
this.contentTypeInstance = contentType;
6769
}
6870

71+
public CSConnectionRequest(GlobalField globalField) {
72+
this.globalFieldInstance = globalField;
73+
}
74+
6975
public void setQueryInstance(Query queryInstance) {
7076
this.queryInstance = queryInstance;
7177
}
@@ -82,6 +88,10 @@ public void setContentTypeInstance(ContentType contentTypeInstance) {
8288
this.contentTypeInstance = contentTypeInstance;
8389
}
8490

91+
public void setGlobalFieldInstance(GlobalField globalFieldInstance) {
92+
this.globalFieldInstance = globalFieldInstance;
93+
}
94+
8595
public void setParams(Object... objects) {
8696
SDKUtil.showLog(TAG, "ParallelTasks------|" + objects[0] + " started");
8797

@@ -249,6 +259,12 @@ public void onRequestFinished(CSHttpConnection request) {
249259
if (request.getCallBackObject() != null) {
250260
((ContentTypesCallback) request.getCallBackObject()).onRequestFinish(model);
251261
}
262+
} else if (controller.equalsIgnoreCase(SDKController.GET_GLOBAL_FIELDS)) {
263+
GlobalFieldsModel model = new GlobalFieldsModel();
264+
model.setJSON(responseJSON);
265+
if (request.getCallBackObject() != null) {
266+
((GlobalFieldsResultCallback) request.getCallBackObject()).onRequestFinish(model);
267+
}
252268
}
253269
}
254270

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package com.contentstack.sdk;
2+
3+
import android.text.TextUtils;
4+
import android.util.ArrayMap;
5+
import android.util.Log;
6+
7+
import org.json.JSONException;
8+
import org.json.JSONObject;
9+
10+
import java.util.HashMap;
11+
import java.util.Iterator;
12+
import java.util.Map;
13+
import java.util.Objects;
14+
15+
public class GlobalField {
16+
protected String TAG = GlobalField.class.getSimpleName();
17+
protected String global_field_uid = null;
18+
protected Stack stackInstance = null;
19+
private ArrayMap<String, Object> localHeader = null;
20+
private ArrayMap<String, Object> stackHeader = null;
21+
22+
JSONObject urlQueries = new JSONObject();
23+
24+
protected GlobalField() {
25+
this.localHeader = new ArrayMap<>();
26+
}
27+
28+
protected GlobalField (String global_field_uid) {
29+
this.global_field_uid = global_field_uid;
30+
this.localHeader = new ArrayMap<>();
31+
}
32+
33+
protected void setStackInstance(Stack stack) {
34+
this.stackInstance = stack;
35+
this.stackHeader = stack.localHeader;
36+
}
37+
38+
/**
39+
* To set headers for Contentstack rest calls.
40+
* <br>
41+
* Scope is limited to this object and followed classes.
42+
*
43+
* @param key header name.
44+
* @param value header value against given header name.
45+
*
46+
**/
47+
48+
public void setHeader(String key, String value) {
49+
if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
50+
localHeader.put(key, value);
51+
}
52+
}
53+
54+
/**
55+
* Remove header key.
56+
*
57+
* @param key custom_header_key
58+
*
59+
* <br>
60+
* <br>
61+
* <b>Example :</b><br>
62+
*
63+
* <pre class="prettyprint">
64+
**/
65+
public void removeHeader(String key) {
66+
if (!TextUtils.isEmpty(key)) {
67+
localHeader.remove(key);
68+
}
69+
}
70+
71+
/**
72+
*
73+
*
74+
* @throws IllegalAccessException
75+
* illegal access exception
76+
*/
77+
78+
public GlobalField includeBranch() {
79+
try {
80+
urlQueries.put("include_branch", true);
81+
} catch (JSONException e) {
82+
Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
83+
}
84+
return this;
85+
}
86+
87+
public GlobalField includeGlobalFieldSchema() {
88+
try {
89+
urlQueries.put("include_global_field_schema", true);
90+
} catch (JSONException e) {
91+
Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
92+
}
93+
return this;
94+
}
95+
96+
public void fetch( GlobalFieldsResultCallback callback) {
97+
try {
98+
String URL = "/" + stackInstance.VERSION + "/global_fields/" + global_field_uid;
99+
ArrayMap<String, Object> headers = getHeader(localHeader);
100+
101+
if (global_field_uid != null && !global_field_uid.isEmpty()) {
102+
fetchGlobalFields(URL, urlQueries, headers, null, callback);
103+
} else {
104+
Error error = new Error();
105+
error.setErrorMessage(SDKConstant.PLEASE_PROVIDE_VALID_JSON);
106+
callback.onRequestFail(ResponseType.UNKNOWN, error);
107+
}
108+
} catch (Exception e) {
109+
Error error = new Error();
110+
error.setErrorMessage(SDKConstant.PLEASE_PROVIDE_GLOBAL_FIELD_UID);
111+
callback.onRequestFail(ResponseType.UNKNOWN, error);
112+
}
113+
114+
}
115+
116+
public void findAll(final GlobalFieldsResultCallback callback) {
117+
try {
118+
String URL = "/" + stackInstance.VERSION + "/global_fields";
119+
ArrayMap<String, Object> headers = getHeader(localHeader);
120+
fetchGlobalFields(URL, urlQueries, headers, null, callback);
121+
} catch (Exception e) {
122+
Error error = new Error();
123+
error.setErrorMessage(SDKConstant.ERROR_MESSAGE_DEFAULT);
124+
callback.onRequestFail(ResponseType.UNKNOWN, error);
125+
}
126+
}
127+
128+
private void fetchGlobalFields(String urlString, JSONObject urlQueries, ArrayMap<String, Object> headers,
129+
String cacheFilePath, GlobalFieldsResultCallback callback) {
130+
131+
if (callback != null) {
132+
133+
HashMap<String, Object> urlParams = getUrlParams(urlQueries);
134+
new CSBackgroundTask(this, stackInstance, SDKController.GET_GLOBAL_FIELDS, urlString, headers, urlParams,
135+
new JSONObject(), cacheFilePath, SDKConstant.callController.GLOBAL_FIELDS.toString(), false,
136+
SDKConstant.RequestMethod.GET, callback);
137+
}
138+
}
139+
140+
private HashMap<String, Object> getUrlParams(JSONObject urlQueriesJSON) {
141+
142+
HashMap<String, Object> hashMap = new HashMap<>();
143+
144+
if (urlQueriesJSON != null && urlQueriesJSON.length() > 0) {
145+
Iterator<String> iter = urlQueriesJSON.keys();
146+
while (iter.hasNext()) {
147+
String key = iter.next();
148+
try {
149+
Object value = urlQueriesJSON.opt(key);
150+
hashMap.put(key, value);
151+
} catch (Exception e) {
152+
SDKUtil.showLog(TAG, "------setQueryJson" + e.toString());
153+
}
154+
}
155+
156+
return hashMap;
157+
}
158+
159+
return null;
160+
}
161+
162+
private ArrayMap<String, Object> getHeader(ArrayMap<String, Object> localHeader) {
163+
ArrayMap<String, Object> mainHeader = stackHeader;
164+
ArrayMap<String, Object> classHeaders = new ArrayMap<>();
165+
166+
if (localHeader != null && localHeader.size() > 0) {
167+
if (mainHeader != null && mainHeader.size() > 0) {
168+
for (Map.Entry<String, Object> entry : localHeader.entrySet()) {
169+
String key = entry.getKey();
170+
classHeaders.put(key, entry.getValue());
171+
}
172+
173+
for (Map.Entry<String, Object> entry : mainHeader.entrySet()) {
174+
String key = entry.getKey();
175+
if (!classHeaders.containsKey(key)) {
176+
classHeaders.put(key, entry.getValue());
177+
}
178+
}
179+
180+
return classHeaders;
181+
182+
} else {
183+
return localHeader;
184+
}
185+
186+
} else {
187+
return stackHeader;
188+
}
189+
}
190+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.contentstack.sdk;
2+
3+
import android.util.Log;
4+
5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
9+
import java.util.Objects;
10+
11+
public class GlobalFieldsModel {
12+
13+
private JSONObject responseJSON = new JSONObject();
14+
private JSONArray responseJSONArray = new JSONArray();
15+
private final String TAG = GlobalFieldsModel.class.getSimpleName();
16+
17+
public void setJSON(JSONObject responseJSON) {
18+
19+
if (responseJSON != null) {
20+
21+
if (responseJSON.has("global_field")) {
22+
try {
23+
this.responseJSON = responseJSON.getJSONObject("global_field");
24+
} catch (JSONException e) {
25+
Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
26+
}
27+
}
28+
29+
if (responseJSON.has("global_fields")) {
30+
try {
31+
this.responseJSONArray = responseJSON.getJSONArray("global_fields");
32+
} catch (JSONException e) {
33+
Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
34+
}
35+
}
36+
37+
}
38+
}
39+
40+
public JSONObject getResponse() {
41+
return responseJSON;
42+
}
43+
44+
public JSONArray getResultArray() {
45+
return responseJSONArray;
46+
}
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.contentstack.sdk;
2+
3+
public abstract class GlobalFieldsResultCallback extends ResultCallBack{
4+
/**
5+
* Triggered after call execution complete.
6+
*
7+
* @param globalFieldsModel {@link GlobalFieldsModel} instance if call success else null.
8+
* @param error {@link Error} instance if call failed else null.
9+
*/
10+
public abstract void onCompletion(GlobalFieldsModel globalFieldsModel, Error error);
11+
void onRequestFinish(GlobalFieldsModel globalFieldsModel) {
12+
onCompletion(globalFieldsModel, null);
13+
}
14+
@Override
15+
void onRequestFail(ResponseType responseType, Error error) {
16+
onCompletion(null, error);
17+
}
18+
@Override
19+
void always() {
20+
}
21+
}

contentstack/src/main/java/com/contentstack/sdk/SDKConstant.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static enum callController {
3131
ASSET,
3232
SYNC,
3333
CONTENT_TYPES,
34+
GLOBAL_FIELDS,
3435
ASSET_LIBRARY;
3536
}
3637

@@ -49,4 +50,7 @@ public static enum callController {
4950
public final static String PROVIDE_VALID_PARAMS = "Please provide valid params.";
5051
public final static String ENTRY_IS_NOT_PRESENT_IN_CACHE = "ENTRY is not present in cache";
5152
public final static String NETWORK_CALL_RESPONSE = "Error while saving network call response.";
53+
public final static String PLEASE_PROVIDE_GLOBAL_FIELD_UID = "Please provide global field uid.";
54+
55+
5256
}

contentstack/src/main/java/com/contentstack/sdk/SDKController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ public class SDKController {
4141
* The constant GET_CONTENT_TYPES.
4242
*/
4343
public static final String GET_CONTENT_TYPES = "getContentTypes";
44+
45+
public static final String GET_GLOBAL_FIELDS = "getGlobalFields";
4446
}

contentstack/src/main/java/com/contentstack/sdk/Stack.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,44 @@ public ContentType contentType(String contentTypeName) {
138138
}
139139

140140

141+
142+
/**
143+
* Represents a Global field instance.
144+
*
145+
* <br><br><b>Example :</b><br>
146+
* <pre class="prettyprint">
147+
* Stack stack = Contentstack.stack(context, "apiKey", "deliveryToken", "stag");
148+
* GlobalField globalField = stack.globalField();
149+
* </pre>
150+
* @return Global field instance
151+
*/
152+
153+
public GlobalField globalField(){
154+
GlobalField globalField = new GlobalField();
155+
globalField.setStackInstance(this);
156+
return globalField;
157+
}
158+
159+
160+
161+
/**
162+
* Represents Global Field with a specific global_field_uid
163+
* <br><br><b>Example :</b><br>
164+
* <pre class="prettyprint">
165+
* Stack stack = Contentstack.stack(context, "apiKey", "deliveryToken", "stag");
166+
* GlobalField globalField = stack.globalField("global_field_uid");
167+
* </pre>
168+
* @param global_field_uid
169+
* @return
170+
*/
171+
public GlobalField globalField(String global_field_uid){
172+
GlobalField globalField = new GlobalField(global_field_uid);
173+
globalField.setStackInstance(this);
174+
return globalField;
175+
}
176+
177+
178+
141179
/**
142180
* Create {@link Asset} instance.
143181
*

0 commit comments

Comments
 (0)