Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

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

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:br.com.ifspsaocarlos.gastock.library.Mongodb.java

public List<BasicDBObject> buscaGeral() {

    DBCursor cursor = collection.find().sort(new BasicDBObject("cod", 1));
    List<BasicDBObject> resultado = new ArrayList<>();

    while (cursor.hasNext()) {
        resultado.add((BasicDBObject) cursor.next());
    }/*  w  w  w . ja  va  2s.c o m*/

    return resultado;
}

From source file:br.com.rbezerra.RegisterQueue.DAO.DefaultRegistroDAO.java

public List<Registro> listar() {
    List<Registro> atendimentos = new ArrayList<Registro>();
    DBCursor cursor = collectionReg.find();
    while (cursor.hasNext()) {
        atendimentos.add((Registro) cursor.next());
    }//from  ww  w. ja  va2s .  c o  m

    return atendimentos;
}

From source file:br.com.teste.mongo.MongoTeste.java

public void buscar(String termo) {
    DB db = connect();/* w ww . ja v a2 s .co m*/
    DBCollection collection = db.getCollection(termo);
    DBCursor cursor = collection.find();
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }
}

From source file:br.edu.unifesspa.lcc.indexer.GerarIndice.java

private void DownloadInfo() throws InterruptedException {
    //Verifica se j existe um banco de demanda criado no Mongo
    List<String> dbs = mongo.getDatabaseNames();
    if (!dbs.contains(assunto.toString())) {//Se o banco no existe, faz o download

        RestTemplate rt = new RestTemplate();
        DB territorios = mongo.getDB("territorios");
        DBCollection colTerritorios = territorios.getCollection("territorio");
        HttpEntity<String> entity = new HttpEntity<>("parameters", Processar.headers);
        BasicDBObject insert = new BasicDBObject();

        BasicDBObject insertDemanda = new BasicDBObject();

        DBCollection colDaDemanda = dbAssunto.getCollection(assunto.toString());

        System.out.println("db =" + dbAssunto);
        DBCollection k_classe = dbAssunto.getCollection("k");
        //K_CLASSES  -----------------------------------------------------------------------------------------------
        try {//www.  j  ava 2 s .c  o  m

            ResponseEntity<domain.K_classes[]> kClasse = rt.exchange(
                    "http://xingu.lcc.unifesspa.edu.br:8080/api/k_classess?idassunto=" + assunto,
                    HttpMethod.GET, entity, domain.K_classes[].class);
            System.out.println("K-classe tamanho: " + kClasse.getBody().length);
            //System.out.println("Tamanho body:" + kClasse.getBody().length);
            for (domain.K_classes k : kClasse.getBody()) {
                insert.append("k_ordem", k.getk_ordem());
                insert.append("k_classes_id", k.getId());
                insert.append("ck", Integer.parseInt(k.getk_codigo()));
                insert.append("dk", k.getk_descricao());

                k_classe.insert(insert);
                insert.clear();

            }
            System.out.println("DEVERIA TER INSERIDO O K");
        } catch (Exception e) {
            e.printStackTrace();
            insert.clear();
        }
        //FIM K-CLASSES E COMEO INPUT_PRESENTES --------------------------------------------------------------------------------------------------------
        BasicDBObject insertCI = new BasicDBObject();
        try {
            System.out.println("Comeando a inserir os Inputs do assunto: " + assunto);
            ResponseEntity<domain.Input_presenteDTO[]> input = rt.exchange(
                    "http://xingu.lcc.unifesspa.edu.br:8080/api/input_presentes/getInputPresenteByAssantoId/"
                            + assunto,
                    HttpMethod.GET, entity, Input_presenteDTO[].class);
            System.out.println("Fez o download do assunto: " + assunto);
            System.out.println("Tamano input: " + input.getBody().length + "  Assunto: " + assunto);
            DBCollection colInput_presente = dbAssunto.getCollection("i"); // inserir assunto na base
            BasicDBObject search2 = new BasicDBObject();

            //    int cont = 0;
            for (Input_presenteDTO Input2 : input.getBody()) {
                //       System.out.println("Entrou");
                //Para a coleo "i"
                search2.append("ci", Integer.parseInt(Input2.getCodigo()));
                DBObject aux = colInput_presente.findOne(search2);
                if (aux != null) {
                    search2.clear();
                    // System.out.println("ja tem esse territorio inserido");
                } else {

                    DBCursor cursor = colTerritorios.find(search2);
                    while (cursor.hasNext()) {

                        aux = cursor.next();
                        // System.out.println("AUX: " + aux.toString());
                        if (Integer.parseInt(aux.get("ci").toString()) == Integer.parseInt(Input2.getCodigo())
                                && Integer.parseInt(aux.get("ano").toString()) == Integer
                                        .parseInt(Input2.getAno_ref())) {
                            // insertCI.append("uf_sigla", aux.get("uf_sigla"));
                            insertCI.append("longitude", Double.parseDouble(aux.get("longitude").toString()));
                            insertCI.append("latitude", Double.parseDouble(aux.get("latitude").toString()));
                            // System.out.println("Achou Territorio");
                        }

                    }
                    insertCI.append("ci", Integer.parseInt(Input2.getI_codigo_amc()));
                    insertCI.append("di", Input2.getTerritorio_nome());
                    insertCI.append("ano_ref", Integer.parseInt(aux.get("ano").toString()));
                    search2.clear();
                    colInput_presente.insert(insertCI);
                    insertCI.clear();
                }

                //Para a coleo com o mesmo nome da base
                insertDemanda.append("ci", Integer.parseInt(Input2.getI_codigo_amc()));
                insertDemanda.append("di", Input2.getTerritorio_nome());
                insertDemanda.append("ck", Integer.parseInt(Input2.getK_classesK_codigo()));
                insertDemanda.append("quant_valor", Double.parseDouble(Input2.getX_valor()));
                insertDemanda.append("ano", Integer.parseInt(Input2.getAno_ref()));

                //Aqui se acha a descrio da K-Classe envolvida, atravs do "k_classesK_codigo"                       
                search2.append("ck", Integer.parseInt(Input2.getK_classesK_codigo()));

                aux = k_classe.findOne(search2);
                insertDemanda.append("dk", (String) aux.get("dk"));
                search2.clear();

                //                    
                colDaDemanda.insert(insertDemanda);

                insertDemanda.clear();

                //   cont++;
                //   System.out.println(cont);
            }
            System.out.println("Inseriu input_presente do assunto: " + assunto);
            //Preparar banco------------------
            System.out.println("Preparando o assunto: " + assunto);
            datadao.prepareDatabase(false, dbAssunto);
            System.out.println("Fim preparar banco do assunto: " + assunto);
            //Fim preparar Banco----------
        } catch (Exception e) {
            e.printStackTrace();

            insert.clear();
            insertCI.clear();
        }

    } else {
        BasicDBObject search = new BasicDBObject();
        DB aux = mongo.getDB("demandas");
        DBCollection colDemandaAtiva = aux.getCollection("demandas_ativas");
        search.append("demanda", assunto.toString());
        boolean situacao = false;
        while (situacao == false) {

            DBCursor demanAtiva = colDemandaAtiva.find(search);
            if (demanAtiva.size() >= 1) {

                situacao = true;
            }
            Thread.currentThread().sleep(5000);
        }

    }

}

From source file:br.rdu.ifpb.mongo.repositorio.PessoaRepositoriMongo.java

@Override
public Pessoa find(String key, String value) {
    Pessoa pessoa = new Pessoa();
    DB db = getConnection();/* w w  w  .j  a v  a 2 s  .c o m*/
    DBCollection table = db.getCollection("pessoa");
    BasicDBObject query = new BasicDBObject().append(key, value);

    DBCursor result = table.find(query);

    result.hasNext();

    DBObject DBO = result.next();
    pessoa.setId(DBO.toMap().get("_id").toString());
    pessoa.setNome(DBO.toMap().get("nome").toString());
    pessoa.setCpf(DBO.toMap().get("cpf").toString());
    int idade = Integer.parseInt(DBO.toMap().get("idade").toString());
    pessoa.setIdade(idade);

    return pessoa;
}

From source file:br.ufabc.impress.mongo.manager.DBHelper.java

@Override
public List<Boolean> modifyNotOveride(String tableName, String json, String _id) {
    if (tableName == null || tableName.equals("") || json == null || json.equals("") || _id == null
            || _id.equals("")) {
        return null;
    }/*  w  ww  .  j a  v a2  s  .  co  m*/
    List<Boolean> resList = new ArrayList();
    DBCollection table = db.getCollection(tableName);
    DBCursor cursor = table.find();
    while (cursor.hasNext()) {
        DBObject updateDocument = cursor.next();
        DBObject searchQuery = cursor.next();
        DBObject dbObject = (DBObject) JSON.parse(json);
        updateDocument.putAll(dbObject.toMap());
        updateDocument.removeField("_id");
        WriteResult result = table.update(searchQuery, updateDocument);
        resList.add(result.isUpdateOfExisting());
    }
    return resList;
}

From source file:BusinessLogic.Service.RestaurantService.java

public String getAll() {
    String response = "";
    try {/* www  . ja  va  2s .co m*/

        ArrayList userConsult = new ArrayList();
        MongoConnection dbSingleton = MongoConnection.getInstance();

        DB db = dbSingleton.getTestdb();

        // get list of collections
        Set<String> collections = db.getCollectionNames();

        // get a single collection
        DBCollection collection = db.getCollection(collName);
        DBCursor cursor = collection.find();
        try {
            while (cursor.hasNext()) {
                response = response + cursor.next().toString() + "\n";
            }
        } finally {
            cursor.close();
        }

        System.out.println("Done");

    } catch (MongoException e) {
        e.printStackTrace();
    }
    return response;
}

From source file:BusinessLogic.Service.RestaurantService.java

public Restaurant getBy(String name) {
    ArrayList restaurantConsult = new ArrayList();
    MongoConnection dbSingleton = MongoConnection.getInstance();

    DB db = dbSingleton.getTestdb();/*from   w  ww .j a va2  s .c o m*/
    DBCollection coll = db.getCollection(collName);
    BasicDBObject whereQuery = new BasicDBObject();
    // Sentence to search one account number
    whereQuery.put("name", name);

    DBCursor cursor = coll.find(whereQuery);
    while (cursor.hasNext()) {
        DBObject consultDocument = cursor.next();
        restaurantConsult.add(consultDocument.get("name"));
        restaurantConsult.add(consultDocument.get("direccion"));
        restaurantConsult.add(consultDocument.get("phone"));

    }
    Restaurant rs = new Restaurant(whereQuery);
    String response = "";
    response = "Name:" + restaurantConsult.get(0) + " ,Address: " + restaurantConsult.get(1) + " ,Phone: "
            + restaurantConsult.get(2);
    System.out.println(rs);
    return rs;

}

From source file:BusinessLogic.Service.RestaurantService.java

public String update(
        //String name, String direccion, String phone) {
        Restaurant rs) {/*from  w  w w .j  a v a2s.c o m*/
    String name = rs.getName();
    String direccion = rs.getAddress();
    String phone = rs.getPhone();
    String reservationUpdate = null;
    try {
        // To connect to mongo dbserver
        MongoConnection dbSingleton = MongoConnection.getInstance();
        DB db = dbSingleton.getTestdb();
        DBCollection coll = db.getCollection(collName);

        System.out.println("Collection restaurants selected successfully");

        BasicDBObject whereQuery = new BasicDBObject();
        // Sentence to search one account number
        whereQuery.put("name", name);

        DBCursor cursor = coll.find(whereQuery);

        while (cursor.hasNext()) {

            DBObject updateDocument = cursor.next();
            updateDocument.put("name", name);
            updateDocument.put("direccion", direccion);
            updateDocument.put("phone", phone);

            coll.update(whereQuery, updateDocument);

        }
        System.out.println("Document updated successfully");
        reservationUpdate = "Success";

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
    if (reservationUpdate != null) {
        return "The restaurant has been updated successfully!";
    } else {
        return "The restaurant has not been updated!";
    }
}

From source file:buysell.login.java

private void loginSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginSActionPerformed
    // TODO add your handling code here:
    shopping sh1 = new shopping();
    try {//from w w w. java  2 s.co m
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("snehal");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("user");
        System.out.println("Collection created successfully");
        String name = jTextField1.getText().toString();
        String passwd = jPasswordField1.getText().toString();
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", name);
        searchQuery.put("passwd", passwd);

        DBCursor cursor = coll.find(searchQuery);
        int i = 0;
        while (cursor.hasNext()) {
            i = 1;
            System.out.println(cursor.next());
        }
        if (i == 0) {
            JOptionPane.showMessageDialog(null, "user name or passwod is wrong");

        } else {
            System.out.println("user get");
            sh1.setVisible(true);
            dispose();
        }
        System.out.println("Document inserted successfully");
    } catch (NumberFormatException e) {
        System.out.println();
    }

}