Example usage for com.mongodb.util JSON parse

List of usage examples for com.mongodb.util JSON parse

Introduction

In this page you can find the example usage for com.mongodb.util JSON parse.

Prototype

public static Object parse(final String jsonString) 

Source Link

Document

Parses a JSON string and returns a corresponding Java object.

Usage

From source file:org.codinjutsu.tools.nosql.mongo.model.MongoQueryOptions.java

License:Apache License

public void setSort(String query) {
    if (!StringUtils.isBlank(query)) {
        sort = (Bson) JSON.parse(query);
    }
}

From source file:org.csi.yucca.storage.datamanagementapi.service.StoreService.java

@POST
@Path("/apiCreateApiStore")
@Produces(MediaType.APPLICATION_JSON)/*from  w w  w. j a v  a  2s .co m*/
public String apiCreateApiStore(final String datasetInput) throws UnknownHostException {

    Gson gson = JSonHelper.getInstance();
    // match @nil elements
    String json = datasetInput.replaceAll("\\{\\n*\\t*.*@nil.*:.*\\n*\\t*\\}", "null");
    try {
        POJOStreams pojoStreams = gson.fromJson(json, POJOStreams.class);
        if (pojoStreams != null && pojoStreams.getStreams() != null
                && pojoStreams.getStreams().getStream() != null) {

            Stream newStream = pojoStreams.getStreams().getStream();

            // Aggiungi Stream allo store
            String tenant = newStream.getCodiceTenant();
            String sensor = newStream.getCodiceVirtualEntity();
            String stream = newStream.getCodiceStream();

            String apiName = tenant + "." + sensor + "_" + stream;
            try {
                createApiforStream(newStream, apiName, false, json);
            } catch (Exception duplicate) {
                if (duplicate.getMessage().toLowerCase().contains("duplicate")) {
                    createApiforStream(newStream, apiName, true, json);
                } else
                    throw duplicate;
            }

            if (newStream.getPublishStream() != 0) {
                publishStore("1.0", apiName, "admin");
                Set<String> tenantSet = new TreeSet<String>();
                if (newStream.getTenantssharing() != null) {
                    for (Tenantsharing tenantSh : newStream.getTenantssharing().getTenantsharing()) {
                        tenantSet.add(tenantSh.getTenantCode());
                        String appName = "userportal_" + tenantSh.getTenantCode();
                        StoreService.addSubscriptionForTenant(apiName, appName);
                    }
                }
                if (!tenantSet.contains(newStream.getCodiceTenant())) {
                    String appName = "userportal_" + newStream.getCodiceTenant();
                    StoreService.addSubscriptionForTenant(apiName, appName);
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e);
        return JSON.parse("{KO:1}").toString();
    }

    return JSON.parse("{OK:1}").toString();
}

From source file:org.csi.yucca.storage.datamanagementapi.service.StoreService.java

@POST
@Path("/apiCreateStreamStore")
@Produces(MediaType.APPLICATION_JSON)/*from   w  w w.jav a 2  s.  c om*/
public String apiCreateStreamStore(final String datasetInput) throws UnknownHostException {

    Gson gson = JSonHelper.getInstance();

    // match @nil elements
    String json = datasetInput.replaceAll("\\{\\n*\\t*.*@nil.*:.*\\n*\\t*\\}", "null");
    try {
        POJOStreams pojoStreams = gson.fromJson(json, POJOStreams.class);
        if (pojoStreams != null && pojoStreams.getStreams() != null
                && pojoStreams.getStreams().getStream() != null) {

            Stream newStream = pojoStreams.getStreams().getStream();

            // Aggiungi Stream allo store
            String tenant = newStream.getCodiceTenant();
            String sensor = newStream.getCodiceVirtualEntity();
            String stream = newStream.getCodiceStream();

            try {
                createStream(newStream, false, json);
            } catch (Exception duplicate) {
                log.error("Error on createStream (maybe duplicate...)", duplicate);
                if (duplicate.getMessage().toLowerCase().contains("duplicate")) {
                    createStream(newStream, true, json);
                } else
                    throw duplicate;
            }
            String apiName = tenant + "." + sensor + "_" + stream + "_stream";
            if (newStream.getPublishStream() != 0) {
                publishStore("1.0", apiName, "admin");
                Set<String> tenantSet = new TreeSet<String>();
                if (newStream.getTenantssharing() != null) {
                    for (Tenantsharing tenantSh : newStream.getTenantssharing().getTenantsharing()) {
                        tenantSet.add(tenantSh.getTenantCode());
                        String appName = "userportal_" + tenantSh.getTenantCode();
                        StoreService.addSubscriptionForTenant(apiName, appName);
                    }
                }
                if (!tenantSet.contains(newStream.getCodiceTenant())) {
                    String appName = "userportal_" + newStream.getCodiceTenant();
                    StoreService.addSubscriptionForTenant(apiName, appName);
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e);
        return JSON.parse("{KO:1}").toString();
    }
    return JSON.parse("{OK:1}").toString();
}

From source file:org.csi.yucca.storage.datamanagementapi.service.StoreService.java

@POST
@Path("/apiPublishStore")
@Produces(MediaType.APPLICATION_JSON)/* w w w.  j  ava  2  s .  c o  m*/
public String apiPublishStore(final String inputJson) throws UnknownHostException {
    try {
        JsonParser parser = new JsonParser();
        JsonObject rootObj = parser.parse(inputJson).getAsJsonObject();

        // String status = rootObj.get("status").getAsString();
        String apiVersion = rootObj.get("apiVersion").getAsString();
        String apiName = rootObj.get("apiName").getAsString();
        String provider = rootObj.get("provider").getAsString();

        publishStore(apiVersion, apiName, provider);

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e);
        return JSON.parse("{KO:1}").toString();
    }
    return JSON.parse("{OK:1}").toString();
}

From source file:org.csi.yucca.storage.datamanagementapi.service.StoreService.java

@POST
@Path("/apiRemoveStore")
@Produces(MediaType.APPLICATION_JSON)//w w  w  .  j  a v a2 s .  c o m
public String apiRemoveStore(final String inputJson) throws UnknownHostException {
    try {
        JsonParser parser = new JsonParser();
        JsonObject rootObj = parser.parse(inputJson).getAsJsonObject();
        String apiVersion = rootObj.get("apiVersion").getAsString();
        String apiName = rootObj.get("apiName").getAsString();
        String provider = rootObj.get("provider").getAsString();

        removeStore(apiVersion, apiName, provider);

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e);
        return JSON.parse("{KO:1}").toString();
    }
    return JSON.parse("{OK:1}").toString();
}

From source file:org.eclipse.birt.data.oda.mongodb.internal.impl.DriverUtil.java

License:Open Source License

static Object parseJSONExpr(String jsonExpr) throws OdaException {
    try {// www .j  a v  a 2s.c  om
        return JSON.parse(jsonExpr);
    } catch (JSONParseException ex) {
        String errMsg = Messages.bind(Messages.driverUtil_parsingError, jsonExpr);
        DriverUtil.getLogger().log(Level.INFO, errMsg, ex); // caller may choose to ignore it; log at INFO level

        OdaException newEx = new OdaException(errMsg);
        newEx.initCause(ex);
        throw newEx;
    }
}

From source file:org.eclipse.linuxtools.tmf.totalads.dbms.DBMS.java

License:Open Source License

/**
 * Inserts an object in the form of JSON representation into the database. Any kind of complex
 *  data structure can be converted to JSON  using gson library and passed to this function 
 * @param database Database name//  w w w  .j  a  v  a  2  s . c  o  m
 * @param jsonObject JSON Object
 * @param collection Collection name in which to insert 
 * @throws TotalADSDBMSException
 */
public void insertUsingJSON(String database, JsonObject jsonObject, String collection)
        throws TotalADSDBMSException {
    try {
        DB db = mongoClient.getDB(database);
        DBCollection coll = db.getCollection(collection);
        BasicDBObject obj = (BasicDBObject) JSON.parse(jsonObject.toString());
        coll.insert(obj);
    } catch (Exception ex) { // If there is any exception here cast it as DBMS exception
        throw new TotalADSDBMSException(ex.getMessage());
    }
}

From source file:org.eclipse.linuxtools.tmf.totalads.dbms.DBMS.java

License:Open Source License

/**
 * Inserts or updates (if already exists) an object in the form of JSON representation into the database. Any kind of complex
 *  data structure can be converted to JSON using gson library and passed to this function 
 * @param database Database name//from   w  w  w .  ja v  a2  s  .  c  om
 * @param jsonObject JSON Object
 * @param collection collection name
 */
public void insertOrUpdateUsingJSON(String database, JsonObject keytoSearch, JsonObject jsonObjectToUpdate,
        String collection) throws TotalADSDBMSException {
    DB db = mongoClient.getDB(database);
    DBCollection coll = db.getCollection(collection);

    BasicDBObject docToUpdate = (BasicDBObject) JSON.parse(jsonObjectToUpdate.toString());

    BasicDBObject keyToSearch = (BasicDBObject) JSON.parse(keytoSearch.toString());

    WriteResult writeRes = coll.update(keyToSearch, docToUpdate, true, false);

    CommandResult cmdResult = writeRes.getLastError();
    if (!cmdResult.ok())
        throw new TotalADSDBMSException("Error : " + cmdResult.getErrorMessage());

}

From source file:org.eclipse.tracecompass.totalads.dbms.MongoDBMS.java

License:Open Source License

@Override
public void insertUsingJSON(String database, JsonObject jsonObject, String collection)
        throws TotalADSDBMSException {
    try {/*  w  ww.  ja v  a 2 s .  co  m*/
        DB db = mongoClient.getDB(database);
        DBCollection coll = db.getCollection(collection);
        BasicDBObject obj = (BasicDBObject) JSON.parse(jsonObject.toString());
        coll.insert(obj);
    } catch (Exception ex) { // If there is any exception here, cast it as
                             // IDataAccessObject exception
        throw new TotalADSDBMSException(ex.getMessage());
    }
}

From source file:org.eclipse.tracecompass.totalads.dbms.MongoDBMS.java

License:Open Source License

@Override
public void insertOrUpdateUsingJSON(String database, JsonObject keytoSearch, JsonObject jsonObjectToUpdate,
        String collection) throws TotalADSDBMSException {
    DB db = mongoClient.getDB(database);
    DBCollection coll = db.getCollection(collection);

    BasicDBObject docToUpdate = (BasicDBObject) JSON.parse(jsonObjectToUpdate.toString());

    BasicDBObject keyToSearch = (BasicDBObject) JSON.parse(keytoSearch.toString());

    WriteResult writeRes = coll.update(keyToSearch, docToUpdate, true, false);

    CommandResult cmdResult = writeRes.getLastError();
    if (!cmdResult.ok()) {
        throw new TotalADSDBMSException("Error : " + cmdResult.getErrorMessage()); //$NON-NLS-1$
    }//w w w  . j a va 2s.c om

}