Example usage for com.mongodb Block Block

List of usage examples for com.mongodb Block Block

Introduction

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

Prototype

Block

Source Link

Usage

From source file:helpers.Pais.java

public static Pais getPaisById(ObjectId id) {
    Pais obj = new Pais();

    MongoManager mongo = MongoManager.getInstance();

    FindIterable<Document> iterable = mongo.db.getCollection("pais").find(new Document("_id", id));

    iterable.forEach(new Block<Document>() {
        @Override/*w  w  w.j  a  va 2  s. co m*/
        public void apply(final Document document) {

            obj.id = (ObjectId) document.get("_id");
            obj.nombre = document.get("nombre").toString();
        }

    });

    return obj;
}

From source file:helpers.Pais.java

public static List<Pais> getAllPais() {
    List<Pais> res = new ArrayList<>();

    MongoManager mongo = MongoManager.getInstance();
    FindIterable<Document> iterable = mongo.db.getCollection("pais").find();
    iterable.forEach(new Block<Document>() {
        @Override//  w  w  w. ja va 2  s. c om
        public void apply(final Document document) {
            Pais obj = new Pais();
            obj.id = (ObjectId) document.get("_id");
            obj.nombre = document.get("nombre").toString();

            res.add(obj);
        }

    });

    return res;
}

From source file:helpers.Provincia.java

public static Provincia getProvinciaByName(String name) {
    Provincia obj = new Provincia();

    MongoManager mongo = MongoManager.getInstance();

    FindIterable<Document> iterable = mongo.db.getCollection("provincia").find(new Document("nombre", name));

    iterable.forEach(new Block<Document>() {
        @Override//from   w w  w.  j  a va2  s  . c  o m
        public void apply(final Document document) {

            obj.id = (ObjectId) document.get("_id");
            obj.idPais = (ObjectId) document.get("idpais");
            obj.nombre = document.get("nombre").toString();

            obj.leyendaPais = Pais.getPaisById(obj.idPais).nombre;
        }

    });

    return obj;
}

From source file:helpers.Provincia.java

public static Provincia getProvinciaById(ObjectId id) {
    Provincia obj = new Provincia();

    MongoManager mongo = MongoManager.getInstance();

    FindIterable<Document> iterable = mongo.db.getCollection("provincia").find(new Document("_id", id));

    iterable.forEach(new Block<Document>() {
        @Override//from   ww w.  ja va  2s.com
        public void apply(final Document document) {

            obj.id = (ObjectId) document.get("_id");
            obj.idPais = (ObjectId) document.get("idpais");
            obj.nombre = document.get("nombre").toString();

            obj.leyendaPais = Pais.getPaisById(obj.idPais).nombre;
        }

    });

    return obj;
}

From source file:helpers.Provincia.java

public static List<Provincia> getAllProvincia() {
    List<Provincia> res = new ArrayList<>();

    MongoManager mongo = MongoManager.getInstance();
    FindIterable<Document> iterable = mongo.db.getCollection("provincia").find();
    iterable.forEach(new Block<Document>() {
        @Override/*from w  ww  .j a v  a2  s . c  om*/
        public void apply(final Document document) {
            Provincia obj = new Provincia();
            obj.id = (ObjectId) document.get("_id");
            obj.idPais = (ObjectId) document.get("idpais");
            obj.nombre = document.get("nombre").toString();

            obj.leyendaPais = Pais.getPaisById(obj.idPais).nombre;

            res.add(obj);
        }

    });

    return res;
}

From source file:helpers.Provincia.java

public static List<Provincia> getAllProvinciaByPais(ObjectId id) {
    List<Provincia> res = new ArrayList<>();

    MongoManager mongo = MongoManager.getInstance();
    FindIterable<Document> iterable = mongo.db.getCollection("provincia").find(new Document("idpais", id));
    iterable.forEach(new Block<Document>() {
        @Override//from w  ww.ja  va  2s . c o  m
        public void apply(final Document document) {
            Provincia obj = new Provincia();
            obj.id = (ObjectId) document.get("_id");
            obj.idPais = (ObjectId) document.get("idpais");
            obj.nombre = document.get("nombre").toString();

            obj.leyendaPais = Pais.getPaisById(obj.idPais).nombre;

            res.add(obj);
        }

    });

    return res;
}

From source file:io.gravitee.gateway.registry.mongodb.MongoDBRegistry.java

License:Apache License

private void readConfiguration() {
    LOGGER.info("Loading Gravitee configuration from MongoDB database '{}'", mongoDatabase.getName());
    final FindIterable<DBObject> apis = mongoDatabase.getCollection("apis", DBObject.class).find();
    apis.forEach(new Block<DBObject>() {
        @Override/*from ww  w  .  ja va2  s  . c om*/
        public void apply(final DBObject dbObject) {
            register(apiConverter.convertFrom(dbObject));
        }
    });
    LOGGER.info("{} API(s) registered", listAll().size());
}

From source file:javasensei.db.managments.BitacoraEjerciciosManager.java

public String obtenerBitacora(String idAlumno, String ejercicioId, String fechaInicial, String fechaFinal,
        String sesionId, String emocionInicial, String emocionFinal) {

    MongoCollection<DBObject> bitacoras = Connection.getDBV3().getCollection("bitacora_ejercicios",
            DBObject.class);
    Document query = new Document();

    if (!idAlumno.isEmpty()) {
        query.append("idAlumno", Integer.parseInt(idAlumno));
    }/*from  w w  w. ja  v a 2s .com*/

    if (!ejercicioId.isEmpty()) {
        query.append("ejercicioId", Integer.parseInt(ejercicioId));
    }

    if (!sesionId.isEmpty()) {
        query.append("sesionId", Integer.parseInt(sesionId));
    }
    //Variables para las fechas
    Date dateI;
    Date dateF;
    if (!fechaFinal.isEmpty() && !fechaInicial.isEmpty()) {

        dateI = Date.from(LocalDateTime.parse(fechaInicial).toInstant(ZoneOffset.UTC));
        dateF = Date.from(LocalDateTime.parse(fechaFinal).toInstant(ZoneOffset.UTC));
        query.append("fecha", new Document("$gte", dateI).append("$lte", dateF));

    } else if (!fechaInicial.isEmpty()) {

        dateI = Date.from(LocalDateTime.parse(fechaInicial).toInstant(ZoneOffset.UTC));
        query.append("fecha", new Document("$gte", dateI));

    } else if (!fechaFinal.isEmpty()) {
        dateF = Date.from(LocalDateTime.parse(fechaFinal).toInstant(ZoneOffset.UTC));
        query.append("fecha", new Document("$lte", dateF));
    }

    //Lista donde se guardaran las bitacoras a enviar
    List<DBObject> dbo = bitacoras.find().into(new ArrayList<>());
    Map<Integer, List<DBObject>> map = new HashMap<>();

    if (!emocionInicial.isEmpty() && !emocionFinal.isEmpty()) {
        FindIterable<DBObject> iterable = bitacoras.find(query)
                .projection(new Document("fotografias", 0).append("_id", 0))
                .sort(new BasicDBObject("fecha", 1));

        //Recorro las bitacoras
        iterable.forEach(new Block<DBObject>() {
            @Override
            public void apply(final DBObject document) {
                Integer sesionId = Integer.parseInt(document.get("sesionId") + "");

                List<DBObject> list = new ArrayList<>();

                if (!map.containsKey(sesionId)) {
                    list.add(document);
                    map.put(sesionId, list);

                } else {
                    map.get(sesionId).add(document);
                }
            }
        });

        List<DBObject> lista = new ArrayList<>();
        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            List<DBObject> l = map.get(it.next());
            String emocionIni = l.get(0).get("emocion").toString().toUpperCase();
            String emocionFin = l.get(l.size() - 1).get("emocion").toString().toUpperCase();

            if (emocionIni.equals(emocionInicial.toUpperCase())
                    && emocionFin.equals(emocionFinal.toUpperCase())) {
                lista.addAll(l);
            }
        }
        dbo = lista;

        //Si solo se busca las bitacoras con la emocin inicial
    } else if (!emocionInicial.isEmpty()) {
        FindIterable<DBObject> iterable = bitacoras.find(query)
                .projection(new Document("fotografias", 0).append("_id", 0))
                .sort(new BasicDBObject("fecha", 1));

        //Recorro las bitacoras
        iterable.forEach(new Block<DBObject>() {
            @Override
            public void apply(final DBObject document) {
                Integer sesionId = Integer.parseInt(document.get("sesionId") + "");

                List<DBObject> list = new ArrayList<>();

                if (!map.containsKey(sesionId)) {
                    list.add(document);
                    map.put(sesionId, list);

                } else {
                    map.get(sesionId).add(document);
                }
            }
        });

        List<DBObject> lista = new ArrayList<>();
        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            List<DBObject> l = map.get(it.next());
            String emocionIni = l.get(0).get("emocion").toString().toUpperCase();

            if (emocionIni.equals(emocionInicial.toUpperCase())) {
                lista.addAll(l);
            }
        }
        dbo = lista;

        //Si solo se busca las bitacoras con la emocion final    
    } else if (!emocionFinal.isEmpty()) {
        FindIterable<DBObject> iterable = bitacoras.find(query)
                .projection(new Document("fotografias", 0).append("_id", 0))
                .sort(new BasicDBObject("fecha", 1));

        //Recorro las bitacoras
        iterable.forEach(new Block<DBObject>() {
            @Override
            public void apply(final DBObject document) {
                Integer sesionId = Integer.parseInt(document.get("sesionId") + "");

                List<DBObject> list = new ArrayList<>();

                if (!map.containsKey(sesionId)) {
                    list.add(document);
                    map.put(sesionId, list);

                } else {
                    map.get(sesionId).add(document);
                }
            }
        });

        List<DBObject> lista = new ArrayList<>();
        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            List<DBObject> l = map.get(it.next());
            String emocionFin = l.get(l.size() - 1).get("emocion").toString().toUpperCase();
            if (emocionFin.equals(emocionFinal.toUpperCase())) {
                lista.addAll(l);
            }
        }
        dbo = lista;

    } else {
        dbo = bitacoras.find(query).projection(new Document("fotografias", 0).append("_id", 0))
                .sort(new BasicDBObject("fecha", 1)).into(new ArrayList<>());
    }

    return dbo.toString();
}

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value find(Value request) throws FaultException {
    Value v = Value.create();//  w w w  .j  a va  2 s  .co  m
    FindIterable<BsonDocument> iterable = null;
    ;
    try {

        String collectionName = request.getFirstChild("collection").strValue();
        MongoCollection<BsonDocument> collection = db.getCollection(collectionName, BsonDocument.class);
        if (request.hasChildren("readConcern")) {

            ReadConcern readConcern = new ReadConcern(
                    ReadConcernLevel.fromString(request.getFirstChild("readConcern").strValue()));
            collection.withReadConcern(readConcern);
        }

        if (request.hasChildren("filter")) {
            BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue());
            prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter"));
            printlnJson("Query filter", bsonQueryDocument);
            if (request.hasChildren("sort") && request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument).limit(limitQuery);
            }

            if (request.hasChildren("sort") && request.hasChildren("limit") && request.hasChildren("skip")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int limitQuery = request.getFirstChild("limit").intValue();
                int skipPosition = request.getFirstChild("skip").intValue();
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument).limit(limitQuery)
                        .skip(skipPosition);
            }

            if (request.hasChildren("sort") && !request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument);
            }
            if (!request.hasChildren("sort") && request.hasChildren("limit")) {
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(bsonQueryDocument).limit(limitQuery);

            }
            if (!request.hasChildren("sort") && !request.hasChildren("limit")) {

                iterable = collection.find(bsonQueryDocument);
            }

        } else {

            if (request.hasChildren("sort") && request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int findLimit = request.getFirstChild("limit").intValue();

                iterable = collection.find(new Document()).sort(bsonSortDocument); ///.sort(bsonSortDocument).limit(limitQuery);
            }
            if (request.hasChildren("sort") && !request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                iterable = collection.find(new Document()).sort(bsonSortDocument);
            }
            if (!request.hasChildren("sort") && request.hasChildren("limit")) {
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(new Document()).limit(limitQuery);
            }
            if (!request.hasChildren("sort") && !request.hasChildren("limit")) {

                iterable = collection.find();
            }

        }
        iterable.forEach(new Block<BsonDocument>() {
            @Override
            public void apply(BsonDocument t) {
                Value queryValue = processQueryRow(t);
                printlnJson("Query document", t);
                v.getChildren("document").add(queryValue);
            }
        });
        showLog();
    } catch (MongoException ex) {
        showLog();

        throw new FaultException("MongoException", ex);

    } catch (JsonParseException ex) {
        showLog();
        throw new FaultException("JsonParseException", ex);
    }

    return v;
}

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value aggregate(Value request) throws FaultException {
    Value v = Value.create();/*w  w w. j a  v a  2  s  .  c  o m*/
    ArrayList<BsonDocument> bsonAggreagationDocuments = new ArrayList<>();
    String collectionName = request.getFirstChild("collection").strValue();
    MongoCollection<BsonDocument> collection = db.getCollection(collectionName, BsonDocument.class);
    if (request.hasChildren("readConcern")) {
        ReadConcern readConcern = new ReadConcern(
                ReadConcernLevel.fromString(request.getFirstChild("readConcern").strValue()));
        collection.withReadConcern(readConcern);
    }
    for (int counter = 0; counter < request.getChildren("filter").size(); counter++) {
        BsonDocument bsonAggregationDocument = BsonDocument
                .parse(request.getChildren("filter").get(counter).strValue());
        prepareBsonQueryData(bsonAggregationDocument, request.getChildren("filter").get(counter));
        printlnJson("Aggregate filter", bsonAggregationDocument);
        bsonAggreagationDocuments.add(bsonAggregationDocument);
    }
    AggregateIterable<BsonDocument> aggregation = db.getCollection(collectionName)
            .aggregate(bsonAggreagationDocuments, BsonDocument.class).allowDiskUse(Boolean.TRUE);
    aggregation.forEach(new Block<BsonDocument>() {
        @Override
        public void apply(BsonDocument t) {
            printlnJson("Aggregate Document", t);
            Value queryResult = processQueryRow(t);
            v.getChildren("document").add(queryResult);
        }
    });

    return v;
}