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
Prev Previous commit
Next Next commit
feat: enhance GlobalField fetch and findAll
  • Loading branch information
harshithad0703 committed May 22, 2025
commit 8d9514bb0e44eb3f121ab23d7f7c35b75e9f25dd
33 changes: 18 additions & 15 deletions src/main/java/com/contentstack/sdk/GlobalField.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ public class GlobalField {
protected static final Logger logger = Logger.getLogger(GlobalField.class.getSimpleName());
protected String globalFieldUid;
protected Stack stackInstance = null;
protected JSONObject params;
protected JSONObject params = new JSONObject();
protected LinkedHashMap<String, Object> headers = null;

protected GlobalField() throws IllegalAccessException {
throw new IllegalAccessException("Can Not Access Private Modifier");
protected GlobalField() {
this.headers = new LinkedHashMap<>();
}

protected GlobalField(String globalFieldUid) {
protected GlobalField(@NotNull String globalFieldUid) {
this.globalFieldUid = globalFieldUid;
this.headers = new LinkedHashMap<>();
}

protected void setStackInstance(Stack stack) {
Expand Down Expand Up @@ -72,23 +73,26 @@ public void removeHeader(String headerKey) {
*/

public GlobalField includeBranch() {
params.put("include_branch", false);
this.params.put("include_branch", true);
return this;
}

public void fetch(@NotNull JSONObject params, final GlobalFieldsCallback callback) throws IllegalAccessException {
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;
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);
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,
Expand All @@ -112,5 +116,4 @@ private HashMap<String, Object> getUrlParams(JSONObject urlQueriesJSON) {
}
return hashMap;
}

}
46 changes: 22 additions & 24 deletions src/main/java/com/contentstack/sdk/GlobalFieldsModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import org.json.JSONArray;
import org.json.JSONObject;



/**
* The GlobalFieldsModel that contains global fields response
*/
Expand All @@ -18,37 +16,37 @@ public class GlobalFieldsModel {

public void setJSON(JSONObject responseJSON) {
if (responseJSON != null) {
String ctKey = "global_field";
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof LinkedHashMap) {
String gfKey = "global_field";
if (responseJSON.has(gfKey) && responseJSON.opt(gfKey) instanceof LinkedHashMap) {
try {
this.response = new JSONObject((LinkedHashMap<?, ?>) responseJSON.get(ctKey));
this.response = new JSONObject((LinkedHashMap<?, ?>) responseJSON.get(gfKey));
} 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.");
}
});
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());
}
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() {
Expand Down
29 changes: 6 additions & 23 deletions src/main/java/com/contentstack/sdk/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,19 @@ public ContentType contentType(String contentTypeUid) {
return ct;
}

public GlobalField globalField(String globalFieldUid) {
public GlobalField globalField(@NotNull 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);
public GlobalField globalField() {
GlobalField gf = new GlobalField();
gf.setStackInstance(this);
return gf;
}

/**
* 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 @@ -567,17 +561,6 @@ 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
22 changes: 15 additions & 7 deletions src/test/java/com/contentstack/sdk/TestGlobalFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ void testSetJSONWithEmptyObject() {
@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() {
globalField.fetch(new GlobalFieldsCallback() {
@Override
public void onCompletion(GlobalFieldsModel model, Error error) {
JSONArray resp = model.getResultArray();
Expand All @@ -46,14 +43,25 @@ public void onCompletion(GlobalFieldsModel model, Error error) {
}

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

@Test
void testFindGlobalFields() throws IllegalAccessException {
GlobalField globalField = stack.globalField().includeBranch();
globalField.findAll(new GlobalFieldsCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
assertTrue(globalFieldsModel.getResultArray() instanceof JSONArray);
assertNotNull(((JSONArray) globalFieldsModel.getResponse()).length());
}
});
}
Expand Down
Loading