io.github.ishaileshmishra.Entry Maven / Gradle / Ivy
package io.github.ishaileshmishra;
import io.github.ishaileshmishra.callback.ResultCallBack;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import retrofit2.Call;
import retrofit2.Retrofit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
/**
* Entry class.
*
* @author shaileshmishra
* @version $Id: $Id
*/
public class Entry extends Queryable {
protected HashMap headers = new HashMap();
protected String uid;
protected Service service;
protected final String contentTypeUid;
protected HashMap entryParam;
private JSONArray referenceArray;
private JSONArray objectUidForOnly;
private JSONArray objectUidForExcept;
private JSONObject onlyJsonObject;
private JSONObject exceptJsonObject;
/**
* Constructor for Entry.
*
* @throws java.lang.IllegalAccessException if any.
*/
protected Entry() throws IllegalAccessException {
throw new IllegalAccessException("Invalid Access");
}
/**
* Constructor for Entry.
*
* @param retrofit a {@link retrofit2.Retrofit} object
* @param headers a {@link java.util.HashMap} object
* @param contentTypeUid a {@link java.lang.String} object
* @param entryUid a {@link java.lang.String} object
*/
protected Entry(@NotNull Retrofit retrofit,
@NotNull HashMap headers,
@NotNull String contentTypeUid,
@NotNull String entryUid) {
Objects.requireNonNull(entryUid, "entry uid can not be Null");
this.service = retrofit.create(Service.class);
this.headers.putAll(headers);
this.contentTypeUid = contentTypeUid;
this.uid = entryUid;
this.entryParam = new HashMap();
}
/**
* Setter for the field uid
.
*
* @param uid a {@link java.lang.String} object
*/
protected void setUid(@NotNull String uid) {
this.uid = uid;
}
/**
* addHeader.
*
* @param key a {@link java.lang.String} object
* @param value a {@link java.lang.String} object
*/
public void addHeader(String key, String value) {
if (!key.isEmpty() && !value.isEmpty()) {
this.headers.put(key, value);
}
}
/**
* removeHeader.
*
* @param key a {@link java.lang.String} object
*/
public void removeHeader(String key) {
if (!key.isEmpty()) {
this.headers.remove(key);
}
}
/**
* setLocale.
*
* @param locale a {@link java.lang.String} object
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry setLocale(@NotNull String locale) {
entryParam.put("locale", locale);
return this;
}
/**
* except.
*
* @param fieldUid an array of {@link java.lang.String} objects
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry except(@NotNull String[] fieldUid) {
if (fieldUid.length > 0) {
if (objectUidForExcept == null) {
objectUidForExcept = new JSONArray();
}
for (String s : fieldUid) {
objectUidForExcept.add(s);
}
}
return this;
}
/**
* includeReference.
*
* @param referenceField a {@link java.lang.String} object
*/
public void includeReference(@NotNull String referenceField) {
if (!referenceField.isEmpty()) {
if (referenceArray == null) {
referenceArray = new JSONArray();
}
referenceArray.add(referenceField);
entryParam.put("include[]", referenceArray);
}
}
/**
* includeReference.
*
* @param referenceFields an array of {@link java.lang.String} objects
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry includeReference(@NotNull String[] referenceFields) {
if (referenceFields.length > 0) {
if (referenceArray == null) {
referenceArray = new JSONArray();
}
for (String referenceField : referenceFields) {
referenceArray.add(referenceField);
}
entryParam.put("include[]", referenceArray);
}
return this;
}
/**
* only.
*
* @param fieldUid an array of {@link java.lang.String} objects
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry only(@NotNull String[] fieldUid) {
if (fieldUid.length > 0) {
if (objectUidForOnly == null) {
objectUidForOnly = new JSONArray();
}
for (String s : fieldUid) {
objectUidForOnly.add(s);
}
}
return this;
}
/**
* onlyWithReferenceUid.
*
* @param fieldUid a {@link java.util.ArrayList} object
* @param referenceFieldUid a {@link java.lang.String} object
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry onlyWithReferenceUid(@NotNull ArrayList fieldUid, @NotNull String referenceFieldUid) {
if (onlyJsonObject == null) {
onlyJsonObject = new JSONObject();
}
JSONArray fieldValueArray = new JSONArray();
for (String s : fieldUid) {
fieldValueArray.add(s);
}
onlyJsonObject.put(referenceFieldUid, fieldValueArray);
includeReference(referenceFieldUid);
return this;
}
/**
* exceptWithReferenceUid.
*
* @param fieldUid a {@link java.util.ArrayList} object
* @param referenceFieldUid a {@link java.lang.String} object
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry exceptWithReferenceUid(@NotNull ArrayList fieldUid,
@NotNull String referenceFieldUid) {
if (exceptJsonObject == null) {
exceptJsonObject = new JSONObject();
}
JSONArray fieldValueArray = new JSONArray();
for (String s : fieldUid) {
fieldValueArray.add(s);
}
exceptJsonObject.put(referenceFieldUid, fieldValueArray);
includeReference(referenceFieldUid);
return this;
}
/**
* fetch.
*
* @param callBack a {@link io.github.ishaileshmishra.callback.ResultCallBack} object
*/
public void fetch(ResultCallBack callBack) {
entryParam.put("environment", this.headers.get("environment"));
this.headers.remove("environment");
Call request = this.service
.entry(this.contentTypeUid, this.uid, this.headers, this.entryParam);
request(request, callBack);
}
/**
* addParam.
*
* @param paramKey a {@link java.lang.String} object
* @param value a {@link java.lang.String} object
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry addParam(@NotNull String paramKey, @NotNull String value) {
entryParam.put(paramKey, value);
return this;
}
/**
* includeReferenceContentTypeUID.
*
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry includeReferenceContentTypeUID() {
entryParam.put("include_reference_content_type_uid", "true");
return this;
}
/**
* includeContentType.
*
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry includeContentType() {
if (entryParam.containsKey("include_schema")) {
entryParam.remove("include_schema");
}
entryParam.put("include_content_type", true);
entryParam.put("include_global_field_schema", true);
return this;
}
/**
* includeFallback.
*
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry includeFallback() {
entryParam.put("include_fallback", true);
return this;
}
/**
* includeEmbeddedItems.
*
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry includeEmbeddedItems() {
entryParam.put("include_embedded_items[]", "BASE");
return this;
}
/**
* includeBranch.
*
* @return a {@link io.github.ishaileshmishra.Entry} object
*/
public Entry includeBranch() {
entryParam.put("include_branch", true);
return this;
}
}