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

com.jelastic.api.data.response.ObjectsResponse Maven / Gradle / Ivy

The newest version!
/*Server class MD5: df8b273a2e5e17b9826f69be58676f25*/
package com.jelastic.api.data.response;

import com.jelastic.api.Response;
import com.jelastic.api.json.JSONSerializable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;

/**
 * @name Jelastic API Client
 * @version 8.11.2
 * @copyright Jelastic, Inc.
 */
public class ObjectsResponse extends Response implements JSONSerializable {

    protected Collection> _fields = null;

    private JSONObject[] objects = null;

    public ObjectsResponse(ObjectsResponse response) {
        _fields = response._fields;
    }

    public ObjectsResponse(Collection> objects) {
        super(OK);
        this._fields = objects;
        toJSON();
    }

    public ObjectsResponse() {
        super(OK);
    }

    public ObjectsResponse(int result, String error) {
        super(result, error);
    }

    public ObjectsResponse(Response resp) {
        this.setResult(resp.getResult());
        this.setError(resp.getError());
    }

    public int size() {
        return _fields == null ? 0 : _fields.size();
    }

    public Map getFirstObject() {
        return size() == 0 ? null : _fields.iterator().next();
    }

    public JSONObject[] getObjects() {
        return objects;
    }

    public Collection> getAllObjects() {
        return _fields;
    }

    private JSONObject _jsonResponse = null;

    @Override
    public JSONObject _toJSON() throws JSONException {
        JSONObject json = super._toJSON();
        if (_jsonResponse == null) {
            if (_fields != null) {
                objects = new JSONObject[_fields.size()];
                int i = 0;
                for (Map f : _fields) {
                    JSONObject fieldJson = new JSONObject();
                    for (String field : f.keySet()) {
                        Object value = f.get(field);
                        fieldJson.put(field, value == null ? JSONObject.NULL : value);
                    }
                    objects[i++] = fieldJson;
                }
                json.put("objects", new JSONArray(objects));
                _jsonResponse = json;
            }
        } else {
            json = _jsonResponse;
        }
        return json;
    }

    @Override
    public ObjectsResponse _fromJSON(JSONObject json) throws JSONException {
        super._fromJSON(json);
        if (json.has("objects")) {
            JSONArray objectsArr = json.getJSONArray("objects");
            int count = objectsArr.length();
            objects = new JSONObject[count];
            _fields = new LinkedList>();
            for (int i = 0; i < count; i++) {
                JSONObject jsonObj = objectsArr.getJSONObject(i);
                objects[i] = jsonObj;
                Iterator iter = jsonObj.keys();
                Map map = new HashMap();
                while (iter.hasNext()) {
                    String key = iter.next();
                    Object value = jsonObj.get(key);
                    map.put(key, value == JSONObject.NULL ? null : value);
                }
                _fields.add(map);
            }
        }
        _jsonResponse = json;
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy