List of usage examples for com.mongodb.util JSON serialize
public static String serialize(final Object object)
Serializes an object into its JSON form.
This method delegates serialization to JSONSerializers.getLegacy
From source file:org.exoplatform.mongo.service.impl.MongoRestServiceImpl.java
License:Open Source License
@GET @Path("/databases/{dbName}/collections/{collName}/documents/{docId}") @Override/*from w w w . j a va 2 s . c o m*/ public Response findDocument(@PathParam("dbName") String dbName, @PathParam("collName") String collName, @PathParam("docId") String docId, @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context SecurityContext securityContext) { if (shutdown) { return Response.status(ServerError.SERVICE_UNAVAILABLE.code()) .entity(ServerError.SERVICE_UNAVAILABLE.message()).build(); } Response response = null; String user = null; try { Credentials credentials = authenticateAndAuthorize(headers, uriInfo, securityContext); user = credentials.getUserName(); String dbNamespace = constructDbNamespace(credentials.getUserName(), dbName); if (mongo.getDatabaseNames().contains(dbNamespace)) { DB db = mongo.getDB(dbNamespace); authServiceAgainstMongo(db); if (db.getCollectionNames().contains(collName)) { DBCollection dbCollection = db.getCollection(collName); DBObject query = new BasicDBObject(); query.put("_id", new ObjectId(docId)); DBObject found = dbCollection.findOne(query); if (found != null) { org.exoplatform.mongo.entity.response.Document document = new org.exoplatform.mongo.entity.response.Document(); document.setJson(JSON.serialize(found)); response = Response.ok(document).build(); } else { response = Response.status(ClientError.NOT_FOUND.code()) .entity(docId + " does not exist in " + collName).build(); } } else { response = Response.status(ClientError.NOT_FOUND.code()) .entity(collName + " does not exist in " + dbName).build(); } } else { response = Response.status(ClientError.NOT_FOUND.code()).entity(dbName + " does not exist").build(); } } catch (Exception exception) { response = lobException(exception, headers, uriInfo); } finally { updateStats(user, "findDocument"); } return response; }
From source file:org.exoplatform.mongo.service.impl.MongoRestServiceImpl.java
License:Open Source License
@GET @Path("/databases/{dbName}/collections/{collName}/documents") @Override/*from www . j av a 2 s.c o m*/ public Response findDocuments(@PathParam("dbName") String dbName, @PathParam("collName") String collName, @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context SecurityContext securityContext) { if (shutdown) { return Response.status(ServerError.SERVICE_UNAVAILABLE.code()) .entity(ServerError.SERVICE_UNAVAILABLE.message()).build(); } Response response = null; String user = null; try { Credentials credentials = authenticateAndAuthorize(headers, uriInfo, securityContext); user = credentials.getUserName(); String dbNamespace = constructDbNamespace(credentials.getUserName(), dbName); if (mongo.getDatabaseNames().contains(dbNamespace)) { DB db = mongo.getDB(dbNamespace); authServiceAgainstMongo(db); List<org.exoplatform.mongo.entity.response.Document> documents = new ArrayList<org.exoplatform.mongo.entity.response.Document>(); if (db.getCollectionNames().contains(collName)) { DBCollection dbCollection = db.getCollection(collName); DBCursor cursor = dbCollection.find(); while (cursor.hasNext()) { DBObject found = cursor.next(); if (found != null) { org.exoplatform.mongo.entity.response.Document document = new org.exoplatform.mongo.entity.response.Document(); document.setJson(JSON.serialize(found)); documents.add(document); } } response = Response.ok(documents).build(); } else { response = Response.status(ClientError.NOT_FOUND.code()) .entity(collName + " does not exist in " + dbName).build(); } } else { response = Response.status(ClientError.NOT_FOUND.code()).entity(dbName + " does not exist").build(); } } catch (Exception exception) { response = lobException(exception, headers, uriInfo); } finally { updateStats(user, "findDocuments"); } return response; }
From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java
License:Open Source License
@Override public <T extends JaqpotEntity> T merge(T entity) { MongoDatabase db = mongoClient.getDatabase(database); Class<T> entityClass = (Class<T>) entity.getClass(); String entityJSON = serializer.write(entity); Document entityBSON = Document.parse(entityJSON); MongoCollection<Document> collection = db.getCollection(collectionNames.get(entity.getClass())); Document oldEntity = collection.findOneAndReplace(new Document("_id", entity.getId()), entityBSON); return serializer.parse(JSON.serialize(oldEntity), entityClass); }
From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java
License:Open Source License
@Override public <T extends JaqpotEntity> T find(Class<T> entityClass, Object primaryKey) { MongoDatabase db = mongoClient.getDatabase(database); MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass)); Document retrieved = collection.find(new Document("_id", primaryKey)).first(); return serializer.parse(JSON.serialize(retrieved), entityClass); }
From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java
License:Open Source License
@Override public <T extends JaqpotEntity> T find(Class<T> entityClass, Object primaryKey, List<String> fields) { MongoDatabase db = mongoClient.getDatabase(database); MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass)); Document retrieved = collection.find(new Document("_id", primaryKey)).projection(include(fields)).first(); return serializer.parse(JSON.serialize(retrieved), entityClass); }
From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java
License:Open Source License
@Override public <T extends JaqpotEntity> List<T> find(Class<T> entityClass, Map<String, Object> properties, Integer start, Integer max) { MongoDatabase db = mongoClient.getDatabase(database); MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass)); properties.entrySet().stream().filter(e -> { return e.getValue() instanceof List; }).forEach(e -> {//from w w w . j a v a 2 s . c o m Map<String, Object> all = new HashMap<>(); all.put("$all", e.getValue()); properties.put(e.getKey(), all); }); List<T> result = new ArrayList<>(); collection.find(new Document(properties)).skip(start != null ? start : 0) .limit(max != null ? max : DEFAULT_PAGE_SIZE) .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result); return result; }
From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java
License:Open Source License
@Override public <T extends JaqpotEntity> List<T> findSorted(Class<T> entityClass, Map<String, Object> properties, Integer start, Integer max, List<String> ascendingFields, List<String> descendingFields) { MongoDatabase db = mongoClient.getDatabase(database); MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass)); properties.entrySet().stream().filter(e -> { return e.getValue() instanceof List; }).forEach(e -> {/*from w w w . j a va 2 s . com*/ Map<String, Object> all = new HashMap<>(); all.put("$all", e.getValue()); properties.put(e.getKey(), all); }); List<T> result = new ArrayList<>(); collection.find(new Document(properties)) .sort(Sorts.orderBy(Sorts.ascending(ascendingFields), Sorts.descending(descendingFields))) .skip(start != null ? start : 0).limit(max != null ? max : DEFAULT_PAGE_SIZE) .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result); return result; }
From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java
License:Open Source License
@Override public <T extends JaqpotEntity> List<T> findSortedAsc(Class<T> entityClass, Map<String, Object> properties, Integer start, Integer max, List<String> ascendingFields) { MongoDatabase db = mongoClient.getDatabase(database); MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass)); properties.entrySet().stream().filter(e -> { return e.getValue() instanceof List; }).forEach(e -> {/* w ww .ja v a 2 s .com*/ Map<String, Object> all = new HashMap<>(); all.put("$all", e.getValue()); properties.put(e.getKey(), all); }); List<T> result = new ArrayList<>(); collection.find(new Document(properties)).sort(Sorts.ascending(ascendingFields)) .skip(start != null ? start : 0).limit(max != null ? max : DEFAULT_PAGE_SIZE) .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result); return result; }
From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java
License:Open Source License
@Override public <T extends JaqpotEntity> List<T> findSortedDesc(Class<T> entityClass, Map<String, Object> properties, Integer start, Integer max, List<String> descendingFields) { MongoDatabase db = mongoClient.getDatabase(database); MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass)); properties.entrySet().stream().filter(e -> { return e.getValue() instanceof List; }).forEach(e -> {/*from w ww . j a va2 s .c om*/ Map<String, Object> all = new HashMap<>(); all.put("$all", e.getValue()); properties.put(e.getKey(), all); }); List<T> result = new ArrayList<>(); collection.find(new Document(properties)).sort(Sorts.descending(descendingFields)) .skip(start != null ? start : 0).limit(max != null ? max : DEFAULT_PAGE_SIZE) .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result); return result; }
From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java
License:Open Source License
@Override public <T extends JaqpotEntity> List<T> find(Class<T> entityClass, List<String> keys, List<String> fields) { MongoDatabase db = mongoClient.getDatabase(database); MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass)); List<T> result = new ArrayList<>(); Document query = new Document("_id", new Document("$in", keys)); Document filter = new Document(); fields.stream().forEach(f -> filter.put(f, 1)); collection.find(query).projection(filter) .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result); return result; }