Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/contentstack/sdk/CSBackgroundTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ protected void checkHeader(@NotNull Map<String, Object> headers) {
}
}

protected CSBackgroundTask(GlobalField globalField, Stack stackInstance, String controller, String url,
HashMap<String, Object> headers, HashMap<String, Object> urlParams, String requestInfo,
ResultCallBack callback) {
checkHeader(headers);
String completeUrl = stackInstance.config.getEndpoint() + url;
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(globalField);
csConnectionRequest.setURLQueries(urlParams);
this.service = stackInstance.service;
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service, stackInstance);
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/contentstack/sdk/CSConnectionRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public CSConnectionRequest(ContentType contentType) {
this.endpoint = contentType.stackInstance.config.getEndpoint();
}

public CSConnectionRequest(GlobalField globalField) {
this.endpoint = globalField.stackInstance.config.getEndpoint();
}

public void setQueryInstance(Query queryInstance) {
this.endpoint = queryInstance.contentTypeInstance.stackInstance.config.getEndpoint();
}
Expand Down Expand Up @@ -167,6 +171,12 @@ public synchronized void onRequestFinished(CSHttpConnection request) {
if (request.getCallBackObject() != null) {
((ContentTypesCallback) request.getCallBackObject()).onRequestFinish(model);
}
} else if (request.getController().equalsIgnoreCase(Constants.FETCHGLOBALFIELDS)) {
GlobalFieldsModel model = new GlobalFieldsModel();
model.setJSON(jsonResponse);
if (request.getCallBackObject() != null) {
((GlobalFieldsCallback) request.getCallBackObject()).onRequestFinish(model);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/contentstack/sdk/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected Constants() {
*/
// REQUEST_CONTROLLER
public enum REQUEST_CONTROLLER {
QUERY, ENTRY, ASSET, SYNC, CONTENTTYPES, ASSETLIBRARY
QUERY, ENTRY, ASSET, SYNC, CONTENTTYPES, ASSETLIBRARY, GLOBALFIELDS
}

// GET REQUEST TYPE
Expand All @@ -65,6 +65,7 @@ public enum REQUEST_CONTROLLER {
public static final String FETCHASSETS = "getAssets";
public static final String FETCHSYNC = "getSync";
public static final String FETCHCONTENTTYPES = "getContentTypes";
public static final String FETCHGLOBALFIELDS = "getGlobalFields";

public static final String CONTENT_TYPE_NAME = "Please set contentType name.";
public static final String QUERY_EXCEPTION = "Please provide valid params.";
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/com/contentstack/sdk/GlobalField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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;

/**
* <a href=
* "https://www.contentstack.com/docs/developers/apis/content-delivery-api/#single-content-type">ContentType</a>
* This
* call returns information of a specific global field. It returns the global
* field schema, but does not include its
* entries.
*
*/
public class GlobalField {

protected static final Logger logger = Logger.getLogger(GlobalField.class.getSimpleName());
protected String globalFieldUid;
protected Stack stackInstance = null;
protected JSONObject params;
protected LinkedHashMap<String, Object> headers = null;

protected GlobalField() throws IllegalAccessException {
throw new IllegalAccessException("Can Not Access Private Modifier");
}

protected GlobalField(String globalFieldUid) {
this.globalFieldUid = globalFieldUid;
}

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() {
params.put("include_branch", false);
return this;
}

public void fetch(@NotNull JSONObject params, final GlobalFieldsCallback callback) throws IllegalAccessException {
String urlString = "global_fields/" + globalFieldUid;
Iterator<String> keys = params.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = params.opt(key);
params.put(key, value);
}
params.put("environment", headers.get("environment"));
if (globalFieldUid == null || globalFieldUid.isEmpty()) {
throw new IllegalAccessException("globalFieldUid is required");
}
fetchGlobalFields(urlString, params, 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;
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/contentstack/sdk/GlobalFieldsCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.contentstack.sdk;

/**
* The callback for Content Types that contains GlobalFieldsModel and Error
*/
public abstract class GlobalFieldsCallback implements ResultCallBack {

public abstract void onCompletion(GlobalFieldsModel globalFieldsModel, Error error);

void onRequestFinish(GlobalFieldsModel globalFieldsModel) {
onCompletion(globalFieldsModel, null);
}

@Override
public void onRequestFail(ResponseType responseType, Error error) {
onCompletion(null, error);
}
}
61 changes: 61 additions & 0 deletions src/main/java/com/contentstack/sdk/GlobalFieldsModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.contentstack.sdk;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;



/**
* The GlobalFieldsModel that contains global fields response
*/
public class GlobalFieldsModel {

private Object response;
private JSONArray responseJSONArray = new JSONArray();

public void setJSON(JSONObject responseJSON) {
if (responseJSON != null) {
String ctKey = "global_field";
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof LinkedHashMap) {
try {
this.response = new JSONObject((LinkedHashMap<?, ?>) responseJSON.get(ctKey));
} catch (Exception e) {
System.err.println("Error processing 'global_field': " + e.getMessage());
}
}
String gfListKey = "global_fields";
if (responseJSON.has(gfListKey) && responseJSON.opt(gfListKey) instanceof ArrayList) {
try {
ArrayList<LinkedHashMap<?, ?>> globalFields = (ArrayList) responseJSON.get(gfListKey);
List<Object> objectList = new ArrayList<>();
if (!globalFields.isEmpty()) {
globalFields.forEach(model -> {
if (model instanceof LinkedHashMap) {
// Convert LinkedHashMap to JSONObject
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) model);
objectList.add(jsonModel);
} else {
System.err.println("Invalid type in 'global_fields' list. Expected LinkedHashMap.");
}
});
}
this.response = new JSONArray(objectList);
this.responseJSONArray = new JSONArray(objectList);
} catch (Exception e) {
System.err.println("Error processing 'global_fields': " + e.getMessage());
}
}
}
}

public Object getResponse() {
return this.response;
}

public JSONArray getResultArray() {
return responseJSONArray;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/contentstack/sdk/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Stack {
protected LinkedHashMap<String, Object> headers;
protected Config config;
protected String contentType;
protected String globalField;
protected String livePreviewEndpoint;
protected APIService service;
protected String apiKey;
Expand Down Expand Up @@ -212,6 +213,25 @@ public ContentType contentType(String contentTypeUid) {
return ct;
}

public GlobalField globalField(String globalFieldUid) {
this.globalField = globalFieldUid;
GlobalField gf = new GlobalField(globalFieldUid);
gf.setStackInstance(this);
return gf;
}

public void getGlobalFields(@NotNull JSONObject params, final GlobalFieldsCallback callback) {
Iterator<String> keys = params.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = params.opt(key);
params.put(key, value);
}
if (this.headers.containsKey(ENVIRONMENT)) {
params.put(ENVIRONMENT, this.headers.get(ENVIRONMENT));
}
fetchGlobalFields("global_fields", params, this.headers, callback);
}
/**
* Assets refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded in your Contentstack
* repository for future use. These files can be attached and used in multiple entries.
Expand Down Expand Up @@ -547,6 +567,17 @@ private void fetchContentTypes(String urlString, JSONObject
}
}

private void fetchGlobalFields(String urlString, JSONObject
globalFieldParam, HashMap<String, Object> headers,
GlobalFieldsCallback callback) {
if (callback != null) {
HashMap<String, Object> queryParam = getUrlParams(globalFieldParam);
String requestInfo = REQUEST_CONTROLLER.GLOBALFIELDS.toString();
new CSBackgroundTask(this, Constants.FETCHGLOBALFIELDS, urlString, headers, queryParam, requestInfo,
callback);
}
}

private void fetchFromNetwork(String urlString, JSONObject urlQueries,
HashMap<String, Object> headers, SyncResultCallBack callback) {
if (callback != null) {
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/contentstack/sdk/TestGlobalFields.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.contentstack.sdk;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class TestGlobalFields {

private GlobalFieldsModel globalFieldsModel;
private final Stack stack = Credentials.getStack();

@BeforeEach
void setUp() {
globalFieldsModel = new GlobalFieldsModel();
}

@Test
void testSetJSONWithNull() {
globalFieldsModel.setJSON(null);
assertNull(globalFieldsModel.getResponse());
assertEquals(0, globalFieldsModel.getResultArray().length());
}

@Test
void testSetJSONWithEmptyObject() {
globalFieldsModel.setJSON(new JSONObject());
assertNull(globalFieldsModel.getResponse());
assertEquals(0, globalFieldsModel.getResultArray().length());
}

@Test
void testFetchGlobalFieldByUid() throws IllegalAccessException {
GlobalField globalField = stack.globalField("specific_gf_uid");
JSONObject paramObj = new JSONObject();
paramObj.put("ctKeyOne", "ctKeyValue1");
paramObj.put("ctKeyTwo", "ctKeyValue2");
globalField.fetch(paramObj, new GlobalFieldsCallback() {
@Override
public void onCompletion(GlobalFieldsModel model, Error error) {
JSONArray resp = model.getResultArray();
Assertions.assertTrue(resp.isEmpty());
}
});
}

@Test
void testFetchAllGlobalFields() {
JSONObject param = new JSONObject();
stack.getGlobalFields(param, new GlobalFieldsCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
assertTrue(globalFieldsModel.getResultArray() instanceof JSONArray);
assertNotNull(((JSONArray) globalFieldsModel.getResponse()).length());

}
});
}
}
Loading