com.contentstack.sdk.QueryResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java Show documentation
Show all versions of java Show documentation
Java SDK for Contentstack Content Delivery API
package com.contentstack.sdk;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* QueryResult works as the Query Response that works as getter as per the Json Key
*/
public class QueryResult {
protected static final Logger logger = Logger.getLogger(QueryResult.class.getSimpleName());
protected JSONObject receiveJson;
protected JSONArray schemaArray;
protected JSONObject contentObject;
protected int count;
protected List resultObjects;
/**
* @return List of {@link Entry} objects list.
*
* Example :
*
*
* List<Entry> list = queryResultObject.getResultObjects();
*
*/
public List getResultObjects() {
return resultObjects;
}
/**
* Returns count of objects available.
* Note : To retrieve this data, {@link Query#includeCount()} or
* {@link Query#count()} should be added in {@link Query} while querying.
*
* @return int count
*
* Example :
*
*
* int count = queryResultObject.getCount();
*
*/
public int getCount() {
return count;
}
/**
* Returns class's schema if call to fetch schema executed successfully.
*
* @return JSONArray schema Array
*
* Example :
*
*
* JSONArray schemaArray = queryResultObject.getSchema();
*
*/
public JSONArray getSchema() {
return schemaArray;
}
/**
* Returns class's content type if call to fetch contentType executed successfully.
*
* @return JSONObject contentObject
*
* Example :
*
*
* JSONObject contentObject = queryResultObject.getContentType();
*
*/
public JSONObject getContentType() {
return contentObject;
}
public void setJSON(JSONObject jsonObject, List objectList) {
receiveJson = jsonObject;
resultObjects = objectList;
extractSchemaArray();
extractContentObject();
extractCount();
}
private void extractSchemaArray() {
try {
if (receiveJson != null && receiveJson.has("schema")) {
JSONArray jsonArray = receiveJson.getJSONArray("schema");
if (jsonArray != null) {
schemaArray = jsonArray;
}
}
} catch (Exception e) {
logException(e);
}
}
private void extractContentObject() {
try {
if (receiveJson != null && receiveJson.has("content_type")) {
JSONObject jsonObject = receiveJson.getJSONObject("content_type");
if (jsonObject != null) {
contentObject = jsonObject;
}
}
} catch (Exception e) {
logException(e);
}
}
private void extractCount() {
try {
if (receiveJson != null) {
count = receiveJson.optInt("count");
if (count == 0 && receiveJson.has("entries")) {
count = receiveJson.optInt("entries");
}
}
} catch (Exception e) {
logException(e);
}
}
private void logException(Exception e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}