Skip to content

Commit

Permalink
more compatibility with mongo 3.0 and other things like constructors …
Browse files Browse the repository at this point in the history
…in model classes
  • Loading branch information
WouterG committed Oct 9, 2015
1 parent ade6064 commit f549bab
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.wouto</groupId>
<artifactId>ModelSyncMongoDB</artifactId>
<version>1.1.2-patch5</version>
<version>1.1.4</version>
<packaging>jar</packaging>
<build>
<plugins>
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/net/wouto/modelsync/mongo/MongoModelManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.wouto.modelsync.mongo;

import com.mongodb.DBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.result.DeleteResult;
Expand Down Expand Up @@ -66,7 +65,7 @@ public void findAndRemove(Query q, final LoadOneCallback callback) {
this.collection.findAndRemove(q, new ReadCallback() {

@Override
public void onQueryDone(DBObject result, Exception err) {
public void onQueryDone(Document result, Exception err) {
if (err != null) {
callback.onQueryDone(null, err);
} else {
Expand All @@ -77,20 +76,20 @@ public void onQueryDone(DBObject result, Exception err) {
});
}

public DBObject findOneAndRemoveSync(Query q) throws Exception {
public Document findOneAndRemoveSync(Query q) throws Exception {
return this.collection.findOneAndRemoveSync(q);
}

public void find(Query q, final LoadMultiCallback callback) {
this.collection.find(q, new MultiReadCallback() {

@Override
public void onQueryDone(DBObject[] result, Exception err) {
public void onQueryDone(Document[] result, Exception err) {
if (err != null) {
callback.onQueryDone(null, err);
} else {
List<T> objects = new ArrayList();
for (DBObject r : result) {
for (Document r : result) {
objects.add(MongoModelManager.this.collection.fromDBObject(type, r));
}
callback.onQueryDone((T[])objects.toArray((T[])Array.newInstance(type, objects.size())), err);
Expand All @@ -108,7 +107,7 @@ public void findOne(Query q, final LoadOneCallback callback) {
this.collection.findOne(q, new ReadCallback() {

@Override
public void onQueryDone(DBObject result, Exception err) {
public void onQueryDone(Document result, Exception err) {
if (err != null) {
callback.onQueryDone(null, err);
} else {
Expand All @@ -119,7 +118,7 @@ public void onQueryDone(DBObject result, Exception err) {
});
}

public DBObject findOneSync(Query q) throws Exception {
public Document findOneSync(Query q) throws Exception {
return this.collection.findOneSync(q);
}

Expand Down
91 changes: 59 additions & 32 deletions src/main/java/net/wouto/modelsync/mongo/SimpleCollection.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.wouto.modelsync.mongo;

import com.mongodb.BasicDBList;
import com.mongodb.DBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import static com.mongodb.client.model.Filters.eq;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
Expand Down Expand Up @@ -108,10 +107,9 @@ public void run() {
}

public UpdateResult updateOrInsertSync(String key, Object value, Update set) throws Exception {
set.append(Update.setOnInsert(key, value));
UpdateOptions u = new UpdateOptions();
u.upsert(true);
return this.collection.updateOne(new Document(key, value), (Document) set.getUpdateQuery(), u);
return this.collection.replaceOne(eq(key, value), (Document) set.getUpdateQuery(), u);
}

public void updateOrInsert(String key, Object value, Update set) {
Expand All @@ -128,9 +126,7 @@ public void run() {
if (callback != null) {
callback.onQueryDone(wr, null);
}
System.out.println("inserted succesfully");
} catch (Exception ex) {
ex.printStackTrace();
if (callback != null) {
callback.onQueryDone(null, ex);
}
Expand Down Expand Up @@ -168,13 +164,13 @@ public void run() {
});
}

public DBObject findOneSync(Query q) throws Exception {
public Document findOneSync(Query q) throws Exception {
MongoCursor cursor = this.collection.find((Document) q.getQuery()).iterator();
Object o = cursor.next();
if (o == null) {
return null;
}
return (DBObject) o;
return (Document) o;
}

public void findOne(final Query q, final ReadCallback callback) {
Expand All @@ -183,7 +179,7 @@ public void findOne(final Query q, final ReadCallback callback) {
@Override
public void run() {
try {
DBObject d = SimpleCollection.this.findOneSync(q);
Document d = SimpleCollection.this.findOneSync(q);
if (callback != null) {
callback.onQueryDone(d, null);
}
Expand All @@ -208,12 +204,12 @@ public void find(final Query q, final MultiReadCallback callback) {
public void run() {
try {
MongoCursor c = SimpleCollection.this.findSync(q);
List<DBObject> data = new ArrayList();
List<Document> data = new ArrayList();
while (c.hasNext()) {
data.add((DBObject) c.next());
data.add((Document) c.next());
}
if (callback != null) {
callback.onQueryDone(data.toArray(new DBObject[data.size()]), null);
callback.onQueryDone(data.toArray(new Document[data.size()]), null);
}
} catch (Exception ex) {
if (callback != null) {
Expand All @@ -225,12 +221,12 @@ public void run() {
});
}

public DBObject findOneAndRemoveSync(Query q) throws Exception {
public Document findOneAndRemoveSync(Query q) throws Exception {
Object o = this.collection.findOneAndDelete((Document) q.getQuery());
if (o == null) {
return null;
}
return (DBObject) o;
return (Document) o;
}

public void findAndRemove(final Query q, final ReadCallback callback) {
Expand All @@ -239,7 +235,7 @@ public void findAndRemove(final Query q, final ReadCallback callback) {
@Override
public void run() {
try {
DBObject obj = SimpleCollection.this.findOneAndRemoveSync(q);
Document obj = SimpleCollection.this.findOneAndRemoveSync(q);
if (callback != null) {
callback.onQueryDone(obj, null);
}
Expand Down Expand Up @@ -290,7 +286,7 @@ public <T> T[] loadAllSync(Class<T> c) throws Exception {
MongoCursor cursor = this.findSync(Query.empty);
ArrayList<T> data = new ArrayList();
while (cursor.hasNext()) {
DBObject obj = (DBObject) JSON.parse(((Document) cursor.next()).toJson());
Document obj = (Document) JSON.parse(((Document) cursor.next()).toJson());
T instance = (T) constructor.newInstance();
SimpleCollection.this.fromDBObject(instance, obj);
data.add(instance);
Expand Down Expand Up @@ -408,7 +404,7 @@ public static <T> Document asDBObject(T instance) {
f.setAccessible(hadAccess);
continue;
} else {
BasicDBList list = new BasicDBList();
ArrayList list = new ArrayList();
for (Object arrayObject : arrayData) {
list.add(asDBObject(arrayObject));
}
Expand All @@ -424,7 +420,7 @@ public static <T> Document asDBObject(T instance) {
f.setAccessible(hadAccess);
continue;
} else {
BasicDBList list = new BasicDBList();
ArrayList list = new ArrayList();
for (Object collectionObject : collectionData) {
Document collectionDBObject = asDBObject(collectionType.cast(collectionObject));
list.add(collectionDBObject);
Expand Down Expand Up @@ -457,7 +453,7 @@ public final <T> void save(T instance) {
for (Field f : fields) {
if (f.getAnnotation(DBSync.class).index()) {
key = f.getName();
value = obj.remove(f.getName());
value = obj.get(f.getName());
break;
}
}
Expand All @@ -466,7 +462,16 @@ public final <T> void save(T instance) {
return;
}
Update update = new Update(obj);
this.updateOrInsert(key, value, update);
this.updateOrInsert(key, value, update, new UpdateCallback() {

@Override
public void onQueryDone(UpdateResult result, Exception err) {
if (err != null) {
err.printStackTrace();
}
}

});
}

private <T> T instantiate(Class<T> cls) throws Exception {
Expand All @@ -477,14 +482,34 @@ private <T> T instantiate(Class<T> cls) throws Exception {
}
final List<Object> params = new ArrayList();
for (Class<?> pType : constr.getParameterTypes()) {
params.add((pType.isPrimitive()) ? ClassUtils.primitiveToWrapper(pType).newInstance() : null);
if (pType.isPrimitive() || primitives.contains(pType)) {
if (pType.isPrimitive()) {
pType = ClassUtils.primitiveToWrapper(pType);
}
if (pType == Integer.class ||
pType == Long.class ||
pType == Short.class ||
pType == Byte.class) {
params.add(pType.cast(0));
} else if (pType == Character.class) {
params.add('\0');
} else if (pType == Boolean.class) {
params.add(false);
} else if (pType == Float.class || pType == Double.class) {
params.add(pType.cast(0.0));
} else if (pType == Object.class || pType == String.class) {
params.add(null);
}
} else {
params.add(null);
}
}
final T instance = constr.newInstance(params.toArray());
constr.setAccessible(a);
return instance;
}

public <T> T fromDBObject(Class<T> type, DBObject data) {
public <T> T fromDBObject(Class<T> type, Document data) {
try {
T obj = (T) instantiate(type);
return fromDBObject(obj, data);
Expand All @@ -496,7 +521,7 @@ public <T> T fromDBObject(Class<T> type, DBObject data) {
return null;
}

public <T> T fromDBObject(T instance, DBObject data) {
public <T> T fromDBObject(T instance, Document data) {
Collection<Field> fields = getAnnotationFields(instance.getClass(), DBSync.class);
for (Field f : fields) {
DBSync load = f.getAnnotation(DBSync.class);
Expand All @@ -507,14 +532,16 @@ public <T> T fromDBObject(T instance, DBObject data) {
if (!load.value().isEmpty()) {
varName = load.value();
}
if (!data.containsField(varName)) {
if (!data.containsKey(varName)) {
continue;
}
boolean accessible = f.isAccessible();
if (!accessible) {
f.setAccessible(true);
}
if (f.getType().isArray()) { // array
// unsupported? ;s - reflection doesn't allow this
/*
Class<?> arrayType = getArrayType(f);
if (isPrimitive(arrayType)) {
try {
Expand All @@ -526,19 +553,19 @@ public <T> T fromDBObject(T instance, DBObject data) {
f.setAccessible(accessible);
continue;
}
BasicDBList arrayData = (BasicDBList) data.get(varName);
ArrayList arrayData = (ArrayList) data.get(varName);
Collection arrayResult = new ArrayList();
for (Object arrayObject : arrayData) {
DBObject arrayObjectDB = (DBObject) arrayObject;
Document arrayObjectDB = (Document) arrayObject;
Object arrayObjectOutput = fromDBObject(arrayType, arrayObjectDB);
arrayResult.add(arrayObjectOutput);
}
try {
f.set(instance, arrayResult.toArray());
f.set(instance, arrayResult.toArray((f.getType().cast(Array.newInstance(f.getType(), arrayResult.size()))));
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(SimpleCollection.class.getName()).log(Level.SEVERE, null, ex);
}
f.setAccessible(accessible);
f.setAccessible(accessible); */
continue;
} else if (Collection.class.isAssignableFrom(f.getType())) { // collection
Class<?> collectionType = getCollectionType(f);
Expand All @@ -551,10 +578,10 @@ public <T> T fromDBObject(T instance, DBObject data) {
f.setAccessible(accessible);
continue;
}
BasicDBList collectionData = (BasicDBList) data.get(varName);
ArrayList collectionData = (ArrayList) data.get(varName);
Collection collectionResult = new ArrayList();
for (Object collectionObject : collectionData) {
DBObject collectionObjectDB = (DBObject) collectionObject;
Document collectionObjectDB = (Document) collectionObject;
Object collectionObjectOutput = fromDBObject(collectionType, collectionObjectDB);
collectionResult.add(collectionObjectOutput);
}
Expand All @@ -566,7 +593,7 @@ public <T> T fromDBObject(T instance, DBObject data) {
f.setAccessible(accessible);
continue;
} else if (!isPrimitive(f.getType())) {
Object o = fromDBObject(f.getType(), (DBObject) data.get(varName));
Object o = fromDBObject(f.getType(), (Document) data.get(varName));
try {
f.set(instance, o);
} catch (IllegalArgumentException | IllegalAccessException ex) {
Expand Down Expand Up @@ -629,7 +656,7 @@ public final <T> void load(final T instance, final ObjectLoadedCallback callback
this.findOne(q, new ReadCallback() {

@Override
public void onQueryDone(DBObject result, Exception err) {
public void onQueryDone(Document result, Exception err) {
if (err != null) {
err.printStackTrace();
} else if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.wouto.modelsync.mongo.callbacks;

import com.mongodb.DBObject;
import org.bson.Document;

public interface MultiReadCallback {

public void onQueryDone(DBObject[] result, Exception err);
public void onQueryDone(Document[] result, Exception err);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.wouto.modelsync.mongo.callbacks;

import com.mongodb.DBObject;
import org.bson.Document;

public interface ReadCallback {

public void onQueryDone(DBObject result, Exception err);
public void onQueryDone(Document result, Exception err);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.wouto.modelsync.mongo.callbacks;

import com.mongodb.BasicDBObject;
import org.bson.Document;

public interface WriteCallback {

public void onQueryDone(BasicDBObject outputDocument, Exception err);
public void onQueryDone(Document outputDocument, Exception err);

}

0 comments on commit f549bab

Please sign in to comment.