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:io.liveoak.mongo.MongoCollectionResource.java

License:Open Source License

@Override
public Map<String, ?> properties(RequestContext ctx) throws Exception {

    queryObject = new BasicDBObject();
    ResourceParams resourceParams = ctx.resourceParams();
    if (resourceParams != null) {
        if (resourceParams.contains("q")) {
            String queryString = resourceParams.value("q");
            try {
                queryObject = (DBObject) JSON.parse(queryString);
            } catch (Exception e) {
                throw new NotAcceptableException(uri().toString(),
                        "Invalid JSON format for the 'query' parameter", e);
            }//from   www .ja v a  2s.  c om
        }
    }

    explainQuery = resourceParams != null && resourceParams.contains("explain")
            && resourceParams.value("explain").equalsIgnoreCase("true");

    DBObject returnFields = new BasicDBObject();
    if (ctx.returnFields() != null && !ctx.returnFields().isAll()) {
        ctx.returnFields().forEach((fieldName) -> {
            returnFields.put(fieldName, true);
        });
    }

    int totalCount = explainQuery ? 1 : (int) dbCollection.getCount(queryObject, returnFields);
    int count = ctx.pagination().offset() >= totalCount ? 0 : totalCount - ctx.pagination().offset();
    count = count < ctx.pagination().limit() ? count : ctx.pagination().limit();

    List<Resource> links = new LinkedList<>();

    PagingLinksBuilder linksBuilder = new PagingLinksBuilder(ctx).uri(uri()).count(count)
            .totalCount(totalCount);

    links.addAll(linksBuilder.build());

    Map<String, Object> result = new HashMap<>();
    if (links.size() > 0) {
        result.put("links", links);
    }
    result.put("type", "collection");
    result.put("count", (long) totalCount);
    result.put("capped", dbCollection.isCapped());

    DBObject collectionDBObject = getDBCollection().getDB().getCollection("system").getCollection("namespaces")
            .findOne(new BasicDBObject("name", this.getDBCollection().getFullName()));

    if (collectionDBObject != null && collectionDBObject.get("options") != null) {
        DBObject collectionOptions = (DBObject) collectionDBObject.get("options");
        result.put("max", collectionOptions.get("max"));
        result.put("size", collectionOptions.get("size"));
    }

    return result;
}

From source file:io.liveoak.mongo.MongoCollectionResource.java

License:Open Source License

@Override
public Collection<Resource> members(RequestContext ctx) throws Exception {

    LinkedList<Resource> members = new LinkedList<>();

    DBObject returnFields = new BasicDBObject();
    if (ctx.returnFields() != null && !ctx.returnFields().child(LiveOak.MEMBERS).isEmpty()) {
        ReturnFields membersReturnFields = ctx.returnFields().child(LiveOak.MEMBERS);
        if (!membersReturnFields.isAll()) {
            membersReturnFields.forEach((fieldName) -> {
                returnFields.put(fieldName, true);
            });//from   w ww.  j  a va  2  s.c om
        }
    }

    DBCursor dbCursor = dbCollection.find(queryObject, returnFields);

    ResourceParams resourceParams = ctx.resourceParams();
    if (resourceParams != null && resourceParams.contains("hint")) {
        String hint = resourceParams.value("hint");
        if (hint.startsWith("{")) {
            try {
                DBObject hintObject = (DBObject) JSON.parse(hint);
                dbCursor.hint(hintObject);
            } catch (Exception e) {
                throw new NotAcceptableException(uri().toString(),
                        "Invalid JSON format for the 'hint' parameter", e);
            }
        } else {
            dbCursor.hint(hint);
        }
    }

    if (explainQuery) {
        members.add(new MongoEmbeddedObjectResource(this, dbCursor.explain()));
    } else {
        Sorting sorting = ctx.sorting();
        if (sorting != null) {
            BasicDBObject sortingObject = new BasicDBObject();
            for (Sorting.Spec spec : sorting) {
                sortingObject.append(spec.name(), spec.ascending() ? 1 : -1);
            }
            dbCursor = dbCursor.sort(sortingObject);
        }

        Pagination pagination = ctx.pagination();
        if (pagination != null) {
            dbCursor.limit(pagination.limit());
            dbCursor.skip(pagination.offset());
        }

        try {
            dbCursor.hasNext();
        } catch (Exception e) {
            throw new ResourceProcessingException(
                    "Exception encountered trying to fetch data from the Mongo Database", e);
        }

        dbCursor.forEach((dbObject) -> {
            members.add(new MongoBaseObjectResource(this, dbObject));
        });
    }

    return members;
}

From source file:jahspotify.storage.statistics.MongoDBHistoricalStorage.java

License:Apache License

@Override
public void addHistory(final TrackHistory trackHistory) {
    final DBCollection history = _db.getCollection("history");
    final BasicDBObject basicDBObject = (BasicDBObject) JSON.parse(new Gson().toJson(trackHistory));
    final WriteResult insert = history.insert(basicDBObject);
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * This update Federation User with element. 
 * @param dbName/*  ww w.jav a 2 s.  c  o m*/
 * @param tableName
 * @param faSite, this is the cloud Id
 * @param docJSON 
 * @author gtricomi
 */
public void insertNetTables(String dbName, String faSite, String docJSON, double version) {

    DB dataBase = this.getDB(dbName);
    DBCollection collezione = this.getCollection(dataBase, "netTables");
    BasicDBObject obj = (BasicDBObject) JSON.parse(docJSON);
    obj.append("referenceSite", faSite);
    obj.append("insertTimestamp", System.currentTimeMillis());
    obj.append("version", version);
    collezione.save(obj);
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * This update Federation User with element. 
 * @param dbName/*ww  w  . jav a  2  s  . c om*/
 * @param faSite, this is the cloud Id
 * @param docJSON 
 * @author gtricomi
 */
public void insertSiteTables(String dbName, String faSite, String docJSON) {

    DB dataBase = this.getDB(dbName);
    DBCollection collezione = this.getCollection(dataBase, "siteTables");
    BasicDBObject obj = (BasicDBObject) JSON.parse(docJSON);
    obj.append("referenceSite", faSite);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * This update Federation User with element. 
 * @param dbName// ww w. ja v a2 s  .  c o m
 * @param tableName
 * @param faSite, this is the cloud Id
 * @param docJSON 
 * @author gtricomi
 */
public void insertTenantTables(String dbName, String tableName, String faSite, String docJSON) {

    DB dataBase = this.getDB(dbName);
    DBCollection collezione = this.getCollection(dataBase, "TenantTables");
    BasicDBObject obj = (BasicDBObject) JSON.parse(docJSON);
    obj.append("referenceSite", faSite);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * This update Federation User with element 
 * @param dbName//from ww  w.j  a  v  a 2s  .co  m
 * @param collectionName
 * @param docJSON 
 */
public void insertUser(String dbName, String collectionName, String docJSON) {

    DB dataBase = this.getDB(dbName);
    DBCollection collezione = this.getCollection(dataBase, collectionName);
    BasicDBObject obj = (BasicDBObject) JSON.parse(docJSON);
    String userName;
    userName = obj.getString("federationUser");
    obj.append("_id", userName);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);
}

From source file:MDBInt.DBMongo.java

License:Apache License

public void insert(String dbName, String collectionName, String docJSON) {

    DB dataBase = this.getDB(dbName);
    DBCollection collezione = this.getCollection(dataBase, collectionName);
    BasicDBObject obj = (BasicDBObject) JSON.parse(docJSON);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);//from   w  w  w  .  j  a  va  2 s. c om
}

From source file:MDBInt.DBMongo.java

License:Apache License

public void updateUser(String dbName, String collectionName, String docJSON) {

    DB dataBase = this.getDB(dbName);
    DBCollection collezione = this.getCollection(dataBase, collectionName);
    BasicDBObject obj = (BasicDBObject) JSON.parse(docJSON);
    String userName;/*from  w w  w .  jav a 2  s.com*/
    userName = obj.getString("federationUser");
    obj.append("_id", userName);

    collezione.save(obj);
}

From source file:MDBInt.DBMongo.java

License:Apache License

public void insertFederatedCloud(String dbName, String collectionName, String docJSON) {

    DB dataBase = this.getDB(dbName);
    DBCollection collezione = this.getCollection(dataBase, collectionName);
    BasicDBObject obj = (BasicDBObject) JSON.parse(docJSON);
    String userName;/*  w w  w.ja v  a 2 s  .c o  m*/
    userName = obj.getString("cloudId");
    obj.append("_id", userName);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);
}