-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGlobalFieldsModel.java
More file actions
59 lines (53 loc) · 2.27 KB
/
GlobalFieldsModel.java
File metadata and controls
59 lines (53 loc) · 2.27 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
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 gfKey = "global_field";
if (responseJSON.has(gfKey) && responseJSON.opt(gfKey) instanceof LinkedHashMap) {
try {
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.");
}
});
}
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;
}
}