Search in sources :

Example 1 with JSONObjectAdapter

use of org.sagebionetworks.schema.adapter.JSONObjectAdapter in project Synapse-Repository-Services by Sage-Bionetworks.

the class EntityBundle method writeToJSONObject.

@Override
public JSONObjectAdapter writeToJSONObject(JSONObjectAdapter writeTo) throws JSONObjectAdapterException {
    if (writeTo == null) {
        throw new IllegalArgumentException("JSONObjectAdapter cannot be null");
    }
    if (entity != null) {
        JSONObjectAdapter joa = writeTo.createNew();
        entity.writeToJSONObject(joa);
        writeTo.put(JSON_ENTITY, joa);
        writeTo.put(JSON_ENTITY_TYPE, entityType);
    }
    if (annotations != null) {
        JSONObjectAdapter joa = writeTo.createNew();
        annotations.writeToJSONObject(joa);
        writeTo.put(JSON_ANNOTATIONS, joa);
    }
    if (permissions != null) {
        JSONObjectAdapter joa = writeTo.createNew();
        permissions.writeToJSONObject(joa);
        writeTo.put(JSON_PERMISSIONS, joa);
    }
    if (path != null) {
        JSONObjectAdapter joa = writeTo.createNew();
        path.writeToJSONObject(joa);
        writeTo.put(JSON_PATH, joa);
    }
    if (referencedBy != null) {
        JSONObjectAdapter joa = writeTo.createNew();
        referencedBy.writeToJSONObject(joa);
        writeTo.put(JSON_REFERENCED_BY, joa);
    }
    if (hasChildren != null) {
        writeTo.put(JSON_HAS_CHILDREN, hasChildren);
    }
    if (acl != null) {
        JSONObjectAdapter joa = writeTo.createNew();
        acl.writeToJSONObject(joa);
        writeTo.put(JSON_ACL, joa);
    }
    if (accessRequirements != null) {
        JSONArrayAdapter arArray = writeTo.createNewArray();
        for (int i = 0; i < accessRequirements.size(); i++) {
            JSONObjectAdapter joa = arArray.createNew();
            accessRequirements.get(i).writeToJSONObject(joa);
            arArray.put(i, joa);
        }
        writeTo.put(JSON_ACCESS_REQUIREMENTS, arArray);
    }
    if (unmetAccessRequirements != null) {
        JSONArrayAdapter arArray = writeTo.createNewArray();
        for (int i = 0; i < unmetAccessRequirements.size(); i++) {
            JSONObjectAdapter joa = arArray.createNew();
            unmetAccessRequirements.get(i).writeToJSONObject(joa);
            arArray.put(i, joa);
        }
        writeTo.put(JSON_UNMET_ACCESS_REQUIREMENTS, arArray);
    }
    return writeTo;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) JSONArrayAdapter(org.sagebionetworks.schema.adapter.JSONArrayAdapter)

Example 2 with JSONObjectAdapter

use of org.sagebionetworks.schema.adapter.JSONObjectAdapter in project Synapse-Repository-Services by Sage-Bionetworks.

the class Annotations method initializeFromJSONObject.

@Override
public JSONObjectAdapter initializeFromJSONObject(JSONObjectAdapter toInitFrom) throws JSONObjectAdapterException {
    if (toInitFrom.has(JSON_ID)) {
        this.id = toInitFrom.getString(JSON_ID);
    }
    if (toInitFrom.has(JSON_URI)) {
        this.uri = toInitFrom.getString(JSON_URI);
    }
    if (toInitFrom.has(JSON_ETAG)) {
        this.etag = toInitFrom.getString(JSON_ETAG);
    }
    if (toInitFrom.has(JSON_CREATION_DATE)) {
        this.creationDate = toInitFrom.convertStringToDate(FORMAT.UTC_MILLISEC, toInitFrom.getString(JSON_CREATION_DATE));
    }
    if (toInitFrom.has(JSON_STRING_ANNOTATIONS)) {
        JSONObjectAdapter object = toInitFrom.getJSONObject(JSON_STRING_ANNOTATIONS);
        this.stringAnnotations = AdapterCollectionUtils.createMapOfCollection(object, String.class);
    }
    if (toInitFrom.has(JSON_DOUBLE_ANNOTATIONS)) {
        JSONObjectAdapter object = toInitFrom.getJSONObject(JSON_DOUBLE_ANNOTATIONS);
        this.doubleAnnotations = AdapterCollectionUtils.createMapOfCollection(object, Double.class);
    }
    if (toInitFrom.has(JSON_LONG_ANNOTATIONS)) {
        JSONObjectAdapter object = toInitFrom.getJSONObject(JSON_LONG_ANNOTATIONS);
        this.longAnnotations = AdapterCollectionUtils.createMapOfCollection(object, Long.class);
    }
    if (toInitFrom.has(JSON_DATE_ANNOTATIONS)) {
        JSONObjectAdapter object = toInitFrom.getJSONObject(JSON_DATE_ANNOTATIONS);
        this.dateAnnotations = AdapterCollectionUtils.createMapOfCollection(object, Date.class);
    }
    if (toInitFrom.has(JSON_BLOB_ANNOTATIONS)) {
        JSONObjectAdapter object = toInitFrom.getJSONObject(JSON_BLOB_ANNOTATIONS);
        this.blobAnnotations = AdapterCollectionUtils.createMapOfCollection(object, byte[].class);
    }
    return toInitFrom;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) Date(java.util.Date)

Example 3 with JSONObjectAdapter

use of org.sagebionetworks.schema.adapter.JSONObjectAdapter in project Synapse-Repository-Services by Sage-Bionetworks.

the class BatchResults method initializeFromJSONObject.

@Override
public JSONObjectAdapter initializeFromJSONObject(JSONObjectAdapter adapter) throws JSONObjectAdapterException {
    if (adapter == null)
        throw new IllegalArgumentException("Adapter cannot be null");
    totalNumberOfResults = adapter.getLong("totalNumberOfResults");
    if (!adapter.isNull("results")) {
        this.results = new ArrayList<T>();
        JSONArrayAdapter array = adapter.getJSONArray("results");
        for (int i = 0; i < array.length(); i++) {
            JSONObjectAdapter childAdapter = array.getJSONObject(i);
            try {
                T newInstance = (T) factory.newInstance(clazz.getName());
                newInstance.initializeFromJSONObject(childAdapter);
                this.results.add(newInstance);
            } catch (Exception e) {
                throw new JSONObjectAdapterException(e);
            }
        }
    }
    return adapter;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONArrayAdapter(org.sagebionetworks.schema.adapter.JSONArrayAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException)

Example 4 with JSONObjectAdapter

use of org.sagebionetworks.schema.adapter.JSONObjectAdapter in project Synapse-Repository-Services by Sage-Bionetworks.

the class EntityBundleCreate method initializeFromJSONObject.

@Override
public JSONObjectAdapter initializeFromJSONObject(JSONObjectAdapter toInitFrom) throws JSONObjectAdapterException {
    if (toInitFrom == null) {
        throw new IllegalArgumentException("org.sagebionetworks.schema.adapter.JSONObjectAdapter cannot be null");
    }
    if (toInitFrom.has(JSON_ENTITY)) {
        entityType = toInitFrom.getString(JSON_ENTITY_TYPE);
        JSONObjectAdapter joa = (JSONObjectAdapter) toInitFrom.getJSONObject(JSON_ENTITY);
        entity = (Entity) autoGenFactory.newInstance(entityType);
        entity.initializeFromJSONObject(joa);
    }
    if (toInitFrom.has(JSON_ANNOTATIONS)) {
        JSONObjectAdapter joa = (JSONObjectAdapter) toInitFrom.getJSONObject(JSON_ANNOTATIONS);
        if (annotations == null)
            annotations = new Annotations();
        annotations.initializeFromJSONObject(joa);
    }
    if (toInitFrom.has(JSON_ACL)) {
        JSONObjectAdapter joa = (JSONObjectAdapter) toInitFrom.getJSONObject(JSON_ACL);
        if (acl == null)
            acl = (AccessControlList) autoGenFactory.newInstance(AccessControlList.class.getName());
        acl.initializeFromJSONObject(joa);
    }
    if (toInitFrom.has(JSON_ACCESS_REQUIREMENT)) {
        JSONObjectAdapter joa = (JSONObjectAdapter) toInitFrom.getJSONObject(JSON_ACCESS_REQUIREMENT);
        accessRequirement = (AccessRequirement) EntityClassHelper.deserialize(joa);
    }
    return toInitFrom;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter)

Example 5 with JSONObjectAdapter

use of org.sagebionetworks.schema.adapter.JSONObjectAdapter in project Synapse-Repository-Services by Sage-Bionetworks.

the class PaginatedResults method initializeFromJSONObject.

@Override
public JSONObjectAdapter initializeFromJSONObject(JSONObjectAdapter adapter) throws JSONObjectAdapterException {
    if (adapter == null)
        throw new IllegalArgumentException("Adapter cannot be null");
    totalNumberOfResults = adapter.getLong("totalNumberOfResults");
    if (!adapter.isNull("results")) {
        this.results = new ArrayList<T>();
        JSONArrayAdapter array = adapter.getJSONArray("results");
        for (int i = 0; i < array.length(); i++) {
            JSONObjectAdapter childAdapter = array.getJSONObject(i);
            try {
                T newInstance = (T) factory.newInstance(clazz.getName());
                newInstance.initializeFromJSONObject(childAdapter);
                this.results.add(newInstance);
            } catch (Exception e) {
                throw new JSONObjectAdapterException(e);
            }
        }
    }
    if (!adapter.isNull("paging")) {
        JSONObjectAdapter pagingAdapter = adapter.getJSONObject("paging");
        this.paging = new HashMap<String, String>();
        Iterator<String> it = pagingAdapter.keys();
        while (it.hasNext()) {
            String key = it.next();
            String value = pagingAdapter.getString(key);
            this.paging.put(key, value);
        }
    }
    return adapter;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONArrayAdapter(org.sagebionetworks.schema.adapter.JSONArrayAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException)

Aggregations

JSONObjectAdapter (org.sagebionetworks.schema.adapter.JSONObjectAdapter)76 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)49 Test (org.junit.Test)28 JSONObjectAdapterException (org.sagebionetworks.schema.adapter.JSONObjectAdapterException)23 JSONObject (org.json.JSONObject)18 ArrayList (java.util.ArrayList)15 Date (java.util.Date)14 JSONArrayAdapter (org.sagebionetworks.schema.adapter.JSONArrayAdapter)13 SynapseException (org.sagebionetworks.client.exceptions.SynapseException)9 PaginatedResults (org.sagebionetworks.repo.model.PaginatedResults)9 Annotations (org.sagebionetworks.repo.model.Annotations)6 EntityHeader (org.sagebionetworks.repo.model.EntityHeader)6 VariableContentPaginatedResults (org.sagebionetworks.repo.model.VariableContentPaginatedResults)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 StringEntity (org.apache.http.entity.StringEntity)5 EntityBundle (org.sagebionetworks.repo.model.EntityBundle)5 ObjectSchema (org.sagebionetworks.schema.ObjectSchema)5 JBlock (com.sun.codemodel.JBlock)3 JFieldRef (com.sun.codemodel.JFieldRef)3