All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.contentstack.sdk.Group Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
package com.contentstack.sdk;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Group is to get different types of DataType of data fromt the JSON response
 */
public class Group {

    protected static final Logger logger = Logger.getLogger(Group.class.getSimpleName());
    private final JSONObject resultJson;
    private final Stack stackInstance;
    protected APIService service;

    protected Group(Stack stack, JSONObject jsonObject) {
        this.service = stack.service;
        resultJson = jsonObject;
        stackInstance = stack;
    }

    /**
     * Get group representation in json
     *
     * @return JSONObject 
*
* Example :
* *
     *         JSONObject json = group.toJSON();
     *         
*/ public JSONObject toJSON() { return resultJson; } /** * Get object value for key. * * @param key * field_uid as key. * @return JSONObject
*
* Example :
* *
     *         Object obj = group.get("key");
     *         
*/ public Object get(String key) { if (resultJson != null && key != null) { return resultJson.get(key); } else { return null; } } /** * Get string value for key. * * @param key * field_uid as key. * @return String
*
* Example :
* *
     *         String value = group.getString("key");
     *         
*/ public String getString(String key) { Object value = get(key); if (value != null) { return (String) value; } return null; } /** * Get boolean value for key. * * @param key * field_uid as key. * @return boolean true or false
*
* Example :
* *
     *         Boolean value = group.getBoolean("key");
     *         
*/ public Boolean getBoolean(String key) { Object value = get(key); if (value != null) { return (Boolean) value; } return false; } /** * Get {@link JSONArray} value for key * * @param key * field_uid as key. * @return JSONArray
*
* Example :
* *
     *         JSONArray value = group.getJSONArray("key");
     *         
*/ public JSONArray getJSONArray(String key) { Object value = get(key); if (value != null) { return (JSONArray) value; } return null; } /** * Get {@link JSONObject} value for key * * @param key * field_uid as key. * @return JSONObject
*
* Example :
* *
     *         JSONObject value = group.getJSONObject("key");
     *         
*/ public JSONObject getJSONObject(String key) { Object value = get(key); if (value != null) { return (JSONObject) value; } return null; } /** * Get {@link JSONObject} value for key * * @param key * field_uid as key. * @return Number
*
* Example :
* *
     *         JSONObject value = group.getJSONObject("key");
     *         
*/ public Number getNumber(String key) { Object value = get(key); if (value != null) { return (Number) value; } return null; } /** * Get integer value for key * * @param key * field_uid as key. * @return int
*
* Example :
* *
     *         int value = group.getInt("key");
     *         
*/ public int getInt(String key) { Number value = getNumber(key); if (value != null) { return value.intValue(); } return 0; } /** * Get integer value for key * * @param key * field_uid as key. * @return float
*
* Example :
* *
     *         float value = group.getFloat("key");
     *         
*/ public float getFloat(String key) { Number value = getNumber(key); if (value != null) { return value.floatValue(); } return 0; } /** * Get double value for key * * @param key * field_uid as key. * @return double
*
* Example :
* *
     *         double value = group.getDouble("key");
     *         
*/ public double getDouble(String key) { Number value = getNumber(key); if (value != null) { return value.doubleValue(); } return 0; } /** * Get long value for key * * @param key * field_uid as key. * @return long
*
* Example :
* *
     *         long value = group.getLong("key");
     *         
*/ public long getLong(String key) { Number value = getNumber(key); if (value != null) { return value.longValue(); } return 0; } /** * Get short value for key * * @param key * field_uid as key. * @return short
*
* Example :
* *
     *         short value = group.getShort("key");
     *         
*/ public short getShort(String key) { Number value = getNumber(key); if (value != null) { return value.shortValue(); } return (short) 0; } /** * Get {@link Calendar} value for key * * @param key * field_uid as key. * @return {@link java.util.Date}
*
* Example :
* *
     *         Calendar value = group.getDate("key");
     *         
*/ public Calendar getDate(String key) { try { String value = getString(key); return Constants.parseDate(value, null); } catch (Exception e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); } return null; } /** * Get an asset from the group * * @param key * field_uid as key. * @return Asset object
*
* Example :
* *
     *         Asset asset = group.getAsset("key");
     *         
*/ public Asset getAsset(String key) { JSONObject assetObject = getJSONObject(key); return stackInstance.asset().configure(assetObject); } /** * Get an assets from the group. This works with multiple true fields * * @param key * field_uid as key.
*
* Example :
* *
     *                                                                                                                                              {@code List asset = group.getAssets("key"); }
     *                                                                                                                                              @return ArrayList of {@link Asset}
     *                                                                                                                                            
*/ public List getAssets(String key) { List assets = new ArrayList<>(); JSONArray assetArray = getJSONArray(key); for (int i = 0; i < assetArray.length(); i++) { if (assetArray.opt(i) instanceof JSONObject) { Asset asset = stackInstance.asset().configure(assetArray.optJSONObject(i)); assets.add(asset); } } return assets; } /** * Get a group from the group. * * @param key * field_uid as key.
*
* Example :
*
     *                                                                                                                  Group innerGroup = group.getGroup("key");
     *                                                                                                                  @return Group
     *                                                                                                                  object
     *                                                                                                                  
* @return the group */ public Group getGroup(String key) { if (!key.isEmpty() && resultJson.has(key) && resultJson.opt(key) instanceof JSONObject) { return new Group(stackInstance, resultJson.optJSONObject(key)); } return null; } /** * Get a list of group from the group. *

* Note :- This will work when group is multiple true. * * @param key * field_uid as key.
*
* Example :
* *

     *                                                                                                                                              Group innerGroup = group.getGroups("key");
     *                                                                                                                                         @return List of {@link Group}
     *                                                                                                                                            
*/ public List getGroups(String key) { List groupList = new ArrayList<>(); if (!key.isEmpty() && resultJson.has(key) && resultJson.opt(key) instanceof JSONArray) { JSONArray array = resultJson.optJSONArray(key); array.forEach(model -> { JSONObject newModel = (JSONObject) model; Group group = new Group(stackInstance, newModel); groupList.add(group); }); } return groupList; } /** * Get value for the given reference key. * * @param refKey * key of a reference field. * @param refContentType * class uid. * @return {@link ArrayList} of {@link Entry} instances. Also specified contentType value will be set as class uid * for all {@link Entry} instance. */ public List getAllEntries(String refKey, String refContentType) { ArrayList entryContainer = new ArrayList<>(); try { if (resultJson != null && resultJson.get(refKey) instanceof JSONArray) { JSONArray toArray = ((JSONArray) resultJson.get(refKey)); for (Object entry : toArray) { EntryModel model = new EntryModel((JSONObject) entry); Entry entryInstance = entryInstance(refContentType); entryInstance.setUid(model.uid); entryInstance.resultJson = model.jsonObject; entryInstance.setTags(model.tags); entryContainer.add(entryInstance); } return entryContainer; } } catch (Exception e) { return entryContainer; } return entryContainer; } private Entry entryInstance(String ct) { Entry entryInstance = null; try { entryInstance = stackInstance.contentType(ct).entry(); } catch (Exception e) { entryInstance = new Entry(ct); } return entryInstance; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy