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

com.jelastic.api.billing.response.ArrayResponse Maven / Gradle / Ivy

There is a newer version: 8.12-1
Show newest version
/*Server class MD5: 0863cb0921858f2d00b82846a26ebc55*/
package com.jelastic.api.billing.response;

import com.jelastic.api.Response;
import com.jelastic.api.billing.response.interfaces.ArrayItem;
import com.jelastic.api.common.Constants;
import com.jelastic.api.system.utils.GridUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
import java.util.Arrays;

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

    public static final String PRIVATE_LIB_PACKAGE_PREFIX = "com." + "hivext";

    public static final String PUBLIC_LIB_PACKAGE_PREFIX = "com.jelastic";

    public static final String SERVER_PACKAGE = ".server";

    public static final String CLASS_NAME = "className";

    private Collection array;

    private Class className;

    public static final String ARRAY_JSON = "array";

    public static final String ITEM_NAME = GridUtils.ITEM_NAME;

    private Set expandablePath;

    public ArrayResponse() {
        super(OK);
        this.array = new ArrayList<>();
    }

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

    public ArrayResponse(Response response) {
        super(response);
    }

    public ArrayResponse(Class generic, Collection array, Set expandablePath) {
        super(OK);
        this.className = generic;
        this.array = (Collection) array;
        this.expandablePath = expandablePath;
    }

    public ArrayResponse(Class generic, Collection array, String expandablePath) {
        super(OK);
        this.className = generic;
        this.array = (Collection) array;
        if (expandablePath == null || expandablePath.isEmpty()) {
            this.expandablePath = Collections.emptySet();
        } else if (expandablePath.contains("*")) {
            this.expandablePath = null;
        } else {
            String[] stringArray = expandablePath.split(Constants.SEMICOLON_SEPARATOR);
            this.expandablePath = new HashSet<>(Arrays.asList(stringArray));
        }
    }

    public ArrayResponse(Class generic, Collection array) {
        this(generic, array, "*");
    }

    public Collection getArray() {
        return array;
    }

    public void setArray(Collection array) {
        this.array = (Collection) array;
    }

    @Override
    protected ArrayResponse _fromJSON(JSONObject json) throws JSONException {
        super._fromJSON(json);
        if (json.has(CLASS_NAME)) {
            String clazzName = getClazzName(json);
            try {
                className = Class.forName(clazzName);
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
        }
        array = new ArrayList<>();
        if (json.has(ARRAY_JSON)) {
            JSONArray arrayJson = json.getJSONArray(ARRAY_JSON);
            for (int i = 0; i < arrayJson.length(); i++) {
                try {
                    Object item = className.newInstance();
                    array.add(((ArrayItem) item)._fromJSON(arrayJson.getJSONObject(i)));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return this;
    }

    private String getClazzName(JSONObject json) throws JSONException {
        boolean isServerClass = this.getClass().getPackage().toString().contains(SERVER_PACKAGE);
        String classNameFromJSON = json.getString(CLASS_NAME);
        if (this.getClass().getPackage().getName().startsWith(PUBLIC_LIB_PACKAGE_PREFIX)) {
            return classNameFromJSON.replace(PRIVATE_LIB_PACKAGE_PREFIX, PUBLIC_LIB_PACKAGE_PREFIX).replace(SERVER_PACKAGE, "");
        }
        return isServerClass ? classNameFromJSON : classNameFromJSON.replace(SERVER_PACKAGE, "");
    }

    @Override
    protected JSONObject _toJSON() throws JSONException {
        JSONObject json = super._toJSON();
        if (array != null) {
            JSONArray arrayJson = new JSONArray();
            for (ArrayItem item : array) {
                if (expandablePath != null) {
                    arrayJson.put(item._toJsonExpandable(expandablePath));
                } else {
                    arrayJson.put(item._toJSON());
                }
            }
            if (className != null) {
                json.put(CLASS_NAME, className.getCanonicalName());
            }
            json.put(ARRAY_JSON, arrayJson);
        }
        return json;
    }

    public Class getClassName() {
        return className;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy