Skip to content

Commit

Permalink
0.9.5.3
Browse files Browse the repository at this point in the history
toDoc(), toJava() corregidos
  • Loading branch information
avbravo committed Jan 13, 2017
1 parent ff94975 commit df0b548
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
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.5.2</version>
<version>0.9.5.3</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ default public Document toDoc(T t) {
return doc;
}

default T toJava(Document doc, T t1) {

// default T toJava(Document doc, T t1) {
//
// T o = fromJsontoJava(doc.toJson(), (Class<T>) t1);
//
// return o;
// }
default T toJava(Document doc, Class<T> clazz) {

T o = fromJsontoJava(doc.toJson(), (Class<T>) t1);
T o = fromJsontoJava(doc.toJson(), clazz);

return o;
}
Expand Down
41 changes: 38 additions & 3 deletions jgmongo/src/main/java/com/jgmongo/persistence/AbstractFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ private Document getDocumentPrimaryKey(T t2) {
private Document getIndexPrimaryKey() {
Document doc = new Document();
try {
for (PrimaryKey p : primaryKeyList) {
primaryKeyList.forEach((p) -> {
doc.put(util.letterToUpper(p.getName()), 1);
}
});
} catch (Exception e) {
Logger.getLogger(AbstractFacade.class.getName() + "docPrimaryKey()").log(Level.SEVERE, null, e);
exception = new Exception("docPrimaryKey() ", e);
Expand Down Expand Up @@ -248,9 +248,10 @@ private Document getDocument(T t2) {
*/
public Boolean save(T t2) {
try {

//verify primaryKey
// if (findById(t2) != null) {
//
// exception = new Exception("Please add the @Id annotation from package com.jgmongo.anotaciones.Id");
// return false;
// }
Expand Down Expand Up @@ -767,6 +768,40 @@ public void apply(final Document document) {

return (T) t1;
}
public T find(String key, Object value) {

try {
Object t = entityClass.newInstance();
MongoDatabase db = getMongoClient().getDatabase(database);
FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value));
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {

Method method;
try {

method = entityClass.getDeclaredMethod("toPojo", Document.class);

t1 = (T) method.invoke(t, document);

} 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("find() ", e);

}

return (T) t1;
}

/**
* search document integer value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
package com.jgmongo.services;

import com.jgmongo.interfaces.ServicesInterfaces;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bson.Document;

/**
*
Expand Down Expand Up @@ -40,6 +43,12 @@ public <T> T fromJsontoPojo(String json, Class<T> clazz) {
T jsonToObject = getGson().fromJson(json, clazz);
return jsonToObject;
}
public <T> T fromJsontoPojo(Document doc, Class<T> clazz) {
T jsonToObject = getGson().fromJson(doc.toJson(), clazz);
return jsonToObject;
}


/**
* para objetos java que contienen un atributo Date
* @param <T>
Expand Down

0 comments on commit df0b548

Please sign in to comment.