Example usage for com.mongodb DBCursor toArray

List of usage examples for com.mongodb DBCursor toArray

Introduction

In this page you can find the example usage for com.mongodb DBCursor toArray.

Prototype

public List<DBObject> toArray() 

Source Link

Document

Converts this cursor to an array.

Usage

From source file:com.restfeel.controller.rest.EntityDataController.java

License:Apache License

@RequestMapping(value = "/api/{projectId}/entities/{name}/list", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody String getEntityDataList(@PathVariable("projectId") String projectId,
        @PathVariable("name") String entityName, @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "limit", required = false) Integer limit,
        @RequestParam(value = "sort", required = false) String sort,
        @RequestParam(value = "query", required = false) String query,
        @RequestHeader(value = "authToken", required = false) String authToken) {

    JSONObject authRes = authService.authorize(projectId, authToken, "USER");
    if (!authRes.getBoolean(SUCCESS)) {
        return authRes.toString(4);
    }// ww  w.ja v  a2  s  .  com

    DBCollection dbCollection = mongoTemplate.getCollection(projectId + "_" + entityName);
    DBCursor cursor;
    if (query != null && !query.isEmpty()) {
        Object queryObject = JSON.parse(query);
        cursor = dbCollection.find((BasicDBObject) queryObject);
    } else {
        cursor = dbCollection.find();
    }

    if (sort != null && !sort.isEmpty()) {
        Object sortObject = JSON.parse(sort);
        cursor.sort((BasicDBObject) sortObject);
    }

    if (limit != null && limit > 0) {
        if (page != null && page > 0) {
            cursor.skip((page - 1) * limit);
        }
        cursor.limit(limit);
    }
    List<DBObject> array = cursor.toArray();

    if (entityName.equals("User")) {
        for (DBObject dbObject : array) {
            dbObject.removeField(PASSWORD);
        }
    }

    for (DBObject dbObject : array) {
        dbRefToRelation(dbObject);
    }
    String json = JSON.serialize(array);

    // Indentation
    JSONArray jsonArr = new JSONArray(json);
    return jsonArr.toString(4);
}

From source file:com.restfiddle.controller.rest.EntityDataController.java

License:Apache License

@RequestMapping(value = "/api/{projectId}/entities/{name}/list", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody String getEntityDataList(@PathVariable("projectId") String projectId,
        @PathVariable("name") String entityName, @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "limit", required = false) Integer limit,
        @RequestParam(value = "sort", required = false) String sort,
        @RequestParam(value = "query", required = false) String query) {
    DBCollection dbCollection = mongoTemplate.getCollection(entityName);
    DBCursor cursor = null;
    if (query != null && !query.isEmpty()) {
        Object queryObject = JSON.parse(query);
        cursor = dbCollection.find((BasicDBObject) queryObject);
    } else {//from   w  ww. j  ava  2 s. c  o m
        cursor = dbCollection.find();
    }

    if (sort != null && !sort.isEmpty()) {
        Object sortObject = JSON.parse(sort);
        cursor.sort((BasicDBObject) sortObject);
    }

    if (limit != null && limit > 0) {
        if (page != null && page > 0) {
            cursor.skip((page - 1) * limit);
        }
        cursor.limit(limit);
    }
    List<DBObject> array = cursor.toArray();
    String json = JSON.serialize(array);

    // Indentation
    JSONArray jsonArr = new JSONArray(json);
    return jsonArr.toString(4);
}

From source file:com.sanaldiyar.projects.nanohttpd.mongodbbasedsessionhandler.MongoDBBasedSessionHandler.java

/**
 * Request parser for session. Gets and builds session information
 *
 * @param request the request/*from ww w  . ja va2 s.  c om*/
 * @return session manager
 */
@Override
public NanoSessionManager parseRequest(Request request) {
    MongoDBBasedSessionManager nanoSessionManager = null;
    DBObject session = null;
    String sessionid = null;
    for (Cookie cookie : request.getCookies()) {
        if (cookie.getName().equals(SESSIONCOOKIEID)) {
            sessionid = cookie.getValue();
            break;
        }
    }
    DBCollection sessions = managers.getCollection("sessions");
    if (sessionid != null) {
        DBCursor cursor = sessions.find(new BasicDBObject("sessionid", sessionid));
        List<DBObject> result = cursor.toArray();
        cursor.close();
        if (result.size() == 1) {
            session = result.get(0);
        }
        if (session != null) {
            if (((Date) session.get("expires")).getTime() <= new Date().getTime()) {
                sessions.remove(new BasicDBObject().append("sessionid", sessionid));
                session = null;
            }
        }
    }
    if (session == null) {
        do {
            sessionid = new BigInteger(128, srng).toString(32);
        } while (sessions.findOne(new BasicDBObject().append("sessionid", sessionid)) != null
                && !sessionid.equals("0"));
        session = new BasicDBObject();
        nanoSessionManager = new MongoDBBasedSessionManager(session);
        nanoSessionManager.setSessionID(sessionid);
        sessions.insert(session);
    } else {
        nanoSessionManager = new MongoDBBasedSessionManager(session);
    }
    return nanoSessionManager;
}

From source file:com.softinstigate.restheart.db.CollectionDAO.java

License:Open Source License

private static ArrayList<DBObject> getDataFromCursor(DBCursor cursor) {
    return new ArrayList<>(cursor.toArray());
}

From source file:com.softinstigate.restheart.db.DocumentDAO.java

License:Open Source License

/**
 *
 * @param cursor//from  w w w .  jav a 2  s . c  o  m
 * @return
 */
public static ArrayList<DBObject> getDataFromCursor(DBCursor cursor) {
    return new ArrayList<>(cursor.toArray());
}

From source file:com.university.mongodb.courses.m101j.blogapplication.BlogPostDAO.java

License:Apache License

public List<DBObject> findByDateDescending(int limit) {

    List<DBObject> posts = null;
    // XXX HW 3.2, Work Here
    // Return a list of DBObjects, each one a post from the posts collection
    DBCursor cursor = postsCollection.find(new BasicDBObject()).limit(limit)
            .sort(new BasicDBObject(POST_DATE, -1));
    posts = new ArrayList<>(cursor.count());

    try {//  www  . j  ava  2 s .c  o m
        posts = cursor.toArray();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cursor.close();
    }

    return posts;
}

From source file:controlador.PaginaControlador.java

public List<DBObject> listar() {

    BasicDBObject index = new BasicDBObject("id", 1);
    try {//from w  w  w  .  ja v a  2s.c  o  m
        DBCursor resultados = paginaCollection.find();
        System.out.println(resultados.toArray());
        return resultados.toArray();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
}

From source file:controlador.PaginaControlador.java

public List<DBObject> buscar(int id) {

    try {/*from  w ww .  j a  va 2s . c  o m*/
        DBCursor resultados = paginaCollection.find(new BasicDBObject("id", id));
        System.out.println(resultados.toArray());
        return resultados.toArray();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
}

From source file:controlador.PalabraControlador.java

public List<DBObject> listar() {

    try {//from   w w  w  .  j av  a2 s  .  c o  m
        DBCursor resultados = palabraCollection.find();
        System.out.println(resultados.toArray());
        return resultados.toArray();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
}

From source file:controlador.PalabraControlador.java

public List<DBObject> buscar(int id_page, String palabra) {

    try {//from  w  w w  . j a  v  a 2s .  c o m
        DBCursor resultados = palabraCollection
                .find(new BasicDBObject("palabra", palabra).append("id_page", id_page));
        return resultados.toArray();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
}