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:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

private MongoResult find(MongoQueryOptions mongoQueryOptions, final MongoResult mongoResult,
        MongoCollection<Document> collection) {
    Bson filter = mongoQueryOptions.getFilter();
    Bson projection = mongoQueryOptions.getProjection();
    Bson sort = mongoQueryOptions.getSort();

    FindIterable<Document> findIterable;
    if (projection == null) {
        findIterable = collection.find(filter);
    } else {//from w w  w. ja  va2s .  c o  m
        findIterable = collection.find(filter).projection(projection);
    }

    if (sort != null) {
        findIterable = findIterable.sort(sort);
    }

    findIterable.limit(mongoQueryOptions.getResultLimit());

    findIterable.forEach(new Block<Document>() {
        @Override
        public void apply(Document document) {
            mongoResult.add(document);
        }
    });

    return mongoResult;
}

From source file:org.dcache.sf.FlushTask.java

License:Open Source License

@Override
public Set<URI> poll() throws URISyntaxException {
    FindIterable<Document> result = files
            .find(Filters.and(Filters.eq("pnfsid", pnfsId.getId()), Filters.exists("bfid")));
    final Set<URI> uris = new HashSet<>();
    result.forEach(new Block<Document>() {
        @Override//from   w  ww.  j a  v  a2s .  c  om
        public void apply(Document document) {
            try {
                String uriString = document.get("bfid").toString();
                uris.add(new URI(uriString));
            } catch (URISyntaxException e) {
                System.out.println(e.getMessage());
            }
        }
    });
    return uris;
}

From source file:org.nuxeo.tools.esync.db.DbMongo.java

License:Open Source License

@Override
public List<Document> getDocumentWithAcl() {
    final List<Document> ret = new ArrayList<>();
    MongoCollection<org.bson.Document> table = getMongoCollection();
    BasicDBObject searchQuery = new BasicDBObject("ecm:acp", new BasicDBObject("$exists", true));
    FindIterable<org.bson.Document> iterable = table.find(searchQuery);
    iterable.forEach(new Block<org.bson.Document>() {
        @Override//from w ww .  j  av  a2 s.  c  o  m
        public void apply(final org.bson.Document document) {
            ArrayList<String> racl = document.get("ecm:racl", ArrayList.class);
            String[] acl = racl.toArray(new String[racl.size()]);
            Document doc = new Document(document.getString("ecm:id"), document.getString("ecm:primaryType"),
                    acl);
            ret.add(doc);
        }
    });
    return ret;
}

From source file:org.nuxeo.tools.esync.db.DbMongo.java

License:Open Source License

private void findAndDump(BasicDBObject searchQuery) {
    MongoCollection<org.bson.Document> table = getMongoCollection();
    FindIterable<org.bson.Document> iterable = table.find(searchQuery);
    iterable.forEach(new Block<org.bson.Document>() {
        @Override//from w w  w  .j  a  v  a  2  s .c  om
        public void apply(final org.bson.Document document) {
            System.out.println(document);
        }
    });
}

From source file:org.nuxeo.tools.esync.db.DbMongo.java

License:Open Source License

@Override
public Document getDocument(String id) {
    final Document[] ret = new Document[1];
    MongoCollection<org.bson.Document> table = getMongoCollection();
    BasicDBObject searchQuery = new BasicDBObject("ecm:id", id);
    new ArrayList<>();
    FindIterable<org.bson.Document> iterable = table.find(searchQuery);
    iterable.forEach(new Block<org.bson.Document>() {
        @Override/*from   w  w w .j ava  2  s  .co  m*/
        public void apply(final org.bson.Document document) {
            ArrayList<String> racl = document.get("ecm:racl", ArrayList.class);
            String[] acl = racl.toArray(new String[racl.size()]);
            Document doc = new Document(document.getString("ecm:id"), document.getString("ecm:primaryType"),
                    acl);
            ret[0] = doc;
        }
    });
    return ret[0];
}

From source file:org.nuxeo.tools.esync.db.DbMongo.java

License:Open Source License

@Override
public Map<String, Long> getTypeCardinality() {
    final Map<String, Long> ret = new LinkedHashMap<>();
    MongoCollection<org.bson.Document> table = getMongoCollection();
    AggregateIterable<org.bson.Document> iterable = table.aggregate(asList(
            new org.bson.Document("$match", new BasicDBObject("ecm:isProxy", new BasicDBObject("$ne", true))),
            new org.bson.Document("$group", new org.bson.Document("_id", "$ecm:primaryType").append("count",
                    new org.bson.Document("$sum", 1)))));
    iterable.forEach(new Block<org.bson.Document>() {
        @Override//w w  w  . j a va  2 s . co m
        public void apply(final org.bson.Document document) {
            String primaryType = document.getString("_id");
            long count = document.getInteger("count");
            if (!ROOT_TYPE.equals(primaryType)) {
                ret.put(primaryType, count);
            }
        }
    });
    return ret;
}

From source file:org.nuxeo.tools.esync.db.DbMongo.java

License:Open Source License

@Override
public Set<String> getDocumentIdsForType(String type) {
    final Set<String> ret = new HashSet<>();
    MongoCollection<org.bson.Document> table = getMongoCollection();
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("ecm:primaryType", type);
    FindIterable<org.bson.Document> iterable = table.find(searchQuery);
    iterable.forEach(new Block<org.bson.Document>() {
        @Override/*from  w  w w . j a  va  2  s . c o  m*/
        public void apply(final org.bson.Document document) {
            ret.add(document.getString("ecm:id"));
        }
    });
    return ret;
}

From source file:org.salarymaster.DAO.SalaryDAOMongo.java

private static List<Salary> iterToList(FindIterable<Document> iterable) {
    final List<Salary> result = new ArrayList<>();

    iterable.forEach(new Block<Document>() {
        @Override//from   w w  w.j  a  v a  2 s  .  com
        public void apply(final Document document) {
            String json = document.toJson();
            Gson gson = new Gson();
            Salary salary = gson.fromJson(json, Salary.class);
            result.add(salary);
        }
    });
    return result;
}

From source file:pt.ua.cbd.cbd_project.service.BookServiceMongo.java

public List<Book> getBooks(String title) {
    List<Book> bookList = new ArrayList<Book>();
    BasicDBObject likeTitle = new BasicDBObject();
    likeTitle.put("title", Pattern.compile(title, Pattern.CASE_INSENSITIVE));

    FindIterable<Document> iterable = this.db.getCollection("book").find(likeTitle).limit(30);
    iterable.forEach(new Block<Document>() {
        @Override/*from   ww w.java  2s. co  m*/
        public void apply(final Document document) {
            Book book = new Book();

            //get authors list
            ArrayList<BookAuthors> bookAuthorsList = new ArrayList<>();
            ArrayList<Document> singleBookAuthorsList = new ArrayList<>();
            singleBookAuthorsList = (ArrayList) document.get("authors"); //get all authors for this.book
            singleBookAuthorsList.stream().forEach((Document d) -> {
                BookAuthors ba = new BookAuthors();
                ba.setName(d.getString("name"));
                ba.setSurname(d.getString("surname"));
                bookAuthorsList.add(ba);
            });
            BookAuthors bookAuthors[] = bookAuthorsList.toArray(new BookAuthors[bookAuthorsList.size()]);
            book.setAuthors(bookAuthors);
            book.setId(document.getInteger("id"));
            book.setTitle(document.getString("title"));
            book.setAddToLibraryDate(document.getString("addToLibraryDate"));
            bookList.add(book);
        }
    });
    return bookList;
}

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;/*w  ww  .  j a v  a  2  s. c  om*/
    String result;
    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);
            }
        });
    }
}