DATADOC-1 addded support for List and arrays

This commit is contained in:
Thomas Risberg
2010-12-15 12:30:03 -05:00
parent 2674565246
commit 8479de82a7
3 changed files with 126 additions and 4 deletions

View File

@@ -172,9 +172,10 @@ public class SimpleMongoConverter implements MongoConverter {
}
// dbo.put(keyToUse, value);
} else {
//TODO exclude Class properties from consideration
logger.warn("Unable to map property " + pd.getName()
if (!"class".equals(pd.getName())) {
logger.warn("Unable to map property " + pd.getName()
+ ". Skipping.");
}
}
// }
@@ -195,6 +196,7 @@ public class SimpleMongoConverter implements MongoConverter {
}
@SuppressWarnings("unchecked")
private void writeCompoundValue(DBObject dbo, String keyToUse, Object value) {
if (value instanceof Map) {
writeMap(dbo, keyToUse, (Map<String, Object>)value);
@@ -202,6 +204,12 @@ public class SimpleMongoConverter implements MongoConverter {
}
if (value instanceof Collection) {
// Should write a collection!
writeArray(dbo, keyToUse, ((Collection<Object>)value).toArray());
return;
}
if (value instanceof Object[]) {
// Should write a collection!
writeArray(dbo, keyToUse, (Object[])value);
return;
}
DBObject nestedDbo = new BasicDBObject();
@@ -231,6 +239,26 @@ public class SimpleMongoConverter implements MongoConverter {
}
}
protected void writeArray(DBObject dbo, String keyToUse, Object[] array) {
//TODO
Object[] dboValues;
if (array != null) {
dboValues = new Object[array.length];
int i = 0;
for (Object o : array) {
if (!isSimpleType(o.getClass())) {
DBObject dboValue = new BasicDBObject();
write(o, dboValue);
dboValues[i] = dboValue;
} else {
dboValues[i] = o;
}
i++;
}
dbo.put(keyToUse, dboValues);
}
}
/*
public Object readNew(Class<? extends Object> clazz, DBObject dbo) {
return conversionContext.convertToObject(clazz, dbo);
@@ -262,6 +290,27 @@ public class SimpleMongoConverter implements MongoConverter {
if (value instanceof DBObject) {
bw.setPropertyValue(pd.getName(), readCompoundValue(pd, (DBObject) value));
}
else if (value instanceof Object[]) {
Object[] values = new Object[((Object[])value).length];
int i = 0;
for (Object o : (Object[])value) {
if (o instanceof DBObject) {
Class type;
if (pd.getPropertyType().isArray()) {
type = pd.getPropertyType().getComponentType();
}
else {
type = getGenericParameterClass(pd.getWriteMethod()).get(0);
}
values[i] = read(type, (DBObject)o);
}
else {
values[i] = o;
}
i++;
}
bw.setPropertyValue(pd.getName(), values);
}
else {
logger.warn("Unable to map compound DBObject field "
+ keyToUse + " to property " + pd.getName()
@@ -337,8 +386,8 @@ public class SimpleMongoConverter implements MongoConverter {
}
protected boolean isValidProperty(PropertyDescriptor descriptor) {
return (descriptor.getReadMethod() != null && descriptor
.getWriteMethod() != null);
return (descriptor.getReadMethod() != null &&
descriptor.getWriteMethod() != null);
}
protected boolean isSimpleType(Class propertyType) {

View File

@@ -22,6 +22,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -129,6 +130,40 @@ public class SimpleMongoConverterTests {
return portfolio;
}
@Test
public void objectWithArrayContainingNonPrimitiveType() {
TradeBatch b = createTradeBatch();
SimpleMongoConverter converter = createConverter();
DBObject dbo = new BasicDBObject();
converter.write(b, dbo);
TradeBatch b2 = (TradeBatch) converter.read(TradeBatch.class, dbo);
Assert.assertEquals(b.getBatchId(), b2.getBatchId());
Assert.assertNotNull(b2.getTradeList());
Assert.assertEquals(b.getTradeList().size(), b2.getTradeList().size());
Assert.assertEquals(b.getTradeList().get(1).getTicker(), b2.getTradeList().get(1).getTicker());
Assert.assertEquals(b.getTrades().length, b2.getTrades().length);
Assert.assertEquals(b.getTrades()[1].getTicker(), b2.getTrades()[1].getTicker());
}
private TradeBatch createTradeBatch() {
TradeBatch tb = new TradeBatch();
tb.setBatchId("123456");
Trade t1 = new Trade();
t1.setOrderType("BUY");
t1.setTicker("AAPL");
t1.setQuantity(1000);
t1.setPrice(320.77D);
Trade t2 = new Trade();
t2.setOrderType("SELL");
t2.setTicker("MSFT");
t2.setQuantity(100);
t2.setPrice(27.92D);
tb.setTrades(new Trade[] {t2, t1});
tb.setTradeList(Arrays.asList(new Trade[] {t1, t2}));
return tb;
}
@Test
public void testReflection() {
Portfolio p = createPortfolioWithManagers();

View File

@@ -0,0 +1,38 @@
package org.springframework.data.document.mongodb;
import java.util.List;
public class TradeBatch {
private String batchId;
private Trade[] trades;
private List<Trade> tradeList;
public String getBatchId() {
return batchId;
}
public void setBatchId(String batchId) {
this.batchId = batchId;
}
public Trade[] getTrades() {
return trades;
}
public void setTrades(Trade[] trades) {
this.trades = trades;
}
public List<Trade> getTradeList() {
return tradeList;
}
public void setTradeList(List<Trade> tradeList) {
this.tradeList = tradeList;
}
}