Example usage for com.mongodb.client MongoCursor hasNext

List of usage examples for com.mongodb.client MongoCursor hasNext

Introduction

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

Prototype

@Override
    boolean hasNext();

Source Link

Usage

From source file:SelectItemController.java

@Override
public void initialize(URL url, ResourceBundle rb) {
    MongoCursor<Document> cursor4 = db.getCollection("ItemDetail").find().iterator();
    try {/*from  www.ja va  2 s  .c  o  m*/
        while (cursor4.hasNext()) {
            String rs = cursor4.next().getString("ItemName");
            ItemList.getItems().addAll(rs);
        }
    } finally {
        cursor4.close();
    }
}

From source file:actor4j.core.persistence.connectors.MongoDBPersistenceAdapter.java

License:Open Source License

@Override
public void receive(ActorMessage<?> message) {
    if (message.tag == PERSIST_EVENTS) {
        try {//  w  w  w .j av  a 2s .c o m
            JSONArray array = new JSONArray(message.valueAsString());
            if (array.length() == 1) {
                Document document = Document.parse(array.get(0).toString());
                events.insertOne(document);
            } else {
                List<WriteModel<Document>> requests = new ArrayList<WriteModel<Document>>();
                for (Object obj : array) {
                    Document document = Document.parse(obj.toString());
                    requests.add(new InsertOneModel<Document>(document));
                }
                events.bulkWrite(requests);
            }
            parent.send(new ActorMessage<Object>(null, INTERNAL_PERSISTENCE_SUCCESS, self(), message.source));
        } catch (Exception e) {
            e.printStackTrace();
            parent.send(new ActorMessage<Exception>(e, INTERNAL_PERSISTENCE_FAILURE, self(), message.source));
        }
    } else if (message.tag == PERSIST_STATE) {
        try {
            Document document = Document.parse(message.valueAsString());
            states.insertOne(document);
            parent.send(new ActorMessage<Object>(null, INTERNAL_PERSISTENCE_SUCCESS, self(), message.source));
        } catch (Exception e) {
            e.printStackTrace();
            parent.send(new ActorMessage<Exception>(e, INTERNAL_PERSISTENCE_FAILURE, self(), message.source));
        }
    } else if (message.tag == RECOVER) {
        try {
            JSONObject obj = new JSONObject();
            Document document = null;

            FindIterable<Document> statesIterable = states
                    .find(new Document("persistenceId", message.valueAsString()))
                    .sort(new Document("timeStamp", -1)).limit(1);
            document = statesIterable.first();
            if (document != null) {
                JSONObject stateValue = new JSONObject(document.toJson());
                stateValue.remove("_id");
                long timeStamp = stateValue.getJSONObject("timeStamp").getLong("$numberLong");
                stateValue.put("timeStamp", timeStamp);
                obj.put("state", stateValue);

                FindIterable<Document> eventsIterable = events
                        .find(new Document("persistenceId", message.valueAsString()).append("timeStamp",
                                new Document("$gte", timeStamp)))
                        .sort(new Document("timeStamp", -1));
                JSONArray array = new JSONArray();
                MongoCursor<Document> cursor = eventsIterable.iterator();
                while (cursor.hasNext()) {
                    document = cursor.next();
                    JSONObject eventValue = new JSONObject(document.toJson());
                    eventValue.remove("_id");
                    timeStamp = eventValue.getJSONObject("timeStamp").getLong("$numberLong");
                    eventValue.put("timeStamp", timeStamp);
                    array.put(eventValue);
                }
                cursor.close();
                obj.put("events", array);
            } else
                obj.put("state", new JSONObject());

            parent.send(new ActorMessage<String>(obj.toString(), INTERNAL_PERSISTENCE_RECOVER, self(),
                    message.source));
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject obj = new JSONObject();
            obj.put("error", e.getMessage());
            parent.send(new ActorMessage<String>(obj.toString(), INTERNAL_PERSISTENCE_RECOVER, self(),
                    message.source));
        }
    }
}

From source file:ARS.DBManager.java

public boolean findUserByName(String name) {
    User uObj;/* ww w. j ava  2  s.  com*/
    int counter = 0;

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject query = new BasicDBObject();
    query.put("first-name", name);
    MongoCursor cursor = collection.find(query).iterator();

    //Counter gives one if it's give the result
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
        counter++;
    }

    if (counter == 1) {
        return true;
    } else {
        return false;
    }
}

From source file:ARS.DBManager.java

public int findAllUsers() {

    int counter = 0;

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject query = new BasicDBObject();

    MongoCursor cursor = collection.find().iterator();
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
        counter++;//from  w  ww  .  j a v a 2  s .c o m
    }
    return counter;
}

From source file:ARS.DBManager.java

public boolean findUserByNumber(Long pNo) {
    int counter = 0;
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject query = new BasicDBObject();
    query.put("phone-number", pNo);
    MongoCursor cursor = collection.find(query).iterator();

    while (cursor.hasNext()) {
        System.out.println(cursor.next());
        counter++;//  w w w. ja  v a  2s. c o  m
    }

    if (counter == 1) {
        return true;
    } else {
        return false;
    }

}

From source file:ARS.DBManager.java

public boolean findNullForGender() {
    User uObj;/*w  w w.j av a2 s. c o  m*/
    int counter = 0;

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject query = new BasicDBObject();
    query.put("gender", null);
    MongoCursor cursor = collection.find(query).iterator();

    //Counter gives one if it's give the result
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
        counter++;
    }

    if (counter >= 1) {
        return true;
    } else {
        return false;
    }
}

From source file:ARS.DBManager.java

public boolean findUserByEmail(String email, String password) {
    User uObj;/*from w w  w.j ava 2 s . com*/
    int counter = 0;

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject query = new BasicDBObject();
    query.put("email", email);
    query.put("password", password);
    MongoCursor cursor = collection.find(query).iterator();

    //Counter gives one if it's give the result
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
        counter++;
    }

    if (counter == 1) {
        return true;
    } else {
        return false;
    }
}

From source file:ARS.DBManager.java

public ArrayList findFlight(String departure, String arrival) {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject query = new BasicDBObject();
    query.put("departure", departure);
    query.put("arrival", arrival);
    MongoCursor cursor = collection.find(query).iterator();

    ArrayList<Flight> flightList = new ArrayList<>();
    //Counter gives one if it's give the result
    while (cursor.hasNext()) {
        flightList.add((Flight) cursor.next());
    }//from  www  .ja va 2  s  . c om
    return flightList;

}

From source file:br.edu.ifpb.bdnc.gory.dao.bd.MongoDAO.java

@Override
public Pagina lePagina(int id) throws PersistenceException {
    MongoCursor<Document> cursor = collection.find(eq("_id", id)).iterator();
    System.out.println("Passou aqui");
    Pagina pagina = null;//from   w  w w .  j  a  va  2s.  c o m
    if (cursor.hasNext()) {
        pagina = new Pagina().fromDocument(cursor.next());
    }
    System.out.println("Passou aqui");
    return pagina;
}

From source file:br.edu.ifpb.bdnc.mongodb.ClienteMongo.java

public Cliente read(String cpf) {
    MongoCursor<Document> cursor = collection.find(eq("_id", cpf));
    Cliente cliente = null;/*  w w  w.  j  a  v a  2s  .  c o m*/
    if (cursor.hasNext()) {
        cliente = new Cliente().fromDocument(cursor.next());
    }
    return cliente;
}