Skip to content

Commit

Permalink
0.9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
avbravo committed Jan 11, 2017
1 parent 610fab4 commit cf222bd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jgmongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.jgmongo</groupId>
<artifactId>jgmongo</artifactId>
<version>0.9.4</version>
<version>0.9.5</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down
75 changes: 75 additions & 0 deletions jgmongo/src/main/java/com/jgmongo/persistence/AbstractFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,81 @@ public void apply(final Document document) {
}
return list;
}

/**
*
* @param key
* @param value
* @param docSort
* @return
*/
public List<T> findBy(String key, Date value, Document... docSort) {
Document sortQuery = new Document();
try {
if (docSort.length != 0) {
sortQuery = docSort[0];

}
Object t = entityClass.newInstance();
list = new ArrayList<>();
Document doc = new Document(key, value);
MongoDatabase db = getMongoClient().getDatabase(database);
FindIterable<Document> iterable = db.getCollection(collection).find(doc).sort(sortQuery);
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
try {
Method method = entityClass.getDeclaredMethod("toPojo", Document.class);
list.add((T) method.invoke(t, document));
} catch (Exception e) {
Logger.getLogger(AbstractFacade.class.getName() + "findAll()").log(Level.SEVERE, null, e);
exception = new Exception("findBy()", e);
}
}
});
} catch (Exception e) {
Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e);
exception = new Exception("findBy() ", e);
}
return list;
}
/**
*
* @param key
* @param value
* @param docSort
* @return
*/
public List<T> findBy(String key, Integer value, Document... docSort) {
Document sortQuery = new Document();
try {
if (docSort.length != 0) {
sortQuery = docSort[0];

}
Object t = entityClass.newInstance();
list = new ArrayList<>();
Document doc = new Document(key, value);
MongoDatabase db = getMongoClient().getDatabase(database);
FindIterable<Document> iterable = db.getCollection(collection).find(doc).sort(sortQuery);
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
try {
Method method = entityClass.getDeclaredMethod("toPojo", Document.class);
list.add((T) method.invoke(t, document));
} catch (Exception e) {
Logger.getLogger(AbstractFacade.class.getName() + "findAll()").log(Level.SEVERE, null, e);
exception = new Exception("findBy()", e);
}
}
});
} catch (Exception e) {
Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e);
exception = new Exception("findBy() ", e);
}
return list;
}

/**
*
Expand Down

0 comments on commit cf222bd

Please sign in to comment.