List of usage examples for com.mongodb Block Block
Block
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
private List<T> processAggregateIterable(AggregateIterable<Document> iterable) { List<T> l = new ArrayList<>(); try {//ww w.ja v a2 s . com iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { t1 = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList, referencedBeansList); l.add(t1); } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "find()").log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } } }); } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("iterableSimple() ", e); } return l; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
private List<JmoordbResult> processAggregateIterableJmoordbResult(AggregateIterable<Document> iterable) { List<JmoordbResult> l = new ArrayList<>(); List<Map<String, Object>> lObject = new ArrayList<>(); try {// ww w .jav a2s . c o m iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { // document.remove("_id"); Map<String, Object> map = new HashMap<>(document); lObject.add(map); } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "find()").log(Level.SEVERE, null, e); exception = new Exception("find() ", e); System.out.println("apply error() " + e.getLocalizedMessage()); } } }); for (Map m : lObject) { JmoordbResult jmoordbResult = new JmoordbResult(); for (Iterator it = m.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next(); // System.out.println("====>key "+entry.getKey() + " value "+entry.getValue().toString()); jmoordbResult.put(entry.getKey(), entry.getValue().toString()); } l.add(jmoordbResult); } } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("iterableSimple() ", e); System.out.println("error() " + e.getLocalizedMessage()); } return l; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param doc//from ww w. j ava2 s . co m * @return el numero de documentos en la coleccion */ public Integer count(Document... doc) { try { contador = 0; Document documento = new Document(); if (doc.length != 0) { documento = doc[0]; MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(documento); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { contador++; } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } } }); } else { // no tiene parametros contador = (int) getMongoClient().getDatabase(database).getCollection(collection).count(); } } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } return contador; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param doc/*from w ww .java 2 s .c om*/ * @return el numero de documentos en la coleccion */ public Integer count(Bson filter) { try { contador = 0; MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(filter); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { contador++; } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } } }); } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } return contador; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
private List<JmoordbResult> processUnknownIterableJmoordbResult(FindIterable<Document> iterable) { List<JmoordbResult> l = new ArrayList<>(); List<Map<String, Object>> lObject = new ArrayList<>(); try {/*from w ww.j av a 2 s. c o m*/ iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { Map<String, Object> map = new HashMap<>(document); lObject.add(map); } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "find()").log(Level.SEVERE, null, e); exception = new Exception("find() ", e); System.out.println("apply error() " + e.getLocalizedMessage()); } } }); for (Map m : lObject) { JmoordbResult jmoordbResult = new JmoordbResult(); for (Iterator it = m.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next(); jmoordbResult.put(entry.getKey(), entry.getValue().toString()); } l.add(jmoordbResult); } } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("processUnkownIterableJmoordbResult() ", e); System.out.println("error() " + e.getLocalizedMessage()); } return l; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
@Override public Optional<T> find(String key, Object value) { try {// ww w . j a va 2 s.c o m // Object t = entityClass.newInstance(); MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value)); haveElements = false; iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { haveElements = true; tlocal = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList, referencedBeansList); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "find()").log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } } }); if (haveElements) { // return tlocal; return Optional.of(tlocal); } // return null; return Optional.empty(); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } // return null; return Optional.empty(); }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
public T search(String key, Object value) { try {/*from ww w.j a v a2s .c o m*/ // Object t = entityClass.newInstance(); MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value)); haveElements = false; iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { haveElements = true; tlocal = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList, referencedBeansList); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "find()").log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } } }); if (haveElements) { return tlocal; } return null; } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } return null; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * Internamente recorre el iterable/* w w w . j av a 2s.c om*/ * * @param iterable * @return */ private T iterableSimple(FindIterable<Document> iterable) { try { // System.out.println("$$$$$$$iterable simple"); haveElements = false; iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { haveElements = true; t1 = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList, referencedBeansList); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "find()").log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } } }); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("iterableSimple() ", e); } if (haveElements) { return (T) t1; } return null; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
private List<T> iterableList(FindIterable<Document> iterable) { List<T> l = new ArrayList<>(); try {//from ww w. j ava2 s. c o m iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { t1 = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList, referencedBeansList); l.add(t1); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "find()").log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } } }); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("iterableSimple() ", e); } return l; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * * @param doc//from ww w . j a va 2s .c o m * @return el numero de documentos en la coleccion */ public Integer count(Document... doc) { try { contador = 0; Document documento = new Document(); if (doc.length != 0) { documento = doc[0]; MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(documento); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { contador++; } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } } }); } else { // no tiene parametros contador = (int) getMongoClient().getDatabase(database).getCollection(collection).count(); } } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } return contador; }