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:org.seadpdt.impl.RepoServicesImpl.java

License:Apache License

@GET
@Path("/{id}/researchobjects/new")
@Produces(MediaType.APPLICATION_JSON)/*  w  ww.  jav a 2  s.c om*/
public Response getNewROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);

    //Match ROs with no status from any repo
    Document match = new Document("Repository", id);
    Document reporter = new Document("reporter", id);
    Document elem = new Document("$elemMatch", reporter);
    Document not = new Document("$not", elem);
    match.put("Status", not);

    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection
                .find(Filters.and(match, Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.and(match, Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(match);
    }

    iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1)
            .append("Repository", 1).append("Status", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    Set<Document> array = new HashSet<Document>();
    while (cursor.hasNext()) {
        Document nextDoc = cursor.next();
        array.add(nextDoc);
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)//  ww w .j  a  v a 2 s  .  com
public Response getROsList(@QueryParam("Purpose") final String purpose) {
    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection.find(Filters.ne("Preferences.Purpose", "Testing-Only"));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.eq("Preferences.Purpose", purpose));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find();
    }
    iter.projection(new Document("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1)
            .append("Aggregation.Title", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        array.put(JSON.parse(cursor.next().toJson()));
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@GET
@Path("/new/")
@Produces(MediaType.APPLICATION_JSON)//from   w w  w . j  a v a 2  s.com
public Response getNewROsList(@QueryParam("Purpose") final String purpose) {
    //Find ROs that have a status not from the services and don't include them :-)
    Document reporterRule = new Document("$ne", Constants.serviceName);
    Document reporter = new Document("reporter", reporterRule);
    Document elem = new Document("$elemMatch", reporter);
    Document not = new Document("$not", elem);
    Document match = new Document("Status", not);

    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection
                .find(Filters.and(match, Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.and(match, Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(match);
    }
    iter.projection(new Document("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1)
            .append("Aggregation.Title", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        array.put(JSON.parse(cursor.next().toJson()));
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@PUT
@Path("/copyoremaps")
public Response copyOreMaps() {
    FindIterable<Document> iter = oreMapCollection.find();
    MongoCursor<Document> cursor = iter.iterator();
    while (cursor.hasNext()) {
        Document document = cursor.next();
        ObjectId objectId = new ObjectId((String) document.get("_id").toString());
        document.remove("_id");
        String fileName = "ore-file";
        ByteArrayInputStream inputStream = new ByteArrayInputStream(document.toJson().getBytes());
        GridFSInputFile gfsFile = oreMapBucket.createFile(inputStream, fileName, true);
        gfsFile.setId(objectId);/*from   w  w w. j a va 2  s .c  om*/
        gfsFile.save();
    }
    return Response.ok().build();
}

From source file:org.seadpdt.RepoServices.java

License:Apache License

@GET
@Path("/{id}/researchobjects")
@Produces(MediaType.APPLICATION_JSON)//from w  w  w.j ava 2s. c  om
public Response getROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);
    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection.find(
                Filters.and(Filters.eq("Repository", id), Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection
                .find(Filters.and(Filters.eq("Repository", id), Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(Filters.eq("Repository", id));
    }

    iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1)
            .append("Repository", 1).append("Status", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    Set<Document> array = new HashSet<Document>();
    while (cursor.hasNext()) {
        array.add(cursor.next());
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:org.seadpdt.RepoServices.java

License:Apache License

@GET
@Path("/{id}/researchobjects/new")
@Produces(MediaType.APPLICATION_JSON)/* w  w w.ja v  a  2 s.  co m*/
public Response getNewROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);

    //Match ROs with no status from any repo
    Document match = new Document("Repository", id);
    Document reporter = new Document("reporter", id);
    Document elem = new Document("$elemMatch", reporter);
    Document not = new Document("$not", elem);
    match.put("Status", not);

    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection
                .find(Filters.and(match, Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.and(match, Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(match);
    }

    iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1)
            .append("Repository", 1).append("Status", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    Set<Document> array = new HashSet<Document>();
    while (cursor.hasNext()) {
        Document nextDoc = cursor.next();
        array.add(nextDoc);
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:ru.schernolyas.performance.driver.DriverTestProgram.java

@Override
public void readSequentially() {
    LongStream.rangeClosed(1, ROWS_COUNT).forEachOrdered((long authorIndex) -> {
        MongoCursor<Document> authorsCursor = authorsCollection.find(new BasicDBObject("_id", authorIndex))
                .iterator();//from  ww w  .  ja  v  a2  s.  c  o m
        while (authorsCursor.hasNext()) {
            readAuthor(authorsCursor.next());

        }
        MongoCursor<Document> booksCursor = booksCollection.find(new BasicDBObject("author_id", authorIndex))
                .iterator();
        while (booksCursor.hasNext()) {
            readBook(booksCursor.next());
        }
    });
}

From source file:ru.schernolyas.performance.driver.DriverTestProgram.java

@Override
public void readRandomly() {
    Random random = new Random();
    LongStream.generate(() -> {
        return 1 + (long) (random.nextDouble() * (ROWS_COUNT - 1));
    }).limit(ROWS_COUNT).forEach((long authorIndex) -> {
        MongoCursor<Document> authorsCursor = authorsCollection.find(new BasicDBObject("_id", authorIndex))
                .iterator();//from   w  ww  .  j  a v  a 2s. c om
        while (authorsCursor.hasNext()) {
            readAuthor(authorsCursor.next());
        }
        MongoCursor<Document> booksCursor = booksCollection.find(new BasicDBObject("author_id", authorIndex))
                .iterator();
        while (booksCursor.hasNext()) {
            readBook(booksCursor.next());
        }
    });
}

From source file:service.MongoLanguageService.java

@Override
public List<Language> getAll() {
    //        List<Language> languageList = new ArrayList<>();
    //        //from   w  ww.jav  a  2s .co  m
    //        List<Document> resultSet = (List<Document>) collection.find();
    //        DBCursor cursor = collection.find();
    //        while(cursor.hasNext()) {
    //            language.setId(resultSet.getInt("ID"));
    //            language.setName(resultSet.getString("NAME"));
    //            System.out.println(cursor.next());
    //        }
    //        try{
    MongoCursor<Document> cursor = collection.find().iterator();
    //        } catch (UnknownHostException e){
    //            throw new IllegalArgumentException(e);
    //        }
    //            List<Language> languages = (List<Language>) collection.find().iterator();
    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next().toJson());
        }
    } finally {
        cursor.close();
    }
    return null;
}

From source file:starsaver.ManageStar.java

private void btn_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_searchActionPerformed

    //DECIDED TO US ITERABLE AND CURSOR OBJECTS FOR DIFFERENT OBJECTIVES
    //MAINLY BECAUSE PROJECTION WOULD NOT WORK WITH CURSOR
    FindIterable<Document> iterableStars = null;
    MongoCursor starCursor = null;
    String result;/*from  ww  w .  j a v a 2  s .c  om*/
    System.out.println("Test2");
    txtArea.setText("");//SETS TEXT AREA EMPTY WHEN SEARCH BUTTON CLICKED TO MAKE ROOM FOR INCOMING TEXT

    //DEPENDING ON WHAT INDEX IS SELECTED FOR MENU DROPDOWN YOU CAN VIEW BY...

    //ID
    if (cbo_viewBy.getSelectedIndex() == 0 && !(txt_viewBy.getText().equals(""))) {

        String id = txt_viewBy.getText();
        txtArea.setText("");
        iterableStars = db.getCollection("stars").find(new Document("_id", id));

    }
    //BY NAME
    else if (cbo_viewBy.getSelectedIndex() == 1 && !(txt_viewBy.getText().equals(""))) {

        String name = txt_viewBy.getText();
        txtArea.setText("");
        iterableStars = db.getCollection("stars").find(new Document("Name", name));
    }
    //CONSTELLATION
    else if (cbo_viewBy.getSelectedIndex() == 2 && !(txt_viewBy.getText().equals(""))) {

        String con = txt_viewBy.getText();
        txtArea.setText("");
        iterableStars = db.getCollection("stars").find(new Document("Constellation", con));
    }
    //MAP REDUCTION FUNCTIONS - USING INTERATORS
    //FINDING STARS FARTHER (AND CLOSER) THAN <INPUT>
    else if (cbo_viewBy.getSelectedIndex() == 3 && !(txt_viewBy.getText().equals(""))
            && !(txt_viewBy.getText().matches(("^[a-zA-Z]+$")))) {
        String dist = txt_viewBy.getText();
        String map = "function() { " + "var category; " + "if ( this.Distance >= " + dist + " ) "
                + "category = 'Stars further than " + dist + " Lightyears from Earth'; " + "else "
                + "category = 'Stars closer than " + dist + " Lightyears from Earth'; "
                + "emit(category, {Name: this.Name});}";

        String reduce = "function(key, values) { " + "var sum = 0; " + "values.forEach(function(doc) { "
                + "sum += 1; " + "}); " + "return {Amount: sum};} ";

        starCursor = starColl.mapReduce(map, reduce).iterator();
    }
    //FINDING STARS LARGER (AND SMALLER) THAN <INPUT> 
    else if (cbo_viewBy.getSelectedIndex() == 4 && !txt_viewBy.getText().equals("")
            && !(txt_viewBy.getText().matches(("^[a-zA-Z]+$")))) {
        String mass = txt_viewBy.getText();
        String map = "function() { " + "var category; " + "if ( this.Mass >= " + mass + " ) "
                + "category = 'Stars greater than " + mass + " M'; " + "else "
                + "category = 'Stars smaller than " + mass + " M'; " + "emit(category, {Name: this.Name});}";

        String reduce = "function(key, values) { " + "var sum = 0; " + "values.forEach(function(doc) { "
                + "sum += 1; " + "}); " + "return {Amount: sum};} ";

        starCursor = starColl.mapReduce(map, reduce).iterator();
    }
    //FINDING STARS BRIGHTER (OR NOT AS BRIGHT) THAN <INPUT> 
    else if (cbo_viewBy.getSelectedIndex() == 5 && !txt_viewBy.getText().equals("")
            && !(txt_viewBy.getText().matches(("^[a-zA-Z]+$")))) {
        String bright = txt_viewBy.getText();
        String map = "function() { " + "var category; " + "if ( this.Brightness >= " + bright + " ) "
                + "category = 'Stars brighter than " + bright + " B'; " + "else "
                + "category = 'Stars not as bright than " + bright + " B'; "
                + "emit(category, {Name: this.Name});}";

        String reduce = "function(key, values) { " + "var sum = 0; " + "values.forEach(function(doc) { "
                + "sum += 1; " + "}); " + "return {Amount: sum};} ";

        //MapReduceIterable documents = starColl.mapReduce(map, reduce);
        starCursor = starColl.mapReduce(map, reduce).iterator();
    } else {
        JOptionPane.showMessageDialog(null, "Please enter a value for search criteria", "Error",
                JOptionPane.ERROR_MESSAGE);
    }
    txt_viewBy.setText("");

    //MAY APPEAR ODD USING A WHILE ABOVE A FOR EACH LOOP WITH AN ELSE IF, BUT BOTH ARE USED FOR DIFFERENT TYPES OF SEARCHES
    if (starCursor != null) {
        while (starCursor.hasNext()) {
            result = starCursor.next().toString() + "\n";
            System.out.println(result);
            txtArea.append(result);
        }
    } else if (iterableStars != null) {
        iterableStars.forEach(new Block<Document>() {
            //@Override
            public void apply(final Document document) {
                txtArea.setLineWrap(true);
                txtArea.append(document.toString() + "\n\n");
                System.out.println(document);
            }
        });
    }
}